Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/routes/admin/navigation.tsx
T

202 lines
9.5 KiB
TypeScript

import { useMemo, useState } from "react";
import { useTranslation } from "react-i18next";
import { CheckIcon, XIcon } from "~/components/ui/icons/vendor";
import { SideBarItem } from "~/application/sidebar/SidebarItem";
import { TenantUserType } from "~/application/enums/tenants/TenantUserType";
import { AdminSidebar } from "~/application/sidebar/AdminSidebar";
import { AppSidebar } from "~/application/sidebar/AppSidebar";
import SidebarIcon from "~/components/layouts/icons/SidebarIcon";
import { LoaderFunctionArgs, MetaFunction } from "react-router";
import { useParams } from "react-router";
import { getTranslations } from "~/locale/i18next.server";
import { useAdminData } from "~/utils/data/useAdminData";
import InputSearch from "~/components/ui/input/InputSearch";
import { useRootData } from "~/utils/data/useRootData";
export const loader = async ({ request }: LoaderFunctionArgs) => {
const { t } = await getTranslations(request);
return {
title: `${t("admin.navigation.title")} | ${process.env.APP_NAME}`,
};
};
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
export function flattenNavigationItems(items: SideBarItem[], inheritedAdminOnly = false): SideBarItem[] {
return items.flatMap((item) => {
if (item.hidden) {
return [];
}
const adminOnly = inheritedAdminOnly || item.adminOnly === true;
const children = flattenNavigationItems(item.items ?? [], adminOnly);
const isToggleOnlyParent = item.isCollapsible === true && (item.items?.length ?? 0) > 0;
const ownItem = item.path && !isToggleOnlyParent ? [{ ...item, adminOnly }] : [];
return [...ownItem, ...children];
});
}
export default function AdminNavigationRoute() {
const { t, i18n } = useTranslation();
const params = useParams();
const adminData = useAdminData();
const rootData = useRootData();
const [searchInput, setSearchInput] = useState("");
const items = useMemo(() => {
const adminItems = flattenNavigationItems(
AdminSidebar({ t, appConfiguration: rootData.appConfiguration, routing: rootData.routing }),
true
);
if (params.tenant) {
return [
...adminItems,
...flattenNavigationItems(
AppSidebar({
t,
i18n,
tenantId: params.tenant,
entities: adminData.entities,
entityGroups: adminData.entityGroups,
appConfiguration: rootData.appConfiguration,
routing: rootData.routing,
})
),
];
}
return adminItems;
}, [adminData.entities, adminData.entityGroups, i18n, params.tenant, rootData.appConfiguration, rootData.routing, t]);
const visibleItems = useMemo(() => {
const normalizedSearch = searchInput.trim().toLowerCase();
if (!normalizedSearch) {
return items;
}
return items.filter((item) => `${item.title} ${item.path}`.toLowerCase().includes(normalizedSearch));
}, [items, searchInput]);
const tenantUserTypes = useMemo(() => {
const roleKeys = Object.keys(TenantUserType).filter((key) => !Number.isNaN(Number(key)));
return roleKeys.map((f) => Number(f));
}, []);
function tenantUserTypeName(type: TenantUserType) {
return t("settings.profile.types." + TenantUserType[type]);
}
function tenantUserTypeHasAccess(item: SideBarItem, role: TenantUserType): boolean {
return !item.adminOnly && allowTenantUserType(item, role);
}
function allowTenantUserType(item: SideBarItem, role: TenantUserType) {
return !item.tenantUserTypes || item.tenantUserTypes.includes(role);
}
return (
<div>
<div className="border-border bg-background w-full border-b py-2 shadow-2xs">
<div className="mx-auto flex max-w-5xl items-center justify-between space-x-2 px-4 sm:px-6 lg:px-8 xl:max-w-7xl 2xl:max-w-(--breakpoint-2xl)">
<h1 className="flex flex-1 items-center truncate font-bold">{t("admin.navigation.title")}</h1>
</div>
</div>
<div className="mx-auto max-w-5xl space-y-2 px-4 pt-2 sm:px-6 lg:px-8 xl:max-w-7xl 2xl:max-w-(--breakpoint-2xl)">
<div className="space-y-2">
<InputSearch value={searchInput} setValue={setSearchInput} />
<div>
<div>
<div className="flex flex-col">
<div className="overflow-x-auto">
<div className="inline-block min-w-full py-2 align-middle">
<div className="border-border overflow-hidden border shadow-xs sm:rounded-lg">
<table className="divide-border min-w-full divide-y">
<thead className="bg-secondary">
<tr>
<th
scope="col"
className="border-border text-muted-foreground select-none truncate border px-4 py-2 text-left text-xs font-medium tracking-wider"
>
{t("admin.navigation.icon")}
</th>
<th
scope="col"
className="border-border text-muted-foreground select-none truncate border px-4 py-2 text-left text-xs font-medium tracking-wider"
>
{t("admin.navigation.menu")}
</th>
<th
scope="col"
className="border-border text-muted-foreground select-none truncate border px-4 py-2 text-left text-xs font-medium tracking-wider"
>
{t("admin.navigation.url")}
</th>
<th className="border-border text-muted-foreground select-none truncate border px-4 py-2 text-left text-xs font-bold tracking-wider">
{t("admin.navigation.sysadmin")}
</th>
{tenantUserTypes.map((role, idx) => {
return (
<th
key={idx}
scope="col"
className="border-border text-muted-foreground select-none truncate border px-4 py-2 text-left text-xs font-bold tracking-wider"
>
<div className="text-muted-foreground flex items-center justify-center space-x-1">{tenantUserTypeName(role)}</div>
</th>
);
})}
</tr>
</thead>
<tbody className="divide-border bg-background divide-y">
{visibleItems.map((item, idx) => {
return (
<tr key={item.id ?? `${item.path}-${idx}`}>
<td className="border-border w-10 whitespace-nowrap border-b border-l border-t px-4 py-2 text-sm">
{item.icon && <SidebarIcon className="mx-auto h-5 w-5 text-slate-700" item={item} />}
</td>
<td className="border-border whitespace-nowrap border-b border-l border-t px-4 py-2 text-sm">{item.title}</td>
<td className="border-border whitespace-nowrap border-b border-l border-t px-4 py-2 text-sm">
<a target="_blank" rel="noreferrer" href={item.path} className="text-blue-500 underline hover:text-blue-700">
{item.path}
</a>
</td>
<td className="border-border text-muted-foreground whitespace-nowrap border px-4 text-center text-sm">
<div className="flex justify-center">
<CheckIcon className="h-4 w-4 text-teal-400" />
</div>
</td>
{tenantUserTypes.map((role) => {
return (
<td className="border-border text-muted-foreground whitespace-nowrap border px-4 text-center text-sm" key={role}>
<div className="flex justify-center">
{(() => {
if (tenantUserTypeHasAccess(item, role)) {
return (
<CheckIcon className="h-4 w-4 text-teal-400" />
);
} else {
return (
<XIcon className="h-4 w-4 text-red-400" />
);
}
})()}
</div>
</td>
);
})}
</tr>
);
})}
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
);
}