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: Refactoring of highlight edges on active table #312

Merged
merged 3 commits into from
Dec 19, 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/mighty-shoes-confess.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: Refactoring of highlight edges on active table
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,8 @@ export const ERDContentInner: FC<Props> = ({
setActiveNodeId(nodeId)
updateActiveTableName(nodeId)

const relatedEdges = edges.filter(
(e) => e.source === nodeId || e.target === nodeId,
)

const updatedEdges = edges.map((e) =>
relatedEdges.includes(e) ? highlightEdge(e) : unhighlightEdge(e),
)

const { nodes: updatedNodes } = highlightNodesAndEdges(
nodes,
edges,
nodeId,
)
const { nodes: updatedNodes, edges: updatedEdges } =
highlightNodesAndEdges(nodes, edges, nodeId)

setEdges(updatedEdges)
setNodes(updatedNodes)
Expand All @@ -132,9 +121,7 @@ export const ERDContentInner: FC<Props> = ({
setActiveNodeId(null)
updateActiveTableName(undefined)

const updatedEdges = edges.map(unhighlightEdge)

const { nodes: updatedNodes } = highlightNodesAndEdges(
const { nodes: updatedNodes, edges: updatedEdges } = highlightNodesAndEdges(
nodes,
edges,
undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,57 +60,111 @@ describe(highlightNodesAndEdges, () => {
),
]

it('When the users is active, the users and related tables are highlighted', () => {
const { nodes: updatedNodes } = highlightNodesAndEdges(
nodes,
edges,
'users',
)
describe('nodes', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nodes test only added indentation.

it('When the users is active, the users and related tables are highlighted', () => {
const { nodes: updatedNodes } = highlightNodesAndEdges(
nodes,
edges,
'users',
)

expect(updatedNodes).toEqual([
aTableNode('users', {
data: aTableData('users', { isActiveHighlighted: true }),
}),
aTableNode('posts', {
data: aTableData('posts', {
isHighlighted: true,
highlightedHandles: ['posts-user_id'],
expect(updatedNodes).toEqual([
aTableNode('users', {
data: aTableData('users', { isActiveHighlighted: true }),
}),
}),
aTableNode('comments'),
aTableNode('comment_users', {
data: aTableData('comment_users', {
isHighlighted: true,
highlightedHandles: ['comment_users-user_id'],
aTableNode('posts', {
data: aTableData('posts', {
isHighlighted: true,
highlightedHandles: ['posts-user_id'],
}),
}),
}),
])
aTableNode('comments'),
aTableNode('comment_users', {
data: aTableData('comment_users', {
isHighlighted: true,
highlightedHandles: ['comment_users-user_id'],
}),
}),
])
})

it('When no active table, no tables are highlighted', () => {
const { nodes: updatedNodes } = highlightNodesAndEdges(
nodes,
edges,
undefined,
)

expect(updatedNodes).toEqual([
aTableNode('users', {
data: aTableData('users', { isActiveHighlighted: false }),
}),
aTableNode('posts', {
data: aTableData('posts', {
isHighlighted: false,
highlightedHandles: [],
}),
}),
aTableNode('comments'),
aTableNode('comment_users', {
data: aTableData('comment_users', {
isHighlighted: false,
highlightedHandles: [],
}),
}),
])
})
})

it('When no active table, no tables are highlighted', () => {
const { nodes: updatedNodes } = highlightNodesAndEdges(
nodes,
edges,
undefined,
)
describe('edges', () => {
it('When the users is active, the users and related edges are highlighted', () => {
const { edges: updatedEdges } = highlightNodesAndEdges(
nodes,
edges,
'users',
)

expect(updatedEdges).toEqual([
anEdge('users', 'posts', 'users-id', 'posts-user_id', {
animated: true,
data: { isHighlighted: true },
}),
anEdge('users', 'comment_users', 'users-id', 'comment_users-user_id', {
animated: true,
data: { isHighlighted: true },
}),
anEdge(
'comments',
'comment_users',
'comments-id',
'comment_users-comment_id',
),
])
})

it('When no active table, no edges are highlighted', () => {
const { edges: updatedEdges } = highlightNodesAndEdges(
nodes,
edges,
undefined,
)

expect(updatedNodes).toEqual([
aTableNode('users', {
data: aTableData('users', { isActiveHighlighted: false }),
}),
aTableNode('posts', {
data: aTableData('posts', {
isHighlighted: false,
highlightedHandles: [],
expect(updatedEdges).toEqual([
anEdge('users', 'posts', 'users-id', 'posts-user_id', {
animated: false,
data: { isHighlighted: false },
}),
}),
aTableNode('comments'),
aTableNode('comment_users', {
data: aTableData('comment_users', {
isHighlighted: false,
highlightedHandles: [],
anEdge('users', 'comment_users', 'users-id', 'comment_users-user_id', {
animated: false,
data: { isHighlighted: false },
}),
}),
])
anEdge(
'comments',
'comment_users',
'comments-id',
'comment_users-comment_id',
),
])
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const isActivelyRelatedNode = (
return edgeMap.get(activeTableName)?.includes(node.data.table.name) ?? false
}

const isActivelyRelatedEdge = (
activeTableName: string | undefined,
edge: Edge,
): boolean => {
return edge.source === activeTableName || edge.target === activeTableName
}

const getHighlightedHandlesForRelatedNode = (
activeTableName: string | undefined,
edges: Edge[],
Expand Down Expand Up @@ -87,6 +94,18 @@ const unhighlightNode = (node: TableNodeType): TableNodeType => ({
},
})

const highlightEdge = (edge: Edge): Edge => ({
...edge,
animated: true,
data: { ...edge.data, isHighlighted: true },
})

const unhighlightEdge = (edge: Edge): Edge => ({
...edge,
animated: false,
data: { ...edge.data, isHighlighted: false },
})

export const highlightNodesAndEdges = (
nodes: Node[],
edges: Edge[],
Expand Down Expand Up @@ -123,5 +142,13 @@ export const highlightNodesAndEdges = (
return unhighlightNode(node)
})

return { nodes: updatedNodes, edges: [] }
const updatedEdges = edges.map((edge) => {
if (isActivelyRelatedEdge(activeTableName, edge)) {
return highlightEdge(edge)
}

return unhighlightEdge(edge)
})

return { nodes: updatedNodes, edges: updatedEdges }
}
Loading