Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[Session] Derive currentAccount from accounts + currentAccountDid (#3795)

* Derive currentAccount from accounts and currentAccountDid

* Add TODOs for divergence with __globalAgent

authored by

dan and committed by
GitHub
d61b366b df9af92e

+28 -22
+28 -22
src/state/session/index.tsx
··· 62 62 63 63 type State = { 64 64 accounts: SessionStateContext['accounts'] 65 - currentAccount: SessionStateContext['currentAccount'] 65 + currentAccountDid: string | undefined 66 66 needsPersist: boolean 67 67 } 68 68 ··· 71 71 const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false) 72 72 const [state, setState] = React.useState<State>({ 73 73 accounts: persisted.get('session').accounts, 74 - currentAccount: undefined, // assume logged out to start 74 + currentAccountDid: undefined, // assume logged out to start 75 75 needsPersist: false, 76 76 }) 77 77 ··· 80 80 setState(s => { 81 81 return { 82 82 accounts: [account, ...s.accounts.filter(a => a.did !== account.did)], 83 - currentAccount: expired ? undefined : account, 83 + currentAccountDid: expired ? undefined : account.did, 84 84 needsPersist: true, 85 85 } 86 86 }) ··· 94 94 configureModerationForGuest() 95 95 setState(s => ({ 96 96 accounts: s.accounts, 97 - currentAccount: undefined, 97 + currentAccountDid: undefined, 98 98 needsPersist: true, 99 99 })) 100 100 }, [setState]) ··· 265 265 refreshJwt: undefined, 266 266 accessJwt: undefined, 267 267 })), 268 - currentAccount: s.currentAccount, 268 + currentAccountDid: s.currentAccountDid, 269 269 needsPersist: true, 270 270 } 271 271 }) ··· 329 329 }) 330 330 331 331 __globalAgent = PUBLIC_BSKY_AGENT 332 + // TODO: Should this update currentAccountDid? 332 333 } 333 334 } else { 334 335 logger.debug(`session: attempting to reuse previous session`) ··· 366 367 }) 367 368 368 369 __globalAgent = PUBLIC_BSKY_AGENT 370 + // TODO: Should this update currentAccountDid? 369 371 }) 370 372 } 371 373 ··· 407 409 setState(s => { 408 410 return { 409 411 accounts: s.accounts.filter(a => a.did !== account.did), 410 - currentAccount: s.currentAccount, 412 + currentAccountDid: s.currentAccountDid, 411 413 needsPersist: true, 412 414 } 413 415 }) ··· 420 422 >( 421 423 account => { 422 424 setState(s => { 423 - const currentAccount = s.currentAccount 424 - 425 + const currentAccount = s.accounts.find( 426 + a => a.did === s.currentAccountDid, 427 + ) 425 428 // ignore, should never happen 426 429 if (!currentAccount) return s 427 430 ··· 444 447 updatedAccount, 445 448 ...s.accounts.filter(a => a.did !== currentAccount.did), 446 449 ], 447 - currentAccount: updatedAccount, 450 + currentAccountDid: s.currentAccountDid, 448 451 needsPersist: true, 449 452 } 450 453 }) ··· 474 477 state.needsPersist = false 475 478 persisted.write('session', { 476 479 accounts: state.accounts, 477 - currentAccount: state.currentAccount, 480 + currentAccount: state.accounts.find( 481 + a => a.did === state.currentAccountDid, 482 + ), 478 483 }) 479 484 } 480 485 }, [state]) 481 486 482 487 React.useEffect(() => { 483 488 return persisted.onUpdate(() => { 484 - const session = persisted.get('session') 489 + const persistedSession = persisted.get('session') 485 490 486 491 logger.debug(`session: persisted onUpdate`, {}) 487 492 488 - const selectedAccount = session.accounts.find( 489 - a => a.did === session.currentAccount?.did, 493 + const selectedAccount = persistedSession.accounts.find( 494 + a => a.did === persistedSession.currentAccount?.did, 490 495 ) 491 496 492 497 if (selectedAccount && selectedAccount.refreshJwt) { 493 - if (selectedAccount.did !== state.currentAccount?.did) { 498 + if (selectedAccount.did !== state.currentAccountDid) { 494 499 logger.debug(`session: persisted onUpdate, switching accounts`, { 495 500 from: { 496 - did: state.currentAccount?.did, 497 - handle: state.currentAccount?.handle, 501 + did: state.currentAccountDid, 498 502 }, 499 503 to: { 500 504 did: selectedAccount.did, 501 - handle: selectedAccount.handle, 502 505 }, 503 506 }) 504 507 ··· 514 517 // @ts-ignore we checked for `refreshJwt` above 515 518 __globalAgent.session = selectedAccount 516 519 } 517 - } else if (!selectedAccount && state.currentAccount) { 520 + } else if (!selectedAccount && state.currentAccountDid) { 518 521 logger.debug( 519 522 `session: persisted onUpdate, logging out`, 520 523 {}, ··· 531 534 } 532 535 533 536 setState(() => ({ 534 - accounts: session.accounts, 535 - currentAccount: selectedAccount, 537 + accounts: persistedSession.accounts, 538 + currentAccountDid: selectedAccount?.did, 536 539 needsPersist: false, // Synced from another tab. Don't persist to avoid cycles. 537 540 })) 538 541 }) ··· 540 543 541 544 const stateContext = React.useMemo( 542 545 () => ({ 543 - ...state, 546 + accounts: state.accounts, 547 + currentAccount: state.accounts.find( 548 + a => a.did === state.currentAccountDid, 549 + ), 544 550 isInitialLoad, 545 551 isSwitchingAccounts, 546 - hasSession: !!state.currentAccount, 552 + hasSession: !!state.currentAccountDid, 547 553 }), 548 554 [state, isInitialLoad, isSwitchingAccounts], 549 555 )