@@ -3,7 +3,7 @@ import { PostgresTable } from '@gregnr/postgres-meta/base'
3
3
import { uniqBy } from 'lodash'
4
4
import { Info , Loader } from 'lucide-react'
5
5
import { useTheme } from 'next-themes'
6
- import { useCallback , useEffect , useMemo } from 'react'
6
+ import { useCallback , useEffect , useMemo , useState } from 'react'
7
7
import ReactFlow , {
8
8
Background ,
9
9
BackgroundVariant ,
@@ -31,10 +31,11 @@ export default function TablesGraph({
31
31
} ) {
32
32
const { resolvedTheme } = useTheme ( )
33
33
const { visibility } = useWorkspace ( )
34
+ const [ isFirstLoad , setIsFirstLoad ] = useState ( true )
34
35
35
36
const { data : allTables , error, isError, isLoading } = useTablesQuery ( { databaseId, schemas } )
36
37
37
- const tables = allTables ?. filter ( ( table ) => table . schema === 'public' )
38
+ const tables = useMemo ( ( ) => allTables ?. filter ( ( table ) => table . schema === 'public' ) , [ allTables ] )
38
39
39
40
const isEmpty = tables && tables . length === 0
40
41
@@ -52,24 +53,28 @@ export default function TablesGraph({
52
53
[ ]
53
54
)
54
55
55
- const fitView = useCallback ( ( ) => {
56
- reactFlowInstance . fitView ( {
57
- padding : 0.4 ,
58
- duration : 500 ,
59
- } )
60
- } , [ reactFlowInstance ] )
56
+ const fitView = useCallback (
57
+ ( duration = 500 ) => {
58
+ reactFlowInstance . fitView ( {
59
+ padding : 0.4 ,
60
+ duration,
61
+ } )
62
+ } ,
63
+ [ reactFlowInstance ]
64
+ )
61
65
62
66
useEffect ( ( ) => {
63
- if ( tables ) {
67
+ if ( tables && tables . length > 0 ) {
64
68
getGraphDataFromTables ( tables ) . then ( ( { nodes, edges } ) => {
65
69
reactFlowInstance . setNodes ( nodes )
66
70
reactFlowInstance . setEdges ( edges )
67
71
68
- // it needs to happen during next event tick
69
- setTimeout ( ( ) => fitView ( ) , 100 )
72
+ // `fitView` needs to happen during next event tick
73
+ setTimeout ( ( ) => fitView ( isFirstLoad ? 0 : 500 ) , 0 )
74
+ setIsFirstLoad ( false )
70
75
} )
71
76
}
72
- } , [ reactFlowInstance , tables , resolvedTheme , fitView ] )
77
+ } , [ reactFlowInstance , tables , resolvedTheme , fitView , isFirstLoad ] )
73
78
74
79
return (
75
80
< div className = "flex flex-col w-full h-full bg-neutral-800 rounded-md border-[0.5px] border-neutral-800 overflow-hidden" >
0 commit comments