import { useQueryClient } from '@tanstack/react-query' import { Undo2 } from 'lucide-react' import { Card, CardContent, CardHeader } from 'ui' import { formatOperationItemValue } from './OperationQueueSidePanel.utils' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { tableRowKeys } from '@/data/table-rows/keys' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { useTableEditorStateSnapshot } from '@/state/table-editor' import { AddRowPayload } from '@/state/table-editor-operation-queue.types' interface AddRowOperationItemProps { operationId: string tableId: number content: AddRowPayload } export const AddRowOperationItem = ({ operationId, tableId, content, }: AddRowOperationItemProps) => { const { table, rowData } = content const tableSchema = table.schema const tableName = table.name const queryClient = useQueryClient() const { data: project } = useSelectedProjectQuery() const snap = useTableEditorStateSnapshot() const fullTableName = `${tableSchema}.${tableName}` // Get first 3 column values for preview const columns = Object.entries(rowData).filter(([key]) => !key.startsWith('__') && key !== 'idx') const previewColumns = columns.slice(0, 3) const remainingCount = columns.length - previewColumns.length const handleDelete = () => { // Remove the operation from the queue snap.removeOperation(operationId) // Invalidate the query to revert the optimistic update if (project) { queryClient.invalidateQueries({ queryKey: tableRowKeys.tableRowsAndCount(project.ref, tableId), }) } } return (
{fullTableName}
New row
} onClick={handleDelete} tooltip={{ content: { side: 'left', align: 'end', text: 'Discard change', }, }} />
{previewColumns.length === 0 && ( No data provided, default values will be used )} {previewColumns.map(([key, value]) => (
+ {key}: {formatOperationItemValue(value)}
))} {remainingCount > 0 && (
+ +{remainingCount} more column(s)
)}
) }