scope last-selected-feed to per-account (#9500)
This commit is contained in:
@@ -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(),
|
||||||
|
|||||||
@@ -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'])
|
||||||
// Fall back to the last chosen one across all tabs.
|
if (feedFromStorage) {
|
||||||
return feedFromPersisted as FeedDescriptor
|
// Fall back to the last chosen one across all tabs.
|
||||||
|
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(
|
||||||
setState(feed)
|
(feed: FeedDescriptor) => {
|
||||||
if (isWeb) {
|
setState(feed)
|
||||||
try {
|
if (isWeb) {
|
||||||
sessionStorage.setItem('lastSelectedHomeFeed', feed)
|
try {
|
||||||
} catch {}
|
sessionStorage.setItem('lastSelectedHomeFeed', feed)
|
||||||
}
|
} 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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,4 +66,6 @@ export type Account = {
|
|||||||
* this device.
|
* this device.
|
||||||
*/
|
*/
|
||||||
birthdateLastUpdatedAt?: string
|
birthdateLastUpdatedAt?: string
|
||||||
|
|
||||||
|
lastSelectedHomeFeed?: string
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user