92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
/**
|
|
* Embedded form renderer route.
|
|
*
|
|
* The route resolves a public form by opaque key; rendering lives in
|
|
* `PublicFormRenderer` so the iframe embed route and the public slug route
|
|
* share one implementation.
|
|
*/
|
|
import { useLoaderData } from "react-router";
|
|
import { FormDefinitionSchema, FormThemeSchema, type FormDefinition, type FormTheme } from "@hivegrid/form-widget";
|
|
import { getSitekey, resolveCaptchaConfig } from "@hivegrid/captcha";
|
|
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 { resolvePublicFormKeyRlsIdentity } 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: { formKey?: string } }): Promise<Response> {
|
|
const formKey = params.formKey;
|
|
if (!formKey) {
|
|
throw new Response(null, { status: 404 });
|
|
}
|
|
const identity = await resolvePublicFormKeyRlsIdentity(formKey);
|
|
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, key: formKey, isEnabled: true },
|
|
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);
|
|
const headers = new Headers({
|
|
"x-frame-options": "ALLOWALL",
|
|
"cache-control": "private, max-age=60",
|
|
});
|
|
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,
|
|
{ headers }
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Route-level headers so the error document (404 "not available") stays
|
|
* embeddable in third-party iframes — the loader's Response headers only
|
|
* apply to the success path.
|
|
*/
|
|
export function headers({ loaderHeaders }: { loaderHeaders: Headers }): HeadersInit {
|
|
return {
|
|
"x-frame-options": "ALLOWALL",
|
|
"cache-control": loaderHeaders.get("cache-control") ?? "private, max-age=0",
|
|
};
|
|
}
|
|
|
|
export default function EmbeddedForm(): React.JSX.Element {
|
|
return <PublicFormRenderer {...(useLoaderData() as LoaderResult)} layout="embed" />;
|
|
}
|
|
|
|
/**
|
|
* Compact V02-style "form not available" message for the iframe context.
|
|
* Keeps the thrown status (404 for unknown/disabled forms) on the document
|
|
* response while rendering a friendly message instead of a bare error.
|
|
*/
|
|
export function ErrorBoundary(): React.JSX.Element {
|
|
return <PublicFormUnavailable variant="embed" />;
|
|
}
|