Skip to content

Commit

Permalink
fix: use chrome.storage.sync to store config properly
Browse files Browse the repository at this point in the history
fixes #9
  • Loading branch information
linonetwo committed Jan 10, 2024
1 parent 53a5163 commit 5aef9c6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/shared/preferences/store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import { wrapStore } from 'webext-zustand';
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { IContent } from '../../popup/hooks/useTransformFormat';
import { getExtensionStorage } from '../../utils/extensionStorage';

export interface PreferenceState {
defaultTagsForAssets: string[];
Expand Down Expand Up @@ -45,6 +46,7 @@ export const usePreferenceStore = create<PreferenceState & PreferenceActions>()(
}),
{
name: 'preference-storage',
storage: createJSONStorage(() => getExtensionStorage('preference-storage')),
},
),
)),
Expand Down
5 changes: 4 additions & 1 deletion src/shared/server/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import cloneDeepRaw from 'rfdc';
import { wrapStore } from 'webext-zustand';
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { createJSONStorage, devtools, persist } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { getExtensionStorage } from '../../utils/extensionStorage';

const cloneDeep = cloneDeepRaw();

Check warning on line 9 in src/shared/server/store.ts

View workflow job for this annotation

GitHub Actions / lint

'cloneDeep' is assigned a value but never used. Allowed unused vars must match /^_/u

Check warning on line 9 in src/shared/server/store.ts

View workflow job for this annotation

GitHub Actions / lint

'cloneDeep' is assigned a value but never used

Expand Down Expand Up @@ -49,6 +50,7 @@ export const useServerStore = create<ServerState & ServerActions>()(
setActive: (id, active) => {
set((state) => {
const server = state.servers[id];

if (server) {
server.active = active;
}
Expand Down Expand Up @@ -95,6 +97,7 @@ export const useServerStore = create<ServerState & ServerActions>()(
}),
{
name: 'server-storage',
storage: createJSONStorage(() => getExtensionStorage('server-storage')),
},
),
)),
Expand Down
18 changes: 18 additions & 0 deletions src/utils/extensionStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable security-node/detect-crlf */
import { StateStorage } from 'zustand/middleware';

export const getExtensionStorage = (storageName: string): StateStorage => ({
getItem: async (key: string): Promise<string | null> => {
console.log(key, `has been retrieved in ${storageName}`);
const result = await chrome.storage.sync.get(`${storageName}-${key}`);
return result[`${storageName}-${key}`] as string | null;
},
setItem: async (key: string, value: string): Promise<void> => {
console.log(key, 'with value', value, `has been saved in ${storageName}`);
await chrome.storage.sync.set({ [`${storageName}-${key}`]: value });
},
removeItem: async (key: string): Promise<void> => {
console.log(key, `has been deleted from ${storageName}`);
await chrome.storage.sync.remove(`${storageName}-${key}`);
},
});

0 comments on commit 5aef9c6

Please sign in to comment.