(wip) react query subscribe on focus
This commit is contained in:
@@ -9,6 +9,7 @@ import {
|
||||
RichText,
|
||||
} from '@atproto/api'
|
||||
import {t} from '@lingui/core/macro'
|
||||
import {useIsFocused} from '@react-navigation/native'
|
||||
import {
|
||||
type InfiniteData,
|
||||
keepPreviousData,
|
||||
@@ -176,10 +177,12 @@ export function getAvatarTypeFromUri(uri: string) {
|
||||
export function useFeedSourceInfoQuery({uri}: {uri: string}) {
|
||||
const type = getFeedTypeFromUri(uri)
|
||||
const agent = useAgent()
|
||||
const isFocused = useIsFocused()
|
||||
|
||||
return useQuery({
|
||||
staleTime: STALE.INFINITY,
|
||||
queryKey: feedSourceInfoQueryKey({uri}),
|
||||
subscribed: isFocused,
|
||||
queryFn: async () => {
|
||||
let view: FeedSourceInfo
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
type ModerationDecision,
|
||||
type ModerationPrefs,
|
||||
} from '@atproto/api'
|
||||
import {useIsFocused} from '@react-navigation/native'
|
||||
import {
|
||||
type InfiniteData,
|
||||
type QueryClient,
|
||||
@@ -134,6 +135,7 @@ export function usePostFeedQuery(
|
||||
const feedTuners = useFeedTuners(feedDesc)
|
||||
const moderationOpts = useModerationOpts()
|
||||
const {data: preferences} = usePreferencesQuery()
|
||||
const isFocused = useIsFocused()
|
||||
/**
|
||||
* Load bearing: we need to await AA state or risk FOUC. This marginally
|
||||
* delays feeds, but AA state is fetched immediately on load and is then
|
||||
@@ -183,6 +185,7 @@ export function usePostFeedQuery(
|
||||
>({
|
||||
enabled,
|
||||
staleTime: STALE.INFINITY,
|
||||
subscribed: isFocused,
|
||||
queryKey: RQKEY(feedDesc, params),
|
||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||
logger.debug('usePostFeedQuery', {feedDesc, cursor: pageParam?.cursor})
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import {useCallback} from 'react'
|
||||
import {useCallback, useContext} from 'react'
|
||||
import {
|
||||
type AppBskyActorDefs,
|
||||
type BskyFeedViewPreference,
|
||||
type LabelPreference,
|
||||
} from '@atproto/api'
|
||||
import {IsFocusedContext} from '@react-navigation/native'
|
||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {PROD_DEFAULT_FEED} from '#/lib/constants'
|
||||
@@ -38,6 +39,7 @@ export const preferencesQueryKey = [PERSISTED_QUERY_ROOT, 'getPreferences']
|
||||
export function usePreferencesQuery() {
|
||||
const agent = useAgent()
|
||||
const aa = useAgeAssurance()
|
||||
const isFocused = useOptionalIsFocused()
|
||||
|
||||
const query = useQuery({
|
||||
staleTime: STALE.SECONDS.FIFTEEN,
|
||||
@@ -45,6 +47,7 @@ export function usePreferencesQuery() {
|
||||
refetchOnWindowFocus: true,
|
||||
queryKey: preferencesQueryKey,
|
||||
gcTime: PERSISTED_QUERY_GCTIME,
|
||||
subscribed: isFocused,
|
||||
queryFn: async () => {
|
||||
if (!agent.did) {
|
||||
return DEFAULT_LOGGED_OUT_PREFERENCES
|
||||
@@ -107,6 +110,11 @@ export function usePreferencesQuery() {
|
||||
return query
|
||||
}
|
||||
|
||||
// same as useIsFocused, but returns true if not in context (i.e. not in a navigator)
|
||||
function useOptionalIsFocused() {
|
||||
return useContext(IsFocusedContext) ?? true
|
||||
}
|
||||
|
||||
export function useClearPreferencesMutation() {
|
||||
const queryClient = useQueryClient()
|
||||
const agent = useAgent()
|
||||
|
||||
Reference in New Issue
Block a user