import { LoaderFunctionArgs, MetaFunction, redirect } from "react-router"; import { useParams, Outlet } from "react-router"; import { useState, Fragment, useRef } from "react"; import { useTranslation } from "react-i18next"; import SidebarIconsLayout, { IconDto } from "~/components/ui/layouts/SidebarIconsLayout"; import { getTranslations } from "~/locale/i18next.server"; import { adminPath, appPath } from "~/utils/app/CanonicalUrlUtils"; import UrlUtils from "~/utils/app/UrlUtils"; import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData"; import { getEntityGroupBySlug } from "~/utils/db/entities/entityGroups.db.server"; import { getTenantIdOrNull } from "~/utils/services/.server/urlService"; import { getEntityPermission, getUserHasPermission } from "~/utils/helpers/PermissionsHelper"; import { translateEntityTitlePlural } from "~/utils/i18n/translateLabel"; type LoaderData = { title: string; }; export const loader = async ({ request, params }: LoaderFunctionArgs) => { const { t } = await getTranslations(request); const tenantId = await getTenantIdOrNull({ request, params }); const group = await getEntityGroupBySlug(params.group!); if (!group) { throw redirect(tenantId ? UrlUtils.currentTenantUrl(params, "404") : "/404"); } const data: LoaderData = { title: `${t(group.title)} | ${process.env.APP_NAME}`, }; return data; }; export const meta: MetaFunction = ({ data }) => [{ title: data?.title }]; export default () => { const { t, i18n } = useTranslation(); const appOrAdminData = useAppOrAdminData(); const params = useParams(); const mainElement = useRef(null); const group = appOrAdminData.entityGroups.find((f) => f.slug === params.group); if (!group) { return null; } const visibleGroupEntities = group.entities.filter(({ entity }) => getUserHasPermission(appOrAdminData, getEntityPermission(entity, "view"))); const thereAreIcons = visibleGroupEntities.some(({ entity }) => !!entity.icon); const items: IconDto[] = [ { name: t("shared.summary"), href: params.tenant ? appPath({ tenantSlug: params.tenant, path: `g/${params.group}`, mode: "host-aware" }) : adminPath(`g/${params.group}`, { mode: "host-aware" }), prefetch: "intent", exact: true, icon: !thereAreIcons ? undefined : ( ), iconSelected: !thereAreIcons ? undefined : ( ), }, ]; visibleGroupEntities.forEach(({ entity }) => { items.push({ name: translateEntityTitlePlural(t, i18n, entity), href: params.tenant ? appPath({ tenantSlug: params.tenant, path: `g/${params.group}/${entity.slug}`, mode: "host-aware" }) : adminPath(`g/${params.group}/${entity.slug}`, { mode: "host-aware" }), prefetch: "intent", textIcon: entity.icon, textIconSelected: entity.icon, }); }); return ( {!params.id ? ( ) : (
)}
); };