import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { File02Icon } from "@hugeicons/core-free-icons"; import { HugeiconsIcon } from "@hugeicons/react"; import { invoke } from "@tauri-apps/api/core"; import { useEffect, useRef, useState } from "react"; type Props = { open: boolean; onOpenChange: (open: boolean) => void; rootPath: string | null; onCreated: (path: string) => void; }; function joinPath(parent: string, name: string): string { if (parent.endsWith("/")) return `${parent}${name}`; return `${parent}/${name}`; } export function NewEditorDialog({ open, onOpenChange, rootPath, onCreated, }: Props) { const [name, setName] = useState("untitled.txt"); const [error, setError] = useState(null); const inputRef = useRef(null); useEffect(() => { if (!open) return; setName("untitled.txt"); setError(null); // Pre-select the basename so the user can quickly retype the filename // while keeping the extension handy. setTimeout(() => { const el = inputRef.current; if (!el) return; el.focus(); const dot = el.value.lastIndexOf("."); el.setSelectionRange(0, dot > 0 ? dot : el.value.length); }, 0); }, [open]); const submit = async () => { const trimmed = name.trim(); if (!trimmed) { setError("Name is required"); return; } if (trimmed.includes("..")) { setError("Path must be relative"); return; } if (!rootPath) { setError("No workspace root"); return; } const path = trimmed.startsWith("/") ? trimmed : joinPath(rootPath, trimmed); try { await invoke("fs_create_file", { path }); onCreated(path); onOpenChange(false); } catch (e) { setError(String(e)); } }; return ( New file Filename (relative to workspace root). The extension determines the language mode. { setName(e.target.value); setError(null); }} onKeyDown={(e) => { if (e.key === "Enter") { e.preventDefault(); void submit(); } }} placeholder="example.ts" /> {error ? (
{error}
) : (
{rootPath ? joinPath(rootPath, name.trim() || "…") : "—"}
)}
); }