1
1
import { useOrganizationList } from '@clerk/shared/react/index' ;
2
2
import type { PropsWithChildren } from 'react' ;
3
- import { useState } from 'react' ;
3
+ import { useEffect , useRef , useState } from 'react' ;
4
4
5
5
import { OrganizationListContext } from '@/ui/contexts' ;
6
6
import { Card } from '@/ui/elements/Card' ;
@@ -16,6 +16,8 @@ import { organizationListParams } from '../../OrganizationSwitcher/utils';
16
16
*/
17
17
export const ForceOrganizationSelectionTask = withCardStateProvider ( ( ) => {
18
18
const { userMemberships, userInvitations, userSuggestions } = useOrganizationList ( organizationListParams ) ;
19
+ const currentFlow = useRef < 'create-organization' | 'organization-selection' > ( ) ;
20
+
19
21
const isLoading = userMemberships ?. isLoading || userInvitations ?. isLoading || userSuggestions ?. isLoading ;
20
22
const hasData = ! ! ( userMemberships ?. count || userInvitations ?. count || userSuggestions ?. count ) ;
21
23
@@ -27,16 +29,27 @@ export const ForceOrganizationSelectionTask = withCardStateProvider(() => {
27
29
) ;
28
30
}
29
31
30
- if ( hasData ) {
31
- return < OrganizationSelectionPage /> ;
32
+ // Only show the organization selection page if organizations exist when the component first mounts.
33
+ // This prevents unwanted screen transitions that could occur from data revalidation,
34
+ // such as when a user accepts an organization invitation and the membership list updates.
35
+ if ( hasData || currentFlow . current === 'organization-selection' ) {
36
+ return < OrganizationSelectionPage currentFlow = { currentFlow } /> ;
32
37
}
33
38
34
- return < CreateOrganizationPage /> ;
39
+ return < CreateOrganizationPage currentFlow = { currentFlow } /> ;
35
40
} ) ;
36
41
37
- const OrganizationSelectionPage = ( ) => {
42
+ type CommonPageProps = {
43
+ currentFlow : React . MutableRefObject < 'create-organization' | 'organization-selection' | undefined > ;
44
+ } ;
45
+
46
+ const OrganizationSelectionPage = ( { currentFlow } : CommonPageProps ) => {
38
47
const [ showCreateOrganizationForm , setShowCreateOrganizationForm ] = useState ( false ) ;
39
48
49
+ useEffect ( ( ) => {
50
+ currentFlow . current = 'organization-selection' ;
51
+ } , [ currentFlow ] ) ;
52
+
40
53
return (
41
54
< OrganizationListContext . Provider
42
55
value = { {
@@ -66,7 +79,11 @@ const OrganizationSelectionPage = () => {
66
79
) ;
67
80
} ;
68
81
69
- const CreateOrganizationPage = ( ) => {
82
+ const CreateOrganizationPage = ( { currentFlow } : CommonPageProps ) => {
83
+ useEffect ( ( ) => {
84
+ currentFlow . current = 'create-organization' ;
85
+ } , [ currentFlow ] ) ;
86
+
70
87
return (
71
88
< FlowCard >
72
89
< Box
0 commit comments