import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { InputWithContext } from "@/components/ui/input-with-context"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { FolderOpen, Save, RotateCcw, Info, Download, Check } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Spinner } from "@/components/ui/spinner"; import { Switch } from "@/components/ui/switch"; import { getSettings, getSettingsWithDefaults, saveSettings, resetToDefaultSettings, applyThemeMode, applyFont, FONT_OPTIONS, type Settings as SettingsType, type FontFamily, type GifQuality, type GifResolution } from "@/lib/settings"; import { themes, applyTheme } from "@/lib/themes"; import { SelectFolder, IsFFmpegInstalled, DownloadFFmpeg, IsExifToolInstalled, DownloadExifTool } from "../../wailsjs/go/main/App"; import { toastWithSound as toast } from "@/lib/toast-with-sound"; export function SettingsPage() { const [savedSettings, setSavedSettings] = useState(getSettings()); const [tempSettings, setTempSettings] = useState(savedSettings); const [isDark, setIsDark] = useState(document.documentElement.classList.contains('dark')); const [ffmpegInstalled, setFfmpegInstalled] = useState(false); const [downloadingFFmpeg, setDownloadingFFmpeg] = useState(false); const [exiftoolInstalled, setExiftoolInstalled] = useState(false); const [downloadingExifTool, setDownloadingExifTool] = useState(false); const [showResetConfirm, setShowResetConfirm] = useState(false); useEffect(() => { applyThemeMode(savedSettings.themeMode); applyTheme(savedSettings.theme); const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); const handleChange = () => { if (savedSettings.themeMode === "auto") { applyThemeMode("auto"); applyTheme(savedSettings.theme); } }; mediaQuery.addEventListener("change", handleChange); return () => mediaQuery.removeEventListener("change", handleChange); }, [savedSettings.themeMode, savedSettings.theme]); useEffect(() => { applyThemeMode(tempSettings.themeMode); applyTheme(tempSettings.theme); applyFont(tempSettings.fontFamily); setTimeout(() => { setIsDark(document.documentElement.classList.contains('dark')); }, 0); }, [tempSettings.themeMode, tempSettings.theme, tempSettings.fontFamily]); useEffect(() => { const loadDefaults = async () => { if (!savedSettings.downloadPath) { const settingsWithDefaults = await getSettingsWithDefaults(); setSavedSettings(settingsWithDefaults); setTempSettings(settingsWithDefaults); } }; loadDefaults(); // Check FFmpeg and ExifTool status IsFFmpegInstalled().then(setFfmpegInstalled); IsExifToolInstalled().then(setExiftoolInstalled); }, []); const handleSave = () => { saveSettings(tempSettings); setSavedSettings(tempSettings); toast.success("Settings saved"); }; const handleReset = async () => { const defaultSettings = await resetToDefaultSettings(); setTempSettings(defaultSettings); setSavedSettings(defaultSettings); applyThemeMode(defaultSettings.themeMode); applyTheme(defaultSettings.theme); applyFont(defaultSettings.fontFamily); setShowResetConfirm(false); toast.success("Settings reset to default"); }; const handleBrowseFolder = async () => { try { const selectedPath = await SelectFolder(tempSettings.downloadPath || ""); if (selectedPath && selectedPath.trim() !== "") { setTempSettings((prev) => ({ ...prev, downloadPath: selectedPath })); } } catch (error) { console.error("Error selecting folder:", error); toast.error(`Error selecting folder: ${error}`); } }; const handleDownloadFFmpeg = async () => { setDownloadingFFmpeg(true); try { await DownloadFFmpeg(); setFfmpegInstalled(true); toast.success("FFmpeg downloaded successfully"); } catch (error) { toast.error("Failed to download FFmpeg"); console.error("Error downloading FFmpeg:", error); } finally { setDownloadingFFmpeg(false); } }; const handleDownloadExifTool = async () => { setDownloadingExifTool(true); try { await DownloadExifTool(); setExiftoolInstalled(true); toast.success("ExifTool downloaded successfully"); } catch (error) { toast.error("Failed to download ExifTool"); console.error("Error downloading ExifTool:", error); } finally { setDownloadingExifTool(false); } }; return (

Settings

{/* Left Column */}
{/* Download Path */}
setTempSettings((prev) => ({ ...prev, downloadPath: e.target.value }))} placeholder="C:\Users\YourUsername\Pictures" />
{/* Theme Mode */}
{/* Accent */}
{/* Font */}
{/* Sound Effects */}
setTempSettings(prev => ({ ...prev, sfxEnabled: checked }))} />
{/* Right Column */}
{/* Metadata Embedding */}
{exiftoolInstalled ? (
Installed
) : ( )}
{/* GIF Conversion */}
{ffmpegInstalled ? (
Installed
) : ( )}
{/* GIF Quality - only show if FFmpeg installed */} {ffmpegInstalled && (
)} {/* Proxy */}
setTempSettings((prev) => ({ ...prev, proxy: e.target.value }))} placeholder="http://proxy:port or socks5://proxy:port (optional)" className="w-[90%]" />
{/* Fetch Timeout */}
{ const inputValue = e.target.value; // Allow empty input for user to type freely if (inputValue === "") { setTempSettings((prev) => ({ ...prev, fetchTimeout: 60 })); return; } const value = parseInt(inputValue, 10); // Allow any number while typing (including values less than min during typing) // Validation will happen on blur if (!isNaN(value)) { setTempSettings((prev) => ({ ...prev, fetchTimeout: value })); } }} onBlur={(e) => { // Validate and clamp value on blur const value = parseInt(e.target.value, 10); if (isNaN(value) || value < 30) { setTempSettings((prev) => ({ ...prev, fetchTimeout: 30 })); } else if (value > 900) { setTempSettings((prev) => ({ ...prev, fetchTimeout: 900 })); } }} placeholder="60" className="w-[20%]" />
{/* Actions */}
{/* Reset Confirmation Dialog */} Reset to Default? This will reset all settings to their default values. Your custom configurations will be lost.
); }