191 lines
9.1 KiB
TypeScript
191 lines
9.1 KiB
TypeScript
// R-P7 (P7 Lead-Inbox) Interim-UI (AD-28) — lead source admin (D-P7
|
|
// "Quellen-Verwaltung", archetype A2/A6), composed from ~/ui. Pure: renders the
|
|
// server-built LeadSourceAdminVM and calls actions.onUpsert / onDelete; the route
|
|
// POSTs the intents. The webhook token is stored hashed, so the table only shows
|
|
// whether one exists. No useLoaderData. Final Subframe optics = tracked
|
|
// design-nachzug.
|
|
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherDatabase } from "@subframe/core";
|
|
import type { LeadSourceAdminVM } from "@hivegrid/view-models";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { Table } from "~/ui/components/Table";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
|
|
export type LeadSourceUpsertInput = { key: string; name: string; defaultZustaendigerUserId: string };
|
|
|
|
export type LeadSourcesAdminActions = {
|
|
onUpsert: (input: LeadSourceUpsertInput) => void;
|
|
onDelete: (sourceId: string) => void;
|
|
};
|
|
|
|
export type LeadSourcesAdminPageProps = {
|
|
vm: LeadSourceAdminVM;
|
|
submitting: boolean;
|
|
errorKey?: string | null;
|
|
actions: LeadSourcesAdminActions;
|
|
};
|
|
|
|
const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
|
|
const cellText = "text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary";
|
|
// Inputs share the card width evenly instead of sizing themselves by content.
|
|
const fieldWidth = "w-full min-w-0 sm:w-auto sm:shrink sm:grow sm:basis-0";
|
|
const NO_OWNER = "__none__";
|
|
|
|
const EMPTY_DRAFT: LeadSourceUpsertInput = { key: "", name: "", defaultZustaendigerUserId: NO_OWNER };
|
|
|
|
export function LeadSourcesAdminPage(props: LeadSourcesAdminPageProps) {
|
|
const { t } = useTranslation();
|
|
const [draft, setDraft] = useState<LeadSourceUpsertInput>(EMPTY_DRAFT);
|
|
const set = (patch: Partial<LeadSourceUpsertInput>) => setDraft((s) => ({ ...s, ...patch }));
|
|
|
|
const data = props.vm.status === "ready" ? props.vm.data : null;
|
|
const rows = data?.rows ?? [];
|
|
const assignableUsers = data?.assignableUsers ?? [];
|
|
|
|
return (
|
|
<PageShell uiId="ui.leads.sources">
|
|
<PageHeader icon={<FeatherDatabase className={PAGE_HEADER_ICON_CLASS} />} title={t("leads.sources.title")} subtitle={t("leads.sources.subtitle")} />
|
|
<PageBody>
|
|
{props.errorKey ? <BadgeFeedback farbe="subtle-negative">{t(props.errorKey)}</BadgeFeedback> : null}
|
|
|
|
{/* Create first, list below — same order as the custom-field admin. */}
|
|
{data?.canWrite ? (
|
|
<Card data-ui-id="ui.leads.sources.create">
|
|
<span className={sectionHeading}>{t("leads.sources.createTitle")}</span>
|
|
<div className="flex w-full flex-col items-stretch gap-4 sm:flex-row sm:items-end">
|
|
{/* The key's format hint rides in the placeholder — as a line of
|
|
help text below the field it pushed this input off the shared
|
|
baseline and made the row look misaligned. */}
|
|
<TextInput className={fieldWidth} label={t("leads.sources.field.key")} showLabel>
|
|
<TextInput.Input
|
|
value={draft.key}
|
|
onChange={(e) => set({ key: e.target.value })}
|
|
placeholder={t("leads.sources.keyPlaceholder")}
|
|
data-ui-id="ui.leads.sources.create.key"
|
|
/>
|
|
</TextInput>
|
|
{/* One name field only: the row's displayLabel is what every
|
|
screen renders, and the service copies the name into it. A
|
|
second "display label" input asked for the same thing twice. */}
|
|
<TextInput className={fieldWidth} label={t("leads.sources.field.name")} showLabel>
|
|
<TextInput.Input value={draft.name} onChange={(e) => set({ name: e.target.value })} data-ui-id="ui.leads.sources.create.name" />
|
|
</TextInput>
|
|
{/* The service already stores a default owner per source and the
|
|
route already reads it — it just had no control until now. */}
|
|
<Select
|
|
className={fieldWidth}
|
|
label={t("leads.sources.field.defaultOwner")}
|
|
showLabel
|
|
value={draft.defaultZustaendigerUserId}
|
|
onValueChange={(value) => set({ defaultZustaendigerUserId: value })}
|
|
data-ui-id="ui.leads.sources.create.defaultOwner"
|
|
>
|
|
<Select.Item value={NO_OWNER}>{t("leads.sources.field.defaultOwnerNone")}</Select.Item>
|
|
{assignableUsers.map((user) => (
|
|
<Select.Item key={user.id} value={user.id}>
|
|
{user.label}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
<Button
|
|
className="shrink-0"
|
|
loading={props.submitting}
|
|
disabled={!draft.key.trim() || !draft.name.trim()}
|
|
onClick={() => {
|
|
props.actions.onUpsert({ ...draft, defaultZustaendigerUserId: draft.defaultZustaendigerUserId === NO_OWNER ? "" : draft.defaultZustaendigerUserId });
|
|
setDraft(EMPTY_DRAFT);
|
|
}}
|
|
data-ui-id="ui.leads.sources.create.submit"
|
|
>
|
|
{t("leads.sources.actions.save")}
|
|
</Button>
|
|
</div>
|
|
</Card>
|
|
) : null}
|
|
|
|
{rows.length === 0 ? (
|
|
<div data-ui-id="ui.leads.sources.empty">
|
|
<EmptyState title={t("leads.sources.empty")} />
|
|
</div>
|
|
) : (
|
|
<div className="w-full overflow-hidden rounded-lg border border-solid border-border-default shadow-sm overflow-x-auto">
|
|
<Table
|
|
data-ui-id="ui.leads.sources.table"
|
|
header={
|
|
<Table.HeaderRow>
|
|
<Table.HeaderCell>{t("leads.sources.field.name")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("leads.sources.field.key")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("leads.sources.field.type")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("leads.sources.field.defaultOwner")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("leads.sources.field.status")}</Table.HeaderCell>
|
|
{/* Action column: just wide enough for the button, centred. */}
|
|
<Table.HeaderCell className="w-24"> </Table.HeaderCell>
|
|
</Table.HeaderRow>
|
|
}
|
|
>
|
|
{rows.map((row) => (
|
|
<Table.Row key={row.id} data-row-id={row.id}>
|
|
<Table.Cell>
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">{row.displayLabel}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className={cellText}>{row.key}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<BadgeFeedback farbe="subtle-neutral" size="small">
|
|
{t(row.typeLabelKey)}
|
|
</BadgeFeedback>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className={cellText}>{row.defaultZustaendigerLabel ?? "—"}</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<div className="flex flex-wrap items-center gap-1">
|
|
{row.isPaused ? (
|
|
<BadgeFeedback farbe="subtle-warning" size="small">
|
|
{t("leads.sources.status.paused")}
|
|
</BadgeFeedback>
|
|
) : null}
|
|
{row.hasWebhookToken ? (
|
|
<BadgeFeedback farbe="subtle-positive" size="small">
|
|
{t("leads.sources.status.webhook")}
|
|
</BadgeFeedback>
|
|
) : null}
|
|
</div>
|
|
</Table.Cell>
|
|
<Table.Cell className="w-24">
|
|
{/* Only custom sources can go: the system ones are wired to
|
|
built-in capture paths and would break if removed. */}
|
|
{data?.canWrite && row.type === "custom" ? (
|
|
<div className="flex w-full justify-center">
|
|
<Button
|
|
variant="tertiary"
|
|
size="small"
|
|
loading={props.submitting}
|
|
onClick={() => props.actions.onDelete(row.id)}
|
|
data-ui-id={`ui.leads.sources.row.delete.${row.id}`}
|
|
>
|
|
{t("leads.sources.actions.delete")}
|
|
</Button>
|
|
</div>
|
|
) : null}
|
|
</Table.Cell>
|
|
</Table.Row>
|
|
))}
|
|
</Table>
|
|
</div>
|
|
)}
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
export default LeadSourcesAdminPage;
|