import { useEffect, useId, useRef, useState } from "react"; import { Check, Loader2, X } from "lucide-react"; import { useTranslation } from "react-i18next"; import { Input } from "@/components/ui/input"; interface SidebarTopicRenameProps { title: string; onSave: (title: string) => Promise; onClose: () => void; } export function SidebarTopicRename({ title, onSave, onClose }: SidebarTopicRenameProps) { const { t } = useTranslation(); const [value, setValue] = useState(title); const [saving, setSaving] = useState(false); const [failed, setFailed] = useState(false); const inputRef = useRef(null); const savingRef = useRef(false); const composingRef = useRef(false); const errorId = useId(); useEffect(() => { inputRef.current?.focus(); inputRef.current?.select(); }, []); const save = async () => { const cleaned = value.trim(); if (!cleaned || savingRef.current) return; savingRef.current = true; setSaving(true); setFailed(false); try { await onSave(cleaned); onClose(); } catch { setFailed(true); inputRef.current?.focus(); } finally { savingRef.current = false; setSaving(false); } }; return (
{ event.preventDefault(); void save(); }} onClick={(event) => event.stopPropagation()} onPointerDown={(event) => event.stopPropagation()} onContextMenu={(event) => event.stopPropagation()} onKeyDown={(event) => { event.stopPropagation(); const composing = composingRef.current || event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229; if (event.key === "Enter" && event.target === inputRef.current) { event.preventDefault(); if (!composing) void save(); } else if (event.key === "Escape" && !composing) { event.preventDefault(); if (!savingRef.current) onClose(); } }} >
{ composingRef.current = true; }} onCompositionEnd={() => { composingRef.current = false; }} onChange={(event) => { setValue(event.target.value); setFailed(false); }} />
{failed ? ( ) : null}
); }