Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(nav): org/project switcher in the mobile nav drawer (LFE-11067) (#15330)

Nikita Kabardin24bc0c3d9c4b
diff --git a/web/src/components/layouts/breadcrumb.tsx b/web/src/components/layouts/breadcrumb.tsx
index 3d22d2b8bf..ddb489343a 100644
--- a/web/src/components/layouts/breadcrumb.tsx
+++ b/web/src/components/layouts/breadcrumb.tsx
@@ -12,8 +12,10 @@ import {
} from "@/src/components/ui/dropdown-menu";
import { ChevronDownIcon, Slash } from "lucide-react";
import { env } from "@/src/env.mjs";
-import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";
-import { useRouter } from "next/router";
+import {
+ useOrgProjectSwitchPaths,
+ useQueryProjectOrOrganization,
+} from "@/src/features/projects/hooks";
import { useSession } from "next-auth/react";
import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { isCloudPlan, planLabels } from "@langfuse/shared";
@@ -29,9 +31,9 @@ const BreadcrumbComponent = ({
items?: { name: string; href?: string }[];
className?: string;
}) => {
- const router = useRouter();
const session = useSession();
const { organization, project } = useQueryProjectOrOrganization();
+ const { getProjectPath, getOrgPath } = useOrgProjectSwitchPaths();
const organizations = session.data?.user?.organizations;
@@ -41,39 +43,6 @@ const BreadcrumbComponent = ({
scope: "projects:create",
});
- /**
- * Truncate the path before the first dynamic segment that is not allowlisted.
- * e.g. /project/[projectId]/traces/[traceId] -> /project/[projectId]/traces
- */
- const truncatePathBeforeDynamicSegments = (path: string) => {
- const allowlistedIds = ["[projectId]", "[organizationId]", "[page]"];
- const segments = router.route.split("/");
- const idSegments = segments.filter(
- (segment) => segment.startsWith("[") && segment.endsWith("]"),
- );
- const stopSegment = idSegments.filter((id) => !allowlistedIds.includes(id));
- if (stopSegment.length === 0) return path;
- const stopIndex = segments.indexOf(stopSegment[0]);
- const truncatedPath = path.split("/").slice(0, stopIndex).join("/");
- return truncatedPath;
- };
-
- const getProjectPath = (projectId: string) =>
- router.query.projectId
- ? truncatePathBeforeDynamicSegments(router.asPath).replace(
- router.query.projectId as string,
- projectId,
- )
- : `/project/${projectId}`;
-
- const getOrgPath = (orgId: string) =>
- router.query.organizationId
- ? truncatePathBeforeDynamicSegments(router.asPath).replace(
- router.query.organizationId as string,
- orgId,
- )
- : `/organization/${orgId}`;
-
return (
<Breadcrumb className={className}>
<BreadcrumbList>
diff --git a/web/src/components/nav/app-sidebar.tsx b/web/src/components/nav/app-sidebar.tsx
index b39ca30632..819a9e288d 100644
--- a/web/src/components/nav/app-sidebar.tsx
+++ b/web/src/components/nav/app-sidebar.tsx
@@ -18,11 +18,13 @@ import {
SidebarMenuButton,
SidebarMenuItem,
SidebarRail,
+ useSidebar,
} from "@/src/components/ui/sidebar";
import { env } from "@/src/env.mjs";
import { useRouter } from "next/router";
import Link from "next/link";
import { LangfuseLogo } from "@/src/components/LangfuseLogo";
+import { MobileNavSwitcher } from "@/src/components/nav/mobile-nav-switcher";
import { SidebarNotifications } from "@/src/components/nav/sidebar-notifications";
import { type RouteGroup } from "@/src/components/layouts/routes";
import { ExternalLink, Grid2X2 } from "lucide-react";
@@ -46,6 +48,8 @@ export function AppSidebar({
userNavProps,
...props
}: AppSidebarProps) {
+ const { isMobile } = useSidebar();
+
return (
<Sidebar collapsible="icon" variant="sidebar" {...props}>
<SidebarHeader>
@@ -56,6 +60,7 @@ export function AppSidebar({
<DemoBadge />
</SidebarHeader>
<SidebarContent>
+ {isMobile && <MobileNavSwitcher />}
<NavMain items={navItems} />
<div className="flex-1" />
<div className="flex flex-col gap-2 p-2">
diff --git a/web/src/components/nav/mobile-nav-switcher.tsx b/web/src/components/nav/mobile-nav-switcher.tsx
new file mode 100644
index 0000000000..f878cdc817
--- /dev/null
+++ b/web/src/components/nav/mobile-nav-switcher.tsx
@@ -0,0 +1,108 @@
+import { ChevronDownIcon } from "lucide-react";
+import { useSession } from "next-auth/react";
+import {
+ DropdownMenu,
+ DropdownMenuTrigger,
+} from "@/src/components/ui/dropdown-menu";
+import {
+ SidebarGroup,
+ SidebarGroupContent,
+ SidebarMenu,
+ SidebarMenuButton,
+ SidebarMenuItem,
+} from "@/src/components/ui/sidebar";
+import { OrganizationDropdownMenu } from "@/src/components/OrganizationDropdownMenu/OrganizationDropdownMenu";
+import { ProjectDropdownMenu } from "@/src/components/ProjectDropdownMenu/ProjectDropdownMenu";
+import {
+ useOrgProjectSwitchPaths,
+ useQueryProjectOrOrganization,
+} from "@/src/features/projects/hooks";
+import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
+
+/**
+ * Org + project switcher for the TOP of the mobile nav drawer.
+ *
+ * The mobile drawer (the `Sheet` branch of `Sidebar`, see ui/sidebar.tsx)
+ * otherwise only lists section links — the same switching affordance the
+ * desktop breadcrumb offers (`BreadcrumbComponent`) is easy to miss on
+ * mobile, since the breadcrumb sits in the page body. This reuses the exact
+ * dropdown menus and path builders the breadcrumb uses, so switching
+ * behavior is identical between both entry points; only rendered by
+ * `AppSidebar` when `useSidebar().isMobile` is true.
+ */
+export function MobileNavSwitcher() {
+ const session = useSession();
+ const { organization, project } = useQueryProjectOrOrganization();
+ const { getProjectPath, getOrgPath } = useOrgProjectSwitchPaths();
+
+ const organizations = session.data?.user?.organizations;
+ const canCreateOrganizations = session.data?.user?.canCreateOrganizations;
+ const canCreateProjects = useHasOrganizationAccess({
+ organizationId: organization?.id,
+ scope: "projects:create",
+ });
+
+ if (!organization) return null;
+
+ return (
+ <SidebarGroup className="border-b">
+ <SidebarGroupContent>
+ <SidebarMenu>
+ <SidebarMenuItem>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <SidebarMenuButton>
+ <span
+ className="min-w-0 flex-1 truncate text-left"
+ title={organization.name}
+ >
+ {organization.name}
+ </span>
+ <ChevronDownIcon className="ml-auto h-4 w-4 shrink-0" />
+ </SidebarMenuButton>
+ </DropdownMenuTrigger>
+ <OrganizationDropdownMenu
+ {...(organizations
+ ? { state: "loaded", organizations }
+ : { state: "loading" })}
+ canCreateOrganizations={!!canCreateOrganizations}
+ getOrgPath={getOrgPath}
+ />
+ </DropdownMenu>
+ </SidebarMenuItem>
+ {project && (
+ <SidebarMenuItem>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <SidebarMenuButton>
+ <span
+ className="min-w-0 flex-1 truncate text-left"
+ title={project.name}
+ >
+ {project.name}
+ </span>
+ <ChevronDownIcon className="ml-auto h-4 w-4 shrink-0" />
+ </SidebarMenuButton>
+ </DropdownMenuTrigger>
+ <ProjectDropdownMenu
+ organizationId={organization.id}
+ {...(organizations
+ ? {
+ state: "loaded",
+ projects:
+ organizations.find(
+ (org) => org.id === organization.id,
+ )?.projects ?? [],
+ }
+ : { state: "loading" })}
+ canCreateProjects={!!canCreateProjects}
+ getProjectPath={getProjectPath}
+ />
+ </DropdownMenu>
+ </SidebarMenuItem>
+ )}
+ </SidebarMenu>
+ </SidebarGroupContent>
+ </SidebarGroup>
+ );
+}
diff --git a/web/src/features/projects/hooks.ts b/web/src/features/projects/hooks.ts
index 9c7ccd9b86..0f70535a0d 100644
--- a/web/src/features/projects/hooks.ts
+++ b/web/src/features/projects/hooks.ts
@@ -67,3 +67,49 @@ export const useQueryProjectOrOrganization = () => {
return p.project ? p : { organization: o, project: null };
};
+
+/**
+ * Builds the target path for switching the current page to a different
+ * organization/project, preserving the page the user is on where possible
+ * (e.g. staying on `.../traces` when switching projects). Shared by every
+ * org/project switcher (breadcrumb, mobile nav drawer) so switching behavior
+ * stays identical across entry points.
+ */
+export const useOrgProjectSwitchPaths = () => {
+ const router = useRouter();
+
+ /**
+ * Truncate the path before the first dynamic segment that is not allowlisted.
+ * e.g. /project/[projectId]/traces/[traceId] -> /project/[projectId]/traces
+ */
+ const truncatePathBeforeDynamicSegments = (path: string) => {
+ const allowlistedIds = ["[projectId]", "[organizationId]", "[page]"];
+ const segments = router.route.split("/");
+ const idSegments = segments.filter(
+ (segment) => segment.startsWith("[") && segment.endsWith("]"),
+ );
+ const stopSegment = idSegments.filter((id) => !allowlistedIds.includes(id));
+ if (stopSegment.length === 0) return path;
+ const stopIndex = segments.indexOf(stopSegment[0]);
+ const truncatedPath = path.split("/").slice(0, stopIndex).join("/");
+ return truncatedPath;
+ };
+
+ const getProjectPath = (projectId: string) =>
+ router.query.projectId
+ ? truncatePathBeforeDynamicSegments(router.asPath).replace(
+ router.query.projectId as string,
+ projectId,
+ )
+ : `/project/${projectId}`;
+
+ const getOrgPath = (orgId: string) =>
+ router.query.organizationId
+ ? truncatePathBeforeDynamicSegments(router.asPath).replace(
+ router.query.organizationId as string,
+ orgId,
+ )
+ : `/organization/${orgId}`;
+
+ return { getProjectPath, getOrgPath };
+};