MCPcopy Create free account
hub / github.com/dailydotdev/apps / usePersistentContext

Function usePersistentContext

packages/shared/src/hooks/usePersistentContext.ts:43–73  ·  view source on GitHub ↗
(
  key: string,
  valueWhenCacheEmpty?: T,
  validValues?: T[],
  fallbackValue?: T,
)

Source from the content-addressed store, hash-verified

41 key: string,
42): UserPersistentContextType<T | null>;
43export default function usePersistentContext<T>(
44 key: string,
45 valueWhenCacheEmpty?: T,
46 validValues?: T[],
47 fallbackValue?: T,
48): UserPersistentContextType<T | null> {
49 const queryKey = [key, valueWhenCacheEmpty];
50 const queryClient = useQueryClient();
51
52 const { data, isFetched } = useQuery({
53 queryKey,
54 queryFn: () =>
55 getAsyncCache<T>(key, valueWhenCacheEmpty || null, validValues) || null,
56 });
57
58 const { mutateAsync: updateValue, isPending: isLoading } = useMutation({
59 mutationFn: (value: T | null) => setCache(key, value),
60
61 onMutate: (mutatedData) => {
62 const current = data;
63 queryClient.setQueryData(queryKey, mutatedData);
64 return current;
65 },
66
67 onError: (_, __, rollback) => {
68 queryClient.setQueryData(queryKey, rollback);
69 },
70 });
71
72 return [data ?? fallbackValue ?? null, updateValue, isFetched, isLoading];
73}
74
75export enum PersistentContextKeys {
76 AlertPushKey = 'alert_push_key',

Callers 15

CustomFeedEmptyScreenFunction · 0.85
BookmarkFeedLayoutFunction · 0.85
MainFeedLayoutFunction · 0.85
FirstNotificationFunction · 0.85
ReadingStreakPopupFunction · 0.85
NewSourceModalFunction · 0.85
QuestOffersTriggerFunction · 0.85
RecruiterJobLinkModalFunction · 0.85
SquadCommentJoinBannerFunction · 0.85
SidebarDesktopV2Function · 0.85
FeedNavFunction · 0.85

Calls 1

getAsyncCacheFunction · 0.85

Tested by

no test coverage detected