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

Function useViewPost

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

Source from the content-addressed store, hash-verified

7import { isSameDayInTimezone } from '../../lib/timezones';
8
9export const useViewPost = (): UseMutateAsyncFunction<
10 unknown,
11 unknown,
12 string
13> => {
14 const client = useQueryClient();
15 const { user } = useAuthContext();
16 const streakKey = generateQueryKey(RequestKey.UserStreak, user);
17 const readKey = generateQueryKey(RequestKey.ReadingStreak30Days, user);
18 const { mutateAsync: onSendViewPost } = useMutation({
19 mutationFn: sendViewPost,
20 onSuccess: async () => {
21 const streak = client.getQueryData<UserStreak>(streakKey);
22 const isNewStreak = !streak?.lastViewAt;
23 const isFirstViewToday =
24 !isNewStreak &&
25 !isSameDayInTimezone(
26 new Date(streak!.lastViewAt!),
27 new Date(),
28 user!.timezone,
29 );
30
31 if (isNewStreak || isFirstViewToday) {
32 await client.refetchQueries({ queryKey: streakKey });
33
34 // just mark the query as stale
35 await client.invalidateQueries({ queryKey: readKey });
36 }
37
38 return null;
39 },
40 });
41
42 return onSendViewPost;
43};

Callers 8

PostContentRawFunction · 0.90
SquadPostContentRawFunction · 0.90
CollectionPostContentRawFunction · 0.90
DigestPostContentRawFunction · 0.90
BriefPostContentRawFunction · 0.90
PollPostContentRawFunction · 0.90
usePollVoteFunction · 0.90

Calls 3

useAuthContextFunction · 0.90
generateQueryKeyFunction · 0.90
isSameDayInTimezoneFunction · 0.90

Tested by

no test coverage detected