UNPKG

@fishjam-cloud/react-client

Version:
25 lines (24 loc) 869 B
const getLocalStorageKey = (deviceType) => `last-selected-${deviceType}-device`; export const getLastDevice = (deviceType) => loadObject(getLocalStorageKey(deviceType), null); export const saveLastDevice = (info, deviceType) => saveObject(getLocalStorageKey(deviceType), info); const loadObject = (key, defaultValue) => { const stringValue = loadString(key, ""); if (stringValue === "") { return defaultValue; } return JSON.parse(stringValue); }; const loadString = (key, defaultValue = "") => { const value = localStorage.getItem(key); if (value === null || value === undefined) { return defaultValue; } return value; }; const saveObject = (key, value) => { const stringValue = JSON.stringify(value); saveString(key, stringValue); }; const saveString = (key, value) => { localStorage.setItem(key, value); };