"use client"; import { useRef, useEffect } from "react"; import { useTranslations } from "next-intl"; import { X } from "lucide-react"; import { Button } from "@/components/ui/button"; import { SettingsSection, SettingItem, ToggleSwitch, RadioGroup } from "@/components/settings/settings-section"; export type FolderLayout = "inline" | "sidebar"; export interface FilesSettings { defaultViewMode: "list" | "grid"; showIcons: boolean; coloredIcons: boolean; defaultSortKey: "name" | "size" | "modified"; defaultSortDir: "asc" | "desc"; showHiddenFiles: boolean; showThumbnails: boolean; folderLayout: FolderLayout; } export const DEFAULT_FILES_SETTINGS: FilesSettings = { defaultViewMode: "list", showIcons: true, coloredIcons: true, defaultSortKey: "name", defaultSortDir: "asc", showHiddenFiles: false, showThumbnails: true, folderLayout: "inline", }; export function loadFilesSettings(): FilesSettings { if (typeof window === "undefined") return DEFAULT_FILES_SETTINGS; try { const raw = localStorage.getItem("files-settings"); if (raw) return { ...DEFAULT_FILES_SETTINGS, ...JSON.parse(raw) }; } catch { /* ignore */ } return DEFAULT_FILES_SETTINGS; } export function saveFilesSettings(settings: FilesSettings) { localStorage.setItem("files-settings", JSON.stringify(settings)); // Dispatch custom event for same-tab listeners (StorageEvent only fires cross-tab) window.dispatchEvent(new CustomEvent("files-settings-changed")); } interface FilesSettingsDialogProps { isOpen: boolean; onClose: () => void; settings: FilesSettings; onSettingsChange: (settings: FilesSettings) => void; } export function FilesSettingsDialog({ isOpen, onClose, settings, onSettingsChange }: FilesSettingsDialogProps) { const t = useTranslations("files"); const modalRef = useRef(null); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; if (isOpen) window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [isOpen, onClose]); useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (modalRef.current && !modalRef.current.contains(e.target as Node)) { onClose(); } }; if (isOpen) document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, [isOpen, onClose]); if (!isOpen) return null; const update = (patch: Partial) => { const next = { ...settings, ...patch }; onSettingsChange(next); saveFilesSettings(next); }; return (

{t("settings_title")}

update({ folderLayout: v as FolderLayout })} options={[ { value: "inline", label: t("settings_folder_layout_inline") }, { value: "sidebar", label: t("settings_folder_layout_sidebar") }, ]} /> update({ defaultViewMode: v as "list" | "grid" })} options={[ { value: "list", label: t("list_view") }, { value: "grid", label: t("grid_view") }, ]} /> update({ defaultSortKey: v as "name" | "size" | "modified" })} options={[ { value: "name", label: t("name") }, { value: "size", label: t("size") }, { value: "modified", label: t("modified") }, ]} /> update({ defaultSortDir: v as "asc" | "desc" })} options={[ { value: "asc", label: t("settings_ascending") }, { value: "desc", label: t("settings_descending") }, ]} /> update({ showIcons: v })} /> update({ coloredIcons: v })} disabled={!settings.showIcons} /> update({ showThumbnails: v })} /> update({ showHiddenFiles: v })} />
); }