diff --git a/interface/src/components/ThemeSelector.tsx b/interface/src/components/ThemeSelector.tsx index 5a5d2a3e..4d241495 100644 --- a/interface/src/components/ThemeSelector.tsx +++ b/interface/src/components/ThemeSelector.tsx @@ -1,3 +1,5 @@ +/* global chrome */ + import { useEffect, useState } from "react"; import themesList from '../assets/themes'; @@ -55,8 +57,16 @@ const listThemes = async () => { return response.themes; } +const disableTheme = async () => { + await chrome.runtime.sendMessage({ + type: 'currentTab', + info: 'DisableTheme', + }); +}; + const ThemeSelector = () => { const [themes, setThemes] = useState([]); + const [enabledThemeName, setEnabledThemeName] = useState(''); useEffect(() => { const initializeThemes = async () => { @@ -75,28 +85,61 @@ const ThemeSelector = () => { }, []); const handleThemeAction = async (themeName: string, themeURL: string) => { - // Find the theme in the state and set its loading property to true - setThemes(prevThemes => prevThemes.map(theme => - theme.name === themeName ? { ...theme, isLoading: true } : theme - )); - - // Call the appropriate method based on whether the theme is downloaded + // Start loading for the selected theme. + 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 && theme.isDownloaded) { - await setTheme(themeName, themeURL); - } else { - await downloadTheme(themeName, themeURL); - // After downloading, update the theme to be marked as downloaded - setThemes(prevThemes => prevThemes.map(t => - t.name === themeName ? { ...t, isDownloaded: true } : t - )); + if (!theme) { + stopLoading(themeName); + return; } - - // Once the action is complete, set the theme's loading property to false - setThemes(prevThemes => prevThemes.map(theme => - theme.name === themeName ? { ...theme, isLoading: false } : theme - )); - }; + + // If theme is downloaded and is the currently enabled theme, disable it. + if (theme.isDownloaded && themeName === enabledThemeName) { + await disableTheme(); + setEnabledThemeName(''); + stopLoading(themeName); + return; + } + + // If theme is downloaded but not enabled, enable it. + if (theme.isDownloaded && themeName !== enabledThemeName) { + await setTheme(themeName, themeURL); + setEnabledThemeName(themeName); + stopLoading(themeName); + return; + } + + // If theme is not downloaded, download and enable it. + if (!theme.isDownloaded) { + await downloadTheme(themeName, themeURL); + markAsDownloaded(themeName); + setEnabledThemeName(themeName); + } + + stopLoading(themeName); + }; return (
@@ -105,7 +148,7 @@ const ThemeSelector = () => { {themes.map((theme) => (