Clean up isDirty handling
parent
499021229a
commit
d0d93168d4
|
@ -112,10 +112,17 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
currentAccount: undefined, // assume logged out to start
|
currentAccount: undefined, // assume logged out to start
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const setStateAndPersist = React.useCallback(
|
||||||
|
(fn: (prev: StateContext) => StateContext) => {
|
||||||
|
isDirty.current = true
|
||||||
|
setState(fn)
|
||||||
|
},
|
||||||
|
[setState],
|
||||||
|
)
|
||||||
|
|
||||||
const upsertAccount = React.useCallback(
|
const upsertAccount = React.useCallback(
|
||||||
(account: persisted.PersistedAccount, expired = false) => {
|
(account: persisted.PersistedAccount, expired = false) => {
|
||||||
isDirty.current = true
|
setStateAndPersist(s => {
|
||||||
setState(s => {
|
|
||||||
return {
|
return {
|
||||||
...s,
|
...s,
|
||||||
currentAccount: expired ? undefined : account,
|
currentAccount: expired ? undefined : account,
|
||||||
|
@ -123,7 +130,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[setState],
|
[setStateAndPersist],
|
||||||
)
|
)
|
||||||
|
|
||||||
const createAccount = React.useCallback<ApiContext['createAccount']>(
|
const createAccount = React.useCallback<ApiContext['createAccount']>(
|
||||||
|
@ -232,8 +239,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
|
|
||||||
const logout = React.useCallback<ApiContext['logout']>(async () => {
|
const logout = React.useCallback<ApiContext['logout']>(async () => {
|
||||||
logger.debug(`session: logout`, {}, logger.DebugContext.session)
|
logger.debug(`session: logout`, {}, logger.DebugContext.session)
|
||||||
isDirty.current = true
|
setStateAndPersist(s => {
|
||||||
setState(s => {
|
|
||||||
return {
|
return {
|
||||||
...s,
|
...s,
|
||||||
agent: PUBLIC_BSKY_AGENT,
|
agent: PUBLIC_BSKY_AGENT,
|
||||||
|
@ -245,7 +251,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}, [setState])
|
}, [setStateAndPersist])
|
||||||
|
|
||||||
const initSession = React.useCallback<ApiContext['initSession']>(
|
const initSession = React.useCallback<ApiContext['initSession']>(
|
||||||
async account => {
|
async account => {
|
||||||
|
@ -303,8 +309,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
|
|
||||||
const removeAccount = React.useCallback<ApiContext['removeAccount']>(
|
const removeAccount = React.useCallback<ApiContext['removeAccount']>(
|
||||||
account => {
|
account => {
|
||||||
isDirty.current = true
|
setStateAndPersist(s => {
|
||||||
setState(s => {
|
|
||||||
return {
|
return {
|
||||||
...s,
|
...s,
|
||||||
accounts: s.accounts.filter(
|
accounts: s.accounts.filter(
|
||||||
|
@ -313,15 +318,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[setState],
|
[setStateAndPersist],
|
||||||
)
|
)
|
||||||
|
|
||||||
const updateCurrentAccount = React.useCallback<
|
const updateCurrentAccount = React.useCallback<
|
||||||
ApiContext['updateCurrentAccount']
|
ApiContext['updateCurrentAccount']
|
||||||
>(
|
>(
|
||||||
account => {
|
account => {
|
||||||
isDirty.current = true
|
setStateAndPersist(s => {
|
||||||
setState(s => {
|
|
||||||
const currentAccount = s.currentAccount
|
const currentAccount = s.currentAccount
|
||||||
|
|
||||||
// ignore, should never happen
|
// ignore, should never happen
|
||||||
|
@ -347,7 +351,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
[setState],
|
[setStateAndPersist],
|
||||||
)
|
)
|
||||||
|
|
||||||
const selectAccount = React.useCallback<ApiContext['selectAccount']>(
|
const selectAccount = React.useCallback<ApiContext['selectAccount']>(
|
||||||
|
@ -367,12 +371,11 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
)
|
)
|
||||||
|
|
||||||
const clearCurrentAccount = React.useCallback(() => {
|
const clearCurrentAccount = React.useCallback(() => {
|
||||||
isDirty.current = true
|
setStateAndPersist(s => ({
|
||||||
setState(s => ({
|
|
||||||
...s,
|
...s,
|
||||||
currentAccount: undefined,
|
currentAccount: undefined,
|
||||||
}))
|
}))
|
||||||
}, [setState])
|
}, [setStateAndPersist])
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (isDirty.current) {
|
if (isDirty.current) {
|
||||||
|
|
Loading…
Reference in New Issue