Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

3.2 KB
Raw Blame
import { useQueryClient } from '@tanstack/react-query'
import { Undo2Icon } 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 { EditCellContentPayload } from '@/state/table-editor-operation-queue.types'

interface OperationItemProps {
  operationId: string
  tableId: number
  content: EditCellContentPayload
}

export const OperationItem = ({ operationId, tableId, content }: OperationItemProps) => {
  const { table, columnName, oldValue, newValue, 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 formattedOldValue = formatOperationItemValue(oldValue)
  const formattedNewValue = formatOperationItemValue(newValue)

  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 (
    <Card className="overflow-hidden">
      <CardHeader className="py-2 px-3 flex flex-row gap-2 space-y-0 items-center">
        <div className="min-w-0 flex-1">
          <code className="dark:bg-surface-300 dark:border-foreground-muted/50">
            {fullTableName}
          </code>
          <div className="text-xs text-foreground mt-1 ml-0.5">
            <span>{columnName}</span>
            <span className="text-foreground-muted mx-1.5">·</span>
            <span>where {whereClause}</span>
          </div>
        </div>
        <ButtonTooltip
          variant="text"
          aria-label="Discard change"
          className="px-1.5"
          icon={<Undo2Icon />}
          onClick={handleDelete}
          tooltip={{
            content: {
              side: 'left',
              align: 'end',
              text: 'Discard change',
            },
          }}
        />
      </CardHeader>

      <CardContent className="py-2 px-3 font-mono text-xs">
        <div className="flex gap-2 py-0.5">
          <span className="text-destructive select-none font-medium">-</span>
          <span className="text-destructive truncate max-w-full" title={formattedOldValue}>
            {formattedOldValue}
          </span>
        </div>

        <div className="flex gap-2 py-0.5">
          <span className="text-primary select-none font-medium">+</span>
          <span className="text-primary truncate max-w-full" title={formattedNewValue}>
            {formattedNewValue}
          </span>
        </div>
      </CardContent>
    </Card>
  )
}