Files

69 lines
2.9 KiB
TypeScript

import { buildPortalDashboardVM } from "@hivegrid/view-models";
import { useFetcher, useLoaderData, useNavigate, useRouteLoaderData, type LoaderFunctionArgs } from "react-router";
import { CustomerPortalDashboardPage } from "~/components/screens/customer-portal/CustomerPortalInterimPages";
import { loadPortalProjectDataset, requirePortalSessionContext } from "~/services/customer-portal/portal-data.server";
import type { PortalRootData } from "~/services/customer-portal/portal-root";
import { portalPublicPath } from "~/services/customer-portal/portal-origin.server";
export async function loader({ request, params }: LoaderFunctionArgs) {
const session = await requirePortalSessionContext(request, params.tenant);
const { identity } = session;
const baseHref = portalPublicPath(session.origin);
const dataset = await loadPortalProjectDataset(identity, undefined, baseHref);
const paths = {
dashboard: baseHref,
projects: portalPublicPath(session.origin, "projects"),
upload: portalPublicPath(session.origin, "upload"),
settings: portalPublicPath(session.origin, "settings"),
logout: portalPublicPath(session.origin, "logout"),
};
const data = {
vm: buildPortalDashboardVM({
portalName: identity.portalTitle,
viewerLabel: viewerLabel(identity),
frozen: identity.tenantFrozen,
projects: dataset.projects,
projectsHref: paths.projects,
logoutAction: paths.logout,
buildProjectHref: (projectId) => portalPublicPath(session.origin, `projects/${projectId}`),
locale: identity.viewerLocale,
}),
brandingLogoHref: portalPublicPath(session.origin, "branding/logo"),
paths,
};
const responseHeaders = new Headers(session.responseHeaders);
responseHeaders.set("Cache-Control", "private, no-store");
return Response.json(data, { headers: responseHeaders });
}
export default function CustomerPortalDashboardRoute() {
const { vm, brandingLogoHref, paths } = useLoaderData<typeof loader>();
const root = useRouteLoaderData<PortalRootData>("root");
const navigate = useNavigate();
const logout = useFetcher();
return (
<CustomerPortalDashboardPage
vm={vm}
brandingLogoHref={brandingLogoHref}
actions={{
onDashboard: () => navigate(paths.dashboard),
onProjects: () => navigate(paths.projects),
onUpload: () => navigate(paths.upload),
onSettings: () => navigate(paths.settings),
onOpenProject: (href) => navigate(href),
onDownloadDocument: (href) => window.location.assign(href),
onLogout: () => {
const form = new FormData();
if (root?.csrf) form.set("csrf", root.csrf);
void logout.submit(form, { method: "post", action: paths.logout });
},
}}
/>
);
}
function viewerLabel(identity: { viewerFirstName: string; viewerLastName: string; portalTitle: string }): string {
return `${identity.viewerFirstName} ${identity.viewerLastName}`.trim() || identity.portalTitle;
}