From 38c31b88e68e62d44dfa524ecbc26a017494b08e Mon Sep 17 00:00:00 2001 From: SethBurkart123 Date: Fri, 5 Apr 2024 08:43:33 +1100 Subject: [PATCH] sync up popup selectedTheme to storage --- src/interface/components/ThemeSelector.tsx | 32 ++++++++++++++-------- src/interface/hooks/settingsState.ts | 5 ++-- src/interface/types/AppProps.ts | 4 +-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/interface/components/ThemeSelector.tsx b/src/interface/components/ThemeSelector.tsx index 9f3d9555..6fefe001 100644 --- a/src/interface/components/ThemeSelector.tsx +++ b/src/interface/components/ThemeSelector.tsx @@ -3,6 +3,8 @@ import { listThemes, deleteTheme, setTheme, disableTheme } from '../hooks/ThemeM import { ThemeCover } from './ThemeCover'; import Browser from 'webextension-polyfill'; import { CustomTheme } from '../types/CustomThemes'; +import { useSettingsContext } from '../SettingsContext'; +import { SettingsState } from '../types/AppProps'; interface ThemeSelectorProps { isEditMode: boolean; @@ -12,12 +14,20 @@ interface ThemeSelectorProps { const ThemeSelector: ForwardRefExoticComponent & RefAttributes> = forwardRef(({ isEditMode = false }, ref) => { const [themes, setThemes] = useState[]>([]); const [isLoading, setIsLoading] = useState(true); - const [selectedThemeId, setSelectedThemeId] = useState(null); + const { settingsState, setSettingsState } = useSettingsContext(); + + const setSelectedTheme = (themeId: string) => { + setSettingsState((prevState: SettingsState) => ({ + ...prevState, + selectedTheme: themeId, + })); + } + useImperativeHandle(ref, () => ({ disableTheme: async () => { await disableTheme(); - setSelectedThemeId(null); + setSelectedTheme(''); } })); @@ -27,7 +37,7 @@ const ThemeSelector: ForwardRefExoticComponent & const { themes, selectedTheme } = await listThemes(); setThemes(themes); - setSelectedThemeId(selectedTheme ? selectedTheme : null); + setSelectedTheme(selectedTheme ? selectedTheme : ''); } catch (error) { console.error('Error fetching themes:', error); } finally { @@ -40,18 +50,18 @@ const ThemeSelector: ForwardRefExoticComponent & const handleThemeSelect = useCallback( async (themeId: string) => { - if (themeId === selectedThemeId) { + if (themeId === settingsState.selectedTheme) { await disableTheme(); - setSelectedThemeId(null); + setSelectedTheme(''); } else { const selectedTheme = themes.find((theme) => theme.id === themeId); if (selectedTheme) { await setTheme(selectedTheme.id); - setSelectedThemeId(themeId); + setSelectedTheme(themeId); } } }, - [selectedThemeId, themes] + [settingsState.selectedTheme, themes] ); const handleThemeDelete = useCallback( @@ -59,14 +69,14 @@ const ThemeSelector: ForwardRefExoticComponent & try { await deleteTheme(themeId); setThemes((prevThemes) => prevThemes.filter((theme) => theme.id !== themeId)); - if (themeId === selectedThemeId) { - setSelectedThemeId(null); + if (themeId === settingsState.selectedTheme) { + setSelectedTheme(''); } } catch (error) { console.error('Error deleting theme:', error); } }, - [selectedThemeId] + [settingsState.selectedTheme] ); if (isLoading) { @@ -81,7 +91,7 @@ const ThemeSelector: ForwardRefExoticComponent & shortcuts: result.shortcuts, customshortcuts: result.customshortcuts, transparencyEffects: result.transparencyEffects, - theme: result.theme + selectedTheme: result.selectedTheme }); }); }); @@ -39,7 +39,8 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) => "onoff": "betterSEQTAPlus", "shortcuts": "shortcuts", "customshortcuts": "customshortcuts", - "transparencyEffects": "transparencyEffects" + "transparencyEffects": "transparencyEffects", + "selectedTheme": "selectedTheme" }), []); const storageChangeListener = (changes: browser.Storage.StorageChange) => { diff --git a/src/interface/types/AppProps.ts b/src/interface/types/AppProps.ts index 0ec056c2..582e31b2 100644 --- a/src/interface/types/AppProps.ts +++ b/src/interface/types/AppProps.ts @@ -1,6 +1,6 @@ export interface SettingsState { notificationCollector: boolean; - theme: string; + selectedTheme: string; lessonAlerts: boolean; telemetry: boolean; animatedBackground: boolean; @@ -60,5 +60,5 @@ export interface MainConfig { shortcuts: Shortcut[]; subjectfilters: Record; transparencyEffects: boolean; - theme: string; + selectedTheme: string; }