From d9e37a222a218bc37e7e7e34c8a438f173d54840 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Thu, 15 Jan 2026 16:07:09 +0200 Subject: [PATCH] scope last-selected-feed to per-account (#9500) --- src/state/persisted/schema.ts | 1 + src/state/shell/selected-feed.tsx | 51 ++++++++++++++++++------------- src/storage/schema.ts | 2 ++ 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/state/persisted/schema.ts b/src/state/persisted/schema.ts index 1381f66b5d..4ee4d6e205 100644 --- a/src/state/persisted/schema.ts +++ b/src/state/persisted/schema.ts @@ -116,6 +116,7 @@ const schema = z.object({ }), hiddenPosts: z.array(z.string()).optional(), // should move to server useInAppBrowser: z.boolean().optional(), + /** @deprecated */ lastSelectedHomeFeed: z.string().optional(), pdsAddressHistory: z.array(z.string()).optional(), disableHaptics: z.boolean().optional(), diff --git a/src/state/shell/selected-feed.tsx b/src/state/shell/selected-feed.tsx index 1f7f7a9c60..460adcd055 100644 --- a/src/state/shell/selected-feed.tsx +++ b/src/state/shell/selected-feed.tsx @@ -1,18 +1,19 @@ -import React from 'react' +import {createContext, useCallback, useContext, useState} from 'react' import {isWeb} from '#/platform/detection' -import * as persisted from '#/state/persisted' import {type FeedDescriptor} from '#/state/queries/post-feed' +import {useSession} from '#/state/session' +import {account} from '#/storage' type StateContext = FeedDescriptor | null type SetContext = (v: FeedDescriptor) => void -const stateContext = React.createContext(null) +const stateContext = createContext(null) stateContext.displayName = 'SelectedFeedStateContext' -const setContext = React.createContext((_: string) => {}) +const setContext = createContext((_: string) => {}) setContext.displayName = 'SelectedFeedSetContext' -function getInitialFeed(): FeedDescriptor | null { +function getInitialFeed(did?: string): FeedDescriptor | null { if (isWeb) { if (window.location.pathname === '/') { const params = new URLSearchParams(window.location.search) @@ -30,27 +31,35 @@ function getInitialFeed(): FeedDescriptor | null { } } - const feedFromPersisted = persisted.get('lastSelectedHomeFeed') - if (feedFromPersisted) { - // Fall back to the last chosen one across all tabs. - return feedFromPersisted as FeedDescriptor + if (did) { + const feedFromStorage = account.get([did, 'lastSelectedHomeFeed']) + if (feedFromStorage) { + // Fall back to the last chosen one across all tabs. + return feedFromStorage as FeedDescriptor + } } return null } export function Provider({children}: React.PropsWithChildren<{}>) { - const [state, setState] = React.useState(() => getInitialFeed()) + const {currentAccount} = useSession() + const [state, setState] = useState(() => getInitialFeed(currentAccount?.did)) - const saveState = React.useCallback((feed: FeedDescriptor) => { - setState(feed) - if (isWeb) { - try { - sessionStorage.setItem('lastSelectedHomeFeed', feed) - } catch {} - } - persisted.write('lastSelectedHomeFeed', feed) - }, []) + const saveState = useCallback( + (feed: FeedDescriptor) => { + setState(feed) + if (isWeb) { + try { + sessionStorage.setItem('lastSelectedHomeFeed', feed) + } catch {} + } + if (currentAccount?.did) { + account.set([currentAccount?.did, 'lastSelectedHomeFeed'], feed) + } + }, + [currentAccount?.did], + ) return ( @@ -60,9 +69,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } export function useSelectedFeed() { - return React.useContext(stateContext) + return useContext(stateContext) } export function useSetSelectedFeed() { - return React.useContext(setContext) + return useContext(setContext) } diff --git a/src/storage/schema.ts b/src/storage/schema.ts index d1e4a8d88c..501dafee4b 100644 --- a/src/storage/schema.ts +++ b/src/storage/schema.ts @@ -66,4 +66,6 @@ export type Account = { * this device. */ birthdateLastUpdatedAt?: string + + lastSelectedHomeFeed?: string }