// WF-P8 S06 "Verlust-Gründe" — rebuilt against Davids Subframe export of // 2026-07-28 (page 3c95e1e1-728b-460c-996f-5a641c971dfe) plus the local // wireframe doc. // // Pure/presentational: renders the server-built LostReasonAdminData and turns // create/edit/archive/reactivate into a typed onSubmit(intent, fields) the // route POSTs. Search and the status facet are client-side (the list is a // handful of rows). No useLoaderData/useFetcher and no server imports. // // Deliberate deltas against the export: // * "Verlauf ansehen" has no screen behind it and is left out rather than // shipped as a dead menu entry. // * The export's DropdownMenuSelect is not in the local ~/ui kit; the row // actions use the synced DropdownMenu, which is the same interaction. import { useMemo, useState, type ChangeEvent } from "react"; import { useTranslation } from "react-i18next"; import type { LostReasonAdminData, LostReasonRowVM } from "@hivegrid/view-models"; import { FeatherAlignLeft, FeatherArchive, FeatherCircleDot, FeatherClock, FeatherFilter, FeatherFolderKanban, FeatherMoreHorizontal, FeatherPencil, FeatherPlus, FeatherThumbsDown, FeatherTrendingDown, FeatherTrendingUp, FeatherUndo2, } from "@subframe/core"; import * as SubframeCore from "@subframe/core"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Dialog } from "~/ui/components/Dialog"; import { DropdownMenu } from "~/ui/components/DropdownMenu"; import { EmptyState } from "~/ui/components/EmptyState"; import { IconButton } from "~/ui/components/IconButton"; import { SearchInput } from "~/ui/components/SearchInput"; import { Select } from "~/ui/components/Select"; import { Table } from "~/ui/components/Table"; import { Tag_Label } from "~/ui/components/Tag_Label"; import { TextArea } from "~/ui/components/TextArea"; import { TextInput } from "~/ui/components/TextInput"; import { useLocaleFormatters } from "~/locale/format"; export type LostReasonAdminSubmit = (intent: string, fields: Record) => void; export type LostReasonAdminPageProps = { data: LostReasonAdminData; submitting: boolean; errorKey?: string | null; actions: { onSubmit: LostReasonAdminSubmit }; }; export type LostReasonStatusFilter = "all" | "active" | "archived"; const insightLabel = "text-body-extra-small-medium-150 font-body-extra-small-medium-150 uppercase tracking-wide text-text-neutral-tertiary"; /** Client-side filter, exported so the rule can be tested without the Radix widget. */ export function filterLostReasons( reasons: readonly LostReasonRowVM[], filters: { search: string; status: LostReasonStatusFilter } ): readonly LostReasonRowVM[] { const q = filters.search.trim().toLowerCase(); return reasons.filter((reason) => { if (filters.status === "active" && reason.archived) return false; if (filters.status === "archived" && !reason.archived) return false; if (!q) return true; return [reason.name, reason.beschreibung, reason.kategorie].some((field) => field?.toLowerCase().includes(q)); }); } export function LostReasonAdminPage(props: LostReasonAdminPageProps) { const { t } = useTranslation(); const { number, dateTime } = useLocaleFormatters(); const submit = props.actions.onSubmit; const { data } = props; const [search, setSearch] = useState(""); const [status, setStatus] = useState("all"); const [createOpen, setCreateOpen] = useState(false); const [draft, setDraft] = useState({ name: "", beschreibung: "", kategorie: "" }); const [editing, setEditing] = useState(null); const rows = useMemo(() => filterLostReasons(data.reasons, { search, status }), [data.reasons, search, status]); function resetDraft() { setDraft({ name: "", beschreibung: "", kategorie: "" }); setCreateOpen(false); } return ( } title={t("projects.lostReasonsAdmin.title")} subtitle={t("projects.lostReasonsAdmin.subtitle")} actions={ /* Loss picture of the last 30 days, straight from the stage-change history — the whole point of maintaining reasons. Both compartments always render: a missing comparison is itself an answer, and a card that changes width between tenants reads as a broken header. */
{t("projects.lostReasonsAdmin.insights.topReason")} {data.insights.topReasonName ?? t("projects.lostReasonsAdmin.insights.noLosses")}
{t("projects.lostReasonsAdmin.insights.monthComparison")} {data.insights.changePercent === null ? ( {t("projects.lostReasonsAdmin.insights.noComparison")} ) : (
{data.insights.changeIsWorse ? ( ) : ( )} {t("projects.lostReasonsAdmin.insights.changeValue", { value: `${data.insights.changePercent > 0 ? "+" : ""}${number(data.insights.changePercent)}`, })} {t("projects.lostReasonsAdmin.insights.versusPreviousMonth")}
)}
} />
{props.errorKey ? {t(props.errorKey)} : null} {/* Search and status facet as one bordered bar, as in the export. The add action sits at its right end: the export draws no button at all, but reasons have to come from somewhere, and the header card is the loss picture, not a place for controls. */}
) => setSearch(event.target.value)} data-ui-id="ui.projects.lostReasons.filter.search" />
{t("projects.lostReasonsAdmin.statusLabel")}
{data.canAdmin ? ( <>
) : null}
{t("projects.lostReasonsAdmin.countLine", { total: number(data.totalCount), templates: number(data.templateCount), own: number(data.ownCount), })}
{/* Creating a reason is a short, self-contained form. As an inline card it pushed the whole table down; as a dialog the list stays put. */} (open ? setCreateOpen(true) : resetDraft())}>
{t("projects.lostReasonsAdmin.newReason")}
setDraft((current) => ({ ...current, name: event.target.value }))} data-ui-id="ui.projects.lostReasons.new.name" /> setDraft((current) => ({ ...current, kategorie: event.target.value }))} data-ui-id="ui.projects.lostReasons.new.kategorie" />
{data.reasons.length === 0 ? (
) : rows.length === 0 ? ( {t("projects.lostReasonsAdmin.noMatches")} ) : (
}>{t("projects.lostReasonsAdmin.columns.reason")}}>{t("projects.lostReasonsAdmin.columns.description")}}>{t("projects.lostReasonsAdmin.columns.projects")}}>{t("projects.lostReasonsAdmin.columns.status")}}>{t("projects.lostReasonsAdmin.columns.lastUsed")} {/* The row menu needs no heading — the export leaves it blank rather than labelling a column of three dots. */} } > {rows.map((reason) => editing === reason.id ? ( setEditing(null)} /> ) : ( setEditing(reason.id)} formatDateTime={dateTime} formatNumber={number} /> ) )}
)} {t("projects.lostReasonsAdmin.footerLine", { total: number(data.totalCount), projects: number(data.assignedProjectCount), })}
); } function LostReasonRow({ reason, canAdmin, submit, onEdit, formatDateTime, formatNumber, }: { reason: LostReasonRowVM; canAdmin: boolean; submit: LostReasonAdminSubmit; onEdit: () => void; formatDateTime: (value: string) => string; formatNumber: (value: number) => string; }) { const { t } = useTranslation(); const muted = reason.archived ? "text-text-neutral-tertiary" : "text-text-neutral-primary"; return (
{reason.name} {t(reason.istVorlage ? "projects.lostReasonsAdmin.origin.template" : "projects.lostReasonsAdmin.origin.own")} {reason.isDemoData ? ( {t("projects.lostReasonsAdmin.demoBadge")} ) : null}
{reason.beschreibung ? ( {reason.beschreibung} ) : null} {reason.kategorie ? ( {t("projects.lostReasonsAdmin.categoryPrefix", { category: reason.kategorie })} ) : null}
{t("projects.lostReasonsAdmin.projectCount", { count: reason.projectCount, formatted: formatNumber(reason.projectCount) })} {t(reason.archived ? "projects.lostReasonsAdmin.status.archived" : "projects.lostReasonsAdmin.status.active")} {reason.lastUsedIso ? formatDateTime(reason.lastUsedIso) : t("projects.lostReasonsAdmin.neverUsed")} {canAdmin ? (
} aria-label={t("projects.lostReasonsAdmin.columns.actions")} data-ui-id={`ui.projects.lostReasons.row.${reason.id}.menu`} /> } onSelect={onEdit} data-ui-id={`ui.projects.lostReasons.row.${reason.id}.edit`} > {t("projects.actions.edit")} {reason.canArchive ? ( } onSelect={() => submit("archiveLostReason", { reasonId: reason.id })} data-ui-id={`ui.projects.lostReasons.row.${reason.id}.archive`} > {t("projects.actions.archive")} ) : ( } onSelect={() => submit("reactivateLostReason", { reasonId: reason.id })} data-ui-id={`ui.projects.lostReasons.row.${reason.id}.reactivate`} > {t("projects.actions.reactivate")} )}
) : null}
); } /** Inline edit row — name, category and description in place of the read row. */ function LostReasonEditRow({ reason, submit, submitting, onDone, }: { reason: LostReasonRowVM; submit: LostReasonAdminSubmit; submitting: boolean; onDone: () => void; }) { const { t } = useTranslation(); const [draft, setDraft] = useState({ name: reason.name, beschreibung: reason.beschreibung ?? "", kategorie: reason.kategorie ?? "", }); return ( setDraft((current) => ({ ...current, name: event.target.value }))} />
setDraft((current) => ({ ...current, beschreibung: event.target.value }))} data-ui-id={`ui.projects.lostReasons.row.${reason.id}.beschreibungInput`} /> setDraft((current) => ({ ...current, kategorie: event.target.value }))} data-ui-id={`ui.projects.lostReasons.row.${reason.id}.kategorieInput`} />
); } export default LostReasonAdminPage;