-
Notifications
You must be signed in to change notification settings - Fork 77
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
18 changed files
with
507 additions
and
147 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
50 changes: 50 additions & 0 deletions
50
clients/admin-ui/src/features/common/hooks/useLocalStorage.ts
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,50 @@ | ||
import { Dispatch, SetStateAction, useState } from "react"; | ||
|
||
/* | ||
Design taken from: https://usehooks.com/useLocalStorage/ | ||
*/ | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function useLocalStorage<T = string>( | ||
key: string, | ||
initialValue: T | ||
): [T, Dispatch<SetStateAction<T>>] { | ||
// State to store our value | ||
// Pass initial state function to useState so logic is only executed once | ||
const [storedValue, setStoredValue] = useState<T>(() => { | ||
if (typeof window === "undefined") { | ||
return initialValue; | ||
} | ||
try { | ||
// Get from local storage by key | ||
const item = window.localStorage.getItem(key); | ||
// Parse stored json or if none return initialValue | ||
return item ? JSON.parse(item) : initialValue; | ||
} catch (error) { | ||
// If error also return initialValue | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
return initialValue; | ||
} | ||
}); | ||
// Return a wrapped version of useState's setter function that ... | ||
// ... persists the new value to localStorage. | ||
const setValue = (value: T | ((x: T) => T)) => { | ||
try { | ||
// Allow value to be a function so we have same API as useState | ||
const valueToStore = | ||
value instanceof Function ? value(storedValue) : value; | ||
// Save state | ||
setStoredValue(valueToStore); | ||
// Save to local storage | ||
if (typeof window !== "undefined") { | ||
window.localStorage.setItem(key, JSON.stringify(valueToStore)); | ||
} | ||
} catch (error) { | ||
// A more advanced implementation would handle the error case | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
} | ||
}; | ||
return [storedValue, setValue]; | ||
} |
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
Oops, something went wrong.