344 lines
11 KiB
TypeScript
344 lines
11 KiB
TypeScript
// // "use client";
|
|
|
|
import * as React from "react";
|
|
import { NavMain } from "./nav-main";
|
|
import { NavQuickLinks } from "./nav-quick-links";
|
|
import { NavSecondary } from "./nav-secondary";
|
|
import { NavUser } from "./nav-user";
|
|
import { Sidebar, SidebarContent, SidebarFooter, SidebarHeader, SidebarMenu, SidebarMenuButton, SidebarMenuItem } from "~/components/ui/sidebar";
|
|
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
|
|
import { AppSidebar } from "~/application/sidebar/AppSidebar";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useRootData } from "~/utils/data/useRootData";
|
|
import { SideBarItem } from "~/application/sidebar/SidebarItem";
|
|
import { AdminSidebar } from "~/application/sidebar/AdminSidebar";
|
|
import { useLocation, useParams } from "react-router";
|
|
import { DocsSidebar } from "~/application/sidebar/DocsSidebar";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import { SidebarGroupDto } from "~/application/sidebar/SidebarGroupDto";
|
|
import { TeamSwitcher } from "./team-switcher";
|
|
import { DARK_SIDEBAR_IN_LIGHT_MODE } from "~/application/Constants";
|
|
import { cn } from "~/lib/utils";
|
|
import { ThemeConfig } from "~/utils/theme/ThemeConfig";
|
|
import { getSidebarSurfaceId } from "~/application/ui/uiRegistry";
|
|
import { getUserHasPermission } from "~/utils/helpers/PermissionsHelper";
|
|
import { appPath } from "~/utils/app/CanonicalUrlUtils";
|
|
import { TenantUserType } from "~/application/enums/tenants/TenantUserType";
|
|
|
|
export function ShadcnAppSidebar({
|
|
themeConfig,
|
|
...props
|
|
}: React.ComponentProps<typeof Sidebar> & {
|
|
layout: "app" | "admin" | "docs";
|
|
items?: SideBarItem[];
|
|
themeConfig?: ThemeConfig | null;
|
|
}) {
|
|
const { t, i18n } = useTranslation();
|
|
const location = useLocation();
|
|
const rootData = useRootData();
|
|
const appOrAdminData = useAppOrAdminData();
|
|
const appData = useAppOrAdminData();
|
|
const params = useParams();
|
|
|
|
// const data = {
|
|
// // user: {
|
|
// // name: "shadcn",
|
|
// // email: "m@example.com",
|
|
// // avatar: "/avatars/shadcn.jpg",
|
|
// // },
|
|
// navMain: [
|
|
// {
|
|
// title: "Playground",
|
|
// url: "#",
|
|
// icon: SquareTerminal,
|
|
// isActive: true,
|
|
// items: [
|
|
// {
|
|
// title: "History",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Starred",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Settings",
|
|
// url: "#",
|
|
// },
|
|
// ],
|
|
// },
|
|
// {
|
|
// title: "Models",
|
|
// url: "#",
|
|
// icon: Bot,
|
|
// items: [
|
|
// {
|
|
// title: "Genesis",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Explorer",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Quantum",
|
|
// url: "#",
|
|
// },
|
|
// ],
|
|
// },
|
|
// {
|
|
// title: "Documentation",
|
|
// url: "#",
|
|
// icon: BookOpen,
|
|
// items: [
|
|
// {
|
|
// title: "Introduction",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Get Started",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Tutorials",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Changelog",
|
|
// url: "#",
|
|
// },
|
|
// ],
|
|
// },
|
|
// {
|
|
// title: "Settings",
|
|
// url: "#",
|
|
// icon: Settings2,
|
|
// items: [
|
|
// {
|
|
// title: "General",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Team",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Billing",
|
|
// url: "#",
|
|
// },
|
|
// {
|
|
// title: "Limits",
|
|
// url: "#",
|
|
// },
|
|
// ],
|
|
// },
|
|
// ],
|
|
// navSecondary: [
|
|
// {
|
|
// title: "Support",
|
|
// url: "#",
|
|
// icon: LifeBuoy,
|
|
// },
|
|
// {
|
|
// title: "Feedback",
|
|
// url: "#",
|
|
// icon: Send,
|
|
// },
|
|
// ],
|
|
// projects: [
|
|
// {
|
|
// name: "Quick link 1",
|
|
// url: "#",
|
|
// icon: Frame,
|
|
// },
|
|
// {
|
|
// name: "Quick link 2",
|
|
// url: "#",
|
|
// icon: PieChart,
|
|
// },
|
|
// {
|
|
// name: "Quick link 3",
|
|
// url: "#",
|
|
// icon: Map,
|
|
// },
|
|
// ],
|
|
// };
|
|
|
|
const getMenuItems = () => {
|
|
let menu: SideBarItem[] = [];
|
|
if (props.items) {
|
|
menu = props.items;
|
|
} else if (props.layout === "admin") {
|
|
menu = AdminSidebar({ t, appConfiguration: rootData.appConfiguration, routing: rootData.routing });
|
|
} else if (props.layout === "app") {
|
|
menu = AppSidebar({
|
|
t,
|
|
i18n,
|
|
tenantId: params.tenant ?? "",
|
|
entities: appOrAdminData?.entities ?? [],
|
|
entityGroups: appOrAdminData?.entityGroups ?? [],
|
|
appConfiguration: rootData.appConfiguration,
|
|
routing: rootData.routing,
|
|
productAccess: appOrAdminData?.productAccess,
|
|
});
|
|
} else if (props.layout === "docs") {
|
|
menu = DocsSidebar();
|
|
}
|
|
|
|
return menu.map((item) => prepareMenuItem(item));
|
|
};
|
|
|
|
function getPath(item: SideBarItem): string {
|
|
return UrlUtils.replaceVariables(params, item.path) ?? "";
|
|
}
|
|
|
|
function normalizePath(path: string): string {
|
|
return path.length > 1 ? path.replace(/\/+$/, "") : path;
|
|
}
|
|
|
|
function pathMatchesCurrent(path: string, exact?: boolean): boolean {
|
|
if (!path) {
|
|
return false;
|
|
}
|
|
const currentPath = normalizePath(location.pathname);
|
|
const itemPath = normalizePath(path);
|
|
if (exact) {
|
|
return currentPath === itemPath;
|
|
}
|
|
return currentPath === itemPath || currentPath.startsWith(`${itemPath}/`);
|
|
}
|
|
|
|
function isCurrent(menuItem: SideBarItem): boolean {
|
|
return pathMatchesCurrent(getPath(menuItem), menuItem.exact);
|
|
}
|
|
|
|
function currentIsChild(menuItem: SideBarItem): boolean {
|
|
return menuItem.items?.some((item) => isCurrent(item) || currentIsChild(item)) ?? false;
|
|
}
|
|
|
|
function prepareMenuItem(item: SideBarItem): SideBarItem {
|
|
const childItems = item.isCollapsible === false ? [] : item.items?.map((child) => prepareMenuItem(child));
|
|
const preparedItem: SideBarItem = {
|
|
...item,
|
|
path: UrlUtils.replaceVariables(params, item.path) ?? item.path,
|
|
redirectTo: item.redirectTo ? (UrlUtils.replaceVariables(params, item.redirectTo) ?? item.redirectTo) : item.redirectTo,
|
|
items: childItems,
|
|
};
|
|
const isActive = isCurrent(preparedItem);
|
|
const hasActiveChild = currentIsChild(preparedItem);
|
|
return {
|
|
...preparedItem,
|
|
isActive: preparedItem.isActive || isActive || hasActiveChild,
|
|
};
|
|
}
|
|
function allowCurrentUserType(item: SideBarItem) {
|
|
if (!item.adminOnly) {
|
|
return true;
|
|
}
|
|
return Boolean(appData?.user?.admin);
|
|
}
|
|
function allowCurrentTenantUserType(item: SideBarItem) {
|
|
const currentRole = "currentRole" in appData ? (appData.currentRole as TenantUserType) : null;
|
|
return !item.tenantUserTypes || (currentRole !== null && item.tenantUserTypes.includes(currentRole));
|
|
}
|
|
function checkUserRolePermissions(item: SideBarItem) {
|
|
return !item.permission || getUserHasPermission(appOrAdminData, item.permission);
|
|
}
|
|
function getVisibleItem(item: SideBarItem): SideBarItem | null {
|
|
if (item.hidden === true || !allowCurrentUserType(item) || !allowCurrentTenantUserType(item)) {
|
|
return null;
|
|
}
|
|
|
|
const items = item.items?.map((child) => getVisibleItem(child)).filter((child): child is SideBarItem => child !== null) ?? [];
|
|
const hasPermission = checkUserRolePermissions(item);
|
|
if (!hasPermission && items.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
...item,
|
|
path: hasPermission ? item.path : "",
|
|
redirectTo: hasPermission ? item.redirectTo : undefined,
|
|
items,
|
|
};
|
|
}
|
|
const getMenu = (): SidebarGroupDto[] => {
|
|
const _menu: SidebarGroupDto[] = [];
|
|
getMenuItems()
|
|
.forEach((f) => {
|
|
const items = f.items?.map((item) => getVisibleItem(item)).filter((item): item is SideBarItem => item !== null) ?? [];
|
|
if (f.hidden === true || items.length === 0) {
|
|
return;
|
|
}
|
|
let type: "main" | "secondary" | "quick-link" = "main";
|
|
if (f.isSecondary) {
|
|
type = "secondary";
|
|
} else if (f.isQuickLink) {
|
|
type = "quick-link";
|
|
}
|
|
_menu.push({
|
|
id: f.id,
|
|
uiIdPattern: f.uiIdPattern,
|
|
uiIdParams: f.uiIdParams,
|
|
title: f.title.toString(),
|
|
items,
|
|
type,
|
|
});
|
|
});
|
|
return _menu.filter((f) => f.items.length > 0);
|
|
};
|
|
|
|
const navMain = getMenu().filter((f) => f.type === "main");
|
|
const navSecondary = getMenu().find((f) => f.type === "secondary") || { items: [] };
|
|
const navQuickLinks = getMenu().find((f) => f.type === "quick-link");
|
|
|
|
// Use theme config to determine sidebar variant and dark mode
|
|
const sidebarVariant = themeConfig?.sidebarVariant || (DARK_SIDEBAR_IN_LIGHT_MODE(props) ? "sidebar" : "inset");
|
|
const forceDarkMode = themeConfig?.sidebarDarkMode ?? DARK_SIDEBAR_IN_LIGHT_MODE(props);
|
|
const sidebarSurfaceId = getSidebarSurfaceId(props.layout);
|
|
|
|
return (
|
|
<Sidebar
|
|
variant={sidebarVariant}
|
|
collapsible="offcanvas"
|
|
{...props}
|
|
data-ui-id={sidebarSurfaceId}
|
|
data-ui-layout={props.layout}
|
|
className={cn(forceDarkMode && "dark")} // force dark mode
|
|
>
|
|
<SidebarHeader data-ui-id={`${sidebarSurfaceId}.header`}>
|
|
<SidebarMenu className="select-none">
|
|
<SidebarMenuItem>
|
|
<SidebarMenuButton
|
|
size="lg"
|
|
asChild
|
|
className="select-none"
|
|
// className="dark:hover:bg-transparent dark:active:bg-transparent"
|
|
>
|
|
{props.layout === "app" && appData.currentTenant?.logo ? (
|
|
<a href={appPath({ tenantSlug: appData.currentTenant.slug, path: "dashboard", mode: "host-aware" })} className="flex justify-center">
|
|
<div className="flex shrink-0 items-center justify-center border-b border-transparent">
|
|
<img className="mx-auto h-10 w-auto p-1.5" src={appData.currentTenant.logo} alt="Logo" />
|
|
</div>
|
|
</a>
|
|
) : null}
|
|
</SidebarMenuButton>
|
|
</SidebarMenuItem>
|
|
</SidebarMenu>
|
|
|
|
{props.layout === "app" && appData?.currentTenant && <TeamSwitcher key={params.tenant} size="sm" tenants={appData?.myTenants ?? []} />}
|
|
</SidebarHeader>
|
|
<SidebarContent className="pt-2" data-ui-id={`${sidebarSurfaceId}.content`}>
|
|
<NavMain items={navMain} />
|
|
{navQuickLinks && <NavQuickLinks item={navQuickLinks} />}
|
|
<NavSecondary item={navSecondary} className="mt-auto" />
|
|
</SidebarContent>
|
|
{appOrAdminData.user && (
|
|
<SidebarFooter data-ui-id={`${sidebarSurfaceId}.footer`}>
|
|
<NavUser layout={props.layout} user={appOrAdminData.user} />
|
|
</SidebarFooter>
|
|
)}
|
|
</Sidebar>
|
|
);
|
|
}
|