sync up popup selectedTheme to storage

This commit is contained in:
SethBurkart123
2024-04-05 08:43:33 +11:00
parent 38e92751ed
commit 38c31b88e6
3 changed files with 26 additions and 15 deletions
+21 -11
View File
@@ -3,6 +3,8 @@ import { listThemes, deleteTheme, setTheme, disableTheme } from '../hooks/ThemeM
import { ThemeCover } from './ThemeCover'; import { ThemeCover } from './ThemeCover';
import Browser from 'webextension-polyfill'; import Browser from 'webextension-polyfill';
import { CustomTheme } from '../types/CustomThemes'; import { CustomTheme } from '../types/CustomThemes';
import { useSettingsContext } from '../SettingsContext';
import { SettingsState } from '../types/AppProps';
interface ThemeSelectorProps { interface ThemeSelectorProps {
isEditMode: boolean; isEditMode: boolean;
@@ -12,12 +14,20 @@ interface ThemeSelectorProps {
const ThemeSelector: ForwardRefExoticComponent<Omit<ThemeSelectorProps, "ref"> & RefAttributes<any>> = forwardRef(({ isEditMode = false }, ref) => { const ThemeSelector: ForwardRefExoticComponent<Omit<ThemeSelectorProps, "ref"> & RefAttributes<any>> = forwardRef(({ isEditMode = false }, ref) => {
const [themes, setThemes] = useState<Omit<CustomTheme, 'CustomImages'>[]>([]); const [themes, setThemes] = useState<Omit<CustomTheme, 'CustomImages'>[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true); const [isLoading, setIsLoading] = useState<boolean>(true);
const [selectedThemeId, setSelectedThemeId] = useState<string | null>(null); const { settingsState, setSettingsState } = useSettingsContext();
const setSelectedTheme = (themeId: string) => {
setSettingsState((prevState: SettingsState) => ({
...prevState,
selectedTheme: themeId,
}));
}
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
disableTheme: async () => { disableTheme: async () => {
await disableTheme(); await disableTheme();
setSelectedThemeId(null); setSelectedTheme('');
} }
})); }));
@@ -27,7 +37,7 @@ const ThemeSelector: ForwardRefExoticComponent<Omit<ThemeSelectorProps, "ref"> &
const { themes, selectedTheme } = await listThemes(); const { themes, selectedTheme } = await listThemes();
setThemes(themes); setThemes(themes);
setSelectedThemeId(selectedTheme ? selectedTheme : null); setSelectedTheme(selectedTheme ? selectedTheme : '');
} catch (error) { } catch (error) {
console.error('Error fetching themes:', error); console.error('Error fetching themes:', error);
} finally { } finally {
@@ -40,18 +50,18 @@ const ThemeSelector: ForwardRefExoticComponent<Omit<ThemeSelectorProps, "ref"> &
const handleThemeSelect = useCallback( const handleThemeSelect = useCallback(
async (themeId: string) => { async (themeId: string) => {
if (themeId === selectedThemeId) { if (themeId === settingsState.selectedTheme) {
await disableTheme(); await disableTheme();
setSelectedThemeId(null); setSelectedTheme('');
} else { } else {
const selectedTheme = themes.find((theme) => theme.id === themeId); const selectedTheme = themes.find((theme) => theme.id === themeId);
if (selectedTheme) { if (selectedTheme) {
await setTheme(selectedTheme.id); await setTheme(selectedTheme.id);
setSelectedThemeId(themeId); setSelectedTheme(themeId);
} }
} }
}, },
[selectedThemeId, themes] [settingsState.selectedTheme, themes]
); );
const handleThemeDelete = useCallback( const handleThemeDelete = useCallback(
@@ -59,14 +69,14 @@ const ThemeSelector: ForwardRefExoticComponent<Omit<ThemeSelectorProps, "ref"> &
try { try {
await deleteTheme(themeId); await deleteTheme(themeId);
setThemes((prevThemes) => prevThemes.filter((theme) => theme.id !== themeId)); setThemes((prevThemes) => prevThemes.filter((theme) => theme.id !== themeId));
if (themeId === selectedThemeId) { if (themeId === settingsState.selectedTheme) {
setSelectedThemeId(null); setSelectedTheme('');
} }
} catch (error) { } catch (error) {
console.error('Error deleting theme:', error); console.error('Error deleting theme:', error);
} }
}, },
[selectedThemeId] [settingsState.selectedTheme]
); );
if (isLoading) { if (isLoading) {
@@ -81,7 +91,7 @@ const ThemeSelector: ForwardRefExoticComponent<Omit<ThemeSelectorProps, "ref"> &
<ThemeCover <ThemeCover
key={theme.id} key={theme.id}
theme={theme} theme={theme}
isSelected={theme.id === selectedThemeId} isSelected={theme.id === settingsState.selectedTheme}
isEditMode={isEditMode} isEditMode={isEditMode}
onThemeSelect={handleThemeSelect} onThemeSelect={handleThemeSelect}
onThemeDelete={handleThemeDelete} onThemeDelete={handleThemeDelete}
+3 -2
View File
@@ -24,7 +24,7 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
shortcuts: result.shortcuts, shortcuts: result.shortcuts,
customshortcuts: result.customshortcuts, customshortcuts: result.customshortcuts,
transparencyEffects: result.transparencyEffects, transparencyEffects: result.transparencyEffects,
theme: result.theme selectedTheme: result.selectedTheme
}); });
}); });
}); });
@@ -39,7 +39,8 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
"onoff": "betterSEQTAPlus", "onoff": "betterSEQTAPlus",
"shortcuts": "shortcuts", "shortcuts": "shortcuts",
"customshortcuts": "customshortcuts", "customshortcuts": "customshortcuts",
"transparencyEffects": "transparencyEffects" "transparencyEffects": "transparencyEffects",
"selectedTheme": "selectedTheme"
}), []); }), []);
const storageChangeListener = (changes: browser.Storage.StorageChange) => { const storageChangeListener = (changes: browser.Storage.StorageChange) => {
+2 -2
View File
@@ -1,6 +1,6 @@
export interface SettingsState { export interface SettingsState {
notificationCollector: boolean; notificationCollector: boolean;
theme: string; selectedTheme: string;
lessonAlerts: boolean; lessonAlerts: boolean;
telemetry: boolean; telemetry: boolean;
animatedBackground: boolean; animatedBackground: boolean;
@@ -60,5 +60,5 @@ export interface MainConfig {
shortcuts: Shortcut[]; shortcuts: Shortcut[];
subjectfilters: Record<string, any>; subjectfilters: Record<string, any>;
transparencyEffects: boolean; transparencyEffects: boolean;
theme: string; selectedTheme: string;
} }