1
- import { useOrganizationList , useSessionContext , useUser } from '@clerk/shared/react' ;
1
+ import { useOrganizationList , useUser } from '@clerk/shared/react' ;
2
2
import { useContext , useState } from 'react' ;
3
3
4
4
import { Action , Actions } from '@/ui/elements/Actions' ;
@@ -84,6 +84,11 @@ export const OrganizationListPage = withCardStateProvider(() => {
84
84
85
85
const { hidePersonal } = useOrganizationListContext ( ) ;
86
86
87
+ const sessionTasksContext = useContext ( SessionTasksContext ) ;
88
+ if ( sessionTasksContext ) {
89
+ return < ForceOrganizationSelectionFlow /> ;
90
+ }
91
+
87
92
return (
88
93
< Card . Root >
89
94
< Card . Content sx = { t => ( { padding : `${ t . space . $8 } ${ t . space . $none } ${ t . space . $none } ` } ) } >
@@ -116,8 +121,6 @@ export const OrganizationListPage = withCardStateProvider(() => {
116
121
const OrganizationListFlows = ( { showListInitially } : { showListInitially : boolean } ) => {
117
122
const { navigateAfterCreateOrganization, skipInvitationScreen, hideSlug } = useOrganizationListContext ( ) ;
118
123
const [ isCreateOrganizationFlow , setCreateOrganizationFlow ] = useState ( ! showListInitially ) ;
119
- const sessionTasksContext = useContext ( SessionTasksContext ) ;
120
- const session = useSessionContext ( ) ;
121
124
122
125
return (
123
126
< >
@@ -133,23 +136,9 @@ const OrganizationListFlows = ({ showListInitially }: { showListInitially: boole
133
136
>
134
137
< CreateOrganizationForm
135
138
flow = 'organizationList'
136
- onComplete = { sessionTasksContext ?. nextTask }
137
139
startPage = { { headerTitle : localizationKeys ( 'organizationList.createOrganization' ) } }
138
140
skipInvitationScreen = { skipInvitationScreen }
139
- navigateAfterCreateOrganization = { org =>
140
- navigateAfterCreateOrganization ( org ) . then ( ( ) => {
141
- const isForceOrganizationSelectionFlow = sessionTasksContext && session ?. currentTask . key === 'org' ;
142
-
143
- // During a force organization selection flow, keep displaying the creation form in a loading state
144
- // rather than showing the organization list. This allows the client-side navigation to complete
145
- // before transitioning away from this view.
146
- if ( isForceOrganizationSelectionFlow ) {
147
- return ;
148
- }
149
-
150
- setCreateOrganizationFlow ( false ) ;
151
- } )
152
- }
141
+ navigateAfterCreateOrganization = { org => navigateAfterCreateOrganization ( org ) }
153
142
onCancel = {
154
143
showListInitially && isCreateOrganizationFlow ? ( ) => setCreateOrganizationFlow ( false ) : undefined
155
144
}
@@ -240,3 +229,71 @@ const OrganizationListPageList = (props: { onCreateOrganizationClick: () => void
240
229
</ >
241
230
) ;
242
231
} ;
232
+
233
+ const ForceOrganizationSelectionFlow = ( ) => {
234
+ const card = useCardState ( ) ;
235
+ const sessionTasksContext = useContext ( SessionTasksContext ) ;
236
+ const { navigateAfterCreateOrganization, hideSlug } = useOrganizationListContext ( ) ;
237
+ const { userMemberships, userSuggestions, userInvitations } = useOrganizationListInView ( ) ;
238
+
239
+ const [ isNavigating , setIsNavigating ] = useState ( false ) ;
240
+ const isLoading = isNavigating || ! ! ( userMemberships ?. count || userInvitations ?. count || userSuggestions ?. count ) ;
241
+
242
+ const [ isCreateOrganizationFlow , setIsCreateOrganizationFlow ] = useState ( ! userMemberships ?. data ?. length ) ;
243
+
244
+ if ( isLoading ) {
245
+ return (
246
+ < Card . Root >
247
+ < Card . Content sx = { t => ( { padding : `${ t . space . $8 } ${ t . space . $none } ${ t . space . $none } ` } ) } >
248
+ < Card . Alert sx = { t => ( { margin : `${ t . space . $none } ${ t . space . $5 } ` } ) } > { card . error } </ Card . Alert >
249
+ < Flex
250
+ direction = { 'row' }
251
+ align = { 'center' }
252
+ justify = { 'center' }
253
+ sx = { t => ( {
254
+ height : '100%' ,
255
+ minHeight : t . sizes . $60 ,
256
+ } ) }
257
+ >
258
+ < Spinner
259
+ size = { 'lg' }
260
+ colorScheme = { 'primary' }
261
+ elementDescriptor = { descriptors . spinner }
262
+ />
263
+ </ Flex >
264
+ </ Card . Content >
265
+ < Card . Footer />
266
+ </ Card . Root >
267
+ ) ;
268
+ }
269
+
270
+ return (
271
+ < Card . Root >
272
+ < Card . Content sx = { t => ( { padding : `${ t . space . $8 } ${ t . space . $none } ${ t . space . $none } ` } ) } >
273
+ < Card . Alert sx = { t => ( { margin : `${ t . space . $none } ${ t . space . $5 } ` } ) } > { card . error } </ Card . Alert >
274
+ { isCreateOrganizationFlow ? (
275
+ < Box
276
+ sx = { t => ( {
277
+ padding : `${ t . space . $none } ${ t . space . $5 } ${ t . space . $5 } ` ,
278
+ } ) }
279
+ >
280
+ < CreateOrganizationForm
281
+ flow = 'organizationList'
282
+ onComplete = { sessionTasksContext ?. nextTask }
283
+ startPage = { { headerTitle : localizationKeys ( 'organizationList.createOrganization' ) } }
284
+ skipInvitationScreen
285
+ navigateAfterCreateOrganization = { org => {
286
+ setIsNavigating ( true ) ;
287
+ return navigateAfterCreateOrganization ( org ) ;
288
+ } }
289
+ hideSlug = { hideSlug }
290
+ />
291
+ </ Box >
292
+ ) : (
293
+ < OrganizationListPageList onCreateOrganizationClick = { ( ) => setIsCreateOrganizationFlow ( true ) } />
294
+ ) }
295
+ </ Card . Content >
296
+ < Card . Footer />
297
+ </ Card . Root >
298
+ ) ;
299
+ } ;
0 commit comments