81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
// AK-P16c public form URL `/f/:tenant/:slug`.
|
|
//
|
|
// This route reuses the sandbox-style embedded renderer but resolves the form
|
|
// via tenant slug + public form slug instead of the opaque embed key.
|
|
|
|
import { FormDefinitionSchema, FormThemeSchema, type FormDefinition, type FormTheme } from "@hivegrid/form-widget";
|
|
import { getSitekey, resolveCaptchaConfig } from "@hivegrid/captcha";
|
|
import { isRouteErrorResponse, useLoaderData, useRouteError } from "react-router";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { PublicFormRenderer } from "~/modules/forms/public/PublicFormRenderer";
|
|
import { PublicFormUnavailable } from "~/modules/forms/public/PublicFormUnavailable";
|
|
import { db } from "~/utils/db.server";
|
|
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
|
import { resolvePublicFormSlugRlsIdentity } from "@hivegrid/db/rls-resolvers.server";
|
|
|
|
type LoaderResult = {
|
|
formKey: string;
|
|
definition: FormDefinition;
|
|
theme: FormTheme;
|
|
captcha: { enabled: boolean; provider: "mcaptcha" | "cap"; instanceUrl: string | undefined; sitekey: string | undefined };
|
|
};
|
|
|
|
export async function loader({ params }: { params: { tenant?: string; slug?: string } }): Promise<Response> {
|
|
const tenantSlug = params.tenant;
|
|
const formSlug = params.slug;
|
|
if (!tenantSlug || !formSlug) throw new Response(null, { status: 404 });
|
|
|
|
const identity = await resolvePublicFormSlugRlsIdentity(tenantSlug, formSlug);
|
|
if (!identity) throw new Response(null, { status: 404 });
|
|
|
|
return runWithRlsContext({ tenantId: identity.tenantId }, async () => {
|
|
const form = await db.form.findFirst({
|
|
where: {
|
|
id: identity.resourceId,
|
|
tenantId: identity.tenantId,
|
|
slug: formSlug,
|
|
isEnabled: true,
|
|
isPublic: true,
|
|
status: "published",
|
|
},
|
|
select: { key: true, definition: true, theme: true },
|
|
});
|
|
if (!form) throw new Response(null, { status: 404 });
|
|
|
|
const def = FormDefinitionSchema.safeParse(form.definition);
|
|
if (!def.success) throw new Response(null, { status: 500 });
|
|
const theme = FormThemeSchema.safeParse(form.theme ?? {});
|
|
const captcha = resolveCaptchaConfig(process.env);
|
|
|
|
return Response.json({
|
|
formKey: form.key,
|
|
definition: def.data,
|
|
theme: theme.success ? theme.data : {},
|
|
captcha: {
|
|
enabled: def.data.captchaEnabled === true,
|
|
provider: captcha.provider,
|
|
instanceUrl: captcha.instanceUrl,
|
|
sitekey: getSitekey(captcha, "forms"),
|
|
},
|
|
} satisfies LoaderResult);
|
|
});
|
|
}
|
|
|
|
export default function PublicSlugForm(): React.JSX.Element {
|
|
return <PublicFormRenderer {...(useLoaderData() as LoaderResult)} layout="standalone" />;
|
|
}
|
|
|
|
/**
|
|
* Renders the friendly V02 "form not available" page for 404s thrown by the
|
|
* loader (unknown tenant/slug, unpublished or disabled form). The document
|
|
* response keeps the thrown 404 status; other errors fall back to the
|
|
* generic error page.
|
|
*/
|
|
export function ErrorBoundary(): React.JSX.Element {
|
|
const error = useRouteError();
|
|
if (isRouteErrorResponse(error) && error.status === 404) {
|
|
return <PublicFormUnavailable variant="standalone" />;
|
|
}
|
|
return <ServerError />;
|
|
}
|