diff --git a/frontend/.changeset/afraid-gifts-push.md b/frontend/.changeset/afraid-gifts-push.md new file mode 100644 index 000000000..67a2bb92d --- /dev/null +++ b/frontend/.changeset/afraid-gifts-push.md @@ -0,0 +1,6 @@ +--- +"@liam-hq/erd-core": patch +"@liam-hq/cli": patch +--- + +Update hidden node cardinalities diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/ERDContent.tsx b/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/ERDContent.tsx index 50c63cadc..207299d7e 100644 --- a/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/ERDContent.tsx +++ b/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/ERDContent.tsx @@ -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, @@ -78,6 +79,7 @@ export const ERDContentInner: FC = ({ } = useERDContentContext() const [activeNodeId, setActiveNodeId] = useState(null) + useUpdateNodeCardinalities(nodes, relationships, setNodes) useInitialAutoLayout() useFitViewWhenActiveTableChange( enabledFeatures?.fitViewWhenActiveTableChange ?? true, diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/TableNode/type.ts b/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/TableNode/type.ts index 8d731b0b6..0e96e3f2b 100644 --- a/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/TableNode/type.ts +++ b/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/TableNode/type.ts @@ -7,7 +7,9 @@ export type Data = { isRelated: boolean highlightedHandles: string[] sourceColumnName: string | undefined - targetColumnCardinalities?: Record | undefined + targetColumnCardinalities?: + | Record + | undefined } export type TableNodeType = Node diff --git a/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/useUpdateNodeCardinalities.ts b/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/useUpdateNodeCardinalities.ts new file mode 100644 index 000000000..164c00be9 --- /dev/null +++ b/frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/useUpdateNodeCardinalities.ts @@ -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]) +}