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

Function useActions

packages/shared/src/hooks/useActions.ts:21–130  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19}
20
21export const useActions = (): UseActions => {
22 const client = useQueryClient();
23 const { user } = useAuthContext();
24 const actionsKey = generateQueryKey(RequestKey.Actions, user);
25
26 const { data, isPending } = useQuery({
27 queryKey: actionsKey,
28 queryFn: async () => {
29 const serverData = await getUserActions();
30 const current = client.getQueryData<ActionQueryData>(actionsKey);
31
32 if (!current || !Array.isArray(current)) {
33 return { actions: serverData, serverLoaded: true };
34 }
35
36 const filtered = serverData.filter(({ type }) =>
37 current.every((action) => action.type !== type),
38 );
39
40 return { actions: [...current, ...filtered], serverLoaded: true };
41 },
42 enabled: !!user,
43 ...disabledRefetch,
44 });
45
46 const actions = useMemo(() => data?.actions ?? [], [data?.actions]);
47 const isActionsFetched = !isPending && !!data?.serverLoaded;
48 const queueRef = useRef<ActionType[]>([]);
49
50 const { mutateAsync: completeAction } = useMutation({
51 mutationFn: completeUserAction,
52 onMutate: (type) => {
53 if (!user?.id) {
54 queueRef.current = [...queueRef.current, type];
55 return () => undefined;
56 }
57
58 client.setQueryData<ActionQueryData>(actionsKey, (old) => {
59 const optimisticAction = {
60 userId: user.id,
61 type,
62 completedAt: new Date(),
63 };
64
65 if (!Array.isArray(old?.actions)) {
66 return { actions: [optimisticAction], serverLoaded: false };
67 }
68
69 return {
70 actions: [...old?.actions, optimisticAction],
71 serverLoaded: !!old?.serverLoaded,
72 };
73 });
74
75 return () =>
76 client.setQueryData<ActionQueryData>(actionsKey, {
77 actions,
78 serverLoaded: !!data?.serverLoaded,

Callers 15

FeedFunction · 0.90
SearchControlHeaderFunction · 0.90
PreferenceOptionsFormFunction · 0.90
OpportunityEntryButtonFunction · 0.90
ReadingStreakPopupFunction · 0.90
BootPopupsFunction · 0.90
IntroQuestModalFunction · 0.90
StreakMilestonePopupFunction · 0.90
NewStreakModalFunction · 0.90

Calls 4

useAuthContextFunction · 0.90
generateQueryKeyFunction · 0.90
getUserActionsFunction · 0.90
rollbackFunction · 0.85

Tested by

no test coverage detected