Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-parents-take.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Do not display create organization form after accepting organization invitation on after-auth flow
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useOrganizationList } from '@clerk/shared/react/index';
import type { PropsWithChildren } from 'react';
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { OrganizationListContext } from '@/ui/contexts';
import { Card } from '@/ui/elements/Card';
Expand All @@ -16,6 +16,8 @@ import { organizationListParams } from '../../OrganizationSwitcher/utils';
*/
export const ForceOrganizationSelectionTask = withCardStateProvider(() => {
const { userMemberships, userInvitations, userSuggestions } = useOrganizationList(organizationListParams);
const currentFlow = useRef<'create-organization' | 'organization-selection'>();

const isLoading = userMemberships?.isLoading || userInvitations?.isLoading || userSuggestions?.isLoading;
const hasData = !!(userMemberships?.count || userInvitations?.count || userSuggestions?.count);

Expand All @@ -27,16 +29,27 @@ export const ForceOrganizationSelectionTask = withCardStateProvider(() => {
);
}

if (hasData) {
return <OrganizationSelectionPage />;
// Only show the organization selection page if organizations exist when the component first mounts.
// This prevents unwanted screen transitions that could occur from data revalidation,
// such as when a user accepts an organization invitation and the membership list updates.
if (hasData || currentFlow.current === 'organization-selection') {
return <OrganizationSelectionPage currentFlow={currentFlow} />;
}

return <CreateOrganizationPage />;
return <CreateOrganizationPage currentFlow={currentFlow} />;
});

const OrganizationSelectionPage = () => {
type CommonPageProps = {
currentFlow: React.MutableRefObject<'create-organization' | 'organization-selection' | undefined>;
};

const OrganizationSelectionPage = ({ currentFlow }: CommonPageProps) => {
const [showCreateOrganizationForm, setShowCreateOrganizationForm] = useState(false);

useEffect(() => {
currentFlow.current = 'organization-selection';
}, [currentFlow]);

return (
<OrganizationListContext.Provider
value={{
Expand Down Expand Up @@ -66,7 +79,11 @@ const OrganizationSelectionPage = () => {
);
};

const CreateOrganizationPage = () => {
const CreateOrganizationPage = ({ currentFlow }: CommonPageProps) => {
useEffect(() => {
currentFlow.current = 'create-organization';
}, [currentFlow]);

return (
<FlowCard>
<Box
Expand Down
Loading