'use client'; import { useState } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, } from '../../utils'; import { Box } from '../../layout'; import { Paper } from '../../surfaces'; import { Typography } from '../../data-display'; import { Button, TextField, Select, Checkbox, FormControlLabel, IconButton, } from '../../inputs'; import { Delete } from '../../icons'; export type TableColumn = { name: string; type: string; length?: number; nullable: boolean; primaryKey: boolean; }; export type CreateTableDialogProps = { open: boolean; onClose: () => void; onCreate: (tableName: string, columns: TableColumn[]) => Promise; dataTypes: string[]; testId?: string; }; /** * CreateTableDialog - A dialog for creating new database tables. * Allows defining table name and column specifications. */ export function CreateTableDialog({ open, onClose, onCreate, dataTypes, testId, }: CreateTableDialogProps) { const [tableName, setTableName] = useState(''); const [columns, setColumns] = useState([ { name: '', type: 'VARCHAR', length: 255, nullable: true, primaryKey: false }, ]); const [loading, setLoading] = useState(false); const handleCreate = async () => { setLoading(true); try { await onCreate( tableName, columns.filter((col) => col.name.trim()) ); handleClose(); } finally { setLoading(false); } }; const handleClose = () => { setTableName(''); setColumns([ { name: '', type: 'VARCHAR', length: 255, nullable: true, primaryKey: false }, ]); onClose(); }; const addColumn = () => { setColumns([ ...columns, { name: '', type: 'VARCHAR', length: 255, nullable: true, primaryKey: false }, ]); }; const updateColumn = ( index: number, field: keyof TableColumn, value: string | number | boolean ) => { const updated = [...columns]; updated[index] = { ...updated[index], [field]: value }; setColumns(updated); }; const removeColumn = (index: number) => { if (columns.length > 1) { setColumns(columns.filter((_, i) => i !== index)); } }; return ( Create New Table setTableName(e.target.value)} sx={{ mt: 2, mb: 2 }} /> Columns: {columns.map((col, index) => ( updateColumn(index, 'name', e.target.value)} sx={{ mr: 1, mb: 1 }} /> {col.type === 'VARCHAR' && ( updateColumn(index, 'length', e.target.value)} sx={{ mr: 1, mb: 1, width: 100 }} /> )} updateColumn(index, 'nullable', e.target.checked) } /> } label="Nullable" sx={{ mr: 1 }} /> updateColumn(index, 'primaryKey', e.target.checked) } /> } label="Primary Key" sx={{ mr: 1 }} /> {columns.length > 1 && ( removeColumn(index)} color="error" size="small" aria-label="Remove column" > )} ))} ); } export default CreateTableDialog;