The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Support peeking referencing record from table editor (#33141)
Joshen Limdbafc765bab5
diff --git a/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx b/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsxindex d5ec8d48b17..4f4f69530da 100644--- a/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx+++ b/apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx@@ -3,14 +3,15 @@ import type { PropsWithChildren } from 'react'import type { RenderCellProps } from 'react-data-grid'import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'-import { EditorTablePageLink } from 'data/prefetchers/project.$ref.editor.$id'+import { ButtonTooltip } from 'components/ui/ButtonTooltip'import { useTableEditorQuery } from 'data/table-editor/table-editor-query'import { isTableLike } from 'data/table-editor/table-editor-types'import { useTablesQuery } from 'data/tables/tables-query'import { useQuerySchemaState } from 'hooks/misc/useSchemaQueryState'-import { Button, Tooltip, TooltipContent, TooltipTrigger } from 'ui'+import { Popover_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_ } from 'ui'import type { SupaRow } from '../../types'import { NullValue } from '../common/NullValue'+import { ReferenceRecordPeek } from './ReferenceRecordPeek'interface Props extends PropsWithChildren<RenderCellProps<SupaRow, unknown>> {projectRef?: string@@ -39,6 +40,7 @@ export const ForeignKeyFormatter = (props: Props) => {)const { data: tables } = useTablesQuery({projectRef: project?.ref,+ includeColumns: true,connectionString: project?.connectionString,schema: relationship?.target_table_schema,})@@ -56,33 +58,24 @@ export const ForeignKeyFormatter = (props: Props) => {{value === null ? <NullValue /> : value}</span>{relationship !== undefined && targetTable !== undefined && value !== null && (- <Tooltip>- <TooltipTrigger asChild>- <Button- asChild+ <Popover_Shadcn_>+ <PopoverTrigger_Shadcn_ asChild>+ <ButtonTooltiptype="default"- size="tiny"- className="translate-y-[2px]"- style={{ padding: '3px' }}- >- <EditorTablePageLink- href={`/project/${projectRef}/editor/${targetTable?.id}?schema=${selectedSchema}&filter=${relationship?.target_column_name}%3Aeq%3A${value}`}- projectRef={projectRef}- id={targetTable && String(targetTable?.id)}- filters={[- {- column: relationship.target_column_name,- operator: '=',- value: String(value),- },- ]}- >- <ArrowRight size={14} />- </EditorTablePageLink>- </Button>- </TooltipTrigger>- <TooltipContent side="bottom">View referencing record</TooltipContent>- </Tooltip>+ className="w-6 h-6"+ icon={<ArrowRight />}+ onClick={(e) => e.stopPropagation()}+ tooltip={{ content: { side: 'bottom', text: 'View referencing record' } }}+ />+ </PopoverTrigger_Shadcn_>+ <PopoverContent_Shadcn_ align="end" className="p-0 w-96">+ <ReferenceRecordPeek+ table={targetTable}+ column={relationship.target_column_name}+ value={value}+ />+ </PopoverContent_Shadcn_>+ </Popover_Shadcn_>)}</div>)diff --git a/apps/studio/components/grid/components/formatter/ReferenceRecordPeek.tsx b/apps/studio/components/grid/components/formatter/ReferenceRecordPeek.tsxnew file mode 100644index 00000000000..e808d084390--- /dev/null+++ b/apps/studio/components/grid/components/formatter/ReferenceRecordPeek.tsx@@ -0,0 +1,139 @@+import { PostgresTable } from '@supabase/postgres-meta'+import { Key } from 'lucide-react'+import DataGrid, { Column } from 'react-data-grid'++import { useParams } from 'common'+import { COLUMN_MIN_WIDTH } from 'components/grid/constants'+import {+ ESTIMATED_CHARACTER_PIXEL_WIDTH,+ getColumnDefaultWidth,+} from 'components/grid/utils/gridColumns'+import { EditorTablePageLink } from 'data/prefetchers/project.$ref.editor.$id'+import { useTableRowsQuery } from 'data/table-rows/table-rows-query'+import { useSelectedProject } from 'hooks/misc/useSelectedProject'+import { Button, cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'+import ShimmeringLoader from 'ui-patterns/ShimmeringLoader'++interface ReferenceRecordPeekProps {+ table: PostgresTable+ column: string+ value: any+}++export const ReferenceRecordPeek = ({ table, column, value }: ReferenceRecordPeekProps) => {+ const { ref } = useParams()+ const project = useSelectedProject()++ const { data, error, isSuccess, isError, isLoading } = useTableRowsQuery(+ {+ projectRef: project?.ref,+ connectionString: project?.connectionString,+ tableId: table.id,+ filters: [{ column, operator: '=', value }],+ page: 1,+ limit: 10,+ },+ { keepPreviousData: true }+ )++ const primaryKeys = table.primary_keys.map((x) => x.name)++ const columns = (table?.columns ?? []).map((column) => {+ const columnDefaultWidth = getColumnDefaultWidth({+ dataType: column.data_type,+ format: column.format,+ } as any)+ const columnWidthBasedOnName =+ (column.name.length + column.format.length) * ESTIMATED_CHARACTER_PIXEL_WIDTH+ const columnWidth =+ columnDefaultWidth < columnWidthBasedOnName ? columnWidthBasedOnName : columnDefaultWidth+ const isPrimaryKey = primaryKeys.includes(column.name)++ const res: Column<any> = {+ key: column.name,+ name: column.name,+ resizable: false,+ draggable: false,+ sortable: false,+ width: columnWidth,+ minWidth: COLUMN_MIN_WIDTH,+ headerCellClass: 'outline-none !shadow-none',+ renderHeaderCell: () => (+ <div className="flex h-full items-center justify-center gap-x-2">+ {isPrimaryKey && (+ <Tooltip>+ <TooltipTrigger>+ <Key size={14} strokeWidth={2} className="text-brand rotate-45" />+ </TooltipTrigger>+ <TooltipContent side="bottom">Primary key</TooltipContent>+ </Tooltip>+ )}+ <span className="text-xs truncate">{column.name}</span>+ <span className="text-xs text-foreground-light font-normal">{column.format}</span>+ </div>+ ),+ renderCell: ({ column, row }) => {+ const value = row[column.name as any]+ return (+ <div+ className={cn(+ 'flex items-center h-full w-full whitespace-pre',+ value === null && 'text-foreground-lighter'+ )}+ >+ {value === null ? 'NULL' : value}+ </div>+ )+ },+ }+ return res+ })++ return (+ <>+ <p className="px-2 py-2 text-xs text-foreground-light border-b">+ Referencing record from{' '}+ <span className="text-foreground">+ {table.schema}.{table.name}+ </span>+ :+ </p>+ <DataGrid+ className="h-32 rounded-b border-0"+ columns={columns}+ rows={data?.rows ?? []}+ onCellDoubleClick={(_, e) => {+ e.preventDefault()+ e.stopPropagation()+ }}+ renderers={{+ noRowsFallback: (+ <div className="w-96 px-2">+ {isLoading && (+ <div className="py-2">+ <ShimmeringLoader />+ </div>+ )}+ {isError && (+ <p className="text-foreground-light">+ Failed to find referencing row: {error.message}+ </p>+ )}+ {isSuccess && <p className="text-foreground-light">No results were returned</p>}+ </div>+ ),+ }}+ />+ <div className="flex items-center justify-end px-2 py-1">+ <EditorTablePageLink+ href={`/project/${ref}/editor/${table.id}?schema=${table.schema}&filter=${column}%3Aeq%3A${value}`}+ projectRef={ref}+ id={String(table.id)}+ filters={[{ column, operator: '=', value: String(value) }]}+ >+ <Button type="default">Open table</Button>+ </EditorTablePageLink>+ </div>+ </>+ )+}