-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathuseMultisessionActions.tsx
More file actions
119 lines (104 loc) · 3.7 KB
/
Copy pathuseMultisessionActions.tsx
File metadata and controls
119 lines (104 loc) · 3.7 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { navigateIfTaskExists } from '@clerk/shared/internal/clerk-js/sessionTasks';
import { useClerk, usePortalRoot } from '@clerk/shared/react';
import type { SignedInSessionResource, UserButtonProps, UserResource } from '@clerk/shared/types';
import { useEnvironment } from '@/ui/contexts';
import { useCardState } from '@/ui/elements/contexts';
import { sleep } from '@/ui/utils/sleep';
import { clerkWindowNavigate } from '@/ui/utils/windowNavigate';
import { useMultipleSessions } from '../../hooks/useMultipleSessions';
import { useRouter } from '../../router';
type UseMultisessionActionsParams = {
user: UserResource | null | undefined;
actionCompleteCallback?: () => void;
navigateAfterSignOut?: () => any;
navigateAfterMultiSessionSingleSignOut?: () => any;
afterSwitchSessionUrl?: string;
userProfileUrl?: string;
signInUrl?: string;
taskUrl?: string | null;
} & Pick<UserButtonProps, 'userProfileMode' | 'appearance' | 'userProfileProps'>;
export const useMultisessionActions = (opts: UseMultisessionActionsParams) => {
const clerk = useClerk();
const { setActive, signOut, openUserProfile } = clerk;
const card = useCardState();
const { signedInSessions, otherSessions } = useMultipleSessions({ user: opts.user });
const { navigate } = useRouter();
const { displayConfig } = useEnvironment();
const getContainer = usePortalRoot();
const handleSignOutSessionClicked = (session: SignedInSessionResource) => () => {
if (otherSessions.length === 0) {
return signOut(opts.navigateAfterSignOut);
}
return signOut(opts.navigateAfterMultiSessionSingleSignOut, { sessionId: session.id }).finally(() =>
card.setIdle(),
);
};
const handleManageAccountClicked = () => {
if (opts.userProfileMode === 'navigation') {
return navigate(opts.userProfileUrl || '').finally(() => {
void (async () => {
await sleep(300);
opts.actionCompleteCallback?.();
})();
});
}
openUserProfile({ getContainer, ...opts.userProfileProps });
return opts.actionCompleteCallback?.();
};
const handleUserProfileActionClicked = (__experimental_startPath?: string) => {
if (opts.userProfileMode === 'navigation') {
return navigate(opts.userProfileUrl || '').finally(() => {
void (async () => {
await sleep(300);
opts.actionCompleteCallback?.();
})();
});
}
openUserProfile({
getContainer,
...opts.userProfileProps,
...(__experimental_startPath && { __experimental_startPath }),
});
return opts.actionCompleteCallback?.();
};
const handleSignOutAllClicked = () => {
return signOut(opts.navigateAfterSignOut);
};
const handleSessionClicked = (session: SignedInSessionResource) => async () => {
card.setLoading();
return setActive({
session,
navigate: async ({ session }) => {
if (!session.currentTask && opts.afterSwitchSessionUrl) {
await navigate(opts.afterSwitchSessionUrl);
return;
}
if (opts.taskUrl) {
await navigate(opts.taskUrl);
return;
}
await navigateIfTaskExists(session, {
baseUrl: opts.signInUrl ?? displayConfig.signInUrl,
navigate,
});
},
}).finally(() => {
card.setIdle();
opts.actionCompleteCallback?.();
});
};
const handleAddAccountClicked = () => {
clerkWindowNavigate(clerk, opts.signInUrl || window.location.href);
return sleep(2000);
};
return {
handleSignOutSessionClicked,
handleManageAccountClicked,
handleUserProfileActionClicked,
handleSignOutAllClicked,
handleSessionClicked,
handleAddAccountClicked,
otherSessions,
signedInSessions,
};
};