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

Function usePersistentContext

packages/shared/src/hooks/usePersistentContext.ts:43–75  ·  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: async (mutatedData) => {
62 const current = data;
63 // A read still in flight would land on top of this value and undo it.
64 await queryClient.cancelQueries({ queryKey });
65 queryClient.setQueryData(queryKey, mutatedData);
66 return current;
67 },
68
69 onError: (_, __, rollback) => {
70 queryClient.setQueryData(queryKey, rollback);
71 },
72 });
73
74 return [data ?? fallbackValue ?? null, updateValue, isFetched, isLoading];
75}
76
77export enum PersistentContextKeys {
78 BookmarkFoldersStripDismissedAt = 'bookmark_folders_strip_dismissed_at',

Callers 15

CustomFeedEmptyScreenFunction · 0.85
BookmarkFeedLayoutFunction · 0.85
MainFeedLayoutFunction · 0.85
BookmarkFoldersStripFunction · 0.85
FirstNotificationFunction · 0.85
ReadingStreakPopupFunction · 0.85
NewSourceModalFunction · 0.85
RecruiterJobLinkModalFunction · 0.85
FeedSheetFunction · 0.85
SquadCommentJoinBannerFunction · 0.85
SidebarDesktopV2Function · 0.85

Calls 1

getAsyncCacheFunction · 0.85

Tested by

no test coverage detected