scope last-selected-feed to per-account (#9500)

This commit is contained in:
Samuel Newman
2026-01-15 16:07:09 +02:00
committed by GitHub
parent 3b98f2f8ab
commit d9e37a222a
3 changed files with 33 additions and 21 deletions
+1
View File
@@ -116,6 +116,7 @@ const schema = z.object({
}), }),
hiddenPosts: z.array(z.string()).optional(), // should move to server hiddenPosts: z.array(z.string()).optional(), // should move to server
useInAppBrowser: z.boolean().optional(), useInAppBrowser: z.boolean().optional(),
/** @deprecated */
lastSelectedHomeFeed: z.string().optional(), lastSelectedHomeFeed: z.string().optional(),
pdsAddressHistory: z.array(z.string()).optional(), pdsAddressHistory: z.array(z.string()).optional(),
disableHaptics: z.boolean().optional(), disableHaptics: z.boolean().optional(),
+23 -14
View File
@@ -1,18 +1,19 @@
import React from 'react' import {createContext, useCallback, useContext, useState} from 'react'
import {isWeb} from '#/platform/detection' import {isWeb} from '#/platform/detection'
import * as persisted from '#/state/persisted'
import {type FeedDescriptor} from '#/state/queries/post-feed' import {type FeedDescriptor} from '#/state/queries/post-feed'
import {useSession} from '#/state/session'
import {account} from '#/storage'
type StateContext = FeedDescriptor | null type StateContext = FeedDescriptor | null
type SetContext = (v: FeedDescriptor) => void type SetContext = (v: FeedDescriptor) => void
const stateContext = React.createContext<StateContext>(null) const stateContext = createContext<StateContext>(null)
stateContext.displayName = 'SelectedFeedStateContext' stateContext.displayName = 'SelectedFeedStateContext'
const setContext = React.createContext<SetContext>((_: string) => {}) const setContext = createContext<SetContext>((_: string) => {})
setContext.displayName = 'SelectedFeedSetContext' setContext.displayName = 'SelectedFeedSetContext'
function getInitialFeed(): FeedDescriptor | null { function getInitialFeed(did?: string): FeedDescriptor | null {
if (isWeb) { if (isWeb) {
if (window.location.pathname === '/') { if (window.location.pathname === '/') {
const params = new URLSearchParams(window.location.search) const params = new URLSearchParams(window.location.search)
@@ -30,27 +31,35 @@ function getInitialFeed(): FeedDescriptor | null {
} }
} }
const feedFromPersisted = persisted.get('lastSelectedHomeFeed') if (did) {
if (feedFromPersisted) { const feedFromStorage = account.get([did, 'lastSelectedHomeFeed'])
if (feedFromStorage) {
// Fall back to the last chosen one across all tabs. // Fall back to the last chosen one across all tabs.
return feedFromPersisted as FeedDescriptor return feedFromStorage as FeedDescriptor
}
} }
return null return null
} }
export function Provider({children}: React.PropsWithChildren<{}>) { 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) => { const saveState = useCallback(
(feed: FeedDescriptor) => {
setState(feed) setState(feed)
if (isWeb) { if (isWeb) {
try { try {
sessionStorage.setItem('lastSelectedHomeFeed', feed) sessionStorage.setItem('lastSelectedHomeFeed', feed)
} catch {} } catch {}
} }
persisted.write('lastSelectedHomeFeed', feed) if (currentAccount?.did) {
}, []) account.set([currentAccount?.did, 'lastSelectedHomeFeed'], feed)
}
},
[currentAccount?.did],
)
return ( return (
<stateContext.Provider value={state}> <stateContext.Provider value={state}>
@@ -60,9 +69,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
} }
export function useSelectedFeed() { export function useSelectedFeed() {
return React.useContext(stateContext) return useContext(stateContext)
} }
export function useSetSelectedFeed() { export function useSetSelectedFeed() {
return React.useContext(setContext) return useContext(setContext)
} }
+2
View File
@@ -66,4 +66,6 @@ export type Account = {
* this device. * this device.
*/ */
birthdateLastUpdatedAt?: string birthdateLastUpdatedAt?: string
lastSelectedHomeFeed?: string
} }