Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import {useCallback} from 'react'import {type QueryClient, useQueryClient} from '@tanstack/react-query'
import type * as bsky from '#/types/bsky'
const unstableProfileViewCacheQueryKeyRoot = 'unstableProfileViewCache'export const unstableProfileViewCacheQueryKey = (didOrHandle: string) => [ unstableProfileViewCacheQueryKeyRoot, didOrHandle,]
/** * Used as a rough cache of profile views to make loading snappier. This method * accepts and stores any profile view type by both handle and DID. * * Access the cache via {@link useUnstableProfileViewCache}. */export function unstableCacheProfileView( queryClient: QueryClient, profile: bsky.profile.AnyProfileView,) { queryClient.setQueryData( unstableProfileViewCacheQueryKey(profile.handle), profile, ) queryClient.setQueryData( unstableProfileViewCacheQueryKey(profile.did), profile, )}
/** * Hook to access the unstable profile view cache. This cache can return ANY * profile view type, so if the object shape is important, you need to use the * identity validators shipped in the atproto SDK e.g. * `AppBskyActorDefs.isValidProfileViewBasic` to confirm before using. * * To cache a profile, use {@link unstableCacheProfileView}. */export function useUnstableProfileViewCache() { const qc = useQueryClient() const getUnstableProfile = useCallback( (didOrHandle: string) => { return qc.getQueryData<bsky.profile.AnyProfileView>( unstableProfileViewCacheQueryKey(didOrHandle), ) }, [qc], ) return {getUnstableProfile}}