Skip to content

Commit aa91015

Browse files
authored
Merge pull request supabase-community#20 from supabase-community/feat/graph-viewport-animation
feat: improve initial graph viewport animation
2 parents b57a7cc + f9f79e4 commit aa91015

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

apps/postgres-new/components/schema/table-graph.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PostgresTable } from '@gregnr/postgres-meta/base'
33
import { uniqBy } from 'lodash'
44
import { Info, Loader } from 'lucide-react'
55
import { useTheme } from 'next-themes'
6-
import { useCallback, useEffect, useMemo } from 'react'
6+
import { useCallback, useEffect, useMemo, useState } from 'react'
77
import 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

Comments
 (0)