forked from ansh/jiffyreader.com
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix: broken getShortcut on firefox * Chore: update runtimehandler refs * Refactor: move debugShow show to context with context selector Update: old and new ui to ref useDebugShow
- Loading branch information
1 parent
1f9e908
commit 663cc12
Showing
6 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Storage } from '@plasmohq/storage'; | ||
import { createContext, useContext, useEffect, useReducer } from 'react'; | ||
|
||
let storage = { | ||
key: 'context.store', | ||
api: new Storage({ area: 'local' }), | ||
get value() { | ||
return this.api.get(this.key) as typeof rawValues; | ||
}, | ||
set value(newStore) { | ||
this.api.set(this.key, newStore); | ||
}, | ||
}; | ||
|
||
const rawValues = { | ||
isDebugDataVisible: !/production/i.test(process.env.NODE_ENV), | ||
}; | ||
|
||
const initialPopupContextValue = { | ||
...rawValues, | ||
dispatch: function <Tkey extends keyof typeof rawValues>([action, value]: [Tkey, typeof rawValues[Tkey]]) { | ||
return; | ||
}, | ||
}; | ||
|
||
function reducer<Tkey extends keyof typeof rawValues>(state, [action, data]: [Tkey, typeof rawValues[Tkey]]): typeof rawValues { | ||
let result = state; | ||
switch (action) { | ||
case 'isDebugDataVisible': { | ||
result = { ...state, [action]: data }; | ||
break; | ||
} | ||
} | ||
|
||
storage.value = result; | ||
return result; | ||
} | ||
|
||
const PopupContext = createContext(initialPopupContextValue); | ||
|
||
export default function PopupContextProvider({ children }) { | ||
const [store, dispatch] = useReducer(reducer, initialPopupContextValue); | ||
|
||
const loadFromStorage = () => { | ||
(async () => { | ||
const savedStore: typeof rawValues = await storage.value; | ||
|
||
if (!savedStore) { | ||
return; | ||
} | ||
|
||
for (const [key, val] of Object.entries(savedStore)) { | ||
dispatch([key as keyof typeof store, val]); | ||
} | ||
})(); | ||
}; | ||
|
||
useEffect(loadFromStorage, []); | ||
|
||
return <PopupContext.Provider value={{ ...store, dispatch }}>{children}</PopupContext.Provider>; | ||
} | ||
|
||
export function usePopupContext() { | ||
const { dispatch, ...store } = useContext(PopupContext); | ||
|
||
return { store, dispatch }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters