'use client' import { useState } from 'react' interface DeltaResult { added_patterns?: Array<{ pattern_name: string; hazard_cats: string[] }> removed_patterns?: Array<{ pattern_name: string; hazard_cats: string[] }> added_hazards?: Array<{ name: string; category: string }> removed_hazards?: Array<{ name: string; category: string }> added_measures?: Array<{ id: string; name: string }> removed_measures?: Array<{ id: string; name: string }> } interface DeltaPreviewModalProps { projectId: string currentInput: { component_library_ids: string[] energy_source_ids: string[] operational_states?: string[] human_roles?: string[] } proposedInput: { component_library_ids: string[] energy_source_ids: string[] operational_states?: string[] human_roles?: string[] } onClose: () => void onApply: () => void changeDescription: string } export function DeltaPreviewModal({ projectId, currentInput, proposedInput, onClose, onApply, changeDescription, }: DeltaPreviewModalProps) { const [result, setResult] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState('') // Auto-run delta analysis on mount useState(() => { runDelta() }) async function runDelta() { setLoading(true) setError('') try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/delta-analysis`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ current: currentInput, proposed: proposedInput }), }) if (!res.ok) { setError('Delta-Analyse fehlgeschlagen') return } setResult(await res.json()) } catch { setError('Verbindung fehlgeschlagen') } finally { setLoading(false) } } const addedP = result?.added_patterns?.length || 0 const removedP = result?.removed_patterns?.length || 0 const addedH = result?.added_hazards?.length || 0 const removedH = result?.removed_hazards?.length || 0 const addedM = result?.added_measures?.length || 0 const removedM = result?.removed_measures?.length || 0 const hasChanges = addedP + removedP + addedH + removedH + addedM + removedM > 0 return (
{/* Header */}

Delta-Vorschau

{changeDescription}

{/* Content */}
{loading && (
Berechne Auswirkungen...
)} {error && (
{error}
)} {result && !loading && (
{/* Summary Grid */}
{!hasChanges && (

Keine Auswirkungen erkannt — die Aenderung beeinflusst keine Patterns.

)} {/* Added Hazards */} {addedH > 0 && (

+ Neue Gefaehrdungen

    {result!.added_hazards!.slice(0, 15).map((h, i) => (
  • + {h.name || h.category}
  • ))} {addedH > 15 &&
  • ... und {addedH - 15} weitere
  • }
)} {/* Removed Hazards */} {removedH > 0 && (

- Entfallene Gefaehrdungen

    {result!.removed_hazards!.slice(0, 10).map((h, i) => (
  • - {h.name || h.category}
  • ))}
)}
)}
{/* Footer */}
) } function DeltaStat({ label, added, removed }: { label: string; added: number; removed: number }) { return (
{label}
{added > 0 && +{added}} {removed > 0 && -{removed}} {added === 0 && removed === 0 && 0}
) }