-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
59 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,9 @@ | ||
/** | ||
* Promise that will resolve after interval milliseconds | ||
* @param ms - Delay in milliseconds | ||
* @returns Promise that will resolve after the delay | ||
*/ | ||
|
||
export const delay = (ms: number): Promise<void> => { | ||
return new Promise((resolve) => setTimeout(resolve, ms)) | ||
} |
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
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,82 @@ | ||
import React, { | ||
FC, | ||
ReactNode, | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useState, | ||
} from "react" | ||
import browser from "webextension-polyfill" | ||
|
||
import { delay } from "../../shared/utils/delay" | ||
import { useAppState } from "../app.state" | ||
|
||
export interface ISoftReloadContext { | ||
key: string | ||
softReload: () => void | ||
} | ||
|
||
export const SoftReloadContext = createContext<ISoftReloadContext | null>(null) | ||
|
||
export const useSoftReloadContext = () => | ||
useContext(SoftReloadContext) as ISoftReloadContext | ||
|
||
/** the key returned should be different each time, does not need to be a unique id */ | ||
const makeReloadKey = () => Date.now().toString() | ||
|
||
const SoftReloadProvider: FC<{ children: ReactNode }> = ({ children }) => { | ||
const [key, setKey] = useState(makeReloadKey()) | ||
|
||
const softReload = useCallback(() => { | ||
useAppState.setState({ isLoading: true, isFirstRender: true }) | ||
setKey(makeReloadKey()) | ||
}, []) | ||
|
||
/** changing the key causes the children to re-render naturally */ | ||
return ( | ||
<SoftReloadContext.Provider value={{ key, softReload }}> | ||
<React.Fragment key={key}>{children}</React.Fragment> | ||
</SoftReloadContext.Provider> | ||
) | ||
} | ||
|
||
export default SoftReloadProvider | ||
|
||
/** re-render the component tree by changing the `key`, without re-loading the HTML page */ | ||
export const useSoftReload = () => { | ||
const { softReload } = useSoftReloadContext() | ||
return softReload | ||
} | ||
|
||
/** re-load the HTML page, can have some side-effects including localStorage+SWR state pollution */ | ||
export const useHardReload = () => { | ||
return useCallback(() => { | ||
const url = browser.runtime.getURL("index.html") | ||
window.location.href = url | ||
}, []) | ||
} | ||
|
||
/** reset local storage while preserving some UI state */ | ||
|
||
export const RESET_CACHE_OMIT_KEYS = ["backupDownload", "networkStatuses-all"] | ||
|
||
export const useResetCache = () => { | ||
return useCallback(() => { | ||
const clearKeys = Object.keys(localStorage).filter( | ||
(key) => !RESET_CACHE_OMIT_KEYS.includes(key), | ||
) | ||
clearKeys.forEach((key) => localStorage.removeItem(key)) | ||
}, []) | ||
} | ||
|
||
/** reset and reload combo */ | ||
export const useHardResetAndReload = () => { | ||
const resetCache = useResetCache() | ||
const hardReload = useHardReload() | ||
return useCallback(async () => { | ||
resetCache() | ||
// delay should allow state to persist | ||
await delay(100) | ||
hardReload() | ||
}, [hardReload, resetCache]) | ||
} |