Files

223 lines
9.9 KiB
TypeScript

// 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 (
<Dialog open={props.open} onOpenChange={props.onOpenChange}>
<Dialog.Content className="w-[560px] max-w-[92vw] p-0">
<div className="flex w-full flex-col" data-ui-id="ui.leads.detail.convert.dialog">
<div className="flex w-full items-start justify-between gap-3 border-b border-solid border-border-default bg-surface-neutral-secondary px-6 py-5 mobile:px-4">
<div className="flex flex-col items-start gap-1">
<span className="text-heading- font-heading- text-text-neutral-primary">{t("leads.detail.convert.dialogTitle")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("leads.detail.convert.hint")}</span>
</div>
<IconButton
type="button"
variant="ghost"
size="small"
icon={<FeatherX />}
aria-label={t("shared.cancel")}
onClick={() => props.onOpenChange(false)}
/>
</div>
<div className="flex max-h-[65vh] w-full flex-col gap-4 overflow-y-auto px-6 py-5 mobile:px-4">
{options.pipelines.length === 0 ? (
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-feedback-negative" data-ui-id="ui.leads.detail.convert.dialog.noPipelines">
{t("leads.detail.convert.noPipelines")}
</span>
) : (
<>
<TextInput className="max-w-none" label={t("leads.detail.convert.projektname")} showLabel>
<TextInput.Input
value={projektname}
onChange={(event) => setProjektname(event.target.value)}
autoFocus
data-ui-id="ui.leads.detail.convert.dialog.projektname"
/>
</TextInput>
<div className="grid w-full gap-4 sm:grid-cols-2">
<Select
className="max-w-none"
label={t("leads.detail.convert.board")}
showLabel
value={pipelineId}
onValueChange={pickPipeline}
data-ui-id="ui.leads.detail.convert.dialog.pipeline"
>
{options.pipelines.map((entry) => (
<Select.Item key={entry.id} value={entry.id}>
{entry.label}
</Select.Item>
))}
</Select>
<Select
className="max-w-none"
label={t("leads.detail.convert.stage")}
showLabel
value={stageId}
onValueChange={setStageId}
data-ui-id="ui.leads.detail.convert.dialog.stage"
>
{stages.map((stage) => (
<Select.Item key={stage.id} value={stage.id}>
{stage.label}
</Select.Item>
))}
</Select>
</div>
<div className="flex w-full flex-col gap-2">
<TextInput className="max-w-none" label={t("leads.detail.convert.kontakt")} showLabel icon={<FeatherSearch />}>
<TextInput.Input
value={contactSearch}
onChange={(event) => setContactSearch(event.target.value)}
placeholder={t("leads.detail.convert.kontaktSearch")}
data-ui-id="ui.leads.detail.convert.dialog.kontaktSearch"
/>
</TextInput>
<Select
className="max-w-none"
value={kontaktId}
onValueChange={setKontaktId}
data-ui-id="ui.leads.detail.convert.dialog.kontakt"
>
<Select.Item value={NO_CONTACT}>{t("leads.detail.convert.kontaktNone")}</Select.Item>
{visibleContacts.map((contact) => (
<Select.Item key={contact.id} value={contact.id}>
{contact.label}
</Select.Item>
))}
</Select>
{/* Never let a capped list read as "these are all contacts". */}
{hiddenContacts > 0 || options.contactsTruncated ? (
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary" data-ui-id="ui.leads.detail.convert.dialog.kontaktHint">
{t("leads.detail.convert.kontaktMore")}
</span>
) : null}
</div>
</>
)}
</div>
<div className="flex w-full items-center justify-end gap-2 border-t border-solid border-border-default bg-surface-neutral-secondary px-6 py-4 mobile:flex-col-reverse mobile:items-stretch mobile:px-4">
<Button
type="button"
className="mobile:w-full"
variant="tertiary"
onClick={() => props.onOpenChange(false)}
data-ui-id="ui.leads.detail.convert.dialog.cancel"
>
{t("shared.cancel")}
</Button>
<Button
type="button"
className="mobile:w-full"
icon={<FeatherArrowRight />}
loading={props.submitting}
disabled={!canSubmit || options.pipelines.length === 0}
onClick={() =>
props.onConvert({
projektname: projektname.trim(),
pipelineId,
stageId,
kontaktId: kontaktId === NO_CONTACT ? "" : kontaktId,
})
}
data-ui-id="ui.leads.detail.convert.dialog.submit"
>
{t("leads.detail.convert.create")}
</Button>
</div>
</div>
</Dialog.Content>
</Dialog>
);
}
export default LeadConvertDialog;