-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathauthMutations.ts
More file actions
91 lines (83 loc) · 2.79 KB
/
authMutations.ts
File metadata and controls
91 lines (83 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
clearAuthScopedQueries,
fetchAuthState,
refreshAuthStateQuery,
} from "@features/auth/hooks/authQueries";
import { useAuthUiStateStore } from "@features/auth/stores/authUiStateStore";
import { useOnboardingStore } from "@features/onboarding/stores/onboardingStore";
import { resetSessionService } from "@features/sessions/service/service";
import { trpcClient } from "@renderer/trpc/client";
import { ANALYTICS_EVENTS } from "@shared/types/analytics";
import type { CloudRegion } from "@shared/types/regions";
import { useNavigationStore } from "@stores/navigationStore";
import { useMutation } from "@tanstack/react-query";
import { track } from "@utils/analytics";
function useAuthFlowMutation(
mutateAuth: (region: CloudRegion) => Promise<{
state: Awaited<ReturnType<typeof trpcClient.auth.getState.query>>;
}>,
) {
return useMutation({
mutationFn: async (region: CloudRegion) => {
return await mutateAuth(region);
},
onSuccess: async ({ state }, region) => {
await refreshAuthStateQuery();
useAuthUiStateStore.getState().clearStaleRegion();
track(ANALYTICS_EVENTS.USER_LOGGED_IN, {
project_id: state.projectId?.toString() ?? "",
region,
});
},
});
}
export function useLoginMutation() {
return useAuthFlowMutation(async (region) => {
return await trpcClient.auth.login.mutate({ region });
});
}
export function useSignupMutation() {
return useAuthFlowMutation(async (region) => {
return await trpcClient.auth.signup.mutate({ region });
});
}
export function useSelectProjectMutation() {
return useMutation({
mutationFn: async (projectId: number) => {
resetSessionService();
return await trpcClient.auth.selectProject.mutate({ projectId });
},
onSuccess: async () => {
clearAuthScopedQueries();
await refreshAuthStateQuery();
useNavigationStore.getState().navigateToTaskInput();
},
});
}
export function useRedeemInviteCodeMutation() {
return useMutation({
mutationFn: async (code: string) =>
await trpcClient.auth.redeemInviteCode.mutate({ code }),
onSuccess: async () => {
await refreshAuthStateQuery();
},
});
}
export function useLogoutMutation() {
return useMutation({
mutationFn: async () => {
const previousState = await fetchAuthState();
track(ANALYTICS_EVENTS.USER_LOGGED_OUT);
resetSessionService();
clearAuthScopedQueries();
const state = await trpcClient.auth.logout.mutate();
return { state, previousState };
},
onSuccess: async ({ previousState }) => {
await refreshAuthStateQuery();
useAuthUiStateStore.getState().setStaleRegion(previousState.cloudRegion);
useNavigationStore.getState().navigateToTaskInput();
useOnboardingStore.getState().resetSelections();
},
});
}