@@ -3,7 +3,7 @@ import { PostgresTable } from '@gregnr/postgres-meta/base'
33import { uniqBy } from 'lodash'
44import { Info , Loader } from 'lucide-react'
55import { useTheme } from 'next-themes'
6- import { useCallback , useEffect , useMemo } from 'react'
6+ import { useCallback , useEffect , useMemo , useState } from 'react'
77import ReactFlow , {
88 Background ,
99 BackgroundVariant ,
@@ -31,10 +31,11 @@ export default function TablesGraph({
3131} ) {
3232 const { resolvedTheme } = useTheme ( )
3333 const { visibility } = useWorkspace ( )
34+ const [ isFirstLoad , setIsFirstLoad ] = useState ( true )
3435
3536 const { data : allTables , error, isError, isLoading } = useTablesQuery ( { databaseId, schemas } )
3637
37- const tables = allTables ?. filter ( ( table ) => table . schema === 'public' )
38+ const tables = useMemo ( ( ) => allTables ?. filter ( ( table ) => table . schema === 'public' ) , [ allTables ] )
3839
3940 const isEmpty = tables && tables . length === 0
4041
@@ -52,24 +53,28 @@ export default function TablesGraph({
5253 [ ]
5354 )
5455
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+ )
6165
6266 useEffect ( ( ) => {
63- if ( tables ) {
67+ if ( tables && tables . length > 0 ) {
6468 getGraphDataFromTables ( tables ) . then ( ( { nodes, edges } ) => {
6569 reactFlowInstance . setNodes ( nodes )
6670 reactFlowInstance . setEdges ( edges )
6771
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 )
7075 } )
7176 }
72- } , [ reactFlowInstance , tables , resolvedTheme , fitView ] )
77+ } , [ reactFlowInstance , tables , resolvedTheme , fitView , isFirstLoad ] )
7378
7479 return (
7580 < 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