import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router"; import { resolvePortalRouteDispatch, type PortalRouteTarget } from "~/services/customer-portal/portal-route-dispatch"; const PORTAL_CUSTOM_HOST_HEADER = "x-hivegrid-portal-custom-host"; export async function dispatchPortalLoader(args: LoaderFunctionArgs) { const resolved = requireDispatch(args.request); const delegated = { ...args, params: resolved.params } as LoaderFunctionArgs; switch (resolved.target) { case "auth": return (await import("./auth")).loader(delegated); case "dashboard": return (await import("./dashboard")).loader(delegated); case "document-download": return (await import("./document-download")).loader(delegated); case "export-download": return (await import("./export-download")).loader(delegated); case "logout": return (await import("./logout")).loader(delegated); case "project-complaint": return (await import("./project-complaint")).loader(delegated); case "project-detail": return (await import("./project-detail")).loader(delegated); case "project-upload": return (await import("./project-upload")).loader(delegated); case "projects": return (await import("./projects")).loader(delegated); case "settings": return (await import("./settings")).loader(delegated); case "upload": return (await import("./upload")).loader(delegated); } } export async function dispatchPortalAction(args: ActionFunctionArgs) { const resolved = requireDispatch(args.request); const delegated = { ...args, params: resolved.params } as ActionFunctionArgs; switch (resolved.target) { case "auth": return (await import("./auth")).action(delegated); case "logout": return (await import("./logout")).action(delegated); case "project-complaint": return (await import("./project-complaint")).action(delegated); case "project-upload": return (await import("./project-upload")).action(delegated); case "settings": return (await import("./settings")).action(delegated); default: throw new Response(null, { status: 404, headers: { "Cache-Control": "no-store" } }); } } function requireDispatch(request: Request): { readonly target: PortalRouteTarget; readonly params: Readonly> } { const customHost = Boolean(request.headers.get(PORTAL_CUSTOM_HOST_HEADER)?.trim()); const resolved = resolvePortalRouteDispatch(new URL(request.url).pathname, customHost); if (!resolved) throw new Response(null, { status: 404, headers: { "Cache-Control": "no-store" } }); return resolved; }