-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathcontexts.tsx
99 lines (86 loc) · 2.89 KB
/
contexts.tsx
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
'use client';
import type {
ClerkOptions,
ClientResource,
LoadedClerk,
OrganizationResource,
SignedInSessionResource,
UserResource,
} from '@clerk/types';
import type { PropsWithChildren } from 'react';
import React from 'react';
import { SWRConfig } from './clerk-swr';
import { createContextAndHook } from './hooks/createContextAndHook';
const [ClerkInstanceContext, useClerkInstanceContext] = createContextAndHook<LoadedClerk>('ClerkInstanceContext');
const [UserContext, useUserContext] = createContextAndHook<UserResource | null | undefined>('UserContext');
const [ClientContext, useClientContext] = createContextAndHook<ClientResource | null | undefined>('ClientContext');
const [SessionContext, useSessionContext] = createContextAndHook<SignedInSessionResource | null | undefined>(
'SessionContext',
);
const OptionsContext = React.createContext<ClerkOptions>({});
function useOptionsContext(): ClerkOptions {
const context = React.useContext(OptionsContext);
if (context === undefined) {
throw new Error('useOptions must be used within an OptionsContext');
}
return context;
}
type OrganizationContextProps = {
organization: OrganizationResource | null | undefined;
};
const [OrganizationContextInternal, useOrganizationContext] = createContextAndHook<{
organization: OrganizationResource | null | undefined;
}>('OrganizationContext');
const OrganizationProvider = ({
children,
organization,
swrConfig,
}: PropsWithChildren<
OrganizationContextProps & {
// Exporting inferred types directly from SWR will result in error while building declarations
swrConfig?: any;
}
>) => {
return (
<SWRConfig value={swrConfig}>
<OrganizationContextInternal.Provider
value={{
value: { organization },
}}
>
{children}
</OrganizationContextInternal.Provider>
</SWRConfig>
);
};
function useAssertWrappedByClerkProvider(displayNameOrFn: string | (() => void)): void {
const ctx = React.useContext(ClerkInstanceContext);
if (!ctx) {
if (typeof displayNameOrFn === 'function') {
displayNameOrFn();
return;
}
throw new Error(
`${displayNameOrFn} can only be used within the <ClerkProvider /> component.
Possible fixes:
1. Ensure that the <ClerkProvider /> is correctly wrapping your application where this component is used.
2. Check for multiple versions of the \`@clerk/shared\` package in your project. Use a tool like \`npm ls @clerk/shared\` to identify multiple versions, and update your dependencies to only rely on one.
Learn more: https://clerk.com/docs/components/clerk-provider`.trim(),
);
}
}
export {
ClientContext,
useClientContext,
OrganizationProvider,
useOrganizationContext,
UserContext,
OptionsContext,
useOptionsContext,
useUserContext,
SessionContext,
useSessionContext,
ClerkInstanceContext,
useClerkInstanceContext,
useAssertWrappedByClerkProvider,
};