feat: scope workspace routes by organization slug
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { Building2, Check, ChevronUp, Ellipsis, Link2, LogOut, Settings } from "lucide-react";
|
||||
import { FormEvent, useEffect, useRef, useState } from "react";
|
||||
|
||||
@@ -11,6 +11,7 @@ import { Input } from "@/components/ui/input";
|
||||
import type { ApiResult } from "@/modules/core/server/api";
|
||||
import type { AccountOrganizationOption, Organization, PublicAccount } from "@/modules/core/types";
|
||||
import { UserAvatar } from "@/modules/shared/components/UserAvatar";
|
||||
import { getWorkspaceHref, getWorkspacePathFromPathname } from "@/modules/shared/lib/workspace-routing";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
type AccountMenuProps = {
|
||||
@@ -25,6 +26,7 @@ export function AccountMenu({
|
||||
organizations,
|
||||
}: AccountMenuProps): React.ReactElement {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
const menuRef = useRef<HTMLDivElement>(null);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [isOrganizationOpen, setIsOrganizationOpen] = useState(false);
|
||||
@@ -71,7 +73,10 @@ export function AccountMenu({
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ organizationId }),
|
||||
});
|
||||
const result = (await response.json()) as ApiResult<Record<string, unknown>>;
|
||||
const result = (await response.json()) as ApiResult<{
|
||||
organization: Organization | null;
|
||||
organizations: AccountOrganizationOption[];
|
||||
}>;
|
||||
setIsPending(false);
|
||||
|
||||
if (!result.success) {
|
||||
@@ -80,6 +85,9 @@ export function AccountMenu({
|
||||
}
|
||||
|
||||
setIsOrganizationOpen(false);
|
||||
const activeOrganization = result.organization ?? result.organizations.find((item) => item.isActive);
|
||||
const workspacePath = getWorkspacePathFromPathname(pathname);
|
||||
router.replace(getWorkspaceHref(activeOrganization?.slug, workspacePath));
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user