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

Function useViewPost

packages/shared/src/hooks/post/useViewPost.ts:47–183  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

45}
46
47export const useViewPost = (): UseMutateAsyncFunction<
48 unknown,
49 unknown,
50 string,
51 ViewPostContext
52> => {
53 const client = useQueryClient();
54 const { requestMethod, isCompanion } = useRequestProtocol();
55 const { user } = useAuthContext();
56 // the companion keeps waiting for the server value
57 const isOptimistic = !isCompanion;
58 const checkStreakEarnBack = useStreakEarnBackProgress();
59 const celebrateStreakGoal = useStreakGoalCelebration();
60 const streakKey = generateQueryKey(RequestKey.UserStreak, user);
61 const readKey = generateQueryKey(RequestKey.ReadingStreak30Days, user);
62 const { mutateAsync: onSendViewPost } = useMutation<
63 unknown,
64 unknown,
65 string,
66 ViewPostContext
67 >({
68 mutationFn: (id: string) => requestMethod(VIEW_POST_MUTATION, { id }),
69 onMutate: () => {
70 const streak = client.getQueryData<UserStreak>(streakKey);
71 const isNewStreak = !streak?.lastViewAt;
72 const isFirstViewToday =
73 !isNewStreak &&
74 !isSameDayInTimezone(
75 new Date(streak!.lastViewAt!),
76 new Date(),
77 user!.timezone,
78 );
79 const shouldRefetchStreak = isNewStreak || isFirstViewToday;
80 const nextStreak =
81 streak && canBumpStreak(streak, user?.timezone)
82 ? streak.current + 1
83 : undefined;
84
85 if (!isOptimistic || !shouldRefetchStreak || nextStreak === undefined) {
86 return { shouldRefetchStreak, nextStreak };
87 }
88
89 const optimisticStreak: UserStreak = {
90 ...streak!,
91 current: nextStreak,
92 total: streak!.total + 1,
93 max: Math.max(streak!.max, nextStreak),
94 lastViewAt: new Date(),
95 };
96 client.setQueryData<UserStreak>(streakKey, optimisticStreak);
97
98 return {
99 previousStreak: streak,
100 optimisticStreak,
101 shouldRefetchStreak,
102 nextStreak,
103 };
104 },

Callers 7

DigestPostContentRawFunction · 0.90
BriefPostContentRawFunction · 0.90
PollPostContentRawFunction · 0.90
useTrackPostViewFunction · 0.90
usePollVoteFunction · 0.90
renderViewPostFunction · 0.90

Calls 13

useRequestProtocolFunction · 0.90
useAuthContextFunction · 0.90
useStreakGoalCelebrationFunction · 0.90
generateQueryKeyFunction · 0.90
isSameDayInTimezoneFunction · 0.90
emitPatchyEventFunction · 0.90
isFinalViewRejectionFunction · 0.90
enqueueStreakViewFunction · 0.90
requestMethodFunction · 0.85
canBumpStreakFunction · 0.85
refetchStreakFunction · 0.85

Tested by 1

renderViewPostFunction · 0.72