modify some existing logic

This commit is contained in:
Hailey
2024-02-07 21:03:30 -08:00
parent d5462f8506
commit 29fc0912b1
+40 -9
View File
@@ -25,28 +25,51 @@ import {STALE} from '#/state/queries'
import {track} from '#/lib/analytics/analytics' import {track} from '#/lib/analytics/analytics'
export const RQKEY = (did: string) => ['profile', did] export const RQKEY = (did: string) => ['profile', did]
export const profileBasicKey = (did: string) => ['profileBasic', did]
export const profilesQueryKey = (handles: string[]) => ['profiles', handles] export const profilesQueryKey = (handles: string[]) => ['profiles', handles]
export function useProfileQuery({ export function useProfileQuery({
did, did,
staleTime = STALE.SECONDS.FIFTEEN, queryClient,
}: { }: {
did: string | undefined did: string | undefined
staleTime?: number staleTime?: number
queryClient: QueryClient
}) { }) {
return useQuery({ // return useQuery({
// WARNING // // WARNING
// this staleTime is load-bearing // // this staleTime is load-bearing
// if you remove it, the UI infinite-loops // // if you remove it, the UI infinite-loops
// -prf // // -prf
staleTime, // staleTime,
// refetchOnWindowFocus: true,
// queryKey: RQKEY(did || ''),
// queryFn: async () => {
// const res = await getAgent().getProfile({actor: did || ''})
// return res.data
// },
// enabled: !!did,
// })
// TODO Figure out a good staleTime for this. We should refetch on every profile push, because we need to check for
// blocks
return useQuery<AppBskyActorDefs.ProfileView>({
staleTime: STALE.SECONDS.FIFTEEN,
refetchOnWindowFocus: true, refetchOnWindowFocus: true,
queryKey: RQKEY(did || ''), queryKey: RQKEY(did ?? ''),
queryFn: async () => { queryFn: async () => {
const res = await getAgent().getProfile({actor: did || ''}) const res = await getAgent().getProfile({actor: did ?? ''})
return res.data return res.data
}, },
enabled: !!did, enabled: !!did,
placeholderData: () => {
return queryClient
.getQueriesData<AppBskyActorDefs.ProfileView>({
queryKey: ['profileBasic'],
exact: false,
})
.find(q => q[1]?.did === did)?.[1]
},
}) })
} }
@@ -78,6 +101,14 @@ export function usePrefetchProfileQuery() {
return prefetchProfileQuery return prefetchProfileQuery
} }
// We don't need a useQuery hook for this. We never actually use this except for
export function cacheProfileBasic(
queryClient: QueryClient,
profile: AppBskyActorDefs.ProfileViewBasic,
) {
queryClient.setQueryData(profileBasicKey(profile.handle), profile)
}
interface ProfileUpdateParams { interface ProfileUpdateParams {
profile: AppBskyActorDefs.ProfileView profile: AppBskyActorDefs.ProfileView
updates: updates: