Files
flipper/desktop/app/src/utils/useLocalStorage.tsx
Michel Weststrate 95d319a700 Store collapsed status of sidebar sections
Summary:
See previous diff, let's store the collapsed state of sidebar sections in local storage.

Introduced a reusable hook to take care of that.

Changelog: Device plugins are now expanded by default, and the expand / collapse state will now be remembered across restarts

Reviewed By: passy

Differential Revision: D21903394

fbshipit-source-id: a3c0231acc0aa0877522ec328eedd09cb11aedb1
2020-06-05 08:30:44 -07:00

63 lines
1.8 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {useState, useCallback} from 'react';
export function useLocalStorage<T>(
key: string,
initialValue: (() => T) | T,
): [T, (newState: T | ((current: T) => T)) => void] {
const [storedKey] = useState(key);
if (storedKey !== key) {
throw new Error(
`The key passed to useLocalStorage should not be changed, '${storedKey}' -> '${key}'`,
);
}
// Based on https://usehooks.com/useLocalStorage/ (with minor adaptions)
// State to store our value
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState<T>(() => {
try {
// Get from local storage by key
const item = window.localStorage.getItem('[useLocalStorage]' + key);
// Parse stored json or if none return initialValue
return item
? JSON.parse(item)
: typeof initialValue === 'function'
? (initialValue as any)()
: initialValue;
} catch (error) {
// If error also return initialValue
console.log(error);
return initialValue;
}
});
// Return a wrapped version of useState's setter function that ...
// ... persists the new value to localStorage.
const setValue = useCallback(
(value) => {
setStoredValue((storedValue) => {
const nextValue =
typeof value === 'function' ? value(storedValue) : value;
// Save to local storage
window.localStorage.setItem(
'[useLocalStorage]' + key,
JSON.stringify(nextValue),
);
return nextValue;
});
},
[key],
);
return [storedValue, setValue];
}