-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3e62ba
commit 2ca99bf
Showing
3 changed files
with
67 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {useCallback} from 'react' | ||
import {QueryClient,useQueryClient} from '@tanstack/react-query' | ||
|
||
import * as atp from '#/types/atproto' | ||
|
||
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: atp.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<atp.profile.AnyProfileView>( | ||
unstableProfileViewCacheQueryKey(didOrHandle), | ||
) | ||
}, | ||
[qc], | ||
) | ||
return {getUnstableProfile} | ||
} |