import { useState, useEffect, useMemo } from 'react'; import { Button, Label, Dialog, DialogContent, DialogTitle, DialogDescription, Select, SelectContent, SelectItem, SelectTrigger, } from '@/components'; import { useTables } from '@/features/database/hooks/useTables'; import { UseFormReturn } from 'react-hook-form'; import { TableFormSchema, TableFormForeignKeySchema } from '../schema'; import { ColumnSchema, OnDeleteActionSchema, OnUpdateActionSchema } from '@insforge/shared-schemas'; import { cn } from '@/lib/utils/utils'; interface ForeignKeyPopoverProps { form: UseFormReturn; mode: 'create' | 'edit'; editTableName?: string; open: boolean; onOpenChange: (open: boolean) => void; onAddForeignKey: (fk: TableFormForeignKeySchema) => void; initialValue?: TableFormForeignKeySchema; } export function ForeignKeyPopover({ form, mode, editTableName, open, onOpenChange, onAddForeignKey, initialValue, }: ForeignKeyPopoverProps) { const [newForeignKey, setNewForeignKey] = useState({ columnName: '', referenceTable: '', referenceColumn: '', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', }); const columns = form.watch('columns'); const { tables, useTableSchema } = useTables(); // Set initial values when editing useEffect(() => { if (open && initialValue) { setNewForeignKey({ columnName: initialValue.columnName, referenceTable: initialValue.referenceTable, referenceColumn: initialValue.referenceColumn, onDelete: initialValue.onDelete, onUpdate: initialValue.onUpdate, }); } else if (!open) { // Reset when closing setNewForeignKey({ columnName: '', referenceTable: '', referenceColumn: '', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', }); } }, [open, initialValue]); // Get available tables const availableTables = tables.filter( (tableName) => mode === 'create' || tableName !== editTableName ); // Get columns for selected reference table const { data: referenceTableSchema } = useTableSchema( newForeignKey.referenceTable || '', !!newForeignKey.referenceTable && open ); // Get the type of the selected source column const getSourceFieldType = useMemo(() => { if (!newForeignKey.columnName) { return null; } const sourceColumn = columns.find((col) => col.columnName === newForeignKey.columnName); return sourceColumn?.type || null; }, [newForeignKey.columnName, columns]); // Calculate if the button should be enabled const isAddButtonEnabled = Boolean( newForeignKey.columnName && newForeignKey.referenceTable && newForeignKey.referenceColumn ); const handleAddForeignKey = () => { if (newForeignKey.columnName && newForeignKey.referenceTable && newForeignKey.referenceColumn) { onAddForeignKey(newForeignKey); setNewForeignKey({ columnName: '', referenceTable: '', referenceColumn: '', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', }); onOpenChange(false); } }; const handleCancelAddForeignKey = () => { setNewForeignKey({ columnName: '', referenceTable: '', referenceColumn: '', onDelete: 'NO ACTION', onUpdate: 'NO ACTION', }); onOpenChange(false); }; return (
{/* Header */}
{initialValue ? 'Edit Foreign Key' : 'Add Foreign Key'} {initialValue ? 'Modify the relationship between tables' : 'Create a relationship between this table and another table'}
{/* Form Content */}
{/* Column selector */}
{/* Reference Table selector */}
{/* Reference Column selector - only shown after table and source column are selected */} {newForeignKey.referenceTable && newForeignKey.columnName && (
)} {/* On Update action */}
{/* On Delete action */}
{/* Footer */}
); }