1+ import type { PostgresTable } from '@supabase/postgres-meta'
12import { ArrowRight } from 'lucide-react'
23import type { PropsWithChildren } from 'react'
34import type { RenderCellProps } from 'react-data-grid'
45
56import { convertByteaToHex } from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils'
67import { ButtonTooltip } from 'components/ui/ButtonTooltip'
8+ import ShimmeringLoader from 'components/ui/ShimmeringLoader'
79import { useTableEditorQuery } from 'data/table-editor/table-editor-query'
810import { isTableLike } from 'data/table-editor/table-editor-types'
9- import { useTablesQuery } from 'data/tables/tables -query'
11+ import { useTablesQuery as useTableRetrieveQuery } from 'data/tables/table-retrieve -query'
1012import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
1113import { Popover_Shadcn_ , PopoverContent_Shadcn_ , PopoverTrigger_Shadcn_ } from 'ui'
1214import type { SupaRow } from '../../types'
@@ -21,7 +23,7 @@ export const ForeignKeyFormatter = (props: Props) => {
2123 const { tableId, row, column } = props
2224 const { data : project } = useSelectedProjectQuery ( )
2325
24- const { data } = useTableEditorQuery ( {
26+ const { data, isLoading } = useTableEditorQuery ( {
2527 projectRef : project ?. ref ,
2628 connectionString : project ?. connectionString ,
2729 id : tableId ,
@@ -35,17 +37,22 @@ export const ForeignKeyFormatter = (props: Props) => {
3537 r . source_table_name === selectedTable ?. name &&
3638 r . source_column_name === column . name
3739 )
38- const { data : tables } = useTablesQuery ( {
39- projectRef : project ?. ref ,
40- includeColumns : true ,
41- connectionString : project ?. connectionString ,
42- schema : relationship ?. target_table_schema ,
43- } )
44- const targetTable = tables ?. find (
45- ( table ) =>
46- table . schema === relationship ?. target_table_schema &&
47- table . name === relationship . target_table_name
48- )
40+
41+ const { data : targetTable , isLoading : isLoadingTargetTable } =
42+ useTableRetrieveQuery < PostgresTable > (
43+ {
44+ projectRef : project ?. ref ,
45+ connectionString : project ?. connectionString ,
46+ schema : relationship ?. target_table_schema ?? '' ,
47+ name : relationship ?. target_table_name ?? '' ,
48+ } ,
49+ {
50+ enabled :
51+ ! ! project ?. ref &&
52+ ! ! relationship ?. target_table_schema &&
53+ ! ! relationship ?. target_table_name ,
54+ }
55+ )
4956
5057 const value = row [ column . key ]
5158 const formattedValue =
@@ -56,25 +63,39 @@ export const ForeignKeyFormatter = (props: Props) => {
5663 < span className = "sb-grid-foreign-key-formatter__text" >
5764 { formattedValue === null ? < NullValue /> : formattedValue }
5865 </ span >
59- { relationship !== undefined && targetTable !== undefined && formattedValue !== null && (
60- < Popover_Shadcn_ >
61- < PopoverTrigger_Shadcn_ asChild >
62- < ButtonTooltip
63- type = "default"
64- className = "w-6 h-6"
65- icon = { < ArrowRight /> }
66- onClick = { ( e ) => e . stopPropagation ( ) }
67- tooltip = { { content : { side : 'bottom' , text : 'View referencing record' } } }
68- />
69- </ PopoverTrigger_Shadcn_ >
70- < PopoverContent_Shadcn_ portal align = "end" className = "p-0 w-96" >
71- < ReferenceRecordPeek
72- table = { targetTable }
73- column = { relationship . target_column_name }
74- value = { formattedValue }
75- />
76- </ PopoverContent_Shadcn_ >
77- </ Popover_Shadcn_ >
66+ { isLoading && formattedValue !== null && (
67+ < div className = "w-6 h-6 flex items-center justify-center" >
68+ < ShimmeringLoader className = "w-4 h-4" />
69+ </ div >
70+ ) }
71+ { ! isLoading && relationship !== undefined && formattedValue !== null && (
72+ < >
73+ { isLoadingTargetTable && (
74+ < div className = "w-6 h-6 flex items-center justify-center" >
75+ < ShimmeringLoader className = "w-4 h-4" />
76+ </ div >
77+ ) }
78+ { ! isLoadingTargetTable && targetTable !== undefined && (
79+ < Popover_Shadcn_ >
80+ < PopoverTrigger_Shadcn_ asChild >
81+ < ButtonTooltip
82+ type = "default"
83+ className = "w-6 h-6"
84+ icon = { < ArrowRight /> }
85+ onClick = { ( e ) => e . stopPropagation ( ) }
86+ tooltip = { { content : { side : 'bottom' , text : 'View referencing record' } } }
87+ />
88+ </ PopoverTrigger_Shadcn_ >
89+ < PopoverContent_Shadcn_ portal align = "end" className = "p-0 w-96" >
90+ < ReferenceRecordPeek
91+ table = { targetTable }
92+ column = { relationship . target_column_name }
93+ value = { formattedValue }
94+ />
95+ </ PopoverContent_Shadcn_ >
96+ </ Popover_Shadcn_ >
97+ ) }
98+ </ >
7899 ) }
79100 </ div >
80101 )
0 commit comments