import React, { useState, useRef, useEffect } from 'react'; import { Config, Wallpaper } from '../types'; import { baseWallpapers } from './utils/baseWallpapers'; import { checkChromeStorageLocalAvailable } from './utils/StorageLocalManager'; import { ConfigurationService } from './services/ConfigurationService'; import GeneralTab from './configuration/GeneralTab'; import ThemeTab from './configuration/ThemeTab'; import ClockTab from './configuration/ClockTab'; import ServerWidgetTab from './configuration/ServerWidgetTab'; interface ConfigurationModalProps { onClose: () => void; onSave: (config: Config) => void; currentConfig: Config; onWallpaperChange: (newConfig: Partial) => void; } const ConfigurationModal: React.FC = ({ onClose, onSave, currentConfig, onWallpaperChange, }) => { const [config, setConfig] = useState(currentConfig); const [activeTab, setActiveTab] = useState('general'); const [userWallpapers, setUserWallpapers] = useState([]); const [chromeStorageAvailable, setChromeStorageAvailable] = useState(false); const [isVisible, setIsVisible] = useState(false); const menuRef = useRef(null); const importInputRef = useRef(null); const isSaving = useRef(false); useEffect(() => { setChromeStorageAvailable(checkChromeStorageLocalAvailable()); setUserWallpapers(ConfigurationService.loadUserWallpapers()); }, []); useEffect(() => { const timer = setTimeout(() => setIsVisible(true), 10); return () => clearTimeout(timer); }, []); useEffect(() => { return () => { if (!isSaving.current) { onWallpaperChange({ currentWallpapers: currentConfig.currentWallpapers }); } }; }, []); useEffect(() => { onWallpaperChange({ currentWallpapers: config.currentWallpapers }); ConfigurationService.resetWallpaperState(); }, [config.currentWallpapers]); const handleClose = () => { setIsVisible(false); setTimeout(onClose, 250); }; const handleConfigChange = (updates: Partial) => { setConfig((prev) => ({ ...prev, ...updates })); }; const handleAddWallpaper = async (name: string, url: string) => { const newWallpaper = await ConfigurationService.addWallpaper(name, url); const updated = [...userWallpapers, newWallpaper]; setUserWallpapers(updated); ConfigurationService.saveUserWallpapers(updated); setConfig((prev) => ({ ...prev, currentWallpapers: [...prev.currentWallpapers, newWallpaper.name], })); }; const handleAddWallpaperFile = async (file: File) => { const newWallpaper = await ConfigurationService.addWallpaperFile(file); const updated = [...userWallpapers, newWallpaper]; setUserWallpapers(updated); ConfigurationService.saveUserWallpapers(updated); setConfig((prev) => ({ ...prev, currentWallpapers: [...prev.currentWallpapers, newWallpaper.name], })); }; const handleDeleteWallpaper = async (wallpaper: Wallpaper) => { try { await ConfigurationService.deleteWallpaper(wallpaper); const updated = userWallpapers.filter((w) => w.name !== wallpaper.name); setUserWallpapers(updated); ConfigurationService.saveUserWallpapers(updated); const newCurrentWallpapers = config.currentWallpapers.filter((n) => n !== wallpaper.name); setConfig((prev) => ({ ...prev, currentWallpapers: newCurrentWallpapers })); onWallpaperChange({ currentWallpapers: newCurrentWallpapers }); } catch (error) { alert('Error deleting wallpaper. Please try again.'); console.error(error); } }; const handleImportConfig = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (!file) return; try { const { config: importedConfig, userWallpapers: importedWallpapers } = await ConfigurationService.importConfig(file); setConfig(importedConfig); setUserWallpapers(importedWallpapers); onWallpaperChange({ currentWallpapers: importedConfig.currentWallpapers || [] }); onSave(importedConfig); alert('Configuration imported successfully. The page will reload to apply all data.'); window.location.reload(); } catch (error) { alert('Could not import configuration. Please use a valid export JSON file.'); console.error(error); } finally { event.target.value = ''; } }; const allWallpapers = [...baseWallpapers, ...userWallpapers]; const tabs = [ { id: 'general', label: 'General' }, { id: 'theme', label: 'Theme' }, { id: 'clock', label: 'Clock' }, { id: 'serverWidget', label: 'Server Widget' }, ]; return (

Configuration

{tabs.map((tab) => ( ))}
{activeTab === 'general' && ( )} {activeTab === 'theme' && ( )} {activeTab === 'clock' && ( )} {activeTab === 'serverWidget' && ( )}
); }; export default ConfigurationModal;