Skip to content

Commit 34bb287

Browse files
authored
Merge pull request #429 from trycompai/mariano/comp-75-org-switcher-creation-is-buggy
fix org switching
2 parents 7d58f7a + 9671562 commit 34bb287

File tree

6 files changed

+55
-46
lines changed

6 files changed

+55
-46
lines changed

apps/app/src/actions/change-organization.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ export const changeOrganizationAction = authActionClient
5555
auth.api.setActiveOrganization({
5656
headers: await headers(),
5757
body: {
58-
organizationId: organizationId,
58+
organizationId: organization.id,
5959
},
6060
});
6161

62-
revalidatePath(`/${organization.id}`, "layout");
62+
revalidatePath(`/${organization.id}`);
6363

6464
return {
6565
success: true,

apps/app/src/app/[locale]/(app)/(dashboard)/[orgId]/layout.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AssistantSheet } from "@/components/sheets/assistant-sheet";
44
import { Sidebar } from "@/components/sidebar";
55
import { SidebarProvider } from "@/context/sidebar-context";
66
import { auth } from "@/utils/auth";
7+
import { db } from "@comp/db";
78
import dynamic from "next/dynamic";
89
import { cookies, headers } from "next/headers";
910
import { redirect } from "next/navigation";
@@ -35,9 +36,18 @@ export default async function Layout({
3536
return redirect("/");
3637
}
3738

39+
const currentOrganization = await db.organization.findUnique({
40+
where: {
41+
id: session.session.activeOrganizationId,
42+
},
43+
});
44+
3845
return (
3946
<SidebarProvider initialIsCollapsed={isCollapsed}>
40-
<AnimatedLayout sidebar={<Sidebar />} isCollapsed={isCollapsed}>
47+
<AnimatedLayout
48+
sidebar={<Sidebar organization={currentOrganization} />}
49+
isCollapsed={isCollapsed}
50+
>
4151
<div className="mx-4 md:ml-[95px] md:mr-10 pb-8">
4252
<Header />
4353
<main>{children}</main>

apps/app/src/components/main-menu.tsx

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { useI18n } from "@/locales/client";
4+
import { authClient } from "@/utils/auth-client";
45
import { Badge } from "@comp/ui/badge";
56
import { cn } from "@comp/ui/cn";
67
import { Icons } from "@comp/ui/icons";
@@ -45,23 +46,24 @@ type MenuItem = {
4546
};
4647

4748
interface ItemProps {
49+
organizationId: string;
4850
item: MenuItem;
4951
isActive: boolean;
5052
disabled: boolean;
51-
organizationId: string;
5253
isCollapsed?: boolean;
5354
onItemClick?: () => void;
5455
}
5556

5657
export function MainMenu({
57-
organizationId,
5858
//userIsAdmin,
5959
isCollapsed = false,
6060
completedOnboarding,
6161
onItemClick,
6262
}: Props) {
6363
const t = useI18n();
6464
const pathname = usePathname();
65+
const session = authClient.useSession();
66+
const organizationId = session?.data?.session?.activeOrganizationId;
6567

6668
const items: MenuItem[] = [
6769
{
@@ -167,12 +169,13 @@ export function MainMenu({
167169
const isPathActive = (itemPath: string) => {
168170
const normalizedItemPath = itemPath.replace(
169171
":organizationId",
170-
organizationId,
172+
organizationId ?? "",
171173
);
172174

173175
// Extract the base path from the menu item (first two segments after normalization)
174176
const itemPathParts = normalizedItemPath.split("/").filter(Boolean);
175-
const itemBaseSegment = itemPathParts.length > 1 ? itemPathParts[1] : "";
177+
const itemBaseSegment =
178+
itemPathParts.length > 1 ? itemPathParts[1] : "";
176179

177180
// Extract the current path parts
178181
const currentPathParts = pathname.split("/").filter(Boolean);
@@ -232,10 +235,10 @@ export function MainMenu({
232235
return (
233236
<Item
234237
key={item.id}
238+
organizationId={organizationId ?? ""}
235239
item={item}
236240
isActive={isActive}
237241
disabled={item.disabled}
238-
organizationId={organizationId}
239242
isCollapsed={isCollapsed}
240243
onItemClick={onItemClick}
241244
/>
@@ -248,10 +251,10 @@ export function MainMenu({
248251
}
249252

250253
const Item = ({
254+
organizationId,
251255
item,
252256
isActive,
253257
disabled,
254-
organizationId,
255258
isCollapsed = false,
256259
onItemClick,
257260
}: ItemProps) => {
@@ -265,7 +268,8 @@ const Item = ({
265268
// Badge variants mapping
266269
const badgeVariants = {
267270
default: "bg-primary/80 text-primary-foreground hover:bg-primary/90",
268-
secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
271+
secondary:
272+
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
269273
outline:
270274
"border-border bg-background hover:bg-accent hover:text-accent-foreground",
271275
new: "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400",
@@ -285,12 +289,14 @@ const Item = ({
285289
<div
286290
className={cn(
287291
"relative border border-transparent flex items-center",
288-
isCollapsed ? "md:w-[45px] md:justify-center" : "md:px-3",
292+
isCollapsed
293+
? "md:w-[45px] md:justify-center"
294+
: "md:px-3",
289295
"w-full px-3 md:w-auto h-[45px]",
290296
"hover:bg-accent hover:border-border",
291297
"transition-all duration-300",
292298
isActive &&
293-
"bg-accent dark:bg-secondary border-border border-r-2 border-r-primary",
299+
"bg-accent dark:bg-secondary border-border border-r-2 border-r-primary",
294300
)}
295301
>
296302
<div
@@ -316,7 +322,10 @@ const Item = ({
316322
variant="outline"
317323
className={cn(
318324
"ml-1.5 text-[9px] px-1 py-0 h-auto",
319-
badgeVariants[item.badge.variant],
325+
badgeVariants[
326+
item.badge
327+
.variant
328+
],
320329
)}
321330
>
322331
{item.badge.text}
@@ -362,9 +371,8 @@ const Item = ({
362371
};
363372

364373
type Props = {
365-
organizationId: string;
366374
//userIsAdmin: boolean;
367375
isCollapsed?: boolean;
368376
completedOnboarding: boolean;
369377
onItemClick?: () => void;
370-
};
378+
};

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import { useState } from "react";
2020
import { CreateOrgModal } from "./modals/create-org-modal";
2121
interface OrganizationSwitcherProps {
2222
organizations: Organization[];
23-
organizationId: string | undefined;
23+
organization: Organization | null;
2424
isCollapsed?: boolean;
2525
}
2626

2727
export function OrganizationSwitcher({
2828
organizations,
29-
organizationId,
29+
organization,
3030
isCollapsed = false,
3131
}: OrganizationSwitcherProps) {
3232
const t = useI18n();
@@ -36,24 +36,22 @@ export function OrganizationSwitcher({
3636

3737
const { execute, status } = useAction(changeOrganizationAction, {
3838
onSuccess: (result) => {
39-
if (result.data?.success) {
40-
router.refresh();
39+
const orgId = result.data?.data?.id;
40+
if (orgId) {
41+
router.push(`/${orgId}`);
42+
setIsOpen(false);
4143
}
4244
},
4345
});
4446

45-
const currentOrganization = organizations.find(
46-
(org) => org.id === organizationId,
47-
);
47+
const currentOrganization = organization;
4848

4949
const otherOrganizations = organizations.filter(
50-
(org) => org.id !== organizationId,
50+
(org) => org.id !== currentOrganization?.id,
5151
);
5252

5353
const handleOrgChange = async (org: Organization) => {
5454
execute({ organizationId: org.id });
55-
router.push(`/${org.id}`);
56-
setIsOpen(false);
5755
};
5856

5957
return (

apps/app/src/components/sidebar-logo.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
"use client";
22

3+
import { authClient } from "@/utils/auth-client";
34
import { cn } from "@comp/ui/cn";
45
import { Icons } from "@comp/ui/icons";
56
import Link from "next/link";
67

78
interface SidebarLogoProps {
89
isCollapsed: boolean;
9-
organizationId: string;
1010
}
1111

12-
export function SidebarLogo({ isCollapsed, organizationId }: SidebarLogoProps) {
12+
export function SidebarLogo({ isCollapsed }: SidebarLogoProps) {
13+
const session = authClient.useSession();
14+
15+
const organizationId = session?.data?.session?.activeOrganizationId;
16+
1317
return (
1418
<div className={cn("transition-all duration-300 flex items-center")}>
1519
<Link href={`/${organizationId}`}>

apps/app/src/components/sidebar.tsx

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,32 @@
11
import { getOnboardingForCurrentOrganization } from "@/data/getOnboarding";
22
import { getOrganizations } from "@/data/getOrganizations";
3-
import { auth } from "@/utils/auth";
4-
import { cookies, headers } from "next/headers";
5-
import { redirect } from "next/navigation";
3+
import type { Organization } from "@comp/db/types";
4+
import { cookies } from "next/headers";
65
import { MainMenu } from "./main-menu";
76
import { OrganizationSwitcher } from "./organization-switcher";
87
import { SidebarCollapseButton } from "./sidebar-collapse-button";
98
import { SidebarLogo } from "./sidebar-logo";
109

11-
export async function Sidebar() {
12-
const session = await auth.api.getSession({
13-
headers: await headers(),
14-
});
15-
const organizationId = session?.session.activeOrganizationId;
10+
export async function Sidebar({
11+
organization,
12+
}: { organization: Organization | null }) {
1613
const cookieStore = await cookies();
1714
const isCollapsed = cookieStore.get("sidebar-collapsed")?.value === "true";
1815
const { completedAll } = await getOnboardingForCurrentOrganization();
19-
20-
if (!organizationId) {
21-
redirect("/");
22-
}
23-
2416
const { organizations } = await getOrganizations();
2517

2618
return (
2719
<div className="h-full flex flex-col gap-0">
2820
<div className="p-4 flex flex-col gap-0">
2921
<div className="flex items-center justify-between">
30-
<SidebarLogo
31-
isCollapsed={isCollapsed}
32-
organizationId={organizationId}
33-
/>
22+
<SidebarLogo isCollapsed={isCollapsed} />
3423
{!isCollapsed && (
3524
<SidebarCollapseButton isCollapsed={isCollapsed} />
3625
)}
3726
</div>
3827
<MainMenu
3928
//userIsAdmin={user?.isAdmin ?? false}
40-
organizationId={organizationId}
29+
4130
isCollapsed={isCollapsed}
4231
completedOnboarding={completedAll}
4332
/>
@@ -52,7 +41,7 @@ export async function Sidebar() {
5241
<div className="flex h-[60px] items-center mx-auto w-full pb-1">
5342
<OrganizationSwitcher
5443
organizations={organizations}
55-
organizationId={organizationId}
44+
organization={organization}
5645
isCollapsed={isCollapsed}
5746
/>
5847
</div>

0 commit comments

Comments
 (0)