Add persistent state provider (#1830)
* Add persistent state provider * Catch write error * Handle read errors, update error msgs * Fix lint * Don't provide initial state to loader * Remove colorMode from shell state * Idea: hook into persisted context from other files * Migrate settings to new hook * Rework persisted state to split individual contexts * Tweak persisted schema and validation --------- Co-authored-by: Paul Frazee <pfrazee@gmail.com>
This commit is contained in:
parent
bfe196bac5
commit
96d8faf4b0
13 changed files with 467 additions and 76 deletions
68
src/state/persisted/schema.ts
Normal file
68
src/state/persisted/schema.ts
Normal file
|
@ -0,0 +1,68 @@
|
|||
import {z} from 'zod'
|
||||
import {deviceLocales} from '#/platform/detection'
|
||||
|
||||
// only data needed for rendering account page
|
||||
const accountSchema = z.object({
|
||||
service: z.string(),
|
||||
did: z.string(),
|
||||
refreshJwt: z.string().optional(),
|
||||
accessJwt: z.string().optional(),
|
||||
handle: z.string(),
|
||||
displayName: z.string(),
|
||||
aviUrl: z.string(),
|
||||
})
|
||||
|
||||
export const schema = z.object({
|
||||
colorMode: z.enum(['system', 'light', 'dark']),
|
||||
session: z.object({
|
||||
accounts: z.array(accountSchema),
|
||||
currentAccount: accountSchema.optional(),
|
||||
}),
|
||||
reminders: z.object({
|
||||
lastEmailConfirmReminder: z.string().optional(),
|
||||
}),
|
||||
languagePrefs: z.object({
|
||||
primaryLanguage: z.string(), // should move to server
|
||||
contentLanguages: z.array(z.string()), // should move to server
|
||||
postLanguage: z.string(), // should move to server
|
||||
postLanguageHistory: z.array(z.string()),
|
||||
}),
|
||||
requireAltTextEnabled: z.boolean(), // should move to server
|
||||
mutedThreads: z.array(z.string()), // should move to server
|
||||
invitedUsers: z.object({
|
||||
seenDids: z.array(z.string()),
|
||||
copiedInvites: z.array(z.string()),
|
||||
}),
|
||||
onboarding: z.object({
|
||||
step: z.string(),
|
||||
}),
|
||||
})
|
||||
export type Schema = z.infer<typeof schema>
|
||||
|
||||
export const defaults: Schema = {
|
||||
colorMode: 'system',
|
||||
session: {
|
||||
accounts: [],
|
||||
currentAccount: undefined,
|
||||
},
|
||||
reminders: {
|
||||
lastEmailConfirmReminder: undefined,
|
||||
},
|
||||
languagePrefs: {
|
||||
primaryLanguage: deviceLocales[0] || 'en',
|
||||
contentLanguages: deviceLocales || [],
|
||||
postLanguage: deviceLocales[0] || 'en',
|
||||
postLanguageHistory: (deviceLocales || [])
|
||||
.concat(['en', 'ja', 'pt', 'de'])
|
||||
.slice(0, 6),
|
||||
},
|
||||
requireAltTextEnabled: false,
|
||||
mutedThreads: [],
|
||||
invitedUsers: {
|
||||
seenDids: [],
|
||||
copiedInvites: [],
|
||||
},
|
||||
onboarding: {
|
||||
step: 'Home',
|
||||
},
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue