// R-P7 (P7 Lead-Inbox) Interim-UI (AD-28) — lead anti-spam settings (D-P7 // "Anti-Spam-Settings", archetype A6), composed from ~/ui. Pure: renders the // server-built LeadAntiSpamVM and calls actions.onSave with the edited config; the // route POSTs it. No useLoaderData. Final Subframe optics = tracked design-nachzug. // // The settings are grouped the way the scoring pipeline actually works, because a // flat list of seven fields gave no clue which knob does what: signals raise a // lead's spam score (detection + limits), the mode decides what happens once the // score is high, and the retention field is not spam detection at all. import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherShieldCheck } from "@subframe/core"; import type { LeadAntiSpamData, LeadAntiSpamVM } from "@hivegrid/view-models"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Card } from "~/ui/components/Card"; import { Checkbox } from "~/ui/components/Checkbox"; import { Select } from "~/ui/components/Select"; import { TextInput } from "~/ui/components/TextInput"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell"; /** Config fields for the anti-spam save action. Numbers arrive as strings; * booleans as "on" (checked) so the route's `=== "on"` check matches. */ export type LeadAntiSpamSaveInput = { mode: string; perSourcePerHourLimit: string; perIpPerHourLimit: string; anomalySpikeFactor: string; lostAutoDeleteDays: string; disposableDomainEnabled?: string; honeypotEnabled?: string; fastSubmitEnabled?: string; }; export type LeadAntiSpamAdminActions = { onSave: (input: LeadAntiSpamSaveInput) => void }; export type LeadAntiSpamAdminPageProps = { vm: LeadAntiSpamVM; submitting: boolean; errorKey?: string | null; actions: LeadAntiSpamAdminActions; }; const MODES = ["quarantine", "spam"] as const; const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary"; const mutedText = "text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary"; // Number inputs share the card width evenly instead of sizing themselves by // content — same rule as the lead-source and custom-field admin forms. const fieldWidth = "w-full min-w-0 sm:w-auto sm:shrink sm:grow sm:basis-0"; // The Subframe checkbox is a `justify-between` button, so in a stretch container // the label sits flush right and jumps left on hover (the label wrapper only gets // its `grow/basis-0` in the hover state). Pinning it to the start kills the jump. const checkboxRow = "w-full justify-start"; // Server-side clamps in leadService.updateLeadAntiSpamConfig. Mirrored here so the // browser rejects the value instead of the server silently storing a different one. const NUMBER_MIN = { perSourcePerHourLimit: 1, perIpPerHourLimit: 1, anomalySpikeFactor: 2, lostAutoDeleteDays: 0 } as const; type NumberField = keyof typeof NUMBER_MIN; function formFromVm(data: LeadAntiSpamData) { return { mode: data.mode, perSourcePerHourLimit: String(data.perSourcePerHourLimit), perIpPerHourLimit: String(data.perIpPerHourLimit), anomalySpikeFactor: String(data.anomalySpikeFactor), lostAutoDeleteDays: String(data.lostAutoDeleteDays), disposableDomainEnabled: data.disposableDomainEnabled, honeypotEnabled: data.honeypotEnabled, fastSubmitEnabled: data.fastSubmitEnabled, }; } export function LeadAntiSpamAdminPage(props: LeadAntiSpamAdminPageProps) { const { t } = useTranslation(); const data = props.vm.status === "ready" ? props.vm.data : null; const [form, setForm] = useState(() => data ? formFromVm(data) : { mode: "quarantine" as const satisfies (typeof MODES)[number], perSourcePerHourLimit: "60", perIpPerHourLimit: "20", anomalySpikeFactor: "10", lostAutoDeleteDays: "0", disposableDomainEnabled: true, honeypotEnabled: true, fastSubmitEnabled: false, } ); const set = (patch: Partial) => setForm((s) => ({ ...s, ...patch })); // Read back what the server actually stored. It clamps (a spike factor of 1 // becomes 2) and drops unparseable numbers, so without this the form would keep // showing a value that was never saved. const vmSignature = data ? JSON.stringify(data) : null; const lastVmSignature = useRef(vmSignature); useEffect(() => { if (!data || lastVmSignature.current === vmSignature) return; lastVmSignature.current = vmSignature; setForm(formFromVm(data)); // eslint-disable-next-line react-hooks/exhaustive-deps }, [vmSignature]); if (!data) { return
; } const numberField = (key: NumberField, labelKey: string, hintKey: string) => ( set({ [key]: e.target.value } as Partial)} data-ui-id={`ui.leads.antiSpam.${key}`} /> ); function save() { props.actions.onSave({ mode: form.mode, perSourcePerHourLimit: form.perSourcePerHourLimit, perIpPerHourLimit: form.perIpPerHourLimit, anomalySpikeFactor: form.anomalySpikeFactor, lostAutoDeleteDays: form.lostAutoDeleteDays, ...(form.disposableDomainEnabled ? { disposableDomainEnabled: "on" } : {}), ...(form.honeypotEnabled ? { honeypotEnabled: "on" } : {}), ...(form.fastSubmitEnabled ? { fastSubmitEnabled: "on" } : {}), }); } return ( } title={t("leads.antiSpam.title")} subtitle={t("leads.antiSpam.subtitle")} actions={ } /> {props.errorKey ? {t(props.errorKey)} : null} {/* --- Signals: what makes a lead suspicious --- */} {t("leads.antiSpam.section.detection")} {t("leads.antiSpam.section.detectionHint")}
set({ disposableDomainEnabled: c })} label={t("leads.antiSpam.field.disposable")} description={t("leads.antiSpam.hint.disposable")} data-ui-id="ui.leads.antiSpam.disposableDomainEnabled" /> set({ honeypotEnabled: c })} label={t("leads.antiSpam.field.honeypot")} description={t("leads.antiSpam.hint.honeypot")} data-ui-id="ui.leads.antiSpam.honeypotEnabled" /> set({ fastSubmitEnabled: c })} label={t("leads.antiSpam.field.fastSubmit")} description={t("leads.antiSpam.hint.fastSubmit")} data-ui-id="ui.leads.antiSpam.fastSubmitEnabled" />
{/* --- Rate signals: same scoring pipeline, but counted per hour --- */} {t("leads.antiSpam.section.limits")} {t("leads.antiSpam.section.limitsHint")} {/* items-start, not items-end: the help texts differ in length and would otherwise drag the inputs off a shared baseline. */}
{numberField("perSourcePerHourLimit", "leads.antiSpam.field.perSource", "leads.antiSpam.hint.perSource")} {numberField("perIpPerHourLimit", "leads.antiSpam.field.perIp", "leads.antiSpam.hint.perIp")} {numberField("anomalySpikeFactor", "leads.antiSpam.field.spike", "leads.antiSpam.hint.spike")}
{/* --- Reaction: what a high score does --- */} {t("leads.antiSpam.section.reaction")} {t("leads.antiSpam.section.reactionHint")} {/* --- Retention: not spam detection, hence its own section --- */} {t("leads.antiSpam.section.retention")} {t("leads.antiSpam.section.retentionHint")}
{numberField("lostAutoDeleteDays", "leads.antiSpam.field.lostDelete", "leads.antiSpam.hint.lostDelete")} {/* Keeps the single field at the same width as the limit row above. */}
); } export default LeadAntiSpamAdminPage;