"use client"; import Image from "next/image"; import { getIconForFile } from "vscode-icons-js"; import { TFile, TTab } from "@/lib/types"; import { useEffect, useRef, useState } from "react"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, } from "@/components/ui/context-menu"; import { Loader2, Pencil, Trash2 } from "lucide-react"; export default function SidebarFile({ data, selectFile, handleRename, handleDeleteFile, }: { data: TFile; selectFile: (file: TTab) => void; handleRename: ( id: string, newName: string, oldName: string, type: "file" | "folder" ) => boolean; handleDeleteFile: (file: TFile) => void; }) { const [imgSrc, setImgSrc] = useState(`/icons/${getIconForFile(data.name)}`); const [editing, setEditing] = useState(false); const inputRef = useRef(null); const [pendingDelete, setPendingDelete] = useState(false); useEffect(() => { if (editing) { setTimeout(() => inputRef.current?.focus(), 0); } }, [editing, inputRef.current]); const renameFile = () => { const renamed = handleRename( data.id, inputRef.current?.value ?? data.name, data.name, "file" ); if (!renamed && inputRef.current) { inputRef.current.value = data.name; } setEditing(false); }; return ( { if (!editing && !pendingDelete) selectFile({ ...data, saved: true }); }} // onDoubleClick={() => { // setEditing(true) // }} className="data-[state=open]:bg-secondary/50 w-full flex items-center h-7 px-1 hover:bg-secondary rounded-sm cursor-pointer transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" > File Icon setImgSrc("/icons/default_file.svg")} /> {pendingDelete ? ( <>
Deleting...
) : (
{ e.preventDefault(); renameFile(); }} > renameFile()} />
)}
{ console.log("rename"); setEditing(true); }} > Rename { console.log("delete"); setPendingDelete(true); handleDeleteFile(data); }} > Delete
); }