import { memo, useCallback, useEffect, useRef, useState } from "react"; import { ConfirmationDialog, Input } from "@tutti-os/ui-system"; import { Button } from "../../../app/renderer/components/ui/button"; import type { AgentGUINodeViewModel } from "../model/agentGuiNodeTypes"; import type { AgentGUIViewLabels } from "./AgentGUINodeView.types"; interface AgentGUIRenameConversationDialogProps { conversation: AgentGUINodeViewModel["rail"]["conversations"][number] | null; open: boolean; labels: AgentGUIViewLabels; onOpenChange: (open: boolean) => void; onRename: (agentSessionId: string, title: string) => Promise; } export const AgentGUIRenameConversationDialog = memo( function AgentGUIRenameConversationDialog({ conversation, open, labels, onOpenChange, onRename }: AgentGUIRenameConversationDialogProps): React.JSX.Element { "use memo"; const [title, setTitle] = useState(""); const [isSaving, setIsSaving] = useState(false); const isSavingRef = useRef(false); const armedPointerActionRef = useRef<"cancel" | "confirm" | null>(null); const inputRef = useRef(null); const trimmedTitle = title.trim(); useEffect(() => { if (!open || !conversation) { isSavingRef.current = false; armedPointerActionRef.current = null; setIsSaving(false); return; } setTitle(conversation.title); }, [conversation, open]); useEffect(() => { if (!open) { return; } // timing: defer focus until after the dialog's open animation mounts the input const timer = window.setTimeout(() => { inputRef.current?.focus(); inputRef.current?.select(); }, 0); return () => window.clearTimeout(timer); }, [open, conversation?.id]); const closeRenameDialog = useCallback(() => { if (!isSavingRef.current) { onOpenChange(false); } }, [onOpenChange]); const confirmRename = useCallback(() => { if (!conversation || isSavingRef.current || !trimmedTitle) { return; } isSavingRef.current = true; setIsSaving(true); void onRename(conversation.id, trimmedTitle) .then(() => { onOpenChange(false); }) .catch(() => { inputRef.current?.focus(); }) .finally(() => { isSavingRef.current = false; setIsSaving(false); }); }, [conversation, onOpenChange, onRename, trimmedTitle]); return ( } open={open} title={labels.renameSessionTitle} onConfirm={confirmRename} onOpenChange={onOpenChange} > setTitle(event.currentTarget.value)} onKeyDown={(event) => { if (event.key === "Enter") { event.preventDefault(); confirmRename(); } }} /> ); } );