Skip to content

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

feat(studio): show expand affordance for large SQL result cells (#45589)

Ali Waseem153f2619bc04
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ResultCell.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ResultCell.tsx
new file mode 100644
index 00000000000..7148af3b016
--- /dev/null
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/ResultCell.tsx
@@ -0,0 +1,48 @@
+import { Expand } from 'lucide-react'
+import { Button, cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
+
+import { formatCellValue, isLargeValue } from './Results.utils'
+
+interface ResultCellProps {
+ column: string
+ value: unknown
+ onContextMenu: (e: React.MouseEvent, column: string, value: unknown) => void
+ onExpand: (column: string, value: unknown) => void
+}
+
+export const ResultCell = ({ column, value, onContextMenu, onExpand }: ResultCellProps) => {
+ const showExpand = isLargeValue(value)
+
+ return (
+ <div
+ className={cn(
+ 'group/cell relative flex items-center h-full font-mono text-xs w-full whitespace-pre',
+ value === null && 'text-foreground-lighter'
+ )}
+ onContextMenu={(e) => {
+ e.preventDefault()
+ onContextMenu(e, column, value)
+ }}
+ >
+ {formatCellValue(value)}
+ {showExpand && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <Button
+ type="default"
+ size="tiny"
+ className="absolute right-1 top-1/2 -translate-y-1/2 px-1 opacity-0 group-hover/cell:opacity-100 focus-visible:opacity-100"
+ icon={<Expand size={10} />}
+ aria-label="View full cell content"
+ onClick={(e) => {
+ e.stopPropagation()
+ onExpand(column, value)
+ }}
+ />
+ </TooltipTrigger>
+ <TooltipContent side="left">View full cell content</TooltipContent>
+ </Tooltip>
+ )}
+ </div>
+ )
+}
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
index 1199a92463b..7e5a34b449d 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
@@ -2,7 +2,6 @@ import { Copy, Expand } from 'lucide-react'
import { useCallback, useMemo, useRef, useState } from 'react'
import DataGrid, { CalculatedColumn } from 'react-data-grid'
import {
- cn,
ContextMenu_Shadcn_,
ContextMenuContent_Shadcn_,
ContextMenuItem_Shadcn_,
@@ -11,12 +10,12 @@ import {
} from 'ui'
import { CellDetailPanel } from './CellDetailPanel'
-import { formatCellValue, formatClipboardValue } from './Results.utils'
+import { ResultCell } from './ResultCell'
+import { formatClipboardValue } from './Results.utils'
import { handleCellKeyDown } from '@/components/grid/SupabaseGrid.utils'
export const Results = ({ rows }: { rows: readonly any[] }) => {
- const [expandCell, setExpandCell] = useState(false)
- const [cellPosition, setCellPosition] = useState<{ column: any; row: any; rowIdx: number }>()
+ const [expandedCell, setExpandedCell] = useState<{ column: string; value: any } | null>(null)
const contextMenuCellRef = useRef<{ column: string; value: any } | null>(null)
const triggerRef = useRef<HTMLDivElement>(null)
@@ -72,23 +71,14 @@ export const Results = ({ rows }: { rows: readonly any[] }) => {
frozen: false,
sortable: false,
isLastFrozenColumn: false,
- renderCell: ({ row }: { row: any }) => {
- const cellValue = row[key]
- return (
- <div
- className={cn(
- 'flex items-center h-full font-mono text-xs w-full whitespace-pre',
- cellValue === null && 'text-foreground-lighter'
- )}
- onContextMenu={(e) => {
- e.preventDefault()
- handleContextMenu(e, key, cellValue)
- }}
- >
- {formatCellValue(cellValue)}
- </div>
- )
- },
+ renderCell: ({ row }: { row: any }) => (
+ <ResultCell
+ column={key}
+ value={row[key]}
+ onContextMenu={handleContextMenu}
+ onExpand={(column, value) => setExpandedCell({ column, value })}
+ />
+ ),
renderHeaderCell: () => columnRender(key),
}
}),
@@ -123,7 +113,10 @@ export const Results = ({ rows }: { rows: readonly any[] }) => {
</ContextMenuItem_Shadcn_>
<ContextMenuItem_Shadcn_
className="gap-x-2"
- onSelect={() => setExpandCell(true)}
+ onSelect={() => {
+ const cell = contextMenuCellRef.current
+ if (cell) setExpandedCell({ column: cell.column, value: cell.value })
+ }}
onFocusCapture={(e) => e.stopPropagation()}
>
<Expand size={12} />
@@ -136,14 +129,13 @@ export const Results = ({ rows }: { rows: readonly any[] }) => {
rows={rows}
className="grow min-h-0 border-t-0"
rowClass={() => '[&>.rdg-cell]:items-center'}
- onSelectedCellChange={setCellPosition}
onCellKeyDown={handleCellKeyDown}
/>
<CellDetailPanel
- column={cellPosition?.column.name ?? ''}
- value={cellPosition?.row?.[cellPosition.column.name]}
- visible={expandCell}
- onClose={() => setExpandCell(false)}
+ column={expandedCell?.column ?? ''}
+ value={expandedCell?.value}
+ visible={expandedCell !== null}
+ onClose={() => setExpandedCell(null)}
/>
</>
)}
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.test.ts b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.test.ts
index f1fb5d068fa..020a9667b88 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.test.ts
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.test.ts
@@ -6,6 +6,7 @@ import {
convertResultsToMarkdown,
formatResults,
getResultsHeaders,
+ isLargeValue,
} from './Results.utils'
describe('Results.utils', () => {
@@ -102,6 +103,52 @@ describe('Results.utils', () => {
})
})
+ describe('isLargeValue', () => {
+ it('returns false for null', () => {
+ expect(isLargeValue(null)).toBe(false)
+ })
+
+ it('returns false for undefined', () => {
+ expect(isLargeValue(undefined)).toBe(false)
+ })
+
+ it('returns false for an empty string', () => {
+ expect(isLargeValue('')).toBe(false)
+ })
+
+ it('returns false for a short string under the threshold', () => {
+ expect(isLargeValue('hello')).toBe(false)
+ })
+
+ it('returns false for a string at the 60-char boundary', () => {
+ expect(isLargeValue('a'.repeat(60))).toBe(false)
+ })
+
+ it('returns true for a string just over the 60-char threshold', () => {
+ expect(isLargeValue('a'.repeat(61))).toBe(true)
+ })
+
+ it('returns true for a short string containing a newline', () => {
+ expect(isLargeValue('hello\nworld')).toBe(true)
+ })
+
+ it('returns true for an object', () => {
+ expect(isLargeValue({ a: 1 })).toBe(true)
+ })
+
+ it('returns true for an array', () => {
+ expect(isLargeValue([1, 2, 3])).toBe(true)
+ })
+
+ it('returns false for a number', () => {
+ expect(isLargeValue(42)).toBe(false)
+ })
+
+ it('returns false for a boolean', () => {
+ expect(isLargeValue(true)).toBe(false)
+ })
+ })
+
describe('convertResultsToCSV', () => {
it('should return undefined for empty results', () => {
expect(convertResultsToCSV([])).toBeUndefined()
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts
index 43a2542b0e1..ca19a5763ec 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts
@@ -17,6 +17,15 @@ export function formatCellValue(value: unknown) {
return JSON.stringify(value)
}
+const LARGE_VALUE_CHAR_THRESHOLD = 60
+
+export function isLargeValue(value: unknown) {
+ if (value === null || value === undefined) return false
+ if (typeof value === 'object') return true
+ const str = String(value)
+ return str.length > LARGE_VALUE_CHAR_THRESHOLD || str.includes('\n')
+}
+
export function formatResults(
results: ResultRow[]
): Record<string, string | number | boolean | null | undefined>[] {
diff --git a/apps/studio/tests/components/SQLEditor/ResultCell.test.tsx b/apps/studio/tests/components/SQLEditor/ResultCell.test.tsx
new file mode 100644
index 00000000000..353c9b85877
--- /dev/null
+++ b/apps/studio/tests/components/SQLEditor/ResultCell.test.tsx
@@ -0,0 +1,56 @@
+import { fireEvent, screen } from '@testing-library/react'
+import userEvent from '@testing-library/user-event'
+import { expect, test, vi } from 'vitest'
+
+import { ResultCell } from '@/components/interfaces/SQLEditor/UtilityPanel/ResultCell'
+import { customRender as render } from '@/tests/lib/custom-render'
+
+const noop = () => {}
+
+test('renders the formatted cell value', () => {
+ render(<ResultCell column="name" value="alice" onContextMenu={noop} onExpand={noop} />)
+ expect(screen.getByText('alice')).toBeTruthy()
+})
+
+test('renders NULL for null values', () => {
+ render(<ResultCell column="name" value={null} onContextMenu={noop} onExpand={noop} />)
+ expect(screen.getByText('NULL')).toBeTruthy()
+})
+
+test('does not render the expand button for short string values', () => {
+ render(<ResultCell column="name" value="alice" onContextMenu={noop} onExpand={noop} />)
+ expect(screen.queryByRole('button', { name: 'View full cell content' })).toBeNull()
+})
+
+test('renders the expand button for object values', () => {
+ render(<ResultCell column="data" value={{ nested: true }} onContextMenu={noop} onExpand={noop} />)
+ expect(screen.getByRole('button', { name: 'View full cell content' })).toBeTruthy()
+})
+
+test('renders the expand button for long string values', () => {
+ render(<ResultCell column="bio" value={'a'.repeat(120)} onContextMenu={noop} onExpand={noop} />)
+ expect(screen.getByRole('button', { name: 'View full cell content' })).toBeTruthy()
+})
+
+test('clicking the expand button calls onExpand with column and value', async () => {
+ const onExpand = vi.fn()
+ const value = { nested: true }
+ render(<ResultCell column="data" value={value} onContextMenu={noop} onExpand={onExpand} />)
+
+ await userEvent.click(screen.getByRole('button', { name: 'View full cell content' }))
+
+ expect(onExpand).toHaveBeenCalledTimes(1)
+ expect(onExpand).toHaveBeenCalledWith('data', value)
+})
+
+test('right-clicking the cell calls onContextMenu with column and value', () => {
+ const onContextMenu = vi.fn()
+ render(<ResultCell column="name" value="alice" onContextMenu={onContextMenu} onExpand={noop} />)
+
+ fireEvent.contextMenu(screen.getByText('alice'))
+
+ expect(onContextMenu).toHaveBeenCalledTimes(1)
+ const [, column, value] = onContextMenu.mock.calls[0]
+ expect(column).toBe('name')
+ expect(value).toBe('alice')
+})