// 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 = { 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) => 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
; } const set = (patch: Partial) => 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) => (
{label} {value}
); return ( } title={ {data.displayName} {t(data.statusLabelKey)} {markers.map((marker) => ( {t(`leads.marker.${marker}`)} ))} } subtitle={[t(data.quelleLabelKey), data.sourceOrigin].filter(Boolean).join(" · ")} actions={ props.clickToCall || canWrite ? ( <> {props.clickToCall ? : null} {canWrite ? ( ) : null} ) : undefined } /> {props.errorKey ? (
{t(props.errorKey)}
) : null} {/* --- Everything the header's save button writes --- */} {t("leads.detail.section.basics")}
set({ vorname: e.target.value })} data-ui-id="ui.leads.detail.vorname" /> set({ nachname: e.target.value })} data-ui-id="ui.leads.detail.nachname" /> set({ email: e.target.value })} data-ui-id="ui.leads.detail.email" /> set({ telefon: e.target.value })} data-ui-id="ui.leads.detail.telefon" /> {/* 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. */}
{/* --- Convert: creates a Projekt, hence its own explicit action --- */} {canWrite ? (
{t("leads.detail.section.convert")} {t(alreadyConverted ? "leads.detail.convert.done" : canConvert ? "leads.detail.convert.hint" : "leads.detail.convert.blocked")}
{canConvert ? ( ) : null}
) : null} {canConvert && data.convert ? ( submit("convert", { ...input })} /> ) : null} {/* --- Where the lead came from and what happened to it --- */} {t("leads.detail.section.meta")}
{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)))}
{/* --- Spam --- */} {t("leads.detail.spam.title")} {t("leads.detail.spam.score", { score: data.spamScore })} {data.spamReason ? {data.spamReason} : null} {/* --- DSGVO auto-delete --- */} {data.autoDeleteAtIso ? ( {t("leads.detail.dsgvo.title")} {t("leads.detail.dsgvo.deadline", { date: dateFormatter.format(new Date(data.autoDeleteAtIso)) })} ) : null}
); } export default LeadDetailPage;