Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+59
-29
@@ -9,7 +9,10 @@ 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;
|
||||
@@ -23,39 +26,66 @@ export async function loader({ params }: { params: { formKey?: string } }): Prom
|
||||
if (!formKey) {
|
||||
throw new Response(null, { status: 404 });
|
||||
}
|
||||
const form = await db.form.findUnique({
|
||||
where: { key: formKey },
|
||||
select: { key: true, isEnabled: true, definition: true, theme: true },
|
||||
});
|
||||
if (!form || !form.isEnabled) {
|
||||
const identity = await resolvePublicFormKeyRlsIdentity(formKey);
|
||||
if (!identity) {
|
||||
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 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 }
|
||||
);
|
||||
});
|
||||
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)} />;
|
||||
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" />;
|
||||
}
|
||||
Reference in new issue
Block a user