-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from SeanCassiere/SeanCassiere/feat-add-sideba…
…r-toggle-func feat: add toggle sidebar functionality
- Loading branch information
Showing
5 changed files
with
127 additions
and
10 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
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
88 changes: 88 additions & 0 deletions
88
packages/trpc-panel/src/react-app/components/hooks/useLocalStorage.tsx
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,88 @@ | ||
// a stripped down version of https://usehooks-ts.com/react-hook/use-local-storage | ||
import { | ||
useCallback, | ||
useEffect, | ||
useState, | ||
type Dispatch, | ||
type SetStateAction, | ||
} from "react"; | ||
|
||
type SetValue<T> = Dispatch<SetStateAction<T>>; | ||
|
||
export function useLocalStorage<T>( | ||
key: string, | ||
initialValue: T | ||
): [T, SetValue<T>] { | ||
// Get from local storage then | ||
// parse stored json or return initialValue | ||
const readValue = useCallback((): T => { | ||
// Prevent build error "window is undefined" but keeps working | ||
if (typeof window === "undefined") { | ||
return initialValue; | ||
} | ||
|
||
try { | ||
const item = window.localStorage.getItem(key); | ||
return item ? (parseJSON(item) as T) : initialValue; | ||
} catch (error) { | ||
console.warn( | ||
`tRPC-Panel.useLocalStorage: Error reading localStorage key “${key}”:`, | ||
error | ||
); | ||
return initialValue; | ||
} | ||
}, [initialValue, key]); | ||
|
||
// State to store our value | ||
// Pass initial state function to useState so logic is only executed once | ||
const [storedValue, setStoredValue] = useState<T>(readValue); | ||
|
||
// Return a wrapped version of useState's setter function that ... | ||
// ... persists the new value to localStorage. | ||
const setValue: SetValue<T> = useCallback( | ||
(value) => { | ||
// Prevent build error "window is undefined" but keeps working | ||
if (typeof window === "undefined") { | ||
console.warn( | ||
`tRPC-Panel.useLocalStorage: Tried setting localStorage key “${key}” even though environment is not a client` | ||
); | ||
} | ||
|
||
try { | ||
// Allow value to be a function so we have the same API as useState | ||
const newValue = value instanceof Function ? value(storedValue) : value; | ||
|
||
// Save to local storage | ||
window.localStorage.setItem(key, JSON.stringify(newValue)); | ||
|
||
// Save state | ||
setStoredValue(newValue); | ||
} catch (error) { | ||
console.warn( | ||
`tRPC-Panel.useLocalStorage: Error setting localStorage key “${key}”:`, | ||
error | ||
); | ||
} | ||
}, | ||
[storedValue] | ||
); | ||
|
||
useEffect(() => { | ||
setStoredValue(readValue()); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return [storedValue, setValue]; | ||
} | ||
|
||
// A wrapper for "JSON.parse()"" to support "undefined" value | ||
function parseJSON<T>(value: string | null): T | undefined { | ||
try { | ||
return value === "undefined" ? undefined : JSON.parse(value ?? ""); | ||
} catch { | ||
console.log("tRPC-Panel.useLocalStorage.parseJSON: parsing error on", { | ||
value, | ||
}); | ||
return undefined; | ||
} | ||
} |