refactor: reduce browser storage dependence, remove unused code

This commit is contained in:
sethburkart123
2024-06-09 18:04:26 +10:00
parent db98f24555
commit 736cf36068
9 changed files with 53 additions and 42 deletions
+8 -12
View File
@@ -3,11 +3,11 @@ import localforage from 'localforage';
import { CustomTheme } from '../../../interface/types/CustomThemes';
import { applyTheme } from './applyTheme';
import { removeTheme } from './removeTheme';
import { settingsState } from '../../utils/listeners/SettingsState';
export const setTheme = async (themeId: string) => {
try {
const enabledTheme = await browser.storage.local.get('selectedTheme') as { selectedTheme: string; };
const theme = await localforage.getItem(themeId) as CustomTheme;
console.debug('Loading theme', theme);
@@ -17,25 +17,21 @@ export const setTheme = async (themeId: string) => {
const styleElement = document.getElementById('custom-theme');
// Remove the currently enabled theme
if (enabledTheme.selectedTheme || styleElement) {
const currentTheme = await localforage.getItem(enabledTheme.selectedTheme) as CustomTheme;
if (settingsState.selectedTheme || styleElement) {
const currentTheme = await localforage.getItem(settingsState.selectedTheme) as CustomTheme;
if (currentTheme) {
await removeTheme(currentTheme);
}
const color = await browser.storage.local.get('originalSelectedColor') as { originalSelectedColor: string; };
originalSelectedColor = { selectedColor: color.originalSelectedColor };
originalSelectedColor = { selectedColor: settingsState.originalSelectedColor };
} else {
originalSelectedColor = await browser.storage.local.get('selectedColor') as { selectedColor: string; };
originalSelectedColor = { selectedColor: settingsState.selectedColor };
}
await applyTheme(theme);
await browser.storage.local.set({
selectedTheme: themeId,
selectedColor: theme.selectedColor ? theme.selectedColor : (theme.defaultColour !== '' ? theme.defaultColour : '#007bff'),
originalSelectedColor: originalSelectedColor.selectedColor
});
settingsState.selectedTheme = themeId
settingsState.selectedColor = theme.selectedColor ? theme.selectedColor : (theme.defaultColour !== '' ? theme.defaultColour : '#007bff')
settingsState.originalSelectedColor = originalSelectedColor.selectedColor
} catch (error) {
console.error('Error setting theme:', error);
}