-
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 #288 from liam-hq/refactor_hidden_node_handles
Refactor hidden node handles
- Loading branch information
Showing
4 changed files
with
55 additions
and
1 deletion.
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 | ||
--- | ||
|
||
Update hidden node cardinalities |
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
44 changes: 44 additions & 0 deletions
44
...end/packages/erd-core/src/components/ERDRenderer/ERDContent/useUpdateNodeCardinalities.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 |
---|---|---|
@@ -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]) | ||
} |