2026-11-26 10:29:49 +00:00
|
|
|
"use client";
|
|
|
|
|
|
2026-11-26 13:02:38 +00:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2026-11-26 10:29:49 +00:00
|
|
|
|
2026-11-26 13:02:38 +00:00
|
|
|
import { api } from "@/lib/api";
|
2026-11-26 11:50:09 +00:00
|
|
|
import { convertEmoticons, withEmoji } from "@/lib/emoticons";
|
2026-11-26 13:02:38 +00:00
|
|
|
import type { ForumAuthor } from "@/lib/forum";
|
|
|
|
|
|
|
|
|
|
import { Avatar } from "./Avatar";
|
|
|
|
|
|
|
|
|
|
/** "@Ho" right before the caret: what was typed after the "@", and where it starts. */
|
|
|
|
|
function mentionAt(text: string, caret: number): { query: string; start: number } | null {
|
|
|
|
|
const before = text.slice(0, caret);
|
|
|
|
|
const match = /(?:^|\s)@([^\s@]{1,24})$/.exec(before);
|
|
|
|
|
if (!match) return null;
|
|
|
|
|
return { query: match[1], start: caret - match[1].length - 1 };
|
|
|
|
|
}
|
2026-11-26 10:29:49 +00:00
|
|
|
|
|
|
|
|
/**
|
2026-11-26 11:50:09 +00:00
|
|
|
* 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).
|
2026-11-26 13:02:38 +00:00
|
|
|
* `mentions`: typing "@Ho" offers the members whose nickname starts so.
|
2026-11-26 13:31:00 +00:00
|
|
|
* `onChange`: the text as it is typed (to keep a draft when the box moves).
|
2026-11-26 10:29:49 +00:00
|
|
|
*/
|
|
|
|
|
export function Composer({
|
|
|
|
|
id,
|
|
|
|
|
label,
|
|
|
|
|
placeholder,
|
|
|
|
|
initial = "",
|
|
|
|
|
submitLabel = "Küldés",
|
|
|
|
|
busy,
|
|
|
|
|
onSubmit,
|
|
|
|
|
onCancel,
|
2026-11-26 11:50:09 +00:00
|
|
|
onTyping,
|
2026-11-26 13:31:00 +00:00
|
|
|
onChange,
|
2026-11-26 13:02:38 +00:00
|
|
|
mentions = false,
|
2026-11-26 10:29:49 +00:00
|
|
|
rows = 3,
|
|
|
|
|
autoFocus = false,
|
|
|
|
|
}: {
|
|
|
|
|
id: string;
|
|
|
|
|
label: string;
|
|
|
|
|
placeholder?: string;
|
|
|
|
|
initial?: string;
|
|
|
|
|
submitLabel?: string;
|
|
|
|
|
busy: boolean;
|
|
|
|
|
onSubmit: (text: string) => Promise<boolean>;
|
|
|
|
|
onCancel?: () => void;
|
2026-11-26 11:50:09 +00:00
|
|
|
onTyping?: () => void;
|
2026-11-26 13:31:00 +00:00
|
|
|
onChange?: (text: string) => void;
|
2026-11-26 13:02:38 +00:00
|
|
|
mentions?: boolean;
|
2026-11-26 10:29:49 +00:00
|
|
|
rows?: number;
|
|
|
|
|
autoFocus?: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const [text, setText] = useState(initial);
|
2026-11-26 13:02:38 +00:00
|
|
|
const [suggest, setSuggest] = useState<{ query: string; start: number; members: ForumAuthor[] } | null>(null);
|
|
|
|
|
const area = useRef<HTMLTextAreaElement>(null);
|
|
|
|
|
const timer = useRef<number | undefined>(undefined);
|
|
|
|
|
|
|
|
|
|
useEffect(() => () => window.clearTimeout(timer.current), []);
|
2026-11-26 13:31:00 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
onChange?.(text);
|
|
|
|
|
}, [text, onChange]);
|
2026-11-26 13:02:38 +00:00
|
|
|
|
|
|
|
|
function lookUp(value: string, caret: number) {
|
|
|
|
|
window.clearTimeout(timer.current);
|
|
|
|
|
const at = mentions ? mentionAt(value, caret) : null;
|
|
|
|
|
if (!at) {
|
|
|
|
|
setSuggest(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
timer.current = window.setTimeout(() => {
|
|
|
|
|
api<{ members: ForumAuthor[] }>(`/forum/members/search?q=${encodeURIComponent(at.query)}`)
|
|
|
|
|
.then((res) => setSuggest(res.members.length ? { ...at, members: res.members } : null))
|
|
|
|
|
.catch(() => setSuggest(null));
|
|
|
|
|
}, 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function pick(nickname: string) {
|
|
|
|
|
if (!suggest) return;
|
|
|
|
|
const el = area.current;
|
|
|
|
|
const caret = el?.selectionStart ?? text.length;
|
|
|
|
|
const inserted = `@${nickname} `;
|
|
|
|
|
const next = text.slice(0, suggest.start) + inserted + text.slice(caret);
|
|
|
|
|
setText(next);
|
|
|
|
|
setSuggest(null);
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
|
el?.focus();
|
|
|
|
|
const at = suggest.start + inserted.length;
|
|
|
|
|
el?.setSelectionRange(at, at);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-11-26 10:29:49 +00:00
|
|
|
|
|
|
|
|
async function submit(e: React.FormEvent) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (!text.trim() || busy) return;
|
2026-11-26 11:50:09 +00:00
|
|
|
const final = convertEmoticons(text, text.length, true).text;
|
2026-11-26 13:02:38 +00:00
|
|
|
if (await onSubmit(final)) {
|
|
|
|
|
setText("");
|
|
|
|
|
setSuggest(null);
|
|
|
|
|
}
|
2026-11-26 10:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className="fc" onSubmit={submit}>
|
|
|
|
|
<label htmlFor={id} className="sr-only">
|
|
|
|
|
{label}
|
|
|
|
|
</label>
|
|
|
|
|
<textarea
|
|
|
|
|
id={id}
|
2026-11-26 13:02:38 +00:00
|
|
|
ref={area}
|
2026-11-26 10:29:49 +00:00
|
|
|
className="fc-text"
|
|
|
|
|
rows={rows}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
value={text}
|
2026-11-26 11:50:09 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
|
withEmoji(e, setText);
|
2026-11-26 13:02:38 +00:00
|
|
|
lookUp(e.target.value, e.target.selectionStart ?? e.target.value.length);
|
2026-11-26 11:50:09 +00:00
|
|
|
onTyping?.();
|
|
|
|
|
}}
|
2026-11-26 13:02:38 +00:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === "Escape") setSuggest(null);
|
|
|
|
|
}}
|
2026-11-26 10:29:49 +00:00
|
|
|
maxLength={10000}
|
|
|
|
|
autoFocus={autoFocus}
|
2026-11-26 13:02:38 +00:00
|
|
|
aria-autocomplete={mentions ? "list" : undefined}
|
2026-11-26 10:29:49 +00:00
|
|
|
/>
|
2026-11-26 13:02:38 +00:00
|
|
|
{suggest && (
|
|
|
|
|
<div className="fc-suggest" role="listbox" aria-label="Tagok">
|
|
|
|
|
{suggest.members.map((m) => (
|
|
|
|
|
<button
|
|
|
|
|
key={m.nickname}
|
|
|
|
|
type="button"
|
|
|
|
|
role="option"
|
|
|
|
|
aria-selected="false"
|
|
|
|
|
className="fc-suggest-item"
|
|
|
|
|
onClick={() => pick(m.nickname)}
|
|
|
|
|
>
|
|
|
|
|
<Avatar avatar={m.avatar} size={24} online={m.online} />
|
|
|
|
|
{m.nickname}
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2026-11-26 10:29:49 +00:00
|
|
|
<div className="fc-actions">
|
|
|
|
|
<span className="fc-spacer" />
|
|
|
|
|
{onCancel && (
|
|
|
|
|
<button type="button" className="btn btn-ghost btn-sm" onClick={onCancel}>
|
|
|
|
|
Mégse
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
<button type="submit" className="btn btn-primary btn-sm" disabled={busy || !text.trim()}>
|
|
|
|
|
{busy ? "Küldés…" : submitLabel}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|