Skip to content

Commit

Permalink
Merge pull request #294 from liam-hq/refactor_hidden_source_node_handles
Browse files Browse the repository at this point in the history
Enable hiding cardinalities on source node
  • Loading branch information
MH4GF authored Dec 17, 2024
2 parents 7e226a2 + 594a73b commit 12d215f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
6 changes: 6 additions & 0 deletions frontend/.changeset/orange-pants-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@liam-hq/erd-core": patch
"@liam-hq/cli": patch
---

Enable hiding cardinalities on source node if target node is hidden
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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 = (
Expand All @@ -10,14 +9,23 @@ export const useUpdateNodeCardinalities = (
setNodes: (nodes: Node[]) => void,
) => {
useEffect(() => {
const visibleNodes = nodes.filter((n) => !n.hidden && isTableNode(n))
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 (!isTableNode(node)) {
return node
}

const { table } = node.data
const { name: tableName } = table
const { targetColumnCardinalities } = node.data

if (!targetColumnCardinalities) return node
if (!targetColumnCardinalities) {
return node
}

const updatedTargetColumnCardinalities = { ...targetColumnCardinalities }

for (const relationship of Object.values(relationships)) {
if (relationship.foreignTableName !== tableName) continue
Expand All @@ -26,16 +34,34 @@ export const useUpdateNodeCardinalities = (
(hiddenNode) => hiddenNode.id === relationship.primaryTableName,
)

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

const primaryRelationships = Object.values(relationships).filter(
(relationship) => relationship.primaryTableName === tableName,
)

const visibleForeignRelationship = primaryRelationships.find(
(relationship) =>
visibleNodes.some(
(visibleNode) => visibleNode.id === relationship.foreignTableName,
),
)

const updatedSourceColumnName = visibleForeignRelationship
? visibleForeignRelationship.primaryColumnName
: undefined

const updatedData = {
...node.data,
sourceColumnName: updatedSourceColumnName,
targetColumnCardinalities: updatedTargetColumnCardinalities,
}

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

Expand Down

0 comments on commit 12d215f

Please sign in to comment.