-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathsessionTasks.ts
50 lines (42 loc) · 1.56 KB
/
sessionTasks.ts
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
import type {
__internal_ComponentNavigationContext,
ClerkOptions,
EnvironmentResource,
SessionTask,
} from '@clerk/types';
import { buildURL } from '../utils';
export const SESSION_TASK_ROUTE_BY_KEY: Record<SessionTask['key'], string> = {
org: 'add-organization',
} as const;
interface NavigateToTaskOptions {
componentNavigationContext: __internal_ComponentNavigationContext | null;
globalNavigate: (to: string) => Promise<unknown>;
options: ClerkOptions;
environment: EnvironmentResource;
}
/**
* Handles navigation to the tasks URL based on the application context such
* as internal component routing or custom flows.
* @internal
*/
export function navigateToTask(
routeKey: keyof typeof SESSION_TASK_ROUTE_BY_KEY,
{ componentNavigationContext, globalNavigate, options, environment }: NavigateToTaskOptions,
) {
const taskRoute = `/tasks/${SESSION_TASK_ROUTE_BY_KEY[routeKey]}`;
if (componentNavigationContext) {
return componentNavigationContext.navigate(componentNavigationContext.indexPath + taskRoute);
}
const signInUrl = options['signInUrl'] || environment.displayConfig.signInUrl;
const signUpUrl = options['signUpUrl'] || environment.displayConfig.signUpUrl;
const isReferrerSignUpUrl = window.location.href.startsWith(signUpUrl);
const sessionTaskUrl = buildURL(
// TODO - Accept custom URL option for custom flows in order to eject out of `signInUrl/signUpUrl`
{
base: isReferrerSignUpUrl ? signUpUrl : signInUrl,
hashPath: taskRoute,
},
{ stringify: true },
);
return globalNavigate(sessionTaskUrl);
}