-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from liam-hq/fix-initialize-nodes
fix: Fixed an issue where the correct table was not focused when sharing URLs in TableDetail
- Loading branch information
Showing
7 changed files
with
69 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@liam-hq/erd-core": patch | ||
"@liam-hq/cli": patch | ||
--- | ||
|
||
fix: Fixed an issue where the correct table was not focused when sharing URLs in TableDetail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
...ages/erd-core/src/components/ERDRenderer/ERDContent/Toolbar/TidyUpButton/TidyUpButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
...tend/packages/erd-core/src/components/ERDRenderer/ERDContent/useActiveTableNameFromUrl.ts
This file was deleted.
Oops, something went wrong.
46 changes: 25 additions & 21 deletions
46
...nd/packages/erd-core/src/components/ERDRenderer/ERDContent/useAutoLayout/useAutoLayout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,41 @@ | ||
import { useReactFlow } from '@xyflow/react' | ||
import type { Node } from '@xyflow/react' | ||
import type { FitViewOptions, Node } from '@xyflow/react' | ||
import { useCallback } from 'react' | ||
import { useERDContentContext } from '../ERDContentContext' | ||
import { getElkLayout } from './getElkLayout' | ||
|
||
export const useAutoLayout = () => { | ||
const { getNodes, setNodes, getEdges, fitView } = useReactFlow() | ||
const { | ||
actions: { setLoading }, | ||
actions: { setLoading, setInitializeComplete }, | ||
} = useERDContentContext() | ||
|
||
const handleLayout = useCallback(async () => { | ||
setLoading(true) | ||
const nodes = getNodes() | ||
const edges = getEdges() | ||
const visibleNodes: Node[] = nodes.filter((node) => !node.hidden) | ||
const hiddenNodes: Node[] = nodes.filter((node) => node.hidden) | ||
const handleLayout = useCallback( | ||
async (fitViewOptions?: FitViewOptions) => { | ||
setLoading(true) | ||
const nodes = getNodes() | ||
const edges = getEdges() | ||
const visibleNodes: Node[] = nodes.filter((node) => !node.hidden) | ||
const hiddenNodes: Node[] = nodes.filter((node) => node.hidden) | ||
|
||
// NOTE: Only include edges where both the source and target are in the nodes | ||
const nodeMap = new Map(visibleNodes.map((node) => [node.id, node])) | ||
const visibleEdges = edges.filter((edge) => { | ||
return nodeMap.get(edge.source) && nodeMap.get(edge.target) | ||
}) | ||
// NOTE: Only include edges where both the source and target are in the nodes | ||
const nodeMap = new Map(visibleNodes.map((node) => [node.id, node])) | ||
const visibleEdges = edges.filter((edge) => { | ||
return nodeMap.get(edge.source) && nodeMap.get(edge.target) | ||
}) | ||
|
||
const newNodes = await getElkLayout({ | ||
nodes: visibleNodes, | ||
edges: visibleEdges, | ||
}) | ||
const newNodes = await getElkLayout({ | ||
nodes: visibleNodes, | ||
edges: visibleEdges, | ||
}) | ||
|
||
setNodes([...hiddenNodes, ...newNodes]) | ||
setLoading(false) | ||
setTimeout(() => fitView(), 0) | ||
}, [getNodes, setNodes, getEdges, fitView, setLoading]) | ||
setNodes([...hiddenNodes, ...newNodes]) | ||
setLoading(false) | ||
setInitializeComplete(true) | ||
setTimeout(() => fitView(fitViewOptions), 0) | ||
}, | ||
[getNodes, setNodes, getEdges, fitView, setLoading, setInitializeComplete], | ||
) | ||
|
||
return { handleLayout } | ||
} |
28 changes: 26 additions & 2 deletions
28
frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/useInitialAutoLayout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
import type { QueryParam } from '@/schemas/queryParam' | ||
import { updateActiveTableName } from '@/stores' | ||
import { useNodesInitialized } from '@xyflow/react' | ||
import { useEffect } from 'react' | ||
import { useERDContentContext } from './ERDContentContext' | ||
import { useAutoLayout } from './useAutoLayout' | ||
|
||
const getActiveTableNameFromUrl = (): string | undefined => { | ||
const urlParams = new URLSearchParams(window.location.search) | ||
const activeQueryParam: QueryParam = 'active' | ||
const tableName = urlParams.get(activeQueryParam) | ||
|
||
return tableName || undefined | ||
} | ||
|
||
export const useInitialAutoLayout = () => { | ||
const nodesInitialized = useNodesInitialized() | ||
const { | ||
state: { initializeComplete }, | ||
} = useERDContentContext() | ||
const { handleLayout } = useAutoLayout() | ||
|
||
useEffect(() => { | ||
if (initializeComplete) { | ||
return | ||
} | ||
|
||
const tableNameFromUrl = getActiveTableNameFromUrl() | ||
updateActiveTableName(tableNameFromUrl) | ||
const fitViewOptions = tableNameFromUrl | ||
? { maxZoom: 1, duration: 300, nodes: [{ id: tableNameFromUrl }] } | ||
: undefined | ||
|
||
if (nodesInitialized) { | ||
handleLayout() | ||
handleLayout(fitViewOptions) | ||
} | ||
}, [nodesInitialized, handleLayout]) | ||
}, [nodesInitialized, initializeComplete, handleLayout]) | ||
} |