|
1 | 1 | import { ActiveSessionResource, ClientResource, InitialState, Resources, UserResource } from '@clerk/types';
|
2 | 2 | import React from 'react';
|
3 | 3 |
|
4 |
| -import IsomorphicClerk from '../isomorphicClerk'; |
| 4 | +import IsomorphicClerk, { NewIsomorphicClerkParams } from '../isomorphicClerk'; |
5 | 5 | import { AuthContext } from './AuthContext';
|
6 | 6 | import { ClientContext } from './ClientContext';
|
7 | 7 | import { IsomorphicClerkContext } from './IsomorphicClerkContext';
|
8 | 8 | import { SessionContext } from './SessionContext';
|
9 | 9 | import { UserContext } from './UserContext';
|
10 | 10 |
|
11 |
| -type ClerkContextWrapperProps = { |
12 |
| - isomorphicClerk: IsomorphicClerk; |
| 11 | +type ClerkContextProvider = { |
| 12 | + isomorphicClerkOptions: NewIsomorphicClerkParams; |
13 | 13 | initialState: InitialState | undefined;
|
14 | 14 | children: React.ReactNode;
|
15 |
| - clerkLoaded: boolean; |
16 | 15 | };
|
17 | 16 |
|
18 | 17 | type ClerkContextProviderState = Resources;
|
19 | 18 |
|
20 |
| -export function ClerkContextProvider(props: ClerkContextWrapperProps): JSX.Element | null { |
21 |
| - const { isomorphicClerk, children, clerkLoaded, initialState } = props; |
22 |
| - const clerk = isomorphicClerk; |
| 19 | +export function ClerkContextProvider(props: ClerkContextProvider): JSX.Element | null { |
| 20 | + const { isomorphicClerkOptions, initialState, children } = props; |
| 21 | + const { isomorphicClerk: clerk, loaded: clerkLoaded } = useLoadedIsomorphicClerk(isomorphicClerkOptions); |
23 | 22 |
|
24 | 23 | const [state, setState] = React.useState<ClerkContextProviderState>({
|
25 | 24 | client: clerk.client as ClientResource,
|
@@ -62,6 +61,17 @@ export function ClerkContextProvider(props: ClerkContextWrapperProps): JSX.Eleme
|
62 | 61 | );
|
63 | 62 | }
|
64 | 63 |
|
| 64 | +const useLoadedIsomorphicClerk = (options: NewIsomorphicClerkParams) => { |
| 65 | + const [loaded, setLoaded] = React.useState(false); |
| 66 | + const isomorphicClerk = React.useMemo(() => IsomorphicClerk.getOrCreateInstance(options), []); |
| 67 | + |
| 68 | + React.useEffect(() => { |
| 69 | + isomorphicClerk.addOnLoaded(() => setLoaded(true)); |
| 70 | + }, []); |
| 71 | + |
| 72 | + return { isomorphicClerk, loaded }; |
| 73 | +}; |
| 74 | + |
65 | 75 | // This should be provided from isomorphicClerk
|
66 | 76 | // TODO: move inside isomorphicClerk
|
67 | 77 | function deriveState(
|
|
0 commit comments