forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {useCallback} from 'react'
2import {type QueryClient, useQueryClient} from '@tanstack/react-query'
3
4import type * as bsky from '#/types/bsky'
5
6const unstableProfileViewCacheQueryKeyRoot = 'unstableProfileViewCache'
7export const unstableProfileViewCacheQueryKey = (didOrHandle: string) => [
8 unstableProfileViewCacheQueryKeyRoot,
9 didOrHandle,
10]
11
12/**
13 * Used as a rough cache of profile views to make loading snappier. This method
14 * accepts and stores any profile view type by both handle and DID.
15 *
16 * Access the cache via {@link useUnstableProfileViewCache}.
17 */
18export function unstableCacheProfileView(
19 queryClient: QueryClient,
20 profile: bsky.profile.AnyProfileView,
21) {
22 queryClient.setQueryData(
23 unstableProfileViewCacheQueryKey(profile.handle),
24 profile,
25 )
26 queryClient.setQueryData(
27 unstableProfileViewCacheQueryKey(profile.did),
28 profile,
29 )
30}
31
32/**
33 * Hook to access the unstable profile view cache. This cache can return ANY
34 * profile view type, so if the object shape is important, you need to use the
35 * identity validators shipped in the atproto SDK e.g.
36 * `AppBskyActorDefs.isValidProfileViewBasic` to confirm before using.
37 *
38 * To cache a profile, use {@link unstableCacheProfileView}.
39 */
40export function useUnstableProfileViewCache() {
41 const qc = useQueryClient()
42 const getUnstableProfile = useCallback(
43 (didOrHandle: string) => {
44 return qc.getQueryData<bsky.profile.AnyProfileView>(
45 unstableProfileViewCacheQueryKey(didOrHandle),
46 )
47 },
48 [qc],
49 )
50 return {getUnstableProfile}
51}