import { Alert, Badge, DataTable, DescriptionList, IconButton, Tabs, Text, type DataTableColumn, } from '@rozenite/ui'; import { KeyRound, RefreshCw, Trash2 } from 'lucide-react'; import { useMemo } from 'react'; import type { SqliteColumnInfo, SqliteEntity, SqliteForeignKeyInfo, SqliteIndexInfo, } from './sqlite-introspection'; import { getPrimaryKeyColumns } from './sqlite-row-mutations'; export type StructureSection = 'columns' | 'keys' | 'indexes'; export type StructureState = { columns: SqliteColumnInfo[]; foreignKeys: SqliteForeignKeyInfo[]; indexes: Array; }; type StructureColumnRow = { name: string; type: string; nullable: string; defaultValue: string; primaryKey: string; foreignKey: string; extra: string; }; type StructureIndexRow = { indexName: string; columns: string; unique: string; type: string; }; type StructurePaneProps = { entity: SqliteEntity; state: StructureState; section: StructureSection; loading: boolean; error: string | null; onSectionChange: (section: StructureSection) => void; onRefresh: () => void; onDrop: () => void; }; const COLUMN_RESIZING = { defaultSize: 220, minSize: 120 }; const SECTIONS: Array<[StructureSection, string]> = [ ['columns', 'Columns'], ['keys', 'Keys'], ['indexes', 'Indexes'], ]; const mono = (value: string) => {value}; const columnTableColumns: DataTableColumn[] = [ { id: 'name', header: 'Name', accessorKey: 'name', enableSorting: false, cell: ({ row }) => mono(row.original.name), }, { id: 'type', header: 'Type', accessorKey: 'type', enableSorting: false, cell: ({ row }) => mono(row.original.type), }, { id: 'nullable', header: 'Nullable', accessorKey: 'nullable', enableSorting: false, cell: ({ row }) => mono(row.original.nullable), }, { id: 'defaultValue', header: 'Default', accessorKey: 'defaultValue', enableSorting: false, cell: ({ row }) => mono(row.original.defaultValue), }, { id: 'primaryKey', header: 'PK', accessorKey: 'primaryKey', enableSorting: false, cell: ({ row }) => row.original.primaryKey === '—' ? ( — ) : ( {row.original.primaryKey} ), }, { id: 'foreignKey', header: 'FK', accessorKey: 'foreignKey', enableSorting: false, cell: ({ row }) => row.original.foreignKey === '—' ? ( — ) : ( FK ), }, { id: 'extra', header: 'Extra', accessorKey: 'extra', enableSorting: false, cell: ({ row }) => mono(row.original.extra), }, ]; const indexTableColumns: DataTableColumn[] = [ { id: 'indexName', header: 'Index Name', accessorKey: 'indexName', enableSorting: false, cell: ({ row }) => mono(row.original.indexName), }, { id: 'columns', header: 'Columns', accessorKey: 'columns', enableSorting: false, cell: ({ row }) => mono(row.original.columns), }, { id: 'unique', header: 'Unique', accessorKey: 'unique', enableSorting: false, cell: ({ row }) => mono(row.original.unique), }, { id: 'type', header: 'Type', accessorKey: 'type', enableSorting: false, cell: ({ row }) => mono(row.original.type), }, ]; export const StructurePane = ({ entity, state, section, loading, error, onSectionChange, onRefresh, onDrop, }: StructurePaneProps) => { const dropLabel = entity.type === 'view' ? 'Drop view' : 'Drop table'; const columnRows = useMemo( () => state.columns.map((column) => ({ name: column.name, type: column.type || '—', nullable: column.notNull ? 'No' : 'Yes', defaultValue: column.defaultValue ?? '—', primaryKey: column.primaryKeyOrder > 0 ? `PK ${column.primaryKeyOrder}` : '—', foreignKey: state.foreignKeys.some((foreignKey) => foreignKey.from === column.name) ? 'Yes' : '—', extra: column.hidden > 0 ? `Hidden ${column.hidden}` : '—', })), [state.columns, state.foreignKeys], ); const indexRows = useMemo( () => state.indexes.map((index) => ({ indexName: index.name, columns: index.columns.join(', ') || '—', unique: index.unique ? 'Yes' : 'No', type: `${index.origin.toUpperCase()}${index.partial ? ' · Partial' : ''}`, })), [state.indexes], ); const primaryKeyColumns = useMemo(() => getPrimaryKeyColumns(state.columns), [state.columns]); return (
onSectionChange(value as StructureSection)}> {SECTIONS.map(([key, label]) => ( {label} ))}
{error ? ( Structure Load Failed {error} ) : null}
{section === 'columns' ? ( ) : section === 'keys' ? ( loading ? ( Loading… ) : ( {primaryKeyColumns.length === 0 ? ( No primary key defined. ) : (
{primaryKeyColumns.map((column) => ( ))}
)}
{state.foreignKeys.length === 0 ? ( No foreign keys defined. ) : (
{state.foreignKeys.map((foreignKey) => (

{foreignKey.from} → {foreignKey.table} {foreignKey.to ? `.${foreignKey.to}` : ''}

Update {foreignKey.onUpdate} · Delete {foreignKey.onDelete}
Match {foreignKey.match}
))}
)}
) ) : ( )}
); };