"use client"; import { useEffect, useState } from "react"; import { Loader2, Upload } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { useI18n } from "@/i18n/i18n-context"; import { normalizeFileMemoryPath, MAX_FILE_CONTENT_BYTES, type FileContentValidationError, type FilePathValidationError, validateFileMemoryContent, validateFileMemoryPath, } from "./file-editor"; import { FILE_MEMORY_BINARY_ACCEPT, fileMemoryBinaryErrorKey, } from "./file-binary-content"; export type FileMutationDialog = "create" | "upload" | "rename" | "delete" | null; const VALIDATION_MESSAGE_KEYS = { "path-required": "memory.files.pathRequired", "path-traversal": "memory.files.pathTraversal", "path-too-large": "memory.files.pathTooLarge", "content-too-large": "memory.files.contentTooLarge", } as const; export function FileMutationDialogs({ mode, selectedPath, pending, onClose, onCreate, onUpload, onRename, onDelete, }: { mode: FileMutationDialog; selectedPath: string | null; pending: boolean; onClose: () => void; onCreate: (path: string, content: string) => Promise; onUpload: (path: string, file: File) => Promise; onRename: (path: string) => Promise; onDelete: () => Promise; }) { const { t } = useI18n(); const [path, setPath] = useState(""); const [content, setContent] = useState(""); const [binaryFile, setBinaryFile] = useState(null); const [error, setError] = useState(null); useEffect(() => { setPath(mode === "rename" ? (selectedPath ?? "") : ""); setContent(""); setBinaryFile(null); setError(null); }, [mode, selectedPath]); function validationMessage( code: FilePathValidationError | FileContentValidationError, ): string { return t(VALIDATION_MESSAGE_KEYS[code], { limit: MAX_FILE_CONTENT_BYTES.toLocaleString("en-US"), }); } async function submit() { setError(null); try { if (mode === "delete") { await onDelete(); } else { const pathError = validateFileMemoryPath(path); if (pathError) { setError(validationMessage(pathError)); return; } if (mode === "create") { const contentError = validateFileMemoryContent(content); if (contentError) { setError(validationMessage(contentError)); return; } await onCreate(normalizeFileMemoryPath(path), content); } else if (mode === "upload") { if (!binaryFile) { setError(t("memory.files.binaryFileRequired")); return; } await onUpload(normalizeFileMemoryPath(path), binaryFile); } else if (mode === "rename") { await onRename(normalizeFileMemoryPath(path)); } } onClose(); } catch (submitError) { const binaryErrorKey = fileMemoryBinaryErrorKey(submitError); let message = t("common.unknownError"); if (binaryErrorKey) { message = t(binaryErrorKey, { limit: MAX_FILE_CONTENT_BYTES.toLocaleString("en-US"), }); } else if (submitError instanceof Error) { message = submitError.message; } setError(message); } } const titles = { create: t("memory.files.createTitle"), upload: t("memory.files.uploadTitle"), rename: t("memory.files.renameTitle"), delete: t("memory.files.deleteTitle"), }; const submitLabels = { create: t("common.create"), upload: t("memory.files.upload"), rename: t("memory.files.rename"), delete: t("common.delete"), }; const title = mode ? titles[mode] : ""; const submitLabel = mode ? submitLabels[mode] : ""; return ( { if (!open && !pending) onClose(); }} > {title} {mode === "delete" ? t("memory.files.deleteDescription", { path: selectedPath ?? "", }) : t("memory.files.pathHelp")} {mode !== "delete" ? (
setPath(event.target.value)} placeholder="/notes/profile.md" disabled={pending} />
{mode === "create" ? (