Files
2026-07-08 16:15:37 +02:00

60 lines
2.2 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 { useLoaderData } from "react-router";
import { PublicFormRenderer } from "~/modules/forms/public/PublicFormRenderer";
import { db } from "~/utils/db.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 tenant = await db.tenant.findUnique({ where: { slug: tenantSlug }, select: { id: true } });
if (!tenant) throw new Response(null, { status: 404 });
const form = await db.form.findFirst({
where: {
tenantId: tenant.id,
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)} />;
}