Forum: answers to me as the only count, reply box in the branch, keyboard-safe inputs

- The "Követett" tab becomes "Válaszok": its count is the unread answers
  to the member (a reply to their post, a first-level reply in their
  thread, or a post naming them), listed on /forum/valaszok with "Mind
  olvasott"; the followed threads are its second view. The other counts
  are gone from the menu; listeners see the waiting threads on their card
  on the front page.
- Replying to a post opens the box at the bottom of its branch and scrolls
  there; another branch moves it (with the text typed so far).
- Phones: while typing, the forum's bottom menu and floating bars step
  aside and the box with its send button is scrolled above the keyboard
  (KeyboardSafe, site-wide; Android resizes the page for the keyboard).
- The "Meghallgató" badge is now "Kísérő", with a new icon.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-26 13:31:00 +00:00
co-authored by Claude Opus 5.5
parent 8bc426460d
commit 42283d740e
25 changed files with 615 additions and 90 deletions
@@ -0,0 +1,103 @@
"use client";
import { useEffect } from "react";
const NOT_TYPED = new Set([
"checkbox",
"radio",
"button",
"submit",
"reset",
"range",
"file",
"color",
"image",
"hidden",
]);
function typedInto(el: Element | null): el is HTMLElement {
if (el instanceof HTMLTextAreaElement) return true;
if (el instanceof HTMLInputElement) return !NOT_TYPED.has(el.type);
return el instanceof HTMLElement && el.isContentEditable;
}
/**
* On phones the on-screen keyboard must not cover the box being typed in.
* While a text field has the focus: `kb-open` on <html> (the forum's bottom
* menu and other fixed bars step aside), `--vv-top` and `--vv-height` give
* the part of the screen the keyboard leaves free (for full-screen panels),
* and the field, with its send button when the form is small, is scrolled
* into that part.
*/
export function KeyboardSafe() {
useEffect(() => {
if (!window.matchMedia("(pointer: coarse)").matches) return;
const root = document.documentElement;
const vv = window.visualViewport;
let timer: number | undefined;
let closing: number | undefined;
const measure = () => {
root.style.setProperty("--vv-top", `${vv ? vv.offsetTop : 0}px`);
root.style.setProperty("--vv-height", `${vv ? vv.height : window.innerHeight}px`);
};
const reveal = () => {
const el = document.activeElement;
if (!typedInto(el)) return;
measure();
const top = vv ? vv.offsetTop : 0;
const height = vv ? vv.height : window.innerHeight;
const form = el.closest("form");
const target = form && form.getBoundingClientRect().height < height * 0.6 ? form : el;
const rect = target.getBoundingClientRect();
const free = { top: top + 12, bottom: top + height - 12 };
if (rect.bottom > free.bottom) {
window.scrollBy({ top: Math.min(rect.bottom - free.bottom, rect.top - free.top), behavior: "smooth" });
} else if (rect.top < free.top) {
window.scrollBy({ top: rect.top - free.top, behavior: "smooth" });
}
};
// After the keyboard has slid up, and again whenever the free part changes.
const later = (ms: number) => {
window.clearTimeout(timer);
timer = window.setTimeout(reveal, ms);
};
const onFocusIn = (e: FocusEvent) => {
if (!typedInto(e.target as Element)) return;
window.clearTimeout(closing);
root.classList.add("kb-open");
measure();
later(350);
};
const onFocusOut = () => {
// Moving to another field keeps it open.
window.clearTimeout(closing);
closing = window.setTimeout(() => {
if (!typedInto(document.activeElement)) root.classList.remove("kb-open");
}, 150);
};
const onViewport = () => {
if (!root.classList.contains("kb-open")) return;
measure();
later(120);
};
document.addEventListener("focusin", onFocusIn);
document.addEventListener("focusout", onFocusOut);
vv?.addEventListener("resize", onViewport);
vv?.addEventListener("scroll", measure);
return () => {
window.clearTimeout(timer);
window.clearTimeout(closing);
document.removeEventListener("focusin", onFocusIn);
document.removeEventListener("focusout", onFocusOut);
vv?.removeEventListener("resize", onViewport);
vv?.removeEventListener("scroll", measure);
root.classList.remove("kb-open");
};
}, []);
return null;
}
@@ -57,14 +57,16 @@ const BADGES: Record<BadgeKind, { label: string; title: string; icon: React.Reac
</>
),
},
// An ear.
// Two walking side by side.
listener: {
label: "Meghallgató",
title: "Önkéntes meghallgató: szívesen meghallgat",
label: "Kísérő",
title: "Önkéntes kísérő: szívesen válaszol annak, aki meghallgatást kér",
icon: (
<>
<path d="M7 9.5a5 5 0 0 1 10 0c0 3-2.5 4-3 6s-1.5 4-3.8 4A2.7 2.7 0 0 1 7.5 17" />
<path d="M10 10a2 2 0 0 1 4 0c0 1.3-1.2 1.7-1.6 2.6" />
<circle cx="8" cy="6.5" r="2.5" />
<circle cx="16" cy="6.5" r="2.5" />
<path d="M3.5 20v-3.5a4.5 4.5 0 0 1 9 0V20" />
<path d="M11.5 20v-3.5a4.5 4.5 0 0 1 9 0V20" />
</>
),
},
@@ -116,7 +118,7 @@ export function RankBadge({ rank, name }: { rank: Rank; name: string }) {
);
}
/** A badge (Moderátor, Meghallgató, Támogató, Szálindító, napi égbolt), with its icon. */
/** A badge (Moderátor, Kísérő, Támogató, Szálindító, napi égbolt), with its icon. */
export function Badge({ kind }: { kind: BadgeKind }) {
const b = BADGES[kind];
return (
@@ -21,6 +21,7 @@ function mentionAt(text: string, caret: number): { query: string; start: number
* emoji as they are typed; phones bring their own emoji keyboard.
* `onTyping`: called on every keystroke (the "… gépel" notice).
* `mentions`: typing "@Ho" offers the members whose nickname starts so.
* `onChange`: the text as it is typed (to keep a draft when the box moves).
*/
export function Composer({
id,
@@ -32,6 +33,7 @@ export function Composer({
onSubmit,
onCancel,
onTyping,
onChange,
mentions = false,
rows = 3,
autoFocus = false,
@@ -45,6 +47,7 @@ export function Composer({
onSubmit: (text: string) => Promise<boolean>;
onCancel?: () => void;
onTyping?: () => void;
onChange?: (text: string) => void;
mentions?: boolean;
rows?: number;
autoFocus?: boolean;
@@ -55,6 +58,9 @@ export function Composer({
const timer = useRef<number | undefined>(undefined);
useEffect(() => () => window.clearTimeout(timer.current), []);
useEffect(() => {
onChange?.(text);
}, [text, onChange]);
function lookUp(value: string, caret: number) {
window.clearTimeout(timer.current);
@@ -9,11 +9,10 @@ import type { ForumNavData } from "@/lib/forum";
/** The menu's counts. */
export interface NavCounts {
unread: number;
/** Unread answers to the member: the only count on the "Válaszok" tab. */
replies: number;
messages: number;
reports: number;
/** Listeners: threads waiting for a first reply. */
waiting: number;
}
/** The live notice a page listens to (`window` event `forum:live`). */
@@ -29,12 +28,7 @@ export interface LiveNotice {
const POLL_MS = 30_000;
function counts(data: ForumNavData): NavCounts {
return {
unread: data.followed?.new_replies ?? 0,
messages: data.messages ?? 0,
reports: data.reports ?? 0,
waiting: data.waiting ?? 0,
};
return { replies: data.replies ?? 0, messages: data.messages ?? 0, reports: data.reports ?? 0 };
}
/**
@@ -105,7 +99,7 @@ function useLive(member: boolean, wsUrl: string, refresh: () => void) {
}, [member, wsUrl, refresh]);
}
function Icon({ name }: { name: "forum" | "followed" | "new" | "messages" | "profile" }) {
function Icon({ name }: { name: "forum" | "replies" | "new" | "messages" | "profile" }) {
return (
<svg viewBox="0 0 24 24" aria-hidden="true" className="fn-icon">
{name === "forum" && (
@@ -114,10 +108,11 @@ function Icon({ name }: { name: "forum" | "followed" | "new" | "messages" | "pro
<path d="M20 9h1a1 1 0 0 1 1 1v8l-3-2h-6" />
</>
)}
{name === "followed" && (
{name === "replies" && (
<>
<path d="M6 16V11a6 6 0 0 1 12 0v5l1.5 2h-15z" />
<path d="M10 20.5a2 2 0 0 0 4 0" />
<path d="M10 8L5 12.5l5 4.5" />
<path d="M5 12.5h9a5 5 0 0 1 5 5V19" />
<path d="M14 4.5h5.5" />
</>
)}
{name === "new" && (
@@ -144,7 +139,8 @@ function Icon({ name }: { name: "forum" | "followed" | "new" | "messages" | "pro
/**
* The forum's own menu: a bar at the bottom of the screen on phones, tabs
* above the content on wider screens. `unread`: new replies in followed threads.
* above the content on wider screens. The "Válaszok" tab counts the unread
* answers to the member (a reply to them, in their thread, or naming them).
*/
export function ForumNav({
member,
@@ -158,13 +154,18 @@ export function ForumNav({
wsUrl: string;
}) {
const path = usePathname();
const [{ unread, messages, reports, waiting }, setCounts] = useState(initial);
const [{ replies, messages, reports }, setCounts] = useState(initial);
const refresh = useCallback(() => {
api<ForumNavData>("/forum/nav")
.then((data) => setCounts(counts(data)))
.catch(() => {});
}, []);
useLive(member !== null, wsUrl, refresh);
// A page that changed what counts (read a thread, "Mind olvasott") asks for fresh counts.
useEffect(() => {
window.addEventListener("forum:nav", refresh);
return () => window.removeEventListener("forum:nav", refresh);
}, [refresh]);
// The menu stays on screen between pages: fresh counts on every page.
const first = useRef(true);
useEffect(() => {
@@ -180,15 +181,13 @@ export function ForumNav({
label: "Fórum",
icon: "forum" as const,
current: path === "/forum" || path.startsWith("/forum/szal"),
// Listeners: what waits for a first reply.
badge: waiting,
},
{
href: loggedIn ? "/forum/kovetett" : "/belepes?vissza=/forum/kovetett",
label: "Követett",
icon: "followed" as const,
current: path.startsWith("/forum/kovetett"),
badge: unread,
href: loggedIn ? "/forum/valaszok" : "/belepes?vissza=/forum/valaszok",
label: "Válaszok",
icon: "replies" as const,
current: path.startsWith("/forum/valaszok") || path.startsWith("/forum/kovetett"),
badge: replies,
},
{
href: loggedIn ? "/forum/uj" : "/belepes?vissza=/forum/uj",
@@ -0,0 +1,15 @@
import Link from "next/link";
/** The two views of the "Válaszok" tab: answers to me, and the threads I follow. */
export function RepliesTabs({ current }: { current: "replies" | "followed" }) {
return (
<nav className="fmod-tabs" aria-label="Válaszok">
<Link href="/forum/valaszok" aria-current={current === "replies" ? "page" : undefined}>
Nekem
</Link>
<Link href="/forum/kovetett" aria-current={current === "followed" ? "page" : undefined}>
Követett szálak
</Link>
</nav>
);
}
@@ -405,6 +405,17 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
const typingNow = useTypingWatch("thread", initial.thread.id);
const imTyping = useTypingSender("thread", initial.thread.id);
const composer = useRef<HTMLDivElement>(null);
// Replying to a post: the box opens at the bottom of its branch (the
// first-level post and the replies under it); what was typed moves along.
const inline = useRef<HTMLDivElement>(null);
const draft = useRef("");
const replyBranch = replyTo ? (replyTo.parent_id ?? replyTo.id) : null;
useEffect(() => {
if (replyBranch === null) return;
const box = inline.current;
box?.scrollIntoView({ behavior: "smooth", block: "center" });
box?.querySelector("textarea")?.focus({ preventScroll: true });
}, [replyBranch, replyTo]);
const { viewer, thread, access } = page;
const member = viewer.member !== null;
const base = `/forum/threads/${thread.id}`;
@@ -414,7 +425,10 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
// Mark read up to the newest post shown.
useEffect(() => {
if (!member || lastId === 0) return;
api(`${base}/read`, { body: { post_id: lastId } }).catch(() => {});
api(`${base}/read`, { body: { post_id: lastId } })
// The answers read here leave the count on the "Válaszok" tab.
.then(() => window.dispatchEvent(new Event("forum:nav")))
.catch(() => {});
}, [member, lastId, base]);
// New replies while the page is open: offered, not pushed into the text being read.
@@ -477,10 +491,7 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
: await api<ForumPost>(`/forum/posts/${post.id}/reaction`, { method: "PUT", body: { kind } });
replacePost(updated);
}),
replyTo: (post) => {
setReplyTo(post);
composer.current?.scrollIntoView({ behavior: "smooth", block: "center" });
},
replyTo: (post) => setReplyTo(post),
edit: async (post, body) => {
const updated = await run(() => api<ForumPost>(`/forum/posts/${post.id}`, { method: "PATCH", body: { body } }));
if (updated) replacePost(updated);
@@ -513,12 +524,14 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
}),
};
async function sendReply(text: string): Promise<boolean> {
const post = await run(() =>
api<ForumPost>(`${base}/posts`, { body: { body: text, parent_id: replyTo?.id ?? null } }),
);
/** A first-level reply (the box at the bottom), or one to `replyTo` (the box in its branch). */
async function sendReply(text: string, to: ForumPost | null): Promise<boolean> {
const post = await run(() => api<ForumPost>(`${base}/posts`, { body: { body: text, parent_id: to?.id ?? null } }));
if (!post) return false;
setReplyTo(null);
if (to) {
draft.current = "";
setReplyTo(null);
}
await reload().catch(() => setPage((p) => ({ ...p, posts: [...p.posts, post] })));
requestAnimationFrame(() => document.getElementById(`h${post.id}`)?.scrollIntoView({ block: "center" }));
return true;
@@ -662,6 +675,35 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
))}
</div>
)}
{replyTo && replyBranch === top.id && (
<div className="ftv-inline" ref={inline}>
<p className="ftv-replyto">
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="M9 14L4 9l5-5" />
<path d="M4 9h10a6 6 0 0 1 6 6v4" />
</svg>
Válasz neki: <strong>{replyTo.author?.nickname ?? "Törölt tag"}</strong>
</p>
<Composer
key={top.id}
mentions
onTyping={imTyping}
id={`valasz-${top.id}`}
label={`Válasz neki: ${replyTo.author?.nickname ?? "Törölt tag"}`}
placeholder="Írd meg a válaszod…"
initial={draft.current}
onChange={(text) => {
draft.current = text;
}}
busy={busy}
onSubmit={(text) => sendReply(text, replyTo)}
onCancel={() => {
draft.current = "";
setReplyTo(null);
}}
/>
</div>
)}
</div>
))}
@@ -699,19 +741,6 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
<p className="ftv-gate">Ez a szál le van zárva, új hozzászólás nem írható.</p>
) : (
<>
{replyTo && (
<p className="ftv-replyto">
Válasz neki: <strong>{replyTo.author?.nickname ?? "Törölt tag"}</strong>
<button
type="button"
className="fp-link"
onClick={() => setReplyTo(null)}
aria-label="Válasz visszavonása"
>
×
</button>
</p>
)}
<Composer
mentions
onTyping={imTyping}
@@ -719,7 +748,7 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
label="Válaszod"
placeholder={thread.topic.listening ? "Írd le, ami benned van…" : "Írd meg a válaszod…"}
busy={busy}
onSubmit={sendReply}
onSubmit={(text) => sendReply(text, null)}
/>
</>
)}