import { useEffect, useState } from "react"; import themesList from '../assets/themes'; import { listThemes, disableTheme, downloadTheme, setTheme, deleteTheme } from "../hooks/ThemeManagment"; interface Theme { name: string; url: string; isDownloaded: boolean; isLoading: boolean; coverImage: JSX.Element; } interface ThemeSelectorProps { selectedType: "background" | "theme"; setSelectedType: (type: "background" | "theme") => void; isEditMode: boolean; } const ThemeSelector = ({ selectedType, setSelectedType, isEditMode }: ThemeSelectorProps) => { const [themes, setThemes] = useState([]); const [enabledThemeName, setEnabledThemeName] = useState(''); useEffect(() => { const initializeThemes = async () => { const downloaded = (await listThemes()); const initializedThemes = themesList.map(theme => ({ ...theme, isDownloaded: downloaded.themes.includes(theme.name), isLoading: false })); if (downloaded.selectedTheme !== '') { setEnabledThemeName(downloaded.selectedTheme); } initializedThemes.sort((a, b) => Number(b.isDownloaded) - Number(a.isDownloaded)); setThemes(initializedThemes); }; initializeThemes(); }, []); const handleDeleteTheme = async (themeName: string) => { await deleteTheme(themeName); setThemes(prevThemes => { // Update the theme's isDownloaded property to false const updatedThemes = prevThemes.map(theme => { if (theme.name === themeName) { return { ...theme, isDownloaded: false }; } return theme; }); // Sort themes so non-downloaded ones appear last updatedThemes.sort((a, b) => Number(b.isDownloaded) - Number(a.isDownloaded)); return updatedThemes; }); // Reset the enabled theme name if the deleted theme was the currently enabled one if (enabledThemeName === themeName) { setEnabledThemeName(''); setSelectedType('background'); } }; const handleThemeAction = async (themeName: string, themeURL: string) => { const startLoading = (name: string) => ( setThemes(prevThemes => prevThemes.map(theme => theme.name === name ? { ...theme, isLoading: true } : theme )) ); // Stop loading for the selected theme. const stopLoading = (name: string) => ( setThemes(prevThemes => prevThemes.map(theme => theme.name === name ? { ...theme, isLoading: false } : theme )) ); // Update the theme as downloaded. const markAsDownloaded = (name: string) => ( setThemes(prevThemes => prevThemes.map(theme => theme.name === name ? { ...theme, isDownloaded: true } : theme )) ); startLoading(themeName); // Early return if theme is not found. const theme = themes.find(t => t.name === themeName); if (!theme) { stopLoading(themeName); return; } // If theme is downloaded and is the currently enabled theme, disable it. if (theme.isDownloaded && themeName === enabledThemeName) { await disableTheme(); setEnabledThemeName(''); setSelectedType('background'); stopLoading(themeName); return; } // If theme is downloaded but not enabled, enable it. if (theme.isDownloaded && themeName !== enabledThemeName) { await setTheme(themeName, themeURL); setEnabledThemeName(themeName); setSelectedType('theme'); stopLoading(themeName); return; } // If theme is not downloaded, download and enable it. if (!theme.isDownloaded) { await downloadTheme(themeName, themeURL); markAsDownloaded(themeName); setSelectedType('theme'); setEnabledThemeName(themeName); } stopLoading(themeName); }; useEffect(() => { if (selectedType === 'background') { setEnabledThemeName(''); } }, [selectedType]); return (
{(isEditMode ? themes.some(theme => theme.isDownloaded) : themes.length > 0) && (

Themes

)}
{themes .filter(theme => !isEditMode || theme.isDownloaded) // Only show downloaded themes in edit mode .map((theme) => ( ))}
); }; export default ThemeSelector;