Forum: typing notices, smileys to emoji while typing, no emoji tray
"Holdfény gépel…" shows in threads and private conversations: the page says over the live connection (every few seconds while typing) that its member types, and the forum service passes it to those who may see that place, never back to the typer. The emoji tray is gone (phones have their emoji keyboard); character smileys turn into emoji as they are typed, from a longer list shared with the server (which also converts titles now; <3 is a red heart). Emoji show in colour: the forum's font stack names the systems' colour emoji fonts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
"use client";
|
||||
|
||||
import { useRef, useState } from "react";
|
||||
import { useState } from "react";
|
||||
|
||||
import { EMOJI } from "@/lib/forum";
|
||||
import { convertEmoticons, withEmoji } from "@/lib/emoticons";
|
||||
|
||||
/**
|
||||
* The text box with an emoji tray (phones have emoji on the keyboard; the
|
||||
* tray is mostly for computers) and the send button.
|
||||
* The text box and the send button. Character smileys (`:)`, `<3`) turn into
|
||||
* emoji as they are typed; phones bring their own emoji keyboard.
|
||||
* `onTyping`: called on every keystroke (the "… gépel" notice).
|
||||
*/
|
||||
export function Composer({
|
||||
id,
|
||||
@@ -17,6 +18,7 @@ export function Composer({
|
||||
busy,
|
||||
onSubmit,
|
||||
onCancel,
|
||||
onTyping,
|
||||
rows = 3,
|
||||
autoFocus = false,
|
||||
}: {
|
||||
@@ -28,36 +30,17 @@ export function Composer({
|
||||
busy: boolean;
|
||||
onSubmit: (text: string) => Promise<boolean>;
|
||||
onCancel?: () => void;
|
||||
onTyping?: () => void;
|
||||
rows?: number;
|
||||
autoFocus?: boolean;
|
||||
}) {
|
||||
const [text, setText] = useState(initial);
|
||||
const [tray, setTray] = useState(false);
|
||||
const area = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
function insert(emoji: string) {
|
||||
const el = area.current;
|
||||
if (!el) {
|
||||
setText((t) => t + emoji);
|
||||
return;
|
||||
}
|
||||
const start = el.selectionStart ?? text.length;
|
||||
const end = el.selectionEnd ?? text.length;
|
||||
const next = text.slice(0, start) + emoji + text.slice(end);
|
||||
setText(next);
|
||||
requestAnimationFrame(() => {
|
||||
el.focus();
|
||||
el.setSelectionRange(start + emoji.length, start + emoji.length);
|
||||
});
|
||||
}
|
||||
|
||||
async function submit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
if (!text.trim() || busy) return;
|
||||
if (await onSubmit(text)) {
|
||||
setText("");
|
||||
setTray(false);
|
||||
}
|
||||
const final = convertEmoticons(text, text.length, true).text;
|
||||
if (await onSubmit(final)) setText("");
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -67,38 +50,18 @@ export function Composer({
|
||||
</label>
|
||||
<textarea
|
||||
id={id}
|
||||
ref={area}
|
||||
className="fc-text"
|
||||
rows={rows}
|
||||
placeholder={placeholder}
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
onChange={(e) => {
|
||||
withEmoji(e, setText);
|
||||
onTyping?.();
|
||||
}}
|
||||
maxLength={10000}
|
||||
autoFocus={autoFocus}
|
||||
/>
|
||||
{tray && (
|
||||
<div className="fc-tray" role="group" aria-label="Emoji">
|
||||
{EMOJI.map((e) => (
|
||||
<button key={e} type="button" className="fc-emoji" onClick={() => insert(e)} aria-label={`${e} beszúrása`}>
|
||||
{e}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="fc-actions">
|
||||
<button
|
||||
type="button"
|
||||
className="fc-icon-btn"
|
||||
aria-label="Emoji"
|
||||
aria-expanded={tray}
|
||||
onClick={() => setTray((t) => !t)}
|
||||
>
|
||||
<svg viewBox="0 0 24 24" aria-hidden="true">
|
||||
<circle cx="12" cy="12" r="9" />
|
||||
<path d="M8.5 14.5a4 4 0 0 0 7 0" />
|
||||
<path d="M9 9.5h.01M15 9.5h.01" strokeWidth="2.6" />
|
||||
</svg>
|
||||
</button>
|
||||
<span className="fc-spacer" />
|
||||
{onCancel && (
|
||||
<button type="button" className="btn btn-ghost btn-sm" onClick={onCancel}>
|
||||
|
||||
@@ -16,8 +16,11 @@ export interface NavCounts {
|
||||
|
||||
/** The live notice a page listens to (`window` event `forum:live`). */
|
||||
export interface LiveNotice {
|
||||
t: "thread" | "conversation" | "nav";
|
||||
t: "thread" | "conversation" | "nav" | "typing";
|
||||
id?: number;
|
||||
/** For `typing`: where (`thread` or `conversation`) and who. */
|
||||
kind?: string;
|
||||
who?: string;
|
||||
}
|
||||
|
||||
/** How often the counts are asked for while there is no live connection (also the online dot). */
|
||||
@@ -68,6 +71,14 @@ function useLive(member: boolean, wsUrl: string, refresh: () => void) {
|
||||
};
|
||||
};
|
||||
connect();
|
||||
// The page says its member is typing somewhere: passed on while connected.
|
||||
const typing = (e: Event) => {
|
||||
const out = (e as CustomEvent<{ kind: string; id: number }>).detail;
|
||||
if (open.current && socket?.readyState === WebSocket.OPEN) {
|
||||
socket.send(JSON.stringify({ typing: out.kind, id: out.id }));
|
||||
}
|
||||
};
|
||||
window.addEventListener("forum:typing-out", typing);
|
||||
// Polling while there is no connection; and when the page comes back to the front.
|
||||
const poll = window.setInterval(() => {
|
||||
if (!open.current && document.visibilityState === "visible") refresh();
|
||||
@@ -81,6 +92,7 @@ function useLive(member: boolean, wsUrl: string, refresh: () => void) {
|
||||
window.clearTimeout(timer);
|
||||
window.clearInterval(poll);
|
||||
document.removeEventListener("visibilitychange", visible);
|
||||
window.removeEventListener("forum:typing-out", typing);
|
||||
socket?.close();
|
||||
};
|
||||
}, [member, wsUrl, refresh]);
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Composer } from "./Composer";
|
||||
import { PostBody } from "./PostBody";
|
||||
import type { LiveNotice } from "./ForumNav";
|
||||
import { ReportButton } from "./ReportButton";
|
||||
import { TypingLine, useTypingSender, useTypingWatch } from "./typing";
|
||||
|
||||
/** How often the open thread looks for new replies (while the page is in front). */
|
||||
const POLL_MS = 30_000;
|
||||
@@ -329,6 +330,8 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// Where the visitor stopped reading when they opened the page.
|
||||
const [readUpTo] = useState(initial.access?.last_read_post_id ?? null);
|
||||
const typingNow = useTypingWatch("thread", initial.thread.id);
|
||||
const imTyping = useTypingSender("thread", initial.thread.id);
|
||||
const composer = useRef<HTMLDivElement>(null);
|
||||
const { viewer, thread, access } = page;
|
||||
const member = viewer.member !== null;
|
||||
@@ -594,6 +597,8 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
|
||||
</p>
|
||||
)}
|
||||
|
||||
<TypingLine text={typingNow} />
|
||||
|
||||
<div className="ftv-compose" id="valasz" ref={composer}>
|
||||
{thread.deleted ? null : !viewer.logged_in ? (
|
||||
<p className="ftv-gate">
|
||||
@@ -630,6 +635,7 @@ export function ThreadView({ initial }: { initial: ThreadPage }) {
|
||||
</p>
|
||||
)}
|
||||
<Composer
|
||||
onTyping={imTyping}
|
||||
id="valasz-szoveg"
|
||||
label="Válaszod"
|
||||
placeholder={thread.topic.listening ? "Írd le, ami benned van…" : "Írd meg a válaszod…"}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
|
||||
import type { LiveNotice } from "./ForumNav";
|
||||
|
||||
/** The live connection's outgoing "I am typing" (ForumNav sends it). */
|
||||
export interface TypingOut {
|
||||
kind: "thread" | "conversation";
|
||||
id: number;
|
||||
}
|
||||
|
||||
/** A typing notice is sent at most this often while typing. */
|
||||
const SEND_EVERY_MS = 3000;
|
||||
/** "X gépel…" disappears this long after the last notice. */
|
||||
const SHOW_MS = 6000;
|
||||
|
||||
/** Call on every keystroke: tells the others (now and then) that the member is typing here. */
|
||||
export function useTypingSender(kind: TypingOut["kind"], id: number) {
|
||||
const last = useRef(0);
|
||||
return useCallback(() => {
|
||||
const now = Date.now();
|
||||
if (now - last.current < SEND_EVERY_MS) return;
|
||||
last.current = now;
|
||||
window.dispatchEvent(new CustomEvent<TypingOut>("forum:typing-out", { detail: { kind, id } }));
|
||||
}, [kind, id]);
|
||||
}
|
||||
|
||||
/** Who is typing here now: "Holdfény gépel…", "Holdfény és Zöldág gépel…", or null. */
|
||||
export function useTypingWatch(kind: TypingOut["kind"], id: number): string | null {
|
||||
const [who, setWho] = useState<Record<string, number>>({});
|
||||
useEffect(() => {
|
||||
const live = (e: Event) => {
|
||||
const n = (e as CustomEvent<LiveNotice & { kind?: string; who?: string }>).detail;
|
||||
if (n.t === "typing" && n.kind === kind && n.id === id && n.who) {
|
||||
setWho((w) => ({ ...w, [n.who as string]: Date.now() }));
|
||||
}
|
||||
// Their post or message arrived: they are done.
|
||||
if ((n.t === "thread" && kind === "thread" && n.id === id) || (n.t === "conversation" && n.id === id)) {
|
||||
setWho({});
|
||||
}
|
||||
};
|
||||
window.addEventListener("forum:live", live);
|
||||
const timer = window.setInterval(() => {
|
||||
setWho((w) => {
|
||||
const now = Date.now();
|
||||
const kept = Object.fromEntries(Object.entries(w).filter(([, at]) => now - at < SHOW_MS));
|
||||
return Object.keys(kept).length === Object.keys(w).length ? w : kept;
|
||||
});
|
||||
}, 1000);
|
||||
return () => {
|
||||
window.removeEventListener("forum:live", live);
|
||||
window.clearInterval(timer);
|
||||
};
|
||||
}, [kind, id]);
|
||||
const names = Object.keys(who);
|
||||
if (names.length === 0) return null;
|
||||
if (names.length === 1) return `${names[0]} gépel…`;
|
||||
if (names.length === 2) return `${names[0]} és ${names[1]} gépel…`;
|
||||
return `${names.length} tag gépel…`;
|
||||
}
|
||||
|
||||
/** The "… gépel" line with three soft dots. */
|
||||
export function TypingLine({ text }: { text: string | null }) {
|
||||
return (
|
||||
<p className="ftyping" aria-live="polite">
|
||||
{text && (
|
||||
<>
|
||||
<span className="ftyping-dots" aria-hidden="true">
|
||||
<span />
|
||||
<span />
|
||||
<span />
|
||||
</span>
|
||||
{text}
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user