mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
restructure components & clean up unused code [skip ci]
This commit is contained in:
@@ -5,7 +5,6 @@ export * from './useGreeting';
|
||||
export * from './useLGQuery';
|
||||
export * from './useLGState';
|
||||
export * from './useOpposingColor';
|
||||
export * from './useSessionStorage';
|
||||
export * from './useStrf';
|
||||
export * from './useTableToString';
|
||||
export * from './useScaledText';
|
||||
|
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
react-use: useSessionStorage
|
||||
https://github.com/streamich/react-use/blob/master/src/useSessionStorage.ts
|
||||
*/
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export const useSessionStorage = (key, initialValue, raw) => {
|
||||
const isClient = typeof window === 'object';
|
||||
if (!isClient) {
|
||||
return [initialValue, () => {}];
|
||||
}
|
||||
|
||||
const [state, setState] = useState(() => {
|
||||
try {
|
||||
const sessionStorageValue = sessionStorage.getItem(key);
|
||||
if (typeof sessionStorageValue !== 'string') {
|
||||
sessionStorage.setItem(key, raw ? String(initialValue) : JSON.stringify(initialValue));
|
||||
return initialValue;
|
||||
} else {
|
||||
return raw ? sessionStorageValue : JSON.parse(sessionStorageValue || 'null');
|
||||
}
|
||||
} catch {
|
||||
// If user is in private mode or has storage restriction
|
||||
// sessionStorage can throw. JSON.parse and JSON.stringify
|
||||
// cat throw, too.
|
||||
return initialValue;
|
||||
}
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
const serializedState = raw ? String(state) : JSON.stringify(state);
|
||||
sessionStorage.setItem(key, serializedState);
|
||||
} catch {
|
||||
// If user is in private mode or has storage restriction
|
||||
// sessionStorage can throw. Also JSON.stringify can throw.
|
||||
}
|
||||
});
|
||||
|
||||
return [state, setState];
|
||||
};
|
Reference in New Issue
Block a user