280 lines
14 KiB
TypeScript
280 lines
14 KiB
TypeScript
// R-P7 (P7 Lead-Inbox) Interim-UI (AD-28) — lead detail + triage (D-P7
|
|
// "Lead-Detail" A3 + "Lead -> Projekt" A5), composed from ~/ui. Pure/presentational:
|
|
// renders LeadDetailVM and turns every mutation into a single onSubmit(intent,
|
|
// fields) the route POSTs. Converting Lead -> Projekt opens LeadConvertDialog,
|
|
// which asks for the board and stage the schema requires. No useLoaderData /
|
|
// useFetcher. Final Subframe optics = tracked design-nachzug.
|
|
//
|
|
// One save button in the header covers everything that is a plain field edit —
|
|
// master data, release flag and status went through three separate submits
|
|
// before. Converting to a project stays its own button: it creates a Projekt and
|
|
// is not something a "save" should trigger as a side effect.
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherTrendingUp } from "@subframe/core";
|
|
import type { LeadDetailData, LeadDetailVM, LeadWeiterverarbeitungFreigabe } from "@hivegrid/view-models";
|
|
import { canTransitionLeadStatus, type LeadStatus } from "~/utils/integrations/leads/leadListQuery";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { TextArea } from "~/ui/components/TextArea";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
import { APP_TIME_ZONE } from "~/locale/format";
|
|
import { ClickToCallEntryPoint, type ClickToCallEntryPointProps } from "../telephony/ClickToCallDialog";
|
|
import { LeadConvertDialog, type LeadConvertSubmitInput } from "./LeadConvertDialog";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
|
|
const STATUSES: readonly LeadStatus[] = ["new", "qualified", "converted", "lost", "spam"];
|
|
const FREIGABEN: readonly LeadWeiterverarbeitungFreigabe[] = ["ja", "nein", "unklar"];
|
|
const markerColor: Record<string, "subtle-warning" | "subtle-negative" | "subtle-positive"> = {
|
|
duplicate: "subtle-warning",
|
|
spam: "subtle-negative",
|
|
quarantined: "subtle-warning",
|
|
converted: "subtle-positive",
|
|
};
|
|
const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
|
|
const mutedText = "text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary";
|
|
const metaLabel = "text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary";
|
|
|
|
export type LeadDetailPageActions = { onSubmit: (intent: string, fields?: Record<string, string>) => void };
|
|
|
|
export type LeadDetailPageProps = {
|
|
vm: LeadDetailVM;
|
|
submitting: boolean;
|
|
errorKey?: string | null;
|
|
language?: string;
|
|
/** Optional telephony affordance; omitted when the feature or permission gate is closed. */
|
|
clickToCall?: ClickToCallEntryPointProps;
|
|
actions: LeadDetailPageActions;
|
|
};
|
|
|
|
function formFromVm(data: LeadDetailData) {
|
|
return {
|
|
vorname: data.vorname ?? "",
|
|
nachname: data.nachname ?? "",
|
|
email: data.email ?? "",
|
|
telefon: data.telefon ?? "",
|
|
message: data.message ?? "",
|
|
weiterverarbeitungFreigabe: data.weiterverarbeitungFreigabe as string,
|
|
status: data.status as string,
|
|
};
|
|
}
|
|
|
|
const EMPTY_FORM = { vorname: "", nachname: "", email: "", telefon: "", message: "", weiterverarbeitungFreigabe: "unklar", status: "new" };
|
|
|
|
export function LeadDetailPage(props: LeadDetailPageProps) {
|
|
const { t, i18n } = useTranslation();
|
|
const submit = props.actions.onSubmit;
|
|
const data = props.vm.status === "ready" ? props.vm.data : null;
|
|
|
|
const [form, setForm] = useState(() => (data ? formFromVm(data) : EMPTY_FORM));
|
|
const [convertOpen, setConvertOpen] = useState(false);
|
|
|
|
// Read back what the server actually stored. It normalises on write (e-mail is
|
|
// lower-cased, blank strings become null), so the raw input must not stick
|
|
// around. Watching the view-model alone is not enough: when the normalised
|
|
// value equals what was already stored, the view-model comes back byte-identical
|
|
// and only the finished submit tells us to re-seed. A failed save keeps the
|
|
// input so the user can correct it.
|
|
const vmSignature = data ? JSON.stringify(data) : null;
|
|
const lastVmSignature = useRef(vmSignature);
|
|
const wasSubmitting = useRef(props.submitting);
|
|
useEffect(() => {
|
|
const submitFinished = wasSubmitting.current && !props.submitting;
|
|
wasSubmitting.current = props.submitting;
|
|
const vmChanged = lastVmSignature.current !== vmSignature;
|
|
lastVmSignature.current = vmSignature;
|
|
if (!data || props.errorKey) return;
|
|
if (!submitFinished && !vmChanged) return;
|
|
setForm(formFromVm(data));
|
|
// A finished conversion re-renders with the project attached, which is the
|
|
// signal that the dialog has done its job.
|
|
setConvertOpen(false);
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [props.submitting, props.errorKey, vmSignature]);
|
|
|
|
if (!data) {
|
|
return <div className="flex w-full flex-col gap-6 p-6" data-ui-id="ui.leads.detail" />;
|
|
}
|
|
|
|
const set = (patch: Partial<typeof form>) => setForm((s) => ({ ...s, ...patch }));
|
|
const canWrite = data.canWrite;
|
|
const dateFormatter = new Intl.DateTimeFormat(props.language ?? i18n.language, { timeZone: APP_TIME_ZONE, dateStyle: "medium", timeStyle: "short" });
|
|
|
|
// Converting runs through "qualified", so a lead that can no longer reach that
|
|
// status cannot be converted either — and one already converted must not be
|
|
// sent through a second time. Same rule the view-model uses for the marker: a
|
|
// lead counts as converted by status alone, even without a linked project.
|
|
const alreadyConverted = Boolean(data.konvertiertProjektId) || data.status === "converted";
|
|
const canConvert = canWrite && !alreadyConverted && canTransitionLeadStatus(data.status, "qualified");
|
|
// A marker that only repeats the status would render the same word twice.
|
|
const markers = data.markers.filter((marker) => marker !== data.status);
|
|
|
|
const metaRow = (label: string, value: string) => (
|
|
<div className="flex flex-col gap-1">
|
|
<span className={metaLabel}>{label}</span>
|
|
<span className={mutedText}>{value}</span>
|
|
</div>
|
|
);
|
|
|
|
return (
|
|
<PageShell uiId="ui.leads.detail" className="bg-surface-neutral-primary">
|
|
<PageHeader
|
|
icon={<FeatherTrendingUp className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={
|
|
<span className="inline-flex flex-wrap items-center gap-2">
|
|
{data.displayName}
|
|
<BadgeFeedback farbe="subtle-neutral" size="small">
|
|
{t(data.statusLabelKey)}
|
|
</BadgeFeedback>
|
|
{markers.map((marker) => (
|
|
<BadgeFeedback key={marker} farbe={markerColor[marker]} size="small">
|
|
{t(`leads.marker.${marker}`)}
|
|
</BadgeFeedback>
|
|
))}
|
|
</span>
|
|
}
|
|
subtitle={[t(data.quelleLabelKey), data.sourceOrigin].filter(Boolean).join(" · ")}
|
|
actions={
|
|
props.clickToCall || canWrite ? (
|
|
<>
|
|
{props.clickToCall ? <ClickToCallEntryPoint {...props.clickToCall} /> : null}
|
|
{canWrite ? (
|
|
<Button loading={props.submitting} onClick={() => submit("save", { ...form })} data-ui-id="ui.leads.detail.save">
|
|
{t("leads.actions.save")}
|
|
</Button>
|
|
) : null}
|
|
</>
|
|
) : undefined
|
|
}
|
|
/>
|
|
|
|
<PageBody>
|
|
{props.errorKey ? (
|
|
<div data-ui-id="ui.leads.detail.error">
|
|
<BadgeFeedback farbe="subtle-negative">{t(props.errorKey)}</BadgeFeedback>
|
|
</div>
|
|
) : null}
|
|
|
|
{/* --- Everything the header's save button writes --- */}
|
|
<Card data-ui-id="ui.leads.detail.basics">
|
|
<span className={sectionHeading}>{t("leads.detail.section.basics")}</span>
|
|
<div className="grid w-full gap-4 sm:grid-cols-2">
|
|
<TextInput label={t("leads.field.vorname")} showLabel className="max-w-none">
|
|
<TextInput.Input value={form.vorname} disabled={!canWrite} onChange={(e) => set({ vorname: e.target.value })} data-ui-id="ui.leads.detail.vorname" />
|
|
</TextInput>
|
|
<TextInput label={t("leads.field.nachname")} showLabel className="max-w-none">
|
|
<TextInput.Input value={form.nachname} disabled={!canWrite} onChange={(e) => set({ nachname: e.target.value })} data-ui-id="ui.leads.detail.nachname" />
|
|
</TextInput>
|
|
<TextInput label={t("leads.field.email")} showLabel className="max-w-none">
|
|
<TextInput.Input type="email" value={form.email} disabled={!canWrite} onChange={(e) => set({ email: e.target.value })} data-ui-id="ui.leads.detail.email" />
|
|
</TextInput>
|
|
<TextInput label={t("leads.field.telefon")} showLabel className="max-w-none">
|
|
<TextInput.Input value={form.telefon} disabled={!canWrite} onChange={(e) => set({ telefon: e.target.value })} data-ui-id="ui.leads.detail.telefon" />
|
|
</TextInput>
|
|
{/* Status moved in here: it is saved by the same button as the rest,
|
|
so a separate "Status setzen" would have been a second truth. */}
|
|
<Select
|
|
label={t("leads.list.column.status")}
|
|
showLabel
|
|
className="max-w-none"
|
|
value={form.status}
|
|
onValueChange={(v) => set({ status: v })}
|
|
disabled={!canWrite}
|
|
data-ui-id="ui.leads.detail.status"
|
|
>
|
|
{STATUSES.map((s) => {
|
|
// The server rejects forbidden transitions; offering them as if
|
|
// they were possible only produced an error message after saving.
|
|
const allowed = canTransitionLeadStatus(data.status, s);
|
|
return (
|
|
<Select.Item key={s} value={s} disabled={!allowed}>
|
|
{t(`leads.status.${s}`)}
|
|
</Select.Item>
|
|
);
|
|
})}
|
|
</Select>
|
|
<Select
|
|
label={t("leads.field.freigabe")}
|
|
showLabel
|
|
className="max-w-none"
|
|
value={form.weiterverarbeitungFreigabe}
|
|
onValueChange={(v) => set({ weiterverarbeitungFreigabe: v })}
|
|
disabled={!canWrite}
|
|
data-ui-id="ui.leads.detail.freigabe"
|
|
>
|
|
{FREIGABEN.map((f) => (
|
|
<Select.Item key={f} value={f}>
|
|
{t(`leads.freigabe.${f}`)}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
</div>
|
|
<TextArea label={t("leads.field.message")} showLabel className="max-w-none">
|
|
<TextArea.Input value={form.message} disabled={!canWrite} onChange={(e) => set({ message: e.target.value })} data-ui-id="ui.leads.detail.message" />
|
|
</TextArea>
|
|
</Card>
|
|
|
|
{/* --- Convert: creates a Projekt, hence its own explicit action --- */}
|
|
{canWrite ? (
|
|
<Card data-ui-id="ui.leads.detail.convert.section">
|
|
<div className="flex w-full flex-wrap items-center justify-between gap-3">
|
|
<div className="flex flex-col items-start gap-1">
|
|
<span className={sectionHeading}>{t("leads.detail.section.convert")}</span>
|
|
<span className={mutedText}>
|
|
{t(alreadyConverted ? "leads.detail.convert.done" : canConvert ? "leads.detail.convert.hint" : "leads.detail.convert.blocked")}
|
|
</span>
|
|
</div>
|
|
{canConvert ? (
|
|
<Button className="shrink-0" onClick={() => setConvertOpen(true)} data-ui-id="ui.leads.detail.convert">
|
|
{t("leads.actions.convertToProject")}
|
|
</Button>
|
|
) : null}
|
|
</div>
|
|
</Card>
|
|
) : null}
|
|
|
|
{canConvert && data.convert ? (
|
|
<LeadConvertDialog
|
|
open={convertOpen}
|
|
onOpenChange={setConvertOpen}
|
|
options={data.convert}
|
|
submitting={props.submitting}
|
|
onConvert={(input: LeadConvertSubmitInput) => submit("convert", { ...input })}
|
|
/>
|
|
) : null}
|
|
|
|
{/* --- Where the lead came from and what happened to it --- */}
|
|
<Card data-ui-id="ui.leads.detail.meta">
|
|
<span className={sectionHeading}>{t("leads.detail.section.meta")}</span>
|
|
<div className="grid w-full gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
|
{metaRow(t("leads.field.quelle"), t(data.quelleLabelKey))}
|
|
{metaRow(t("leads.list.column.owner"), data.zustaendigerLabel ?? "—")}
|
|
{metaRow(t("leads.list.column.createdAt"), dateFormatter.format(new Date(data.createdAtIso)))}
|
|
{metaRow(t("leads.detail.meta.updated"), dateFormatter.format(new Date(data.updatedAtIso)))}
|
|
</div>
|
|
</Card>
|
|
|
|
{/* --- Spam --- */}
|
|
<Card data-ui-id="ui.leads.detail.spam">
|
|
<span className={sectionHeading}>{t("leads.detail.spam.title")}</span>
|
|
<span className={mutedText}>{t("leads.detail.spam.score", { score: data.spamScore })}</span>
|
|
{data.spamReason ? <span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">{data.spamReason}</span> : null}
|
|
</Card>
|
|
|
|
{/* --- DSGVO auto-delete --- */}
|
|
{data.autoDeleteAtIso ? (
|
|
<Card variant="warning" data-ui-id="ui.leads.detail.dsgvo">
|
|
<span className={sectionHeading}>{t("leads.detail.dsgvo.title")}</span>
|
|
<span className={mutedText}>{t("leads.detail.dsgvo.deadline", { date: dateFormatter.format(new Date(data.autoDeleteAtIso)) })}</span>
|
|
</Card>
|
|
) : null}
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
export default LeadDetailPage;
|