-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Fixed an issue where the correct table was not focused when sharing URLs in TableDetail #275
Changes from all commits
8ff7ee7
9c44a6a
19e499a
4cef3ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 was deleted.
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 } | ||
} |
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 The contents of |
||
|
||
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 }] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 same as useFitViewWhenActiveTableChange |
||
: undefined | ||
|
||
if (nodesInitialized) { | ||
handleLayout() | ||
handleLayout(fitViewOptions) | ||
} | ||
}, [nodesInitialized, handleLayout]) | ||
}, [nodesInitialized, initializeComplete, handleLayout]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 It is only possible to pass fitViewOptions.