Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/leads/LeadAntiSpamAdminPage.tsx
T

234 lines
10 KiB
TypeScript

// 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<typeof form>) => 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 <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.leads.antiSpam" />;
}
const numberField = (key: NumberField, labelKey: string, hintKey: string) => (
<TextInput className={fieldWidth} label={t(labelKey)} showLabel helpText={t(hintKey)}>
<TextInput.Input
type="number"
min={NUMBER_MIN[key]}
value={form[key]}
onChange={(e) => set({ [key]: e.target.value } as Partial<typeof form>)}
data-ui-id={`ui.leads.antiSpam.${key}`}
/>
</TextInput>
);
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 (
<PageShell uiId="ui.leads.antiSpam">
<PageHeader
icon={<FeatherShieldCheck className={PAGE_HEADER_ICON_CLASS} />}
title={t("leads.antiSpam.title")}
subtitle={t("leads.antiSpam.subtitle")}
actions={
<Button loading={props.submitting} disabled={!data.canWrite} onClick={save} data-ui-id="ui.leads.antiSpam.save">
{t("leads.actions.save")}
</Button>
}
/>
<PageBody>
{props.errorKey ? <BadgeFeedback farbe="subtle-negative">{t(props.errorKey)}</BadgeFeedback> : null}
{/* --- Signals: what makes a lead suspicious --- */}
<Card data-ui-id="ui.leads.antiSpam.detection">
<span className={sectionHeading}>{t("leads.antiSpam.section.detection")}</span>
<span className={mutedText}>{t("leads.antiSpam.section.detectionHint")}</span>
<div className="flex w-full flex-col gap-3">
<Checkbox
className={checkboxRow}
checked={form.disposableDomainEnabled}
onCheckedChange={(c) => set({ disposableDomainEnabled: c })}
label={t("leads.antiSpam.field.disposable")}
description={t("leads.antiSpam.hint.disposable")}
data-ui-id="ui.leads.antiSpam.disposableDomainEnabled"
/>
<Checkbox
className={checkboxRow}
checked={form.honeypotEnabled}
onCheckedChange={(c) => set({ honeypotEnabled: c })}
label={t("leads.antiSpam.field.honeypot")}
description={t("leads.antiSpam.hint.honeypot")}
data-ui-id="ui.leads.antiSpam.honeypotEnabled"
/>
<Checkbox
className={checkboxRow}
checked={form.fastSubmitEnabled}
onCheckedChange={(c) => set({ fastSubmitEnabled: c })}
label={t("leads.antiSpam.field.fastSubmit")}
description={t("leads.antiSpam.hint.fastSubmit")}
data-ui-id="ui.leads.antiSpam.fastSubmitEnabled"
/>
</div>
</Card>
{/* --- Rate signals: same scoring pipeline, but counted per hour --- */}
<Card data-ui-id="ui.leads.antiSpam.limits">
<span className={sectionHeading}>{t("leads.antiSpam.section.limits")}</span>
<span className={mutedText}>{t("leads.antiSpam.section.limitsHint")}</span>
{/* items-start, not items-end: the help texts differ in length and would
otherwise drag the inputs off a shared baseline. */}
<div className="flex w-full flex-col items-stretch gap-4 sm:flex-row sm:items-start">
{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")}
</div>
</Card>
{/* --- Reaction: what a high score does --- */}
<Card data-ui-id="ui.leads.antiSpam.reaction">
<span className={sectionHeading}>{t("leads.antiSpam.section.reaction")}</span>
<span className={mutedText}>{t("leads.antiSpam.section.reactionHint")}</span>
<Select
className="w-full sm:max-w-[280px]"
label={t("leads.antiSpam.field.mode")}
showLabel
value={form.mode}
onValueChange={(v) => set({ mode: v as typeof form.mode })}
data-ui-id="ui.leads.antiSpam.mode"
>
{MODES.map((m) => (
<Select.Item key={m} value={m}>
{t(`leads.antiSpam.mode.${m}`)}
</Select.Item>
))}
</Select>
</Card>
{/* --- Retention: not spam detection, hence its own section --- */}
<Card data-ui-id="ui.leads.antiSpam.retention">
<span className={sectionHeading}>{t("leads.antiSpam.section.retention")}</span>
<span className={mutedText}>{t("leads.antiSpam.section.retentionHint")}</span>
<div className="flex w-full flex-col items-stretch gap-4 sm:flex-row sm:items-start">
{numberField("lostAutoDeleteDays", "leads.antiSpam.field.lostDelete", "leads.antiSpam.hint.lostDelete")}
{/* Keeps the single field at the same width as the limit row above. */}
<div className={`hidden sm:block ${fieldWidth}`} />
<div className={`hidden sm:block ${fieldWidth}`} />
</div>
</Card>
</PageBody>
</PageShell>
);
}
export default LeadAntiSpamAdminPage;