mirror of
https://github.com/ritz078/transform.git
synced 2026-11-21 00:32:56 +03:00
* feat(init): added EditorPanel with prettier, copy and settings support * chore(static): ignore static * feat: added navigator * feat: * feat(settings): added sessionStorage support for settings * feat(message): add messaging support in editors * feat(preview): added support for preview panel * fix(settings): use localstorage for settings instead of session storage * feat: * feat(css-to-js): implemented React section * feat(schema): add json to schema conversions * feat(others): implemented xml, yaml and markdown conversions * feat(babel): added flow to typescript converter * feat(graphql): implemented graphql to typescript * feat(graphql): added some transformations * fix: add support for different default value in one language * feat(graphql): added components, resolver signatures and mongodb * feat: * scala case class * fix(memory): worker-loader patch applied to fix memory overflow crash * feat: added json schema to typescript * icon: <GraphQLIcon />, * feat(navigator): shorter naming and new categories * route fixes * navigator * perf(workers): create separate file and remove fallback * fix(navigator): completed * build(snack): * fixes * fixes * fix(safari): * added json-to-mobx-state tree * fix(native): snacksession * fixes * added logo * navigator * fixes * custom header * fixes * refactor(settings): move settings to left side * feat: set up redirects * feat(seo): added title and description * feat(error): added custom error page * ga-script * feat: * fixes * fixe * fixes * fixes * fix:
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { useState } from "react";
|
|
|
|
const prefix = "transform:";
|
|
|
|
export function useLocalStorage(key, initialValue) {
|
|
// State to store our value
|
|
// Pass initial state function to useState so logic is only executed once
|
|
|
|
const [storedValue, setStoredValue] = useState(() => {
|
|
try {
|
|
// Get from local storage by key
|
|
const item = IN_BROWSER
|
|
? window.localStorage.getItem(prefix + key) || initialValue
|
|
: initialValue;
|
|
// Parse stored json or if none return initialValue
|
|
return JSON.parse(item);
|
|
} catch (error) {
|
|
// If error also return initialValue
|
|
return initialValue;
|
|
}
|
|
});
|
|
|
|
// Return a wrapped version of useState's setter function that ...
|
|
// ... persists the new value to sessionStorage.
|
|
const setValue = value => {
|
|
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 (IN_BROWSER)
|
|
window.localStorage.setItem(prefix + key, JSON.stringify(valueToStore));
|
|
} catch (error) {
|
|
// A more advanced implementation would handle the error case
|
|
console.log(error);
|
|
}
|
|
};
|
|
|
|
return [storedValue, setValue];
|
|
}
|