refactor: settingsState relies on main storageState

This commit is contained in:
sethburkart123
2024-06-19 09:47:33 +10:00
parent 2d4e02d0df
commit 9d13879936
4 changed files with 23 additions and 17 deletions
+10 -8
View File
@@ -237,10 +237,10 @@ function SetStorageValue(object: any) {
} }
} }
function UpdateCurrentValues() { async function UpdateCurrentValues() {
const result = browser.storage.local.get() try {
function open (items: any) { const items = await browser.storage.local.get();
var CurrentValues = items; const CurrentValues = items;
const NewValue = Object.assign({}, DefaultValues, CurrentValues); const NewValue = Object.assign({}, DefaultValues, CurrentValues);
@@ -248,13 +248,12 @@ function UpdateCurrentValues() {
for (let i in element) { for (let i in element) {
if (typeof element[i] === 'object') { if (typeof element[i] === 'object') {
// @ts-expect-error // @ts-expect-error
if (typeof DefaultValues[i].length == 'undefined') { if (!Array.isArray(DefaultValues[i])) {
// @ts-expect-error // @ts-expect-error
NewValue[i] = Object.assign({}, DefaultValues[i], CurrentValues[i]); NewValue[i] = Object.assign({}, DefaultValues[i], CurrentValues[i]);
} else { } else {
// If the object is an array, turn it back after
// @ts-expect-error // @ts-expect-error
let length = DefaultValues[i].length; const length = DefaultValues[i].length;
// @ts-expect-error // @ts-expect-error
NewValue[i] = Object.assign({}, DefaultValues[i], CurrentValues[i]); NewValue[i] = Object.assign({}, DefaultValues[i], CurrentValues[i]);
let NewArray = []; let NewArray = [];
@@ -266,6 +265,7 @@ function UpdateCurrentValues() {
} }
} }
} }
CheckInnerElement(DefaultValues); CheckInnerElement(DefaultValues);
if (items['customshortcuts']) { if (items['customshortcuts']) {
@@ -273,8 +273,10 @@ function UpdateCurrentValues() {
} }
SetStorageValue(NewValue); SetStorageValue(NewValue);
console.log('[BetterSEQTA+] Values updated successfully');
} catch (error) {
console.error('[BetterSEQTA+] Error updating values:', error);
} }
result.then(open, console.error)
} }
browser.runtime.onInstalled.addListener(function (event) { browser.runtime.onInstalled.addListener(function (event) {
+2 -1
View File
@@ -23,7 +23,8 @@ export const SettingsContextProvider: React.FC<{ children: ReactNode }> = ({ chi
shortcuts: [], shortcuts: [],
customshortcuts: [], customshortcuts: [],
transparencyEffects: false, transparencyEffects: false,
selectedTheme: '' selectedTheme: '',
animations: true
}); });
const [showPicker, setShowPicker] = useState<boolean>(false); const [showPicker, setShowPicker] = useState<boolean>(false);
+8 -6
View File
@@ -1,7 +1,7 @@
import browser from 'webextension-polyfill' import browser from 'webextension-polyfill'
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
import { SettingsProps } from "../types/SettingsProps"; import { SettingsProps } from "../types/SettingsProps";
import { MainConfig, SettingsState } from "../types/AppProps"; import { SettingsState } from "../types/AppProps";
import { SettingsState as StorageSettingsState } from '../../types/storage'; import { SettingsState as StorageSettingsState } from '../../types/storage';
let RanOnce = false; let RanOnce = false;
@@ -25,7 +25,8 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
customshortcuts: result.customshortcuts, customshortcuts: result.customshortcuts,
transparencyEffects: result.transparencyEffects, transparencyEffects: result.transparencyEffects,
selectedTheme: result.selectedTheme, selectedTheme: result.selectedTheme,
timeFormat: result.timeFormat timeFormat: result.timeFormat,
animations: result.animations
}); });
}); });
}); });
@@ -41,7 +42,8 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
"customshortcuts": "customshortcuts", "customshortcuts": "customshortcuts",
"transparencyEffects": "transparencyEffects", "transparencyEffects": "transparencyEffects",
"selectedTheme": "selectedTheme", "selectedTheme": "selectedTheme",
"timeFormat": "timeFormat" "timeFormat": "timeFormat",
"animations": "animations"
}), []); }), []);
const storageChangeListener = (changes: browser.Storage.StorageChange) => { const storageChangeListener = (changes: browser.Storage.StorageChange) => {
@@ -55,7 +57,7 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
} }
// @ts-expect-error - TODO: Fix this // @ts-expect-error - TODO: Fix this
const stateKey = keyToStateMap[key as keyof MainConfig]; const stateKey = keyToStateMap[key as keyof StorageSettingsState];
if (stateKey) { if (stateKey) {
setSettingsState((prevState: SettingsState) => ({ setSettingsState((prevState: SettingsState) => ({
...prevState, ...prevState,
@@ -73,7 +75,7 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
}; };
}); });
const setStorage = (key: keyof MainConfig, value: any) => { const setStorage = (key: keyof StorageSettingsState, value: any) => {
browser.storage.local.set({ [key]: value }); browser.storage.local.set({ [key]: value });
} }
@@ -84,7 +86,7 @@ const useSettingsState = ({ settingsState, setSettingsState }: SettingsProps) =>
const storageKey = Object.keys(keyToStateMap).find(k => keyToStateMap[k] === key); const storageKey = Object.keys(keyToStateMap).find(k => keyToStateMap[k] === key);
// @ts-expect-error - TODO: Fix this // @ts-expect-error - TODO: Fix this
if (storageKey && value !== previousSettingsState[key]) { if (storageKey && value !== previousSettingsState[key]) {
setStorage(storageKey as keyof MainConfig, value); setStorage(storageKey as keyof StorageSettingsState, value);
} }
} }
} }
+3 -2
View File
@@ -10,6 +10,7 @@ export interface SettingsState {
customshortcuts: CustomShortcut[]; customshortcuts: CustomShortcut[];
transparencyEffects: boolean; transparencyEffects: boolean;
timeFormat?: string; timeFormat?: string;
animations: boolean;
} }
interface ToggleItem { interface ToggleItem {
@@ -27,7 +28,7 @@ export interface CustomShortcut {
icon: string; icon: string;
} }
export interface MainConfig { /* export interface MainConfig {
DarkMode: boolean; DarkMode: boolean;
animatedbk: boolean; animatedbk: boolean;
bksliderinput: string; bksliderinput: string;
@@ -60,4 +61,4 @@ export interface MainConfig {
subjectfilters: Record<string, any>; subjectfilters: Record<string, any>;
transparencyEffects: boolean; transparencyEffects: boolean;
selectedTheme: string; selectedTheme: string;
} } */