'use client' import React, { useState, useEffect } from 'react' import { useParams } from 'next/navigation' import { Hazard, LibraryHazard, HazardFormData } from './_components/types' import { HazardForm } from './_components/HazardForm' import { LibraryModal } from './_components/LibraryModal' import { HazardTable } from './_components/HazardTable' export default function HazardsPage() { const params = useParams() const projectId = params.projectId as string const [hazards, setHazards] = useState([]) const [library, setLibrary] = useState([]) const [loading, setLoading] = useState(true) const [showForm, setShowForm] = useState(false) const [showLibrary, setShowLibrary] = useState(false) const [suggestingAI, setSuggestingAI] = useState(false) useEffect(() => { fetchHazards() }, [projectId]) async function fetchHazards() { try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/hazards`) if (res.ok) { const json = await res.json() setHazards(json.hazards || json || []) } } catch (err) { console.error('Failed to fetch hazards:', err) } finally { setLoading(false) } } async function fetchLibrary() { try { const res = await fetch('/api/sdk/v1/iace/hazard-library') if (res.ok) { const json = await res.json() setLibrary(json.hazards || json || []) } } catch (err) { console.error('Failed to fetch hazard library:', err) } setShowLibrary(true) } async function handleAddFromLibrary(item: LibraryHazard) { try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/hazards`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: item.name, description: item.description, category: item.category, severity: item.default_severity, exposure: item.default_exposure, probability: item.default_probability, }), }) if (res.ok) await fetchHazards() } catch (err) { console.error('Failed to add from library:', err) } } async function handleSubmit(data: HazardFormData) { try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/hazards`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) if (res.ok) { setShowForm(false) await fetchHazards() } } catch (err) { console.error('Failed to add hazard:', err) } } async function handleAISuggestions() { setSuggestingAI(true) try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/hazards/suggest`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, }) if (res.ok) await fetchHazards() } catch (err) { console.error('Failed to get AI suggestions:', err) } finally { setSuggestingAI(false) } } async function handleDelete(id: string) { if (!confirm('Gefaehrdung wirklich loeschen?')) return try { const res = await fetch(`/api/sdk/v1/iace/projects/${projectId}/hazards/${id}`, { method: 'DELETE' }) if (res.ok) await fetchHazards() } catch (err) { console.error('Failed to delete hazard:', err) } } if (loading) { return (
) } return (
{/* Header */}

Hazard Log

Gefaehrdungsanalyse mit Risikobewertung nach S x E x P Methode.

{/* Stats */} {hazards.length > 0 && (
{hazards.length}
Gesamt
{hazards.filter((h) => h.risk_level === 'critical').length}
Kritisch
{hazards.filter((h) => h.risk_level === 'high').length}
Hoch
{hazards.filter((h) => h.risk_level === 'medium').length}
Mittel
{hazards.filter((h) => h.risk_level === 'low').length}
Niedrig
)} {showForm && ( setShowForm(false)} /> )} {showLibrary && ( setShowLibrary(false)} /> )} {hazards.length > 0 ? ( ) : ( !showForm && (

Kein Hazard Log vorhanden

Beginnen Sie mit der systematischen Erfassung von Gefaehrdungen. Nutzen Sie die Bibliothek oder KI-Vorschlaege als Ausgangspunkt.

) )}
) }