11import { useClerk , useSession , useUser } from '@clerk/shared/react' ;
2- import { useState } from 'react' ;
2+ import { type ComponentType , useState } from 'react' ;
33
44import { useSignOutContext , withCoreSessionSwitchGuard } from '@/ui/contexts' ;
55import { descriptors , Flex , Flow , localizationKeys , Spinner } from '@/ui/customizables' ;
66import { Card } from '@/ui/elements/Card' ;
77import { withCardStateProvider } from '@/ui/elements/contexts' ;
8+ import { Header } from '@/ui/elements/Header' ;
89import { useMultipleSessions } from '@/ui/hooks/useMultipleSessions' ;
910import { useOrganizationListInView } from '@/ui/hooks/useOrganizationListInView' ;
1011
@@ -13,24 +14,10 @@ import { ChooseOrganizationScreen } from './ChooseOrganizationScreen';
1314import { CreateOrganizationScreen } from './CreateOrganizationScreen' ;
1415
1516const TaskChooseOrganizationInternal = ( ) => {
16- const { signOut } = useClerk ( ) ;
17- const { user } = useUser ( ) ;
18- const { session } = useSession ( ) ;
1917 const { userMemberships, userSuggestions, userInvitations } = useOrganizationListInView ( ) ;
20- const { otherSessions } = useMultipleSessions ( { user } ) ;
21- const { navigateAfterSignOut, navigateAfterMultiSessionSingleSignOutUrl } = useSignOutContext ( ) ;
22-
23- const handleSignOut = ( ) => {
24- if ( otherSessions . length === 0 ) {
25- return signOut ( navigateAfterSignOut ) ;
26- }
27-
28- return signOut ( navigateAfterMultiSessionSingleSignOutUrl , { sessionId : session ?. id } ) ;
29- } ;
3018
3119 const isLoading = userMemberships ?. isLoading || userInvitations ?. isLoading || userSuggestions ?. isLoading ;
3220 const hasExistingResources = ! ! ( userMemberships ?. count || userInvitations ?. count || userSuggestions ?. count ) ;
33- const identifier = user ?. primaryEmailAddress ?. emailAddress ?? user ?. username ;
3421
3522 return (
3623 < Flow . Root flow = 'taskChooseOrganization' >
@@ -58,34 +45,56 @@ const TaskChooseOrganizationInternal = () => {
5845 ) }
5946 </ Card . Content >
6047
61- < Card . Footer >
62- < Card . Action
63- elementId = 'signOut'
64- gap = { 2 }
65- justify = 'center'
66- sx = { ( ) => ( { width : '100%' } ) }
67- >
68- { identifier && (
69- < Card . ActionText
70- truncate
71- localizationKey = { localizationKeys ( 'taskChooseOrganization.signOut.actionText' , {
72- identifier,
73- } ) }
74- />
75- ) }
76- < Card . ActionLink
77- sx = { ( ) => ( { flexShrink : 0 } ) }
78- onClick = { handleSignOut }
79- localizationKey = { localizationKeys ( 'taskChooseOrganization.signOut.actionLink' ) }
80- />
81- </ Card . Action >
82- </ Card . Footer >
48+ < TaskChooseOrganizationCardFooter />
8349 </ Card . Root >
8450 </ Flow . Part >
8551 </ Flow . Root >
8652 ) ;
8753} ;
8854
55+ const TaskChooseOrganizationCardFooter = ( ) => {
56+ const { signOut } = useClerk ( ) ;
57+ const { user } = useUser ( ) ;
58+ const { session } = useSession ( ) ;
59+ const { otherSessions } = useMultipleSessions ( { user } ) ;
60+ const { navigateAfterSignOut, navigateAfterMultiSessionSingleSignOutUrl } = useSignOutContext ( ) ;
61+
62+ const handleSignOut = ( ) => {
63+ if ( otherSessions . length === 0 ) {
64+ return signOut ( navigateAfterSignOut ) ;
65+ }
66+
67+ return signOut ( navigateAfterMultiSessionSingleSignOutUrl , { sessionId : session ?. id } ) ;
68+ } ;
69+
70+ const identifier = user ?. primaryEmailAddress ?. emailAddress ?? user ?. username ;
71+
72+ return (
73+ < Card . Footer >
74+ < Card . Action
75+ elementId = 'signOut'
76+ gap = { 2 }
77+ justify = 'center'
78+ sx = { ( ) => ( { width : '100%' } ) }
79+ >
80+ { identifier && (
81+ < Card . ActionText
82+ truncate
83+ localizationKey = { localizationKeys ( 'taskChooseOrganization.signOut.actionText' , {
84+ identifier,
85+ } ) }
86+ />
87+ ) }
88+ < Card . ActionLink
89+ sx = { ( ) => ( { flexShrink : 0 } ) }
90+ onClick = { handleSignOut }
91+ localizationKey = { localizationKeys ( 'taskChooseOrganization.signOut.actionLink' ) }
92+ />
93+ </ Card . Action >
94+ </ Card . Footer >
95+ ) ;
96+ } ;
97+
8998type TaskChooseOrganizationFlowsProps = {
9099 initialFlow : 'create' | 'choose' ;
91100} ;
@@ -104,6 +113,44 @@ const TaskChooseOrganizationFlows = withCardStateProvider((props: TaskChooseOrga
104113 return < ChooseOrganizationScreen onCreateOrganizationClick = { ( ) => setCurrentFlow ( 'create' ) } /> ;
105114} ) ;
106115
116+ export const withOrganizationCreationEnabledGuard = < T extends object > ( Component : ComponentType < T > ) => {
117+ return ( props : T ) => {
118+ const { user } = useUser ( ) ;
119+
120+ if ( ! user ?. createOrganizationEnabled ) {
121+ return < OrganizationCreationDisabledScreen /> ;
122+ }
123+
124+ return < Component { ...props } /> ;
125+ } ;
126+ } ;
127+
128+ function OrganizationCreationDisabledScreen ( ) {
129+ return (
130+ < Flow . Root flow = 'taskChooseOrganization' >
131+ < Flow . Part part = 'organizationCreationDisabled' >
132+ < Card . Root >
133+ < Card . Content >
134+ < Header . Root showLogo >
135+ < Header . Title
136+ localizationKey = { localizationKeys ( 'taskChooseOrganization.organizationCreationDisabled.title' ) }
137+ />
138+ < Header . Subtitle
139+ localizationKey = { localizationKeys ( 'taskChooseOrganization.organizationCreationDisabled.subtitle' ) }
140+ />
141+ </ Header . Root >
142+ </ Card . Content >
143+
144+ < TaskChooseOrganizationCardFooter />
145+ </ Card . Root >
146+ </ Flow . Part >
147+ </ Flow . Root >
148+ ) ;
149+ }
150+
107151export const TaskChooseOrganization = withCoreSessionSwitchGuard (
108- withTaskGuard ( withCardStateProvider ( TaskChooseOrganizationInternal ) , 'choose-organization' ) ,
152+ withTaskGuard (
153+ withCardStateProvider ( withOrganizationCreationEnabledGuard ( TaskChooseOrganizationInternal ) ) ,
154+ 'choose-organization' ,
155+ ) ,
109156) ;
0 commit comments