Update persisted schema for new source of truth, implement in existing session
This commit is contained in:
@@ -4,7 +4,10 @@ import {deviceLocales} from '#/platform/detection'
|
|||||||
|
|
||||||
const externalEmbedOptions = ['show', 'hide'] as const
|
const externalEmbedOptions = ['show', 'hide'] as const
|
||||||
|
|
||||||
// only data needed for rendering account page
|
/**
|
||||||
|
* A account persisted to storage. Stored in the `accounts[]` array. Contains
|
||||||
|
* base account info and access tokens.
|
||||||
|
*/
|
||||||
const accountSchema = z.object({
|
const accountSchema = z.object({
|
||||||
service: z.string(),
|
service: z.string(),
|
||||||
did: z.string(),
|
did: z.string(),
|
||||||
@@ -17,12 +20,26 @@ const accountSchema = z.object({
|
|||||||
})
|
})
|
||||||
export type PersistedAccount = z.infer<typeof accountSchema>
|
export type PersistedAccount = z.infer<typeof accountSchema>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current account. Stored in the `currentAccount` field.
|
||||||
|
*
|
||||||
|
* In previous versions, this included tokens and other info. Now, it's used
|
||||||
|
* only to reference the `did` field, and all other fields are marked as
|
||||||
|
* optional. They should be considered deprecated and not used, but are kept
|
||||||
|
* here for backwards compat.
|
||||||
|
*/
|
||||||
|
const currentAccountSchema = accountSchema.extend({
|
||||||
|
service: z.string().optional(),
|
||||||
|
handle: z.string().optional(),
|
||||||
|
})
|
||||||
|
export type PersistedCurrentAccount = z.infer<typeof currentAccountSchema>
|
||||||
|
|
||||||
export const schema = z.object({
|
export const schema = z.object({
|
||||||
colorMode: z.enum(['system', 'light', 'dark']),
|
colorMode: z.enum(['system', 'light', 'dark']),
|
||||||
darkTheme: z.enum(['dim', 'dark']).optional(),
|
darkTheme: z.enum(['dim', 'dark']).optional(),
|
||||||
session: z.object({
|
session: z.object({
|
||||||
accounts: z.array(accountSchema),
|
accounts: z.array(accountSchema),
|
||||||
currentAccount: accountSchema.optional(),
|
currentAccount: currentAccountSchema.optional(),
|
||||||
}),
|
}),
|
||||||
reminders: z.object({
|
reminders: z.object({
|
||||||
lastEmailConfirm: z.string().optional(),
|
lastEmailConfirm: z.string().optional(),
|
||||||
|
|||||||
@@ -581,20 +581,24 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
|
|
||||||
logger.debug(`session: persisted onUpdate`, {})
|
logger.debug(`session: persisted onUpdate`, {})
|
||||||
|
|
||||||
if (session.currentAccount && session.currentAccount.refreshJwt) {
|
const selectedAccount = session.accounts.find(
|
||||||
if (session.currentAccount?.did !== state.currentAccount?.did) {
|
a => a.did === session.currentAccount?.did,
|
||||||
|
)
|
||||||
|
|
||||||
|
if (selectedAccount && selectedAccount.refreshJwt) {
|
||||||
|
if (selectedAccount.did !== state.currentAccount?.did) {
|
||||||
logger.debug(`session: persisted onUpdate, switching accounts`, {
|
logger.debug(`session: persisted onUpdate, switching accounts`, {
|
||||||
from: {
|
from: {
|
||||||
did: state.currentAccount?.did,
|
did: state.currentAccount?.did,
|
||||||
handle: state.currentAccount?.handle,
|
handle: state.currentAccount?.handle,
|
||||||
},
|
},
|
||||||
to: {
|
to: {
|
||||||
did: session.currentAccount.did,
|
did: selectedAccount.did,
|
||||||
handle: session.currentAccount.handle,
|
handle: selectedAccount.handle,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
initSession(session.currentAccount)
|
initSession(selectedAccount)
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`session: persisted onUpdate, updating session`, {})
|
logger.debug(`session: persisted onUpdate, updating session`, {})
|
||||||
|
|
||||||
@@ -604,9 +608,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
* already persisted, and we'll get a loop between tabs.
|
* already persisted, and we'll get a loop between tabs.
|
||||||
*/
|
*/
|
||||||
// @ts-ignore we checked for `refreshJwt` above
|
// @ts-ignore we checked for `refreshJwt` above
|
||||||
__globalAgent.session = session.currentAccount
|
__globalAgent.session = selectedAccount
|
||||||
}
|
}
|
||||||
} else if (!session.currentAccount && state.currentAccount) {
|
} else if (!selectedAccount && state.currentAccount) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`session: persisted onUpdate, logging out`,
|
`session: persisted onUpdate, logging out`,
|
||||||
{},
|
{},
|
||||||
@@ -625,7 +629,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
setState(s => ({
|
setState(s => ({
|
||||||
...s,
|
...s,
|
||||||
accounts: session.accounts,
|
accounts: session.accounts,
|
||||||
currentAccount: session.currentAccount,
|
currentAccount: selectedAccount,
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
}, [state, setState, clearCurrentAccount, initSession])
|
}, [state, setState, clearCurrentAccount, initSession])
|
||||||
|
|||||||
Reference in New Issue
Block a user