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 { DeleteRowPayload } from '@/state/table-editor-operation-queue.types' interface DeleteRowOperationItemProps { operationId: string tableId: number content: DeleteRowPayload } export const DeleteRowOperationItem = ({ operationId, tableId, content, }: DeleteRowOperationItemProps) => { const { table, rowIdentifiers } = content const tableSchema = table.schema const tableName = table.name const queryClient = useQueryClient() const { data: project } = useSelectedProjectQuery() const snap = useTableEditorStateSnapshot() const fullTableName = `${tableSchema}.${tableName}` const whereClause = Object.entries(rowIdentifiers) .map(([key, value]) => `${key} = ${formatOperationItemValue(value)}`) .join(', ') 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}
Delete row ยท where {whereClause}
} onClick={handleDelete} tooltip={{ content: { side: 'left', align: 'end', text: 'Discard change', }, }} />
Row will be deleted
) }