Skip to content

Commit 1cf3850

Browse files
authored
Merge pull request #805 from trycompai/claudio/comp-184-fix-issue-with-double-selected-orgs
[dev] [claudfuen] claudio/comp-184-fix-issue-with-double-selected-orgs
2 parents 7454cdc + a930de6 commit 1cf3850

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

apps/app/src/components/organization-switcher.tsx

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
import { Check, ChevronsUpDown, Plus, Search, Loader2 } from "lucide-react";
2323
import { useAction } from "next-safe-action/hooks";
2424
import { useRouter } from "next/navigation";
25-
import { useState, useEffect } from "react";
25+
import { useState } from "react";
2626
import { CreateOrgModal } from "./modals/create-org-modal";
2727
import { useQueryState } from "nuqs";
2828

@@ -145,14 +145,6 @@ function OrganizationInitialsAvatar({
145145
);
146146
}
147147

148-
// Helper to get display name with unique identifier for duplicates
149-
function getDisplayName(org: Organization) {
150-
if (org.name) {
151-
return `${org.name}`;
152-
}
153-
return org.name;
154-
}
155-
156148
export function OrganizationSwitcher({
157149
organizations,
158150
organization,
@@ -163,7 +155,6 @@ export function OrganizationSwitcher({
163155
const [isDialogOpen, setIsDialogOpen] = useState(false);
164156
const [showCreateOrg, setShowCreateOrg] = useState(false);
165157
const [pendingOrgId, setPendingOrgId] = useState<string | null>(null);
166-
const [isLoading, setIsLoading] = useState(false);
167158

168159
const [showOrganizationSwitcher, setShowOrganizationSwitcher] =
169160
useQueryState("showOrganizationSwitcher", {
@@ -177,32 +168,52 @@ export function OrganizationSwitcher({
177168
const orgId = result.data?.data?.id;
178169
if (orgId) {
179170
router.push(`/${orgId}/`);
180-
setIsDialogOpen(false);
181171
}
172+
setIsDialogOpen(false);
173+
setPendingOrgId(null);
182174
},
183-
onExecute: () => {
184-
setIsLoading(true);
175+
onExecute: (args) => {
176+
setPendingOrgId(args.input.organizationId);
185177
},
186178
onError: () => {
187-
setIsLoading(false);
179+
setPendingOrgId(null);
188180
},
189181
});
190182

183+
const orgNameCounts = organizations.reduce(
184+
(acc, org) => {
185+
if (org.name) {
186+
acc[org.name] = (acc[org.name] || 0) + 1;
187+
}
188+
return acc;
189+
},
190+
{} as Record<string, number>,
191+
);
192+
193+
const getDisplayName = (org: Organization) => {
194+
if (!org.name) return `Org (${org.id.substring(0, 4)})`;
195+
if (orgNameCounts[org.name] > 1) {
196+
return `${org.name} (${org.id.substring(0, 4)})`;
197+
}
198+
return org.name;
199+
};
200+
191201
const currentOrganization = organization;
192202

193-
const handleOrgChange = async (org: Organization) => {
194-
setPendingOrgId(org.id);
203+
const handleOrgChange = (org: Organization) => {
195204
execute({ organizationId: org.id });
196205
};
197206

207+
const handleOpenChange = (open: boolean) => {
208+
setShowOrganizationSwitcher(open);
209+
setIsDialogOpen(open);
210+
};
211+
198212
return (
199213
<div className="w-full">
200214
<Dialog
201215
open={showOrganizationSwitcher ?? isDialogOpen}
202-
onOpenChange={(open) => {
203-
setShowOrganizationSwitcher(open);
204-
setIsDialogOpen(open);
205-
}}
216+
onOpenChange={handleOpenChange}
206217
>
207218
<DialogTrigger asChild>
208219
<Button
@@ -250,35 +261,26 @@ export function OrganizationSwitcher({
250261
{"No results found"}
251262
</CommandEmpty>
252263
<CommandGroup className="max-h-[300px] overflow-y-auto">
253-
{organizations.map((org) => (
254-
<CommandItem
255-
key={org.id}
256-
value={getDisplayName(org) || org.id}
257-
onSelect={() => {
258-
if (
259-
org.id !==
260-
currentOrganization?.id
261-
) {
264+
{organizations.map((org) => (
265+
<CommandItem
266+
key={org.id}
267+
value={org.id}
268+
onSelect={() => {
269+
if (org.id !== currentOrganization?.id) {
262270
handleOrgChange(org);
263271
} else {
264-
setIsDialogOpen(false);
272+
handleOpenChange(false);
265273
}
266274
}}
267-
disabled={isLoading}
275+
disabled={status === "executing"}
268276
>
269-
{isLoading && pendingOrgId === org.id ? (
277+
{status === "executing" &&
278+
pendingOrgId === org.id ? (
270279
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
280+
) : currentOrganization?.id === org.id ? (
281+
<Check className="mr-2 h-4 w-4" />
271282
) : (
272-
!isLoading && currentOrganization?.id === org.id ? (
273-
<Check
274-
className={cn(
275-
"mr-2 h-4 w-4",
276-
"opacity-100"
277-
)}
278-
/>
279-
) : (
280-
<span className="mr-2 h-4 w-4" />
281-
)
283+
<span className="mr-2 h-4 w-4" />
282284
)}
283285
<OrganizationInitialsAvatar
284286
name={org.name}

0 commit comments

Comments
 (0)