-
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 #309 from liam-hq/fix-active-highlight-node
refactor: Refactoring and testing of highlights on active tables
- Loading branch information
Showing
6 changed files
with
274 additions
and
45 deletions.
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 | ||
--- | ||
|
||
Refactoring and testing of highlights on active tables |
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
116 changes: 116 additions & 0 deletions
116
...nd/packages/erd-core/src/components/ERDRenderer/ERDContent/highlightNodesAndEdges.test.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,116 @@ | ||
import { aTable } from '@liam-hq/db-structure' | ||
import type { Edge } from '@xyflow/react' | ||
import { describe, expect, it } from 'vitest' | ||
import type { Data, TableNodeType } from './TableNode' | ||
import { highlightNodesAndEdges } from './highlightNodesAndEdges' | ||
|
||
const aTableData = (name: string, override?: Partial<Data>): Data => ({ | ||
table: aTable({ name }), | ||
isActiveHighlighted: false, | ||
isHighlighted: false, | ||
highlightedHandles: [], | ||
sourceColumnName: undefined, | ||
...override, | ||
}) | ||
|
||
const aTableNode = ( | ||
name: string, | ||
override?: Partial<TableNodeType>, | ||
): TableNodeType => ({ | ||
id: name, | ||
type: 'table', | ||
position: { x: 0, y: 0 }, | ||
...override, | ||
data: aTableData(name, override?.data), | ||
}) | ||
|
||
const anEdge = ( | ||
source: string, | ||
target: string, | ||
sourceHandle: string | null, | ||
targetHandle: string | null, | ||
override?: Partial<Edge>, | ||
): Edge => ({ | ||
id: `${source}-${target}`, | ||
source, | ||
sourceHandle, | ||
target, | ||
targetHandle, | ||
animated: false, | ||
data: { isHighlighted: false, ...override?.data }, | ||
...override, | ||
}) | ||
|
||
describe(highlightNodesAndEdges, () => { | ||
const nodes: TableNodeType[] = [ | ||
aTableNode('users'), | ||
aTableNode('posts'), | ||
aTableNode('comments'), | ||
aTableNode('comment_users'), | ||
] | ||
|
||
const edges: Edge[] = [ | ||
anEdge('users', 'posts', 'users-id', 'posts-user_id'), | ||
anEdge('users', 'comment_users', 'users-id', 'comment_users-user_id'), | ||
anEdge( | ||
'comments', | ||
'comment_users', | ||
'comments-id', | ||
'comment_users-comment_id', | ||
), | ||
] | ||
|
||
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'], | ||
}), | ||
}), | ||
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: [], | ||
}), | ||
}), | ||
]) | ||
}) | ||
}) |
127 changes: 127 additions & 0 deletions
127
frontend/packages/erd-core/src/components/ERDRenderer/ERDContent/highlightNodesAndEdges.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,127 @@ | ||
import type { Edge, Node } from '@xyflow/react' | ||
import { type TableNodeType, isTableNode } from './TableNode' | ||
|
||
type SourceTableName = string | ||
type TargetTableName = string | ||
type EdgeMap = Map<SourceTableName, TargetTableName[]> | ||
|
||
const isActiveNode = ( | ||
activeTableName: string | undefined, | ||
node: TableNodeType, | ||
): boolean => { | ||
return node.data.table.name === activeTableName | ||
} | ||
|
||
const isActivelyRelatedNode = ( | ||
activeTableName: string | undefined, | ||
edgeMap: EdgeMap, | ||
node: TableNodeType, | ||
): boolean => { | ||
if (!activeTableName) { | ||
return false | ||
} | ||
|
||
return edgeMap.get(activeTableName)?.includes(node.data.table.name) ?? false | ||
} | ||
|
||
const getHighlightedHandlesForRelatedNode = ( | ||
activeTableName: string | undefined, | ||
edges: Edge[], | ||
node: TableNodeType, | ||
): string[] => { | ||
if (!activeTableName) { | ||
return [] | ||
} | ||
|
||
const handles: string[] = [] | ||
for (const edge of edges) { | ||
if ( | ||
edge.targetHandle !== undefined && | ||
edge.targetHandle !== null && | ||
edge.source === activeTableName && | ||
edge.target === node.data.table.name | ||
) { | ||
handles.push(edge.targetHandle) | ||
} | ||
|
||
if ( | ||
edge.sourceHandle !== undefined && | ||
edge.sourceHandle !== null && | ||
edge.source === node.data.table.name && | ||
edge.target === activeTableName | ||
) { | ||
handles.push(edge.sourceHandle) | ||
} | ||
} | ||
|
||
return handles | ||
} | ||
|
||
const activeHighlightNode = (node: TableNodeType): TableNodeType => ({ | ||
...node, | ||
data: { | ||
...node.data, | ||
isActiveHighlighted: true, | ||
}, | ||
}) | ||
|
||
const highlightNode = ( | ||
node: TableNodeType, | ||
handles: string[], | ||
): TableNodeType => ({ | ||
...node, | ||
data: { | ||
...node.data, | ||
isHighlighted: true, | ||
highlightedHandles: handles, | ||
}, | ||
}) | ||
|
||
const unhighlightNode = (node: TableNodeType): TableNodeType => ({ | ||
...node, | ||
data: { | ||
...node.data, | ||
isActiveHighlighted: false, | ||
isHighlighted: false, | ||
highlightedHandles: [], | ||
}, | ||
}) | ||
|
||
export const highlightNodesAndEdges = ( | ||
nodes: Node[], | ||
edges: Edge[], | ||
activeTableName?: string | undefined, | ||
): { nodes: Node[]; edges: Edge[] } => { | ||
const edgeMap: EdgeMap = new Map() | ||
for (const edge of edges) { | ||
const sourceTableName = edge.source | ||
const targetTableName = edge.target | ||
if (!edgeMap.has(sourceTableName)) { | ||
edgeMap.set(sourceTableName, []) | ||
} | ||
edgeMap.get(sourceTableName)?.push(targetTableName) | ||
} | ||
|
||
const updatedNodes = nodes.map((node) => { | ||
if (!isTableNode(node)) { | ||
return node | ||
} | ||
|
||
if (isActiveNode(activeTableName, node)) { | ||
return activeHighlightNode(node) | ||
} | ||
|
||
if (isActivelyRelatedNode(activeTableName, edgeMap, node)) { | ||
const highlightedHandles = getHighlightedHandlesForRelatedNode( | ||
activeTableName, | ||
edges, | ||
node, | ||
) | ||
return highlightNode(node, highlightedHandles) | ||
} | ||
|
||
return unhighlightNode(node) | ||
}) | ||
|
||
return { nodes: updatedNodes, edges: [] } | ||
} |
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,11 @@ | ||
// biome-ignore lint/correctness/noNodejsModules: Because this file is a config file | ||
import * as path from 'node:path' | ||
import { defineConfig } from 'vitest/config' | ||
|
||
export default defineConfig({ | ||
test: { | ||
alias: { | ||
'@': path.resolve(__dirname, './src'), | ||
}, | ||
}, | ||
}) |