import { useScene, type AnyNode } from "@pascal-app/core"; import { useCallback, useEffect, useRef, useState } from "react"; import { Pencil } from "lucide-react"; import { cn } from "@/lib/utils"; interface InlineRenameInputProps { node: AnyNode; isEditing: boolean; onStopEditing: () => void; defaultName: string; className?: string; onStartEditing?: () => void; } export function InlineRenameInput({ node, isEditing, onStopEditing, defaultName, className, onStartEditing, }: InlineRenameInputProps) { const updateNode = useScene((s) => s.updateNode); const [value, setValue] = useState(node.name || ""); const inputRef = useRef(null); useEffect(() => { if (isEditing) { setValue(node.name || ""); // Focus and select all text after a short delay setTimeout(() => { if (inputRef.current) { inputRef.current.focus(); inputRef.current.select(); } }, 0); } }, [isEditing, node.name]); const handleSave = useCallback(() => { const trimmed = value.trim(); if (trimmed !== node.name) { updateNode(node.id, { name: trimmed || undefined }); } onStopEditing(); }, [value, node.id, node.name, updateNode, onStopEditing]); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); handleSave(); } else if (e.key === "Escape") { e.preventDefault(); onStopEditing(); } }; if (!isEditing) { return (
{node.name || defaultName} {onStartEditing && ( )}
); } return ( setValue(e.target.value)} onKeyDown={handleKeyDown} onBlur={handleSave} placeholder={defaultName} className={cn( "flex-1 w-full bg-transparent text-foreground outline-none border-b border-primary/50 focus:border-primary rounded-none px-0 py-0 m-0 h-5 text-sm", className )} onClick={(e) => e.stopPropagation()} onDoubleClick={(e) => e.stopPropagation()} /> ); }