Skip to content

Commit ac6db41

Browse files
authored
Fix: Make the FK editor faster when loading tables (supabase#40259)
* updated selector code to be more efficient * updated styling to show a cleaner table loader * updated format assignment * disable selector when loading * updated request to fetch tables * updated key rendering
1 parent e456054 commit ac6db41

File tree

4 files changed

+315
-259
lines changed

4 files changed

+315
-259
lines changed

apps/studio/components/grid/components/formatter/ForeignKeyFormatter.tsx

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import type { PostgresTable } from '@supabase/postgres-meta'
12
import { ArrowRight } from 'lucide-react'
23
import type { PropsWithChildren } from 'react'
34
import type { RenderCellProps } from 'react-data-grid'
45

56
import { convertByteaToHex } from 'components/interfaces/TableGridEditor/SidePanelEditor/RowEditor/RowEditor.utils'
67
import { ButtonTooltip } from 'components/ui/ButtonTooltip'
8+
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
79
import { useTableEditorQuery } from 'data/table-editor/table-editor-query'
810
import { 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'
1012
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
1113
import { Popover_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_ } from 'ui'
1214
import 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
)

apps/studio/components/interfaces/TableGridEditor/SidePanelEditor/ColumnEditor/ColumnForeignKey.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const ColumnForeignKey = ({
4747
return {
4848
id: c.id,
4949
name: c.name,
50-
format: column.format || c.format,
50+
format: c.format || column.format,
5151
isNewColumn: false,
5252
}
5353
})

0 commit comments

Comments
 (0)