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