2026-11-26 10:29:49 +00:00
|
|
|
"use client";
|
|
|
|
|
|
2026-11-26 11:50:09 +00:00
|
|
|
import { useState } from "react";
|
2026-11-26 10:29:49 +00:00
|
|
|
|
2026-11-26 11:50:09 +00:00
|
|
|
import { convertEmoticons, withEmoji } from "@/lib/emoticons";
|
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 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 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 10:29:49 +00:00
|
|
|
rows?: number;
|
|
|
|
|
autoFocus?: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const [text, setText] = useState(initial);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
if (await onSubmit(final)) setText("");
|
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}
|
|
|
|
|
className="fc-text"
|
|
|
|
|
rows={rows}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
value={text}
|
2026-11-26 11:50:09 +00:00
|
|
|
onChange={(e) => {
|
|
|
|
|
withEmoji(e, setText);
|
|
|
|
|
onTyping?.();
|
|
|
|
|
}}
|
2026-11-26 10:29:49 +00:00
|
|
|
maxLength={10000}
|
|
|
|
|
autoFocus={autoFocus}
|
|
|
|
|
/>
|
|
|
|
|
<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>
|
|
|
|
|
);
|
|
|
|
|
}
|