"use client"; import { useEffect, useState } from "react"; import { Fragment } from "react"; import { getCircuitTree } from "../utils/api"; import type { CircuitTreeCircuitDto, CircuitTreeResponseDto } from "../types"; function formatNumber(value: number | undefined, digits = 2) { if (value === undefined || Number.isNaN(value)) { return "-"; } return new Intl.NumberFormat("de-DE", { minimumFractionDigits: digits, maximumFractionDigits: digits, }).format(value); } function formatPhaseType(value: string | undefined) { if (value === "three_phase") { return "Dreiphasig"; } if (value === "single_phase") { return "Einphasig"; } return value ?? "-"; } function renderCircuitSummaryLabel(circuit: CircuitTreeCircuitDto) { if (circuit.displayName?.trim()) { return circuit.displayName; } if (circuit.deviceRows.length > 1) { return `${circuit.deviceRows.length} Geräte`; } return "Reserve"; } export function CircuitTreePreview(props: { projectId: string; circuitListId: string }) { const { projectId, circuitListId } = props; const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { setIsLoading(true); setError(null); getCircuitTree(projectId, circuitListId) .then(setData) .catch((err: unknown) => setError(err instanceof Error ? err.message : "Die Stromkreisstruktur konnte nicht geladen werden.") ) .finally(() => setIsLoading(false)); }, [projectId, circuitListId]); if (isLoading) { return
Stromkreisstruktur wird geladen …
; } if (error) { return
{error}
; } if (!data || !data.sections.length) { return
Keine Bereiche oder Stromkreise vorhanden.
; } const hasAnyCircuits = data.sections.some((section) => section.circuits.length > 0); if (!hasAnyCircuits) { return
Bereiche sind vorhanden, enthalten aber noch keine Stromkreise.
; } return (
{data.sections.map((section) => ( ))}
Betriebsmittelkennzeichen Anzeigename Phasenart Anschlussart Kostengruppe Kategorie Ebene Raumnummer Raumname Anzahl Leistung / Gerät Gleichzeitigkeit cos φ Zeilensumme Stromkreissumme Schutzart Bemessungsstrom Charakteristik Kabeltyp Kabelquerschnitt Kabellänge Bemerkung
); } function SectionRows(props: { section: CircuitTreeResponseDto["sections"][number] }) { const { section } = props; return ( <> {section.displayName} {section.circuits.map((circuit) => { if (circuit.deviceRows.length === 0) { return ( {circuit.equipmentIdentifier} {circuit.displayName?.trim() || "Reserve"} - {formatNumber(circuit.circuitTotalPower)} {circuit.protectionType ?? "-"} {formatNumber(circuit.protectionRatedCurrent)} {circuit.protectionCharacteristic ?? "-"} {circuit.cableType ?? "-"} {circuit.cableCrossSection ?? "-"} {formatNumber(circuit.cableLength)} {circuit.remark ?? "-"} ); } if (circuit.deviceRows.length === 1) { const row = circuit.deviceRows[0]; return ( {circuit.equipmentIdentifier} {row.displayName || row.name} {formatPhaseType(row.phaseType)} {row.connectionKind ?? "-"} {row.costGroup ?? "-"} {row.category ?? "-"} {row.level ?? "-"} {row.roomNumberSnapshot ?? "-"} {row.roomNameSnapshot ?? "-"} {formatNumber(row.quantity, 0)} {formatNumber(row.powerPerUnit)} {formatNumber(row.simultaneityFactor)} {formatNumber(row.cosPhi)} {formatNumber(row.rowTotalPower)} {formatNumber(circuit.circuitTotalPower)} {circuit.protectionType ?? "-"} {formatNumber(circuit.protectionRatedCurrent)} {circuit.protectionCharacteristic ?? "-"} {circuit.cableType ?? "-"} {circuit.cableCrossSection ?? "-"} {formatNumber(circuit.cableLength)} {row.remark ?? circuit.remark ?? "-"} ); } return ( {circuit.equipmentIdentifier} {renderCircuitSummaryLabel(circuit)} - {formatNumber(circuit.circuitTotalPower)} {circuit.protectionType ?? "-"} {formatNumber(circuit.protectionRatedCurrent)} {circuit.protectionCharacteristic ?? "-"} {circuit.cableType ?? "-"} {circuit.cableCrossSection ?? "-"} {formatNumber(circuit.cableLength)} {circuit.remark ?? "-"} {circuit.deviceRows.map((row) => ( {row.displayName || row.name} {formatPhaseType(row.phaseType)} {row.connectionKind ?? "-"} {row.costGroup ?? "-"} {row.category ?? "-"} {row.level ?? "-"} {row.roomNumberSnapshot ?? "-"} {row.roomNameSnapshot ?? "-"} {formatNumber(row.quantity, 0)} {formatNumber(row.powerPerUnit)} {formatNumber(row.simultaneityFactor)} {formatNumber(row.cosPhi)} {formatNumber(row.rowTotalPower)} - - - - - - - {row.remark ?? "-"} ))} ); })} -frei- Freie Zeile ); }