// Nutzungseinheiten und Wohnungsspiegel im Flächenbilanz-Panel ([278]). // // • Liste der Einheiten (Nummer, Typ, Nutzung, Farbe) bearbeiten, neue Einheit anlegen (leer oder aus der Raum-Auswahl), // Auswahl einer Einheit zuordnen, Einheit löschen (die Räume bleiben, ohne Einheit). // • Wohnungsspiegel (Einheiten mit Zimmerzahl und Flächen, Summe je Typ und je Geschoss) aus demselben Berichtsmodell wie // CSV/PDF (`buildAreaReport` mit `units`); gerechnet im Kern (`unit_schedule.rs`). // Alle Änderungen laufen über `applyChange` (Ops), eine Aktion = ein Undo-Schritt; Tippen verschmilzt über den coalesceKey. import { useMemo } from "react"; import { t } from "../i18n"; import { useStore } from "../state/appStore"; import { UNIT_USAGES, buildAreaReport, computeUnitSchedule, exportProjectName, areaReportToCsv, buildAreaReportPdf } from "../core"; import type { Project, UnitUsage } from "../model/types"; import { ReportTable } from "./ReportTable"; import { downloadCsv } from "./downloadFile"; import { runWithAnonymityGate } from "../state/anonymityGate"; import { addUnitOps, assignRoomsOps, deleteUnitOps, draftUnit, patchUnitOps } from "./unitOps"; /** Auswahlwert „keine Einheit“ (leer ist der Platzhalter). */ const NONE = "-"; export function UnitsSection() { const project = useStore((s) => s.project); const applyChange = useStore((s) => s.applyChange); const notify = useStore((s) => s.notify); const selectedRooms = useStore((s) => s.selection.rooms); const units = project.units ?? []; const sched = useMemo(() => computeUnitSchedule(project), [project]); const rowOf = new Map(sched.units.map((r) => [r.id, r])); // Reihenfolge der Liste wie im Spiegel (nach Nummer natürlich sortiert). const ordered = sched.units.map((r) => units.find((u) => u.id === r.id)).filter((u): u is NonNullable => !!u); const report = useMemo(() => buildAreaReport(project, { areas: false, units: true }), [project]); const stem = `${exportProjectName(project) || "projekt"}-wohnungsspiegel`; const add = (fromSelection: boolean) => applyChange((p: Project) => addUnitOps(p, draftUnit(p, t("units.nameDefault")), fromSelection ? selectedRooms : [])); const patch = (id: string, field: string, v: Parameters[2]) => applyChange((p: Project) => patchUnitOps(p, id, v), `unit:${id}:${field}`); const assign = (unitId: string) => { if (selectedRooms.length === 0 || unitId === "") return; applyChange((p: Project) => assignRoomsOps(p, selectedRooms, unitId === NONE ? undefined : unitId)); }; const exportCsv = () => { if (!units.length) return notify(t("units.export.empty")); runWithAnonymityGate(project, [`${stem}.csv`], () => downloadCsv(`${stem}.csv`, areaReportToCsv(report))); }; const exportPdf = () => { if (!units.length) return notify(t("units.export.empty")); runWithAnonymityGate(project, [`${stem}.pdf`], async () => { try { (await buildAreaReportPdf(report)).save(`${stem}.pdf`); } catch { notify(t("units.exportPdf.fail")); } }); }; return (
{t("units.title")}
{units.length === 0 &&
{t("units.empty")}
} {ordered.length > 0 && (
{ordered.map((u) => { const r = rowOf.get(u.id); return (
patch(u.id, "name", { name: e.target.value })} /> patch(u.id, "type", { unitType: e.target.value === "" ? undefined : e.target.value })} /> patch(u.id, "color", { color: e.target.value })} /> {r ? `${r.usage === "living" ? t("units.zi", { n: r.roomCount }) + " · " : ""}${r.hnf.toFixed(1)} m²` : ""}
); })}
)}
{units.length > 0 && ( )}
{report.sections.map((s, si) => ( ))} {report.sections.length > 0 && (
    {report.notes.map((n, i) => (
  • {n}
  • ))}
)}
); }