Skip to content

Commit

Permalink
Merge pull request #288 from liam-hq/refactor_hidden_node_handles
Browse files Browse the repository at this point in the history
Refactor hidden node handles
  • Loading branch information
MH4GF authored Dec 17, 2024
2 parents 710b457 + a19690c commit f290a9d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions frontend/.changeset/afraid-gifts-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@liam-hq/erd-core": patch
"@liam-hq/cli": patch
---

Update hidden node cardinalities
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RelationshipEdge } from './RelationshipEdge'
import { TableNode } from './TableNode'
import { useFitViewWhenActiveTableChange } from './useFitViewWhenActiveTableChange'
import { useInitialAutoLayout } from './useInitialAutoLayout'
import { useUpdateNodeCardinalities } from './useUpdateNodeCardinalities'

const nodeTypes = {
table: TableNode,
Expand Down Expand Up @@ -78,6 +79,7 @@ export const ERDContentInner: FC<Props> = ({
} = useERDContentContext()
const [activeNodeId, setActiveNodeId] = useState<string | null>(null)

useUpdateNodeCardinalities(nodes, relationships, setNodes)
useInitialAutoLayout()
useFitViewWhenActiveTableChange(
enabledFeatures?.fitViewWhenActiveTableChange ?? true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export type Data = {
isRelated: boolean
highlightedHandles: string[]
sourceColumnName: string | undefined
targetColumnCardinalities?: Record<string, Cardinality> | undefined
targetColumnCardinalities?:
| Record<string, Cardinality | undefined>
| undefined
}

export type TableNodeType = Node<Data, 'table'>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Relationships } from '@liam-hq/db-structure'
import type { Node } from '@xyflow/react'
import { useEffect } from 'react'
import type { TableNodeType } from './TableNode'
import { isTableNode } from './TableNode'

export const useUpdateNodeCardinalities = (
nodes: Node[],
relationships: Relationships,
setNodes: (nodes: Node[]) => void,
) => {
useEffect(() => {
const hiddenNodes = nodes.filter((n) => n.hidden && isTableNode(n))

const updatedNodes = nodes.map((node) => {
const nodeData = node as TableNodeType
const tableName = nodeData.data.table.name
const targetColumnCardinalities = nodeData.data.targetColumnCardinalities

if (!targetColumnCardinalities) return node

for (const relationship of Object.values(relationships)) {
if (relationship.foreignTableName !== tableName) continue

const isPrimaryTableHidden = hiddenNodes.some(
(hiddenNode) => hiddenNode.id === relationship.primaryTableName,
)

targetColumnCardinalities[relationship.foreignColumnName] =
isPrimaryTableHidden ? undefined : relationship.cardinality
}

return {
...node,
data: {
...nodeData.data,
targetColumnCardinalities,
},
}
})

setNodes(updatedNodes)
}, [nodes, relationships, setNodes])
}

0 comments on commit f290a9d

Please sign in to comment.