import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { developerApi } from '../../services/api'; import Card from '../../components/ui/Card'; import Button from '../../components/ui/Button'; import Badge from '../../components/ui/Badge'; import Modal from '../../components/ui/Modal'; import { Database, Table, ArrowRight, Edit, Trash2, Save, X, ChevronLeft, ChevronRight, GitBranch } from 'lucide-react'; import ERDiagram from './ERDiagram'; interface TableMeta { name: string; model: string; primaryKey: string; readonlyFields: string[]; requiredFields: string[]; relations: { field: string; targetTable: string; type: 'one' | 'many' }[]; foreignKeys: { field: string; targetTable: string }[]; } export default function DatabaseStructure() { const [selectedTable, setSelectedTable] = useState(null); const [page, setPage] = useState(1); const [editingRow, setEditingRow] = useState<{ id: string; data: Record } | null>(null); const [showERDiagram, setShowERDiagram] = useState(false); const queryClient = useQueryClient(); const { data: schemaData, isLoading: schemaLoading, error: schemaError } = useQuery({ queryKey: ['developer-schema'], queryFn: developerApi.getSchema, }); // Debug logging console.log('Schema data:', schemaData); console.log('Schema error:', schemaError); const { data: tableData, isLoading: tableLoading } = useQuery({ queryKey: ['developer-table', selectedTable, page], queryFn: () => developerApi.getTableData(selectedTable!, page), enabled: !!selectedTable, }); const updateMutation = useMutation({ mutationFn: ({ tableName, id, data }: { tableName: string; id: string; data: Record }) => developerApi.updateRow(tableName, id, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['developer-table', selectedTable] }); setEditingRow(null); }, onError: (error: any) => { alert(error.response?.data?.error || 'Fehler beim Speichern'); }, }); const deleteMutation = useMutation({ mutationFn: ({ tableName, id }: { tableName: string; id: string }) => developerApi.deleteRow(tableName, id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['developer-table', selectedTable] }); }, onError: (error: any) => { alert(error.response?.data?.error || 'Fehler beim Löschen'); }, }); const tables: TableMeta[] = schemaData?.data || []; const currentTableMeta = tables.find((t) => t.name === selectedTable); const getRowId = (row: any, meta: TableMeta) => { if (meta.primaryKey.includes(',')) { return meta.primaryKey.split(',').map((k) => row[k]).join('-'); } return String(row[meta.primaryKey]); }; const formatValue = (value: any): string => { if (value === null || value === undefined) return '-'; if (typeof value === 'boolean') return value ? 'Ja' : 'Nein'; if (typeof value === 'object') { if (value instanceof Date || (typeof value === 'string' && value.match(/^\d{4}-\d{2}-\d{2}/))) { return new Date(value).toLocaleString('de-DE'); } return JSON.stringify(value); } return String(value); }; const handleSaveEdit = () => { if (!editingRow || !selectedTable) return; updateMutation.mutate({ tableName: selectedTable, id: editingRow.id, data: editingRow.data, }); }; const handleDelete = (id: string) => { if (!selectedTable) return; if (!confirm('Datensatz wirklich löschen?')) return; deleteMutation.mutate({ tableName: selectedTable, id }); }; if (schemaLoading) { return
Laden...
; } const handleDiagramSelectTable = (tableName: string) => { setSelectedTable(tableName); setPage(1); setShowERDiagram(false); }; return (

Datenbankstruktur

{/* Tabellen-Liste */}
{tables.map((table) => (
{/* Pagination */} {(tableData as any)?.pagination && (tableData as any).pagination.totalPages > 1 && (

Seite {(tableData as any).pagination.page} von {(tableData as any).pagination.totalPages} ({(tableData as any).pagination.total} Einträge)

)} )}
) : (
Wähle eine Tabelle aus der Liste aus
)}
{/* Edit Modal */} setEditingRow(null)} title={`${selectedTable} bearbeiten`} > {editingRow && currentTableMeta && (
{Object.entries(editingRow.data).map(([key, value]) => { const isReadonly = currentTableMeta.readonlyFields.includes(key); const isRequired = currentTableMeta.requiredFields.includes(key); return (
{isReadonly ? (
{formatValue(value)}
) : typeof value === 'boolean' ? ( ) : ( setEditingRow({ ...editingRow, data: { ...editingRow.data, [key]: typeof value === 'number' ? (e.target.value ? Number(e.target.value) : null) : e.target.value || null, }, }) } className="w-full px-3 py-2 border rounded-lg font-mono text-sm" disabled={isReadonly} /> )}
); })}
)}
{/* ER Diagram Modal */} {showERDiagram && (
setShowERDiagram(false)} />

ER-Diagramm - Datenbankbeziehungen

)}
); }