migrate old app settings to multi-account
This commit is contained in:
parent
9ce9e82eb4
commit
79800aeea4
1 changed files with 11 additions and 9 deletions
|
|
@ -87,20 +87,22 @@ const defaultSettings: Settings = {
|
|||
const STORAGE_KEY = 'settings';
|
||||
const getStoreKey = (userId: string): string => `${STORAGE_KEY}${userId}`;
|
||||
|
||||
// export const settingsAtom = atomWithLocalStorage<Settings>(
|
||||
// STORAGE_KEY,
|
||||
// (key) => getLocalStorageItem<Settings>(key, defaultSettings),
|
||||
// (key, settings) => {
|
||||
// setLocalStorageItem(key, settings);
|
||||
// }
|
||||
// );
|
||||
|
||||
export type SettingsAtom = WritableAtom<Settings, [Settings], void>;
|
||||
|
||||
export const makeSettingsAtom = (userId: string): SettingsAtom => {
|
||||
const settingsAtom = atomWithLocalStorage<Settings>(
|
||||
getStoreKey(userId),
|
||||
(key) => getLocalStorageItem<Settings>(key, defaultSettings),
|
||||
(key) => {
|
||||
// migrate from settings item without username postfix
|
||||
const migrateSettings = getLocalStorageItem<Settings>(STORAGE_KEY, defaultSettings);
|
||||
if (migrateSettings !== defaultSettings) {
|
||||
setLocalStorageItem(key, migrateSettings);
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
}
|
||||
// end of migration
|
||||
|
||||
return getLocalStorageItem<Settings>(key, migrateSettings);
|
||||
},
|
||||
(key, settings) => {
|
||||
setLocalStorageItem(key, settings);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue