import { type FormEvent, useEffect, useRef, useState } from "react"; import { Button, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@ace/ui"; type Props = { open: boolean; name: string; title?: string; label?: string; description?: string; maxLength?: number; pattern?: string; required?: boolean; onSave: (name: string) => void | Promise; onCancel: () => void; }; export function Rename( { open, name, title = "Rename tab", label = "Tab name", description, maxLength = 80, pattern, required, onSave, onCancel, }: Props, ) { const input = useRef(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); useEffect(() => { if (!open) return; const frame = requestAnimationFrame(() => { const node = input.current; if (!node) return; node.value = name; node.focus(); node.select(); }); return () => cancelAnimationFrame(frame); }, [open, name]); async function save(event: FormEvent) { event.preventDefault(); if (busy) return; setBusy(true); setError(""); try { await onSave(input.current?.value ?? name); } catch (error) { setError((error as Error).message); } finally { setBusy(false); } } return ( (next || busy ? undefined : onCancel())}>
{title} {description || "Set a custom tab name."} {error &&

{error}

}
); }