Skip to content

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

Add dynamic column generation for UnifiedLogs table (#37239)

Jonathan Summers-Muireb0106179ea7
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx
index 6683e4db27c..3c8ee438661 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.tsx
@@ -41,7 +41,7 @@ import {
Separator,
} from 'ui'
import { RefreshButton } from '../../ui/DataTable/RefreshButton'
-import { UNIFIED_LOGS_COLUMNS } from './components/Columns'
+import { generateDynamicColumns, UNIFIED_LOGS_COLUMNS } from './components/Columns'
import { DownloadLogsButton } from './components/DownloadLogsButton'
import { LogsListPanel } from './components/LogsListPanel'
import { ServiceFlowPanel } from './ServiceFlowPanel'
@@ -165,13 +165,18 @@ export const UnifiedLogs = () => {
return cn(levelClassName, isPast ? 'opacity-50' : 'opacity-100', 'h-[30px]')
}
+ // Generate dynamic columns based on current data
+ const { columns: dynamicColumns, columnVisibility: dynamicColumnVisibility } = useMemo(() => {
+ return generateDynamicColumns(flatData)
+ }, [flatData])
+
const table: Table<any> = useReactTable({
data: flatData,
- columns: UNIFIED_LOGS_COLUMNS,
+ columns: dynamicColumns,
state: {
columnFilters,
sorting,
- columnVisibility,
+ columnVisibility: { ...columnVisibility, ...dynamicColumnVisibility },
rowSelection,
columnOrder,
},
diff --git a/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx b/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx
index b8e1863fa82..421bcd8935e 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx
+++ b/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx
@@ -10,176 +10,215 @@ import { HoverCardTimestamp } from './HoverCardTimestamp'
import { LogTypeIcon } from './LogTypeIcon'
import { TextWithTooltip } from './TextWithTooltip'
-export const UNIFIED_LOGS_COLUMNS: ColumnDef<ColumnSchema>[] = [
- {
- accessorKey: 'level',
- header: '',
- cell: ({ row }) => {
- const level = row.getValue<ColumnSchema['level']>('level')
- return <DataTableColumnLevelIndicator value={level} />
- },
- enableHiding: false,
- enableResizing: false,
- enableSorting: false,
- filterFn: (row, columnId, filterValue) => true,
- size: 48,
- minSize: 48,
- maxSize: 48,
- meta: {
- headerClassName:
- 'w-[--header-level-size] max-w-[--header-level-size] min-w-[--header-level-size]',
- cellClassName: 'w-[--col-level-size] max-w-[--col-level-size] min-w-[--col-level-size]',
- },
- },
- {
- accessorKey: 'date',
- header: ({ column }) => <DataTableColumnHeader column={column} title="Date" />,
- cell: ({ row }) => {
- const date = new Date(row.getValue<ColumnSchema['date']>('date'))
- return <HoverCardTimestamp date={date} />
- },
- filterFn: (row, columnId, filterValue) => true,
- enableResizing: false,
- enableSorting: false,
- size: 130,
- minSize: 130,
- maxSize: 130,
- meta: {
- headerClassName:
- 'w-[--header-date-size] max-w-[--header-date-size] min-w-[--header-date-size]',
- cellClassName:
- 'font-mono w-[--col-date-size] max-w-[--col-date-size] min-w-[--col-date-size]',
- },
- },
- {
- accessorKey: 'log_type',
- header: '',
- cell: ({ row }) => {
- const logType = row.getValue<ColumnSchema['log_type']>('log_type')
- return (
- <div className="flex items-center justify-end gap-1">
- <LogTypeIcon type={logType} size={16} className="text-foreground/70" />
- </div>
- )
- },
- enableHiding: false,
- filterFn: (row, columnId, filterValue) => true,
- size: 40,
- minSize: 40,
- maxSize: 40,
- enableResizing: false,
- enableSorting: false,
- meta: {
- headerClassName:
- 'w-[--header-log_type-size] max-w-[--header-log_type-size] min-w-[--header-log_type-size]',
- cellClassName:
- 'text-right px-0 relative justify-end items-center font-mono w-[--col-log_type-size] max-w-[--col-log_type-size] min-w-[--col-log_type-size]',
- },
- },
- {
- // TODO: make it a type of MethodSchema!
- accessorKey: 'method',
- header: 'Method',
- filterFn: 'arrIncludesSome',
- cell: ({ row }) => {
- const value = row.getValue<ColumnSchema['method']>('method')
- return <span className="text-foreground-lighter">{value}</span>
+/**
+ * Determines if a column should be hidden based on its values in the data.
+ * Returns true if all values for the specified columnKey are empty, null, or undefined.
+ * Returns false if data is empty or if any value is present.
+ *
+ * @param data - Array of data rows to check
+ * @param columnKey - The key of the column to check for emptiness
+ * @returns boolean - true if column should be hidden, false otherwise
+ */
+function shouldHideColumn(data: ColumnSchema[], columnKey: keyof ColumnSchema): boolean {
+ // If there is no data or the data array is empty, do not hide the column
+ if (!data || data.length === 0) return false
+
+ // Check if every row has an empty, null, or undefined value for the given columnKey
+ return data.every((row) => {
+ const value = row[columnKey]
+ return !value || value === '' || value === null || value === undefined
+ })
+}
+
+// Generate dynamic columns based on data
+export function generateDynamicColumns(data: ColumnSchema[]): {
+ columns: ColumnDef<ColumnSchema>[]
+ columnVisibility: Record<string, boolean>
+} {
+ const hideMethod = shouldHideColumn(data, 'method')
+ const hidePathname = shouldHideColumn(data, 'pathname')
+ const hideEventMessage = shouldHideColumn(data, 'event_message')
+
+ const columns: ColumnDef<ColumnSchema>[] = [
+ // Level column - always visible
+ {
+ accessorKey: 'level',
+ header: '',
+ cell: ({ row }) => {
+ const level = row.getValue<ColumnSchema['level']>('level')
+ return <DataTableColumnLevelIndicator value={level} />
+ },
+ enableHiding: false,
+ enableResizing: false,
+ enableSorting: false,
+ filterFn: (row, columnId, filterValue) => true,
+ size: 48,
+ minSize: 48,
+ maxSize: 48,
+ meta: {
+ cellClassName: 'w-[32px]',
+ headerClassName: 'w-[32px]',
+ },
},
- enableResizing: false,
- enableSorting: false,
- size: 70,
- minSize: 70,
- meta: {
- cellClassName:
- 'font-mono text-muted-foreground w-[--col-method-size] max-w-[--col-method-size] min-w-[--col-method-size]',
- headerClassName:
- 'w-[--header-method-size] max-w-[--header-method-size] min-w-[--header-method-size]',
+ // Date column - always visible
+ {
+ accessorKey: 'date',
+ header: ({ column }) => <DataTableColumnHeader column={column} title="Date" />,
+ cell: ({ row }) => {
+ const date = new Date(row.getValue<ColumnSchema['date']>('date'))
+ return <HoverCardTimestamp date={date} />
+ },
+ filterFn: (row, columnId, filterValue) => true,
+ enableResizing: false,
+ enableSorting: false,
+ size: 130,
+ minSize: 130,
+ maxSize: 130,
+ meta: {
+ cellClassName: 'font-mono w-[130px]',
+ headerClassName: 'w-[130px]',
+ },
},
- },
- {
- accessorKey: 'status',
- header: '',
- cell: ({ row }) => {
- const value = row.getValue<ColumnSchema['status']>('status')
- return (
- <div className="flex items-center gap-1">
- {row.original.auth_user && (
- <div>
- <AuthUserHoverCard authUser={row.original.auth_user} />
- </div>
- )}
- <DataTableColumnStatusCode
- value={value}
- level={row.getValue<ColumnSchema['level']>('level')}
- />
- </div>
- )
+ // Log type column - always visible
+ {
+ accessorKey: 'log_type',
+ header: '',
+ cell: ({ row }) => {
+ const logType = row.getValue<ColumnSchema['log_type']>('log_type')
+ return (
+ <div className="flex items-center justify-end gap-1">
+ <LogTypeIcon type={logType} size={16} className="text-foreground/70" />
+ </div>
+ )
+ },
+ enableHiding: false,
+ filterFn: (row, columnId, filterValue) => true,
+ enableResizing: false,
+ enableSorting: false,
+ size: 40,
+ minSize: 40,
+ maxSize: 40,
+ meta: {
+ cellClassName: 'w-[32px]',
+ headerClassName: 'w-[32px]',
+ },
},
- filterFn: (row, columnId, filterValue) => true,
- enableResizing: false,
- enableSorting: false,
- size: 55,
- minSize: 55,
- meta: {
- headerClassName:
- 'w-[--header-status-size] max-w-[--header-status-size] min-w-[--header-status-size]',
- cellClassName:
- 'font-mono w-[--col-status-size] max-w-[--col-status-size] min-w-[--col-status-size]',
+ // Status column - always visible
+ {
+ accessorKey: 'status',
+ header: '',
+ cell: ({ row }) => {
+ const value = row.getValue<ColumnSchema['status']>('status')
+ return (
+ <div className="flex items-center gap-1">
+ {/* {row.original.auth_user && (
+ // to do: add auth user hover card
+ <div>
+ <AuthUserHoverCard authUser={row.original.auth_user} />
+ </div>
+ )} */}
+ <DataTableColumnStatusCode
+ value={value}
+ level={row.getValue<ColumnSchema['level']>('level')}
+ />
+ </div>
+ )
+ },
+ filterFn: (row, columnId, filterValue) => true,
+ enableResizing: false,
+ enableSorting: false,
+ size: 70,
+ minSize: 70,
+ maxSize: 70,
+ meta: {
+ cellClassName: 'w-[70px]',
+ headerClassName: 'w-[70px]',
+ },
},
- },
- {
- accessorKey: 'pathname',
- header: 'Pathname',
- cell: ({ row }) => {
- const value = row.getValue<ColumnFilterSchema['pathname']>('pathname') ?? ''
- return <TextWithTooltip text={value} />
+ // Method column - controlled by columnVisibility
+ {
+ accessorKey: 'method',
+ header: 'Method',
+ filterFn: 'arrIncludesSome',
+ cell: ({ row }) => {
+ const value = row.getValue<ColumnSchema['method']>('method')
+ return <span className="text-foreground-lighter">{value}</span>
+ },
+ enableResizing: false,
+ enableSorting: false,
+ size: 70,
+ minSize: 70,
+ maxSize: 70,
+ meta: {
+ cellClassName: 'w-[70px]',
+ headerClassName: 'w-[70px]',
+ },
},
- enableSorting: false,
- size: 320,
- minSize: 320,
- meta: {
- cellClassName: 'font-mono w-[--col-pathname-size] max-w-[--col-pathname-size]',
- headerClassName: 'min-w-[--header-pathname-size] w-[--header-pathname-size]',
+ // Pathname column - controlled by columnVisibility
+ {
+ accessorKey: 'pathname',
+ header: 'Pathname',
+ cell: ({ row }) => {
+ const value = row.getValue<ColumnFilterSchema['pathname']>('pathname') ?? ''
+ return <TextWithTooltip text={value} />
+ },
+ enableSorting: false,
+ enableResizing: false,
+ size: 200,
+ minSize: 200,
+ maxSize: 200,
+ meta: {
+ cellClassName: 'max-w-[320px]',
+ headerClassName: 'max-w-[320px]',
+ },
},
- },
- {
- accessorKey: 'event_message',
- header: ({ column }) => <DataTableColumnHeader column={column} title="event_message" />,
- cell: ({ row }) => {
- const value = row.getValue<ColumnSchema['event_message']>('event_message')
- const logCount = row.original.log_count
+ // Event message column - controlled by columnVisibility
+ {
+ accessorKey: 'event_message',
+ header: ({ column }) => <DataTableColumnHeader column={column} title="Event message" />,
+ cell: ({ row }) => {
+ const value = row.getValue<ColumnSchema['event_message']>('event_message')
+ const logCount = row.original.log_count
- return (
- <div className="flex flex-row gap-2 items-center">
- {logCount && (
- <Tooltip>
- <TooltipTrigger>
- <span className="text-foreground-lighter bg-surface-400 px-[6px] py-[2px] h-fit rounded-md text-[0.8rem] leading-none">
- {logCount}
- </span>
- </TooltipTrigger>
- <TooltipContent>
- {logCount} {logCount === 1 ? 'log' : 'logs'} for this execution
- </TooltipContent>
- </Tooltip>
- )}
- {value && (
- <span className="text-muted-foreground">
- <TextWithTooltip text={value} />
- </span>
- )}
- </div>
- )
- },
- enableResizing: true,
- enableSorting: false,
- size: 200,
- minSize: 200,
- meta: {
- headerClassName:
- 'w-[--header-event_message-size] max-w-[--header-event_message-size] min-w-[--header-event_message-size]',
- cellClassName:
- 'font-mono w-[--col-event_message-size] max-w-[--col-event_message-size] min-w-[--col-event_message-size]',
+ return (
+ <div className="flex flex-row gap-2 items-center">
+ {logCount && (
+ <Tooltip>
+ <TooltipTrigger>
+ <span className="text-foreground-lighter bg-surface-400 px-[6px] py-[2px] h-fit rounded-md text-[0.8rem] leading-none">
+ {logCount}
+ </span>
+ </TooltipTrigger>
+ <TooltipContent>
+ {logCount} {logCount === 1 ? 'log' : 'logs'} for this execution
+ </TooltipContent>
+ </Tooltip>
+ )}
+ {value && (
+ <span className="text-muted-foreground">
+ <TextWithTooltip text={value} />
+ </span>
+ )}
+ </div>
+ )
+ },
+ enableResizing: true,
+ enableSorting: false,
+ size: 200,
+ minSize: 200,
+ maxSize: 400,
},
- },
-]
+ ]
+
+ // Define column visibility based on data
+ const columnVisibility = {
+ method: !hideMethod,
+ pathname: !hidePathname,
+ event_message: !hideEventMessage,
+ }
+
+ return { columns, columnVisibility }
+}
+
+// Static fallback columns
+export const UNIFIED_LOGS_COLUMNS: ColumnDef<ColumnSchema>[] = generateDynamicColumns([]).columns