import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { ChevronRight } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/dialog"; import { Button } from "@/components/button"; import { Input } from "@/components/input"; import { DEFAULT_FOLDER_COLOR, DEFAULT_FOLDER_ICON, FolderIconEl, HexColorPicker, IconPicker, } from "@/components/folder-style"; import { normalizePath, splitPath } from "./FolderPathPicker"; import { getCredentials } from "@/main-axios"; export type FolderMetadataValue = { name: string; color: string; icon: string; credentialId: number | null; }; type CredentialOption = { id: string; name: string; username?: string }; export function FolderMetadataDialog({ open, mode, initial, onOpenChange, onSubmit, }: { open: boolean; mode: "create" | "edit"; initial?: { name: string; color?: string; icon?: string; credentialId?: number | null; }; onOpenChange: (v: boolean) => void; onSubmit: (value: FolderMetadataValue) => void; }) { const { t } = useTranslation(); const [name, setName] = useState(""); const [color, setColor] = useState(DEFAULT_FOLDER_COLOR); const [icon, setIcon] = useState(DEFAULT_FOLDER_ICON); const [credentialId, setCredentialId] = useState(""); const [credentials, setCredentials] = useState([]); useEffect(() => { if (open) { setName(initial?.name ?? ""); setColor(initial?.color ?? DEFAULT_FOLDER_COLOR); setIcon(initial?.icon ?? DEFAULT_FOLDER_ICON); setCredentialId( initial?.credentialId ? String(initial.credentialId) : "", ); } }, [open, initial]); useEffect(() => { if (!open) return; getCredentials() .then((data) => { const list = Array.isArray(data) ? data : []; setCredentials( list.map((c) => ({ id: String(c.id), name: String(c.name ?? ""), username: c.username ? String(c.username) : undefined, })), ); }) .catch(() => setCredentials([])); }, [open]); function handleSubmit() { const normalized = normalizePath(name); if (!normalized) return; onSubmit({ name: normalized, color, icon, credentialId: credentialId ? Number(credentialId) : null, }); onOpenChange(false); } return ( {mode === "create" ? t("hosts.createFolderTitle") : t("hosts.editFolderTitle")} {t("hosts.folderDialogDescription")}
setName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") handleSubmit(); }} />

{t("hosts.folderNestingHint")}

{t("hosts.folderCredentialHint")}

{name ? splitPath(name).map((seg, i) => ( {i > 0 && ( )} {seg} )) : t("hosts.folderNameFallback")}
); }