'use client' import React, { useState, useEffect } from 'react' import { useSDK } from '@/lib/sdk' import { StepHeader, STEP_EXPLANATIONS } from '@/components/sdk/StepHeader' import { AISystem } from './_components/types' import { LoadingSkeleton } from './_components/LoadingSkeleton' import { RiskPyramid } from './_components/RiskPyramid' import { AddSystemForm } from './_components/AddSystemForm' import { AISystemCard } from './_components/AISystemCard' export default function AIActPage() { const { state } = useSDK() const [systems, setSystems] = useState([]) const [filter, setFilter] = useState('all') const [showAddForm, setShowAddForm] = useState(false) const [editingSystem, setEditingSystem] = useState(null) const [assessingId, setAssessingId] = useState(null) const [error, setError] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { const fetchSystems = async () => { setLoading(true) try { const res = await fetch('/api/sdk/v1/compliance/ai/systems') if (res.ok) { const data = await res.json() const backendSystems = data.systems || [] setSystems(backendSystems.map((s: Record) => ({ id: s.id as string, name: s.name as string, description: (s.description || '') as string, purpose: (s.purpose || '') as string, sector: (s.sector || '') as string, classification: (s.classification || 'unclassified') as AISystem['classification'], status: (s.status || 'draft') as AISystem['status'], obligations: (s.obligations || []) as string[], assessmentDate: s.assessment_date ? new Date(s.assessment_date as string) : null, assessmentResult: (s.assessment_result || null) as Record | null, }))) } } catch { // Backend unavailable — start with empty list } finally { setLoading(false) } } fetchSystems() }, []) const handleAddSystem = async (data: Omit) => { setError(null) if (editingSystem) { try { const res = await fetch(`/api/sdk/v1/compliance/ai/systems/${editingSystem.id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: data.name, description: data.description, purpose: data.purpose, sector: data.sector, classification: data.classification, status: data.status, obligations: data.obligations, }), }) if (res.ok) { const updated = await res.json() setSystems(prev => prev.map(s => s.id === editingSystem.id ? { ...s, ...data, id: updated.id || editingSystem.id } : s )) } else { setError('Speichern fehlgeschlagen') } } catch { setSystems(prev => prev.map(s => s.id === editingSystem.id ? { ...s, ...data } : s)) } setEditingSystem(null) } else { try { const res = await fetch('/api/sdk/v1/compliance/ai/systems', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: data.name, description: data.description, purpose: data.purpose, sector: data.sector, classification: data.classification, status: data.status, obligations: data.obligations, }), }) if (res.ok) { const created = await res.json() const newSystem: AISystem = { ...data, id: created.id, assessmentDate: created.assessment_date ? new Date(created.assessment_date) : null, assessmentResult: created.assessment_result || null, } setSystems(prev => [...prev, newSystem]) } else { setError('Registrierung fehlgeschlagen') } } catch { const newSystem: AISystem = { ...data, id: `ai-${Date.now()}`, assessmentDate: data.classification !== 'unclassified' ? new Date() : null, assessmentResult: null, } setSystems(prev => [...prev, newSystem]) } } setShowAddForm(false) } const handleDelete = async (id: string) => { if (!confirm('Moechten Sie dieses KI-System wirklich loeschen?')) return try { const res = await fetch(`/api/sdk/v1/compliance/ai/systems/${id}`, { method: 'DELETE' }) if (res.ok) { setSystems(prev => prev.filter(s => s.id !== id)) } else { setError('Loeschen fehlgeschlagen') } } catch { setError('Backend nicht erreichbar') } } const handleEdit = (system: AISystem) => { setEditingSystem(system) setShowAddForm(true) } const handleAssess = async (systemId: string) => { setAssessingId(systemId) setError(null) try { const res = await fetch(`/api/sdk/v1/compliance/ai/systems/${systemId}/assess`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, }) if (res.ok) { const result = await res.json() setSystems(prev => prev.map(s => s.id === systemId ? { ...s, assessmentDate: result.assessment_date ? new Date(result.assessment_date) : new Date(), assessmentResult: result.assessment_result || result, classification: (result.classification || s.classification) as AISystem['classification'], status: (result.status || 'classified') as AISystem['status'], obligations: result.obligations || s.obligations, } : s )) } else { const errData = await res.json().catch(() => ({ error: 'Bewertung fehlgeschlagen' })) setError(errData.error || errData.detail || 'Bewertung fehlgeschlagen') } } catch { setError('Verbindung zum KI-Service fehlgeschlagen. Bitte versuchen Sie es spaeter erneut.') } finally { setAssessingId(null) } } const filteredSystems = filter === 'all' ? systems : systems.filter(s => s.classification === filter || s.status === filter) const highRiskCount = systems.filter(s => s.classification === 'high-risk').length const compliantCount = systems.filter(s => s.status === 'compliant').length const unclassifiedCount = systems.filter(s => s.classification === 'unclassified').length const stepInfo = STEP_EXPLANATIONS['ai-act'] return (
{error && (
{error}
)} {showAddForm && ( { setShowAddForm(false); setEditingSystem(null) }} initialData={editingSystem} /> )}
KI-Systeme gesamt
{systems.length}
Hochrisiko
{highRiskCount}
Konform
{compliantCount}
Nicht klassifiziert
{unclassifiedCount}
Filter: {['all', 'high-risk', 'limited-risk', 'minimal-risk', 'unclassified', 'compliant', 'non-compliant'].map(f => ( ))}
{loading && } {!loading && (
{filteredSystems.map(system => ( handleAssess(system.id)} onEdit={() => handleEdit(system)} onDelete={() => handleDelete(system.id)} assessing={assessingId === system.id} /> ))}
)} {!loading && filteredSystems.length === 0 && (

Keine KI-Systeme gefunden

Passen Sie den Filter an oder registrieren Sie ein neues KI-System.

)}
) }