114 lines
4.7 KiB
TypeScript
114 lines
4.7 KiB
TypeScript
import { LoaderFunctionArgs, redirect, MetaFunction, useLoaderData } from "react-router";
|
|
import { useSearchParams } from "react-router";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { getAppDashboardStats } from "~/utils/services/appDashboardService";
|
|
import { DashboardStats } from "~/components/ui/stats/DashboardStats";
|
|
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
|
import { Stat } from "~/application/dtos/stats/Stat";
|
|
import InputSelector from "~/components/ui/input/InputSelector";
|
|
import PeriodHelper, { defaultPeriodFilter, PeriodFilters } from "~/utils/helpers/PeriodHelper";
|
|
import { useTranslation } from "react-i18next";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { serverTimingHeaders } from "~/modules/metrics/utils/defaultHeaders";
|
|
import { getTenant } from "~/utils/db/tenants.db.server";
|
|
import { EntityGroupWithDetails, getEntityGroupBySlug } from "~/utils/db/entities/entityGroups.db.server";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import EditPageLayout from "~/components/ui/layouts/EditPageLayout";
|
|
import { requireAuth } from "~/utils/loaders.middleware";
|
|
import { adminPath, appPath } from "~/utils/app/CanonicalUrlUtils";
|
|
import { getPermissionsByUser, getUserRoleInAdmin } from "~/utils/db/permissions/userRoles.db.server";
|
|
import { getEntityPermission } from "~/utils/helpers/PermissionsHelper";
|
|
import { getUserInfo } from "~/utils/session.server";
|
|
import { DefaultAdminRoles } from "~/application/dtos/shared/DefaultAdminRoles";
|
|
import { translateLabel } from "~/utils/i18n/translateLabel";
|
|
export { serverTimingHeaders as headers };
|
|
|
|
type LoaderData = {
|
|
title: string;
|
|
group: EntityGroupWithDetails;
|
|
stats: Stat[];
|
|
};
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
await requireAuth({ request, params });
|
|
const { t } = await getTranslations(request);
|
|
const tenantId = await getTenantIdFromUrl(params);
|
|
const userInfo = await getUserInfo(request);
|
|
const group = await getEntityGroupBySlug(params.group!);
|
|
const tenant = await getTenant(tenantId);
|
|
if (!group) {
|
|
throw redirect(tenantId ? UrlUtils.currentTenantUrl(params, "404") : "/404");
|
|
}
|
|
const isSuperAdmin = Boolean(await getUserRoleInAdmin(userInfo.userId, DefaultAdminRoles.SuperAdmin));
|
|
const permissions = isSuperAdmin ? null : (await getPermissionsByUser(userInfo.userId, tenantId)).map((permission) => permission as string);
|
|
const visibleEntities = group.entities.flatMap((e) => e.entity).filter((entity) => isSuperAdmin || permissions?.includes(getEntityPermission(entity, "view")));
|
|
const stats = await getAppDashboardStats({
|
|
t,
|
|
tenant,
|
|
gte: PeriodHelper.getGreaterThanOrEqualsFromRequest({ request }),
|
|
entities: visibleEntities,
|
|
});
|
|
stats
|
|
.filter((f) => f.entity)
|
|
.forEach((stat) => {
|
|
const tenantSlug = tenant?.slug ?? params.tenant ?? tenantId;
|
|
stat.path = tenantId
|
|
? appPath({ tenantSlug, path: `g/${params.group}/${stat.entity?.slug}`, mode: "host-aware" })
|
|
: adminPath(`g/${params.group}/${stat.entity?.slug}`, { mode: "host-aware" });
|
|
});
|
|
|
|
const data: LoaderData = {
|
|
title: `${t(group.title)} | ${process.env.APP_NAME}`,
|
|
group,
|
|
stats,
|
|
};
|
|
return data;
|
|
};
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export default function GroupIndexRoute() {
|
|
const { t, i18n } = useTranslation();
|
|
const data = useLoaderData<LoaderData>();
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
return (
|
|
<EditPageLayout>
|
|
{data.stats.length > 0 && (
|
|
<div className="space-y-3">
|
|
<div className="flex items-center justify-between space-x-2">
|
|
<h3 className="text-foreground grow font-medium leading-4">{translateLabel(t, i18n, data.group.title)}</h3>
|
|
<div>
|
|
<InputSelector
|
|
className="w-44"
|
|
withSearch={false}
|
|
name="period"
|
|
value={searchParams.get("period")?.toString() ?? defaultPeriodFilter}
|
|
options={PeriodFilters.map((f) => {
|
|
return {
|
|
value: f.value,
|
|
name: t(f.name),
|
|
};
|
|
})}
|
|
setValue={(value) => {
|
|
if (value) {
|
|
searchParams.set("period", value?.toString() ?? "");
|
|
} else {
|
|
searchParams.delete("period");
|
|
}
|
|
setSearchParams(searchParams);
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<DashboardStats items={data.stats} />
|
|
</div>
|
|
)}
|
|
</EditPageLayout>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|