// R-P7 Interim-UI (AD-28) — "Lead zu Projekt konvertieren" as a modal on the lead // detail page, same shape as the "Neuer Lead" dialog. Presentational only: it // renders the server-built LeadConvertOptions and hands the picked values to the // injected onConvert; the route POSTs them. No useLoaderData / useFetcher // (DoD A.5). Final Subframe optics = tracked design-nachzug. // // Board + stage are asked for because the schema requires them: Projekt.pipelineId // and Projekt.stageId are non-null, and the conversion used to fill them silently // with the default sales pipeline — so every converted lead landed on one board. import { useEffect, useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherArrowRight, FeatherSearch, FeatherX } from "@subframe/core"; import type { LeadConvertOptions } from "@hivegrid/view-models"; import { Button } from "~/ui/components/Button"; import { Dialog } from "~/ui/components/Dialog"; import { IconButton } from "~/ui/components/IconButton"; import { Select } from "~/ui/components/Select"; import { TextInput } from "~/ui/components/TextInput"; /** Select needs a non-empty value, so "no contact" gets its own sentinel. */ const NO_CONTACT = "__none__"; /** Keeps the contact dropdown navigable when a tenant has hundreds of contacts. */ const CONTACT_RENDER_LIMIT = 50; export type LeadConvertSubmitInput = { projektname: string; pipelineId: string; stageId: string; kontaktId: string; }; export type LeadConvertDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; options: LeadConvertOptions; submitting: boolean; onConvert: (input: LeadConvertSubmitInput) => void; }; export function LeadConvertDialog(props: LeadConvertDialogProps) { const { t } = useTranslation(); const { options } = props; const [projektname, setProjektname] = useState(options.defaultProjektname); const [pipelineId, setPipelineId] = useState(options.pipelines[0]?.id ?? ""); const [stageId, setStageId] = useState(options.pipelines[0]?.stages[0]?.id ?? ""); const [kontaktId, setKontaktId] = useState(options.suggestedKontaktId ?? NO_CONTACT); const [contactSearch, setContactSearch] = useState(""); // Reopening must not show what a previous, abandoned attempt had typed. useEffect(() => { if (!props.open) return; setProjektname(options.defaultProjektname); setPipelineId(options.pipelines[0]?.id ?? ""); setStageId(options.pipelines[0]?.stages[0]?.id ?? ""); setKontaktId(options.suggestedKontaktId ?? NO_CONTACT); setContactSearch(""); // eslint-disable-next-line react-hooks/exhaustive-deps }, [props.open]); const pipeline = options.pipelines.find((entry) => entry.id === pipelineId) ?? options.pipelines[0]; const stages = pipeline?.stages ?? []; function pickPipeline(nextId: string) { setPipelineId(nextId); // The stage belongs to the board — carrying the old one over would post a // stage the target board does not own, which the server rejects. const next = options.pipelines.find((entry) => entry.id === nextId); setStageId(next?.stages[0]?.id ?? ""); } const visibleContacts = useMemo(() => { const needle = contactSearch.trim().toLowerCase(); const matches = needle ? options.contacts.filter((contact) => contact.label.toLowerCase().includes(needle)) : options.contacts; const shown = matches.slice(0, CONTACT_RENDER_LIMIT); // The picked contact must stay in the list, or the Select would show a blank. if (kontaktId !== NO_CONTACT && !shown.some((contact) => contact.id === kontaktId)) { const picked = options.contacts.find((contact) => contact.id === kontaktId); if (picked) return [picked, ...shown]; } return shown; }, [contactSearch, options.contacts, kontaktId]); const hiddenContacts = Math.max(0, options.contacts.length - visibleContacts.length); const canSubmit = Boolean(projektname.trim() && pipelineId && stageId) && !props.submitting; return (
{t("leads.detail.convert.dialogTitle")} {t("leads.detail.convert.hint")}
} aria-label={t("shared.cancel")} onClick={() => props.onOpenChange(false)} />
{options.pipelines.length === 0 ? ( {t("leads.detail.convert.noPipelines")} ) : ( <> setProjektname(event.target.value)} autoFocus data-ui-id="ui.leads.detail.convert.dialog.projektname" />
}> setContactSearch(event.target.value)} placeholder={t("leads.detail.convert.kontaktSearch")} data-ui-id="ui.leads.detail.convert.dialog.kontaktSearch" /> {/* Never let a capped list read as "these are all contacts". */} {hiddenContacts > 0 || options.contactsTruncated ? ( {t("leads.detail.convert.kontaktMore")} ) : null}
)}
); } export default LeadConvertDialog;