271 lines
11 KiB
TypeScript
271 lines
11 KiB
TypeScript
// R-P7 (P7 Lead-Inbox) Interim-UI (AD-28) — incoming-leads Kanban board, composed
|
|
// from ~/ui (KanbanCard) on top of the shared SortableKanbanBoard (dnd-kit).
|
|
// Pure/presentational: renders the server-built LeadBoardData and turns a valid
|
|
// column move into a single onSetStatus callback the route POSTs (intent
|
|
// `setStatus`). Every drag funnels through requestMove so:
|
|
// - a drop into "Gewonnen" (converted) is NEVER a silent flip — it points the
|
|
// user at the detail page project conversion (createProjectFromQualifiedLead);
|
|
// - an invalid transition (backward / from a terminal status) is rejected
|
|
// client-side with a plaintext error and the card snaps back (resetToken);
|
|
// - a valid move (new->qualified, ->lost) submits and the server re-validates.
|
|
// No useLoaderData/useFetcher, no server imports. Final Subframe optics = tracked
|
|
// design-nachzug.
|
|
|
|
import { useEffect, useMemo, useState, type ReactNode } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherColumns, FeatherEye, FeatherList, FeatherPlus, FeatherTrendingUp } from "@subframe/core";
|
|
import type { LeadBoardData, LeadBoardStatus } from "@hivegrid/view-models";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { KanbanCard } from "~/ui/components/KanbanCard";
|
|
import { ToggleGroup } from "~/ui/components/ToggleGroup";
|
|
import { SortableKanbanBoard, type SortableKanbanColumn } from "~/components/ui/kanban/SortableKanbanBoard";
|
|
import { useRecommendedSidebarCollapse } from "~/components/app/AppShellSidebarContext";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
import { FacetFilterBar, type ClientFacet } from "../shared/FacetFilterBar";
|
|
import { useLiveFilter } from "~/hooks/use-live-filter";
|
|
import { AppLink } from "~/components/app/AppLink";
|
|
|
|
export type LeadBoardActions = {
|
|
onSetStatus: (leadId: string, status: LeadBoardStatus) => void;
|
|
onOpenList: () => void;
|
|
onNew: () => void;
|
|
onSearch: (value: string) => void;
|
|
onToggleSpam: (checked: boolean) => void;
|
|
/**
|
|
* Clears search and spam visibility in ONE navigation. Calling onSearch and
|
|
* onToggleSpam back to back would not do: both derive their next params from
|
|
* the same render's snapshot, so the second would resurrect what the first
|
|
* dropped.
|
|
*/
|
|
onResetFilters: () => void;
|
|
};
|
|
|
|
export type LeadBoardPageProps = {
|
|
data: LeadBoardData;
|
|
submitting: boolean;
|
|
errorKey?: string | null;
|
|
language?: string;
|
|
search: string;
|
|
actions: LeadBoardActions;
|
|
};
|
|
|
|
type BoardCard = LeadBoardData["columns"][number]["cards"][number];
|
|
|
|
const mutedText = "text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary";
|
|
|
|
export function LeadBoardPage(props: LeadBoardPageProps) {
|
|
const { t } = useTranslation();
|
|
const { data } = props;
|
|
useRecommendedSidebarCollapse("leads");
|
|
// Client-side rejection feedback + snap-back trigger for the shared board.
|
|
const [localError, setLocalError] = useState<string | null>(null);
|
|
const [convertHint, setConvertHint] = useState<{ name: string; href: string } | null>(null);
|
|
const [resetToken, setResetToken] = useState(0);
|
|
const [searchDraft, setSearchDraft] = useState(props.search);
|
|
useEffect(() => setSearchDraft(props.search), [props.search]);
|
|
// The shared bar has no Enter affordance, so the term applies debounced —
|
|
// exactly as it does in the list view.
|
|
useLiveFilter(searchDraft.trim(), props.search, () => props.actions.onSearch(searchDraft.trim()));
|
|
|
|
const cardById = useMemo(() => {
|
|
const map = new Map<string, BoardCard>();
|
|
for (const column of data.columns) for (const card of column.cards) map.set(card.id, card);
|
|
return map;
|
|
}, [data.columns]);
|
|
|
|
function snapBack() {
|
|
setResetToken((token) => token + 1); // revert the optimistic drop to server truth
|
|
}
|
|
|
|
// Every drag-end (mouse or keyboard) funnels through here.
|
|
function requestMove(leadId: string, targetStatus: LeadBoardStatus) {
|
|
const card = cardById.get(leadId);
|
|
if (!card || targetStatus === card.status) return;
|
|
setLocalError(null);
|
|
setConvertHint(null);
|
|
if (targetStatus === "spam") {
|
|
// Marking as spam schedules the lead for deletion — too consequential to
|
|
// happen from a drag. Same stance as "Gewonnen" below.
|
|
setLocalError("leads.board.error.spamViaList");
|
|
snapBack();
|
|
return;
|
|
}
|
|
if (targetStatus === "converted") {
|
|
// "Gewonnen" means a Projekt was created from the lead — never do that
|
|
// silently from a drag. Point the user at the detail page conversion.
|
|
setConvertHint({ name: card.displayName, href: card.detailHref });
|
|
snapBack();
|
|
return;
|
|
}
|
|
if (!card.allowedTargetStatuses.includes(targetStatus)) {
|
|
setLocalError("leads.board.error.invalidTransition");
|
|
snapBack();
|
|
return;
|
|
}
|
|
props.actions.onSetStatus(leadId, targetStatus);
|
|
}
|
|
|
|
function renderLeadCard(card: BoardCard, dragHandle?: ReactNode): ReactNode {
|
|
return (
|
|
<KanbanCard
|
|
titleSlot={
|
|
<AppLink href={card.detailHref} className="text-text-feedback-link-default">
|
|
{card.displayName}
|
|
</AppLink>
|
|
}
|
|
valueSlot={<span className={mutedText}>{card.ownerLabel ?? t("leads.board.card.unassigned")}</span>}
|
|
tagsSlot={
|
|
<div className="flex w-full items-center justify-between gap-2">
|
|
<BadgeFeedback farbe="subtle-neutral" size="small">
|
|
{t(card.quelleLabelKey)}
|
|
</BadgeFeedback>
|
|
{dragHandle}
|
|
</div>
|
|
}
|
|
footerLeftSlot={card.contact ? <span className={`${mutedText} truncate`}>{card.contact}</span> : undefined}
|
|
data-row-id={card.id}
|
|
data-ui-id={`ui.leads.board.card.${card.id}`}
|
|
/>
|
|
);
|
|
}
|
|
|
|
const boardColumns: SortableKanbanColumn<BoardCard>[] = data.columns.map((column) => ({
|
|
id: column.status,
|
|
label: t(column.labelKey),
|
|
name: t(column.labelKey),
|
|
badge: (
|
|
<BadgeFeedback farbe="subtle-neutral" size="small">
|
|
{String(column.count)}
|
|
</BadgeFeedback>
|
|
),
|
|
sumMode: "hidden",
|
|
accentColor: {
|
|
new: "#3B82F6",
|
|
qualified: "#8B5CF6",
|
|
converted: "#16A34A",
|
|
lost: "#64748B",
|
|
spam: "#DC2626",
|
|
}[column.status],
|
|
emptyContent: (
|
|
<div className="flex min-h-20 w-72 flex-col items-center justify-center gap-1 rounded-md border border-dashed border-border-default px-4 text-center">
|
|
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-secondary">
|
|
{t("leads.board.columnEmpty.title", { status: t(column.labelKey) })}
|
|
</span>
|
|
<span className={mutedText}>{t("leads.board.columnEmpty.body")}</span>
|
|
</div>
|
|
),
|
|
cards: column.cards,
|
|
}));
|
|
|
|
const isEmpty = data.columns.every((column) => column.cards.length === 0);
|
|
|
|
const facets: ClientFacet[] = [
|
|
{
|
|
key: "visibility",
|
|
label: t("leads.filter.visibility"),
|
|
icon: <FeatherEye />,
|
|
tone: "sand-subtle",
|
|
options: [{ value: "spam", label: t("leads.filter.showSpam") }],
|
|
selected: new Set(data.includeSpam ? ["spam"] : []),
|
|
},
|
|
];
|
|
|
|
return (
|
|
<PageShell uiId="ui.leads.board">
|
|
<PageHeader
|
|
icon={<FeatherTrendingUp className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t("leads.board.title")}
|
|
subtitle={t("leads.board.subtitle")}
|
|
actions={
|
|
<>
|
|
{/* Labelled board/list switch, same shape as the projects boards. */}
|
|
<ToggleGroup value="board" onValueChange={(value: string) => value === "list" && props.actions.onOpenList()} data-ui-id="ui.leads.board.viewToggle">
|
|
<ToggleGroup.Item icon={<FeatherColumns />} value="board" aria-label={t("leads.viewMode.board")} data-ui-id="ui.leads.board.viewToggle.board">
|
|
{t("leads.viewMode.board")}
|
|
</ToggleGroup.Item>
|
|
<ToggleGroup.Item icon={<FeatherList />} value="list" aria-label={t("leads.viewMode.list")} data-ui-id="ui.leads.board.viewToggle.list">
|
|
{t("leads.viewMode.list")}
|
|
</ToggleGroup.Item>
|
|
</ToggleGroup>
|
|
{data.canWrite ? (
|
|
<Button icon={<FeatherPlus />} onClick={props.actions.onNew} data-ui-id="ui.leads.board.new">
|
|
{t("leads.actions.new")}
|
|
</Button>
|
|
) : null}
|
|
</>
|
|
}
|
|
/>
|
|
{/* --- Filter bar: the same shared facet strip the list view wears, so
|
|
switching between board and list does not change what the filter
|
|
chrome looks like. The board has no status facet — its columns ARE
|
|
the statuses — so it carries the search plus the Einblenden facet,
|
|
which here holds spam alone. --- */}
|
|
<FacetFilterBar
|
|
uiId="ui.leads.board.filter"
|
|
facets={facets}
|
|
onToggle={(_facetKey, value) => value === "spam" && props.actions.onToggleSpam(!data.includeSpam)}
|
|
search={searchDraft}
|
|
onSearchChange={setSearchDraft}
|
|
searchPlaceholder={t("leads.filter.searchPlaceholder")}
|
|
onReset={() => {
|
|
setSearchDraft("");
|
|
props.actions.onResetFilters();
|
|
}}
|
|
labels={{ reset: t("leads.filter.resetAll"), removeChip: t("leads.filter.removeChip") }}
|
|
/>
|
|
<PageBody className="max-w-none">
|
|
{props.errorKey ? (
|
|
<div data-ui-id="ui.leads.board.serverError">
|
|
<BadgeFeedback farbe="subtle-negative">{t(props.errorKey)}</BadgeFeedback>
|
|
</div>
|
|
) : null}
|
|
{localError ? (
|
|
<div data-ui-id="ui.leads.board.error">
|
|
<BadgeFeedback farbe="subtle-negative">{t(localError)}</BadgeFeedback>
|
|
</div>
|
|
) : null}
|
|
{convertHint ? (
|
|
<div className="flex flex-wrap items-center gap-2" data-ui-id="ui.leads.board.convertHint">
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary">
|
|
{t("leads.board.convertHint", { name: convertHint.name })}
|
|
</span>
|
|
<AppLink href={convertHint.href} className="text-text-feedback-link-default" data-ui-id="ui.leads.board.convertHint.link">
|
|
{t("leads.board.convertLink")}
|
|
</AppLink>
|
|
</div>
|
|
) : null}
|
|
|
|
{/* No "saving" line here: it rode on the router's navigation state, so it
|
|
also flashed for a filter toggle, and the drop is optimistic anyway. */}
|
|
{data.columns.some((column) => column.truncated) ? (
|
|
<span className={mutedText} data-ui-id="ui.leads.board.truncatedHint">
|
|
{t("leads.board.truncatedHint", { count: Math.max(...data.columns.map((column) => column.loadedCount)) })}
|
|
</span>
|
|
) : null}
|
|
|
|
{isEmpty ? (
|
|
<div data-ui-id="ui.leads.board.empty">
|
|
<EmptyState title={t("leads.board.empty.headline")} body={t("leads.board.empty.body")} />
|
|
</div>
|
|
) : (
|
|
<SortableKanbanBoard<BoardCard>
|
|
columns={boardColumns}
|
|
renderCard={renderLeadCard}
|
|
getCardLabel={(card) => card.displayName}
|
|
onCardMove={(cardId, toColumnId) => requestMove(cardId, toColumnId as LeadBoardStatus)}
|
|
disabled={!data.canWrite}
|
|
errorKey={props.errorKey ?? localError}
|
|
resetToken={resetToken}
|
|
dragHandlePlacement="inline"
|
|
data-ui-id="ui.leads.board.columns"
|
|
/>
|
|
)}
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
export default LeadBoardPage;
|