Skip to content

Commit acf4b69

Browse files
committed
Simplify conditions to render UI on mount
1 parent 670ba78 commit acf4b69

File tree

2 files changed

+35
-55
lines changed

2 files changed

+35
-55
lines changed

.changeset/chatty-llamas-wink.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
'@clerk/shared': patch
3+
'@clerk/clerk-react': patch
4+
---
Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useOrganizationList } from '@clerk/shared/react/index';
2-
import type { ComponentProps, PropsWithChildren } from 'react';
3-
import { useEffect, useState } from 'react';
2+
import type { PropsWithChildren } from 'react';
3+
import { useState } from 'react';
44

55
import { OrganizationListContext } from '@/ui/contexts';
66
import { Card } from '@/ui/elements/Card';
@@ -11,8 +11,13 @@ import { CreateOrganizationForm } from '../../CreateOrganization/CreateOrganizat
1111
import { OrganizationListPageList } from '../../OrganizationList/OrganizationListPage';
1212
import { organizationListParams } from '../../OrganizationSwitcher/utils';
1313

14-
const ForceOrganizationSelectionFlows = () => {
15-
const { isLoading, hasData, currentFlow, setCurrentFlow } = useForceOrganizationSelectionFlows();
14+
/**
15+
* @internal
16+
*/
17+
export const ForceOrganizationSelectionTask = withCardStateProvider(() => {
18+
const { userMemberships, userInvitations, userSuggestions } = useOrganizationList(organizationListParams);
19+
const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
20+
const hasData = !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);
1621

1722
if (isLoading) {
1823
return (
@@ -22,26 +27,16 @@ const ForceOrganizationSelectionFlows = () => {
2227
);
2328
}
2429

25-
// Do not render the organization selection flow when organization memberships
26-
// get invalidated after the create organization mutation
27-
if (hasData && currentFlow !== 'create-organization') {
28-
return <OrganizationSelectionPage setCurrentFlow={setCurrentFlow} />;
30+
if (hasData) {
31+
return <OrganizationSelectionPage />;
2932
}
3033

31-
return <CreateOrganizationPage setCurrentFlow={setCurrentFlow} />;
32-
};
34+
return <CreateOrganizationPage />;
35+
});
3336

34-
const OrganizationSelectionPage = ({ setCurrentFlow }: CommonPageProps) => {
37+
const OrganizationSelectionPage = () => {
3538
const [showCreateOrganizationForm, setShowCreateOrganizationForm] = useState(false);
3639

37-
useEffect(() => {
38-
setCurrentFlow('organization-selection');
39-
}, [setCurrentFlow]);
40-
41-
if (showCreateOrganizationForm) {
42-
return <CreateOrganizationPage setCurrentFlow={setCurrentFlow} />;
43-
}
44-
4540
return (
4641
<OrganizationListContext.Provider
4742
value={{
@@ -50,20 +45,28 @@ const OrganizationSelectionPage = ({ setCurrentFlow }: CommonPageProps) => {
5045
}}
5146
>
5247
<FlowCard>
53-
<OrganizationListPageList onCreateOrganizationClick={() => setShowCreateOrganizationForm(true)} />
48+
{showCreateOrganizationForm ? (
49+
<Box
50+
sx={t => ({
51+
padding: `${t.space.$none} ${t.space.$5} ${t.space.$5}`,
52+
})}
53+
>
54+
<CreateOrganizationForm
55+
flow='default'
56+
startPage={{ headerTitle: localizationKeys('organizationList.createOrganization') }}
57+
skipInvitationScreen
58+
onCancel={() => setShowCreateOrganizationForm(false)}
59+
/>
60+
</Box>
61+
) : (
62+
<OrganizationListPageList onCreateOrganizationClick={() => setShowCreateOrganizationForm(true)} />
63+
)}
5464
</FlowCard>
5565
</OrganizationListContext.Provider>
5666
);
5767
};
5868

59-
const CreateOrganizationPage = ({
60-
onCancel,
61-
setCurrentFlow,
62-
}: CommonPageProps & Pick<ComponentProps<typeof CreateOrganizationForm>, 'onCancel'>) => {
63-
useEffect(() => {
64-
setCurrentFlow('create-organization');
65-
}, [setCurrentFlow]);
66-
69+
const CreateOrganizationPage = () => {
6770
return (
6871
<FlowCard>
6972
<Box
@@ -75,7 +78,6 @@ const CreateOrganizationPage = ({
7578
flow='default'
7679
startPage={{ headerTitle: localizationKeys('organizationList.createOrganization') }}
7780
skipInvitationScreen
78-
onCancel={onCancel}
7981
/>
8082
</Box>
8183
</FlowCard>
@@ -112,29 +114,3 @@ const FlowLoadingState = () => (
112114
/>
113115
</Flex>
114116
);
115-
116-
type Flow = 'create-organization' | 'organization-selection';
117-
118-
type CommonPageProps = {
119-
setCurrentFlow: React.Dispatch<React.SetStateAction<Flow | undefined>>;
120-
};
121-
122-
const useForceOrganizationSelectionFlows = () => {
123-
const [currentFlow, setCurrentFlow] = useState<Flow | undefined>();
124-
const { userMemberships, userInvitations, userSuggestions } = useOrganizationList(organizationListParams);
125-
126-
const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
127-
const hasData = !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);
128-
129-
return {
130-
currentFlow,
131-
setCurrentFlow,
132-
hasData,
133-
isLoading,
134-
};
135-
};
136-
137-
/**
138-
* @internal
139-
*/
140-
export const ForceOrganizationSelectionTask = withCardStateProvider(ForceOrganizationSelectionFlows);

0 commit comments

Comments
 (0)