Skip to content

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

fix(studio): account for SQL result column headers (#48676)

Hoonceace2e90b93
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
index 49f6883ea8e..393f243f8e0 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.tsx
@@ -11,7 +11,11 @@ import {
import { CellDetailPanel } from './CellDetailPanel'
import { ResultCell } from './ResultCell'
-import { formatClipboardValue } from './Results.utils'
+import {
+ calculateResultColumnWidth,
+ formatClipboardValue,
+ RESULT_COLUMN_MIN_WIDTH,
+} from './Results.utils'
import { handleCellKeyDown } from '@/components/grid/SupabaseGrid.utils'
export const Results = ({ rows }: { rows: readonly any[] }) => {
@@ -41,22 +45,9 @@ export const Results = ({ rows }: { rows: readonly any[] }) => {
return <div className="flex h-full items-center justify-center font-mono text-xs">{name}</div>
}
- const EST_CHAR_WIDTH = 8.25
- const MIN_COLUMN_WIDTH = 100
- const MAX_COLUMN_WIDTH = 500
-
const columns: CalculatedColumn<any>[] = useMemo(
() =>
Object.keys(rows?.[0] ?? []).map((key, idx) => {
- const maxColumnValueLength = rows
- .map((row) => String(row[key]).length)
- .reduce((a, b) => Math.max(a, b), 0)
-
- const columnWidth = Math.max(
- Math.min(maxColumnValueLength * EST_CHAR_WIDTH, MAX_COLUMN_WIDTH),
- MIN_COLUMN_WIDTH
- )
-
return {
idx,
key,
@@ -64,8 +55,8 @@ export const Results = ({ rows }: { rows: readonly any[] }) => {
resizable: true,
parent: undefined,
level: 0,
- width: columnWidth,
- minWidth: MIN_COLUMN_WIDTH,
+ width: calculateResultColumnWidth(key, rows),
+ minWidth: RESULT_COLUMN_MIN_WIDTH,
maxWidth: undefined,
draggable: false,
frozen: false,
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 6006cde9feb..88224d90764 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.test.ts
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import {
+ calculateResultColumnWidth,
convertResultsToCSV,
convertResultsToJSON,
convertResultsToMarkdown,
@@ -12,6 +13,46 @@ import {
} from './Results.utils'
describe('Results.utils', () => {
+ describe('calculateResultColumnWidth', () => {
+ it('uses the minimum width when the column name and values are short', () => {
+ expect(calculateResultColumnWidth('id', [{ id: 1 }])).toBe(100)
+ })
+
+ it('accounts for a column name that is longer than its values', () => {
+ expect(calculateResultColumnWidth('source_campaign_id', [{ source_campaign_id: null }])).toBe(
+ 148.5
+ )
+ })
+
+ it('accounts for a value that is longer than the column name', () => {
+ expect(calculateResultColumnWidth('name', [{ name: 'a'.repeat(20) }])).toBe(165)
+ })
+
+ it('accounts for the formatted JSON representation of an object value', () => {
+ expect(
+ calculateResultColumnWidth('metadata', [{ metadata: { campaign: 'a'.repeat(20) } }])
+ ).toBe(288.75)
+ })
+
+ it('accounts for the formatted JSON representation of an array value', () => {
+ expect(calculateResultColumnWidth('tags', [{ tags: ['a'.repeat(10), 'b'.repeat(10)] }])).toBe(
+ 222.75
+ )
+ })
+
+ it('caps the width when the column name exceeds the maximum', () => {
+ expect(calculateResultColumnWidth('a'.repeat(100), [])).toBe(500)
+ })
+
+ it('caps the width when a value exceeds the maximum', () => {
+ expect(calculateResultColumnWidth('value', [{ value: 'a'.repeat(100) }])).toBe(500)
+ })
+
+ it('uses the minimum width when there are no rows', () => {
+ expect(calculateResultColumnWidth('id', [])).toBe(100)
+ })
+ })
+
describe('formatClipboardValue', () => {
it('returns empty string for null', () => {
expect(formatClipboardValue(null)).toBe('')
diff --git a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts
index ca19a5763ec..fb03dc9faf2 100644
--- a/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts
+++ b/apps/studio/components/interfaces/SQLEditor/UtilityPanel/Results.utils.ts
@@ -3,6 +3,22 @@ import Papa from 'papaparse'
type ResultRow = Record<string, unknown>
+const ESTIMATED_CHARACTER_WIDTH = 8.25
+export const RESULT_COLUMN_MIN_WIDTH = 100
+const MAX_COLUMN_WIDTH = 500
+
+export function calculateResultColumnWidth(columnName: string, rows: readonly ResultRow[]) {
+ const maxContentLength = rows.reduce(
+ (maxLength, row) => Math.max(maxLength, (formatCellValue(row[columnName]) ?? '').length),
+ columnName.length
+ )
+
+ return Math.min(
+ Math.max(maxContentLength * ESTIMATED_CHARACTER_WIDTH, RESULT_COLUMN_MIN_WIDTH),
+ MAX_COLUMN_WIDTH
+ )
+}
+
export function formatClipboardValue(value: unknown) {
if (value === null) return ''
if (typeof value == 'object' || Array.isArray(value)) {