-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: syncs all profile localStorage to disk (#2537)
- Loading branch information
Showing
12 changed files
with
115 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Profile } from "@/stores/config"; | ||
|
||
export function useProfileStorage<K extends keyof Profile>(key: K, defaultValue: NonNullable<Profile[K]>) { | ||
const storageKey = "DOZZLE_" + key.toUpperCase(); | ||
const storage = useStorage<NonNullable<Profile[K]>>(storageKey, defaultValue, undefined, { | ||
writeDefaults: false, | ||
mergeDefaults: true, | ||
}); | ||
|
||
if (config.profile?.[key]) { | ||
if (storage.value instanceof Set && config.profile[key] instanceof Array) { | ||
storage.value = new Set([...(config.profile[key] as Iterable<any>)]) as unknown as NonNullable<Profile[K]>; | ||
} else if (config.profile[key] instanceof Array) { | ||
storage.value = config.profile[key] as NonNullable<Profile[K]>; | ||
} else if (config.profile[key] instanceof Object) { | ||
Object.assign(storage.value, config.profile[key]); | ||
} else { | ||
storage.value = config.profile[key] as NonNullable<Profile[K]>; | ||
} | ||
} | ||
|
||
if (config.user) { | ||
watch( | ||
storage, | ||
(value) => { | ||
fetch(withBase("/api/profile"), { | ||
method: "PATCH", | ||
body: JSON.stringify({ [key]: value }, (_, value) => (value instanceof Set ? [...value] : value)), | ||
}); | ||
}, | ||
{ deep: true }, | ||
); | ||
} | ||
|
||
return storage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters