Skip to content
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

refactor: Update handleLayout to accept nodes and improve hidden node handling #342

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend/.changeset/calm-books-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@liam-hq/erd-core": patch
"@liam-hq/cli": patch
---

refactor: Update handleLayout to accept nodes and improve hidden node handling
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { useCliVersion } from '@/providers'
import { useUserEditingStore } from '@/stores'
import { IconButton, TidyUpIcon } from '@liam-hq/ui'
import { ToolbarButton } from '@radix-ui/react-toolbar'
import { useReactFlow } from '@xyflow/react'
import { type FC, useCallback } from 'react'
import { useAutoLayout } from '../../useAutoLayout'

export const TidyUpButton: FC = () => {
const { getNodes } = useReactFlow()
const { handleLayout } = useAutoLayout()
const { showMode } = useUserEditingStore()
const { cliVersion } = useCliVersion()
Expand All @@ -16,8 +18,8 @@ export const TidyUpButton: FC = () => {
showMode,
cliVer: cliVersion.version,
})
handleLayout()
}, [handleLayout, showMode, cliVersion.version])
handleLayout(getNodes())
}, [handleLayout, showMode, getNodes, cliVersion.version])

return (
<ToolbarButton asChild>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@ import { useERDContentContext } from '../ERDContentContext'
import { getElkLayout } from './getElkLayout'

export const useAutoLayout = () => {
const { getNodes, setNodes, getEdges, fitView } = useReactFlow()
const { setNodes, getEdges, fitView } = useReactFlow()
const {
actions: { setLoading, setInitializeComplete },
} = useERDContentContext()

const handleLayout = useCallback(
async (
fitViewOptions: FitViewOptions = {},
initialHiddenNodeIds: string[] = [],
) => {
async (nodes: Node[], fitViewOptions: FitViewOptions = {}) => {
setLoading(true)
const nodes = getNodes()
const edges = getEdges()

const hiddenNodes: Node[] = []
const visibleNodes: Node[] = []
for (const node of nodes) {
if (initialHiddenNodeIds.includes(node.id) || node.hidden) {
hiddenNodes.push({ ...node, hidden: true })
if (node.hidden) {
hiddenNodes.push(node)
} else {
visibleNodes.push(node)
}
Expand All @@ -47,7 +43,7 @@ export const useAutoLayout = () => {
setInitializeComplete(true)
}, 0)
},
[getNodes, setNodes, getEdges, fitView, setLoading, setInitializeComplete],
[setNodes, getEdges, fitView, setLoading, setInitializeComplete],
)

return { handleLayout }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ export const useInitialAutoLayout = (nodes: Node[]) => {
updateActiveTableName(tableNameFromUrl)
const hiddenNodeIds = await getHiddenNodeIdsFromUrl()
addHiddenNodeIds(hiddenNodeIds)
const appliedNodes = nodes.map((node) => ({
...node,
hidden: hiddenNodeIds.includes(node.id),
}))

const fitViewOptions = tableNameFromUrl
? { maxZoom: 1, duration: 300, nodes: [{ id: tableNameFromUrl }] }
: undefined

if (tableNodesInitialized) {
handleLayout(fitViewOptions, hiddenNodeIds)
handleLayout(appliedNodes, fitViewOptions)
}
}

initialize()
}, [tableNodesInitialized, initializeComplete, handleLayout])
}, [tableNodesInitialized, initializeComplete, handleLayout, nodes])
}
Loading