|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useClerk, useOrganization, useUser } from '@clerk/shared/react'; |
| 4 | +import type { EnvironmentResource, OrganizationProfileProps } from '@clerk/shared/types'; |
| 5 | +import type { PropsWithChildren, ReactNode } from 'react'; |
| 6 | + |
| 7 | +import type { Appearance } from '@/ui/internal/appearance'; |
| 8 | + |
| 9 | +import { OrganizationProfileContext } from '../../contexts/components/OrganizationProfile'; |
| 10 | +import { SubscriberTypeContext } from '../../contexts/components/SubscriberType'; |
| 11 | +import { fallbackModuleManager, ProfileProviderShell } from '../ProfileProviderShell'; |
| 12 | + |
| 13 | +type OrganizationProfileProviderProps = PropsWithChildren<{ |
| 14 | + appearance?: Appearance; |
| 15 | + afterLeaveOrganizationUrl?: OrganizationProfileProps['afterLeaveOrganizationUrl']; |
| 16 | + apiKeysProps?: OrganizationProfileProps['apiKeysProps']; |
| 17 | +}>; |
| 18 | + |
| 19 | +export const OrganizationProfileProvider = (props: OrganizationProfileProviderProps): ReactNode => { |
| 20 | + const { children, appearance, afterLeaveOrganizationUrl, apiKeysProps } = props; |
| 21 | + const clerk = useClerk(); |
| 22 | + const { isLoaded, user } = useUser(); |
| 23 | + const { organization } = useOrganization(); |
| 24 | + |
| 25 | + const environment = (clerk as any).__internal_environment as EnvironmentResource | null | undefined; |
| 26 | + const moduleManager = clerk.__internal_moduleManager ?? fallbackModuleManager; |
| 27 | + |
| 28 | + if (!isLoaded || !user || !organization || !environment) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + const orgProfileCtxValue = { |
| 33 | + componentName: 'OrganizationProfile' as const, |
| 34 | + mode: 'mounted' as const, |
| 35 | + routing: 'hash' as const, |
| 36 | + path: undefined, |
| 37 | + afterLeaveOrganizationUrl, |
| 38 | + apiKeysProps, |
| 39 | + customPages: [], |
| 40 | + }; |
| 41 | + |
| 42 | + return ( |
| 43 | + <ProfileProviderShell |
| 44 | + clerk={clerk} |
| 45 | + environment={environment} |
| 46 | + moduleManager={moduleManager} |
| 47 | + appearanceKey='organizationProfile' |
| 48 | + flow='organizationProfile' |
| 49 | + globalAppearance={clerk.__internal_getOption('appearance')} |
| 50 | + appearance={appearance} |
| 51 | + > |
| 52 | + <SubscriberTypeContext.Provider value='organization'> |
| 53 | + <OrganizationProfileContext.Provider value={orgProfileCtxValue}>{children}</OrganizationProfileContext.Provider> |
| 54 | + </SubscriberTypeContext.Provider> |
| 55 | + </ProfileProviderShell> |
| 56 | + ); |
| 57 | +}; |
0 commit comments