43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { useEffect } from "react";
|
|
import { LoaderFunctionArgs, MetaFunction, redirect } from "react-router";
|
|
import { Outlet, useLocation, useNavigate } from "react-router";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import { adminPath } from "~/utils/app/CanonicalUrlUtils";
|
|
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
|
import { t as i18nT } from "i18next";
|
|
|
|
export function isAdminMetricsRootPath(pathname: string) {
|
|
return UrlUtils.stripTrailingSlash(pathname) === adminPath("metrics", { mode: "host-aware" });
|
|
}
|
|
|
|
export function adminMetricsRedirectTarget() {
|
|
return adminPath("metrics/summary", { mode: "host-aware" });
|
|
}
|
|
|
|
export const loader = async ({ request }: LoaderFunctionArgs) => {
|
|
await verifyUserHasPermission(request, "admin.metrics.view");
|
|
if (isAdminMetricsRootPath(new URL(request.url).pathname)) {
|
|
throw redirect(adminMetricsRedirectTarget());
|
|
}
|
|
|
|
return {
|
|
title: i18nT("metrics.metaTitle", { appName: process.env.APP_NAME }),
|
|
};
|
|
};
|
|
|
|
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
|
|
|
|
export default function AdminApiRoute() {
|
|
const location = useLocation();
|
|
const navigate = useNavigate();
|
|
|
|
useEffect(() => {
|
|
if (isAdminMetricsRootPath(location.pathname)) {
|
|
navigate(adminMetricsRedirectTarget());
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [location.pathname]);
|
|
|
|
return <Outlet />;
|
|
}
|