Skip to content

Commit 2069427

Browse files
Added getLinkNodeId function (see #1438)
Given that the link object is at times a string and at times an object (couldn't replicate, so exact cause unknown, but in line with the types of `force-graph` API), adding a function to properly extract the ID from the object
1 parent 6a849d2 commit 2069427

File tree

1 file changed

+16
-3
lines changed
  • packages/foam-vscode/static/dataviz

1 file changed

+16
-3
lines changed

packages/foam-vscode/static/dataviz/graph.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const graph = ForceGraph();
9696
const gui = initGUI();
9797

9898
function update(patch) {
99+
const startTime = performance.now();
99100
// Apply the patch function to the model..
100101
patch(model);
101102
// ..then compute the derived state
@@ -121,6 +122,7 @@ function update(patch) {
121122
model.focusLinks = focusLinks;
122123

123124
gui.update(model);
125+
console.log(`Updated model in ${performance.now() - startTime}ms`);
124126
}
125127

126128
const Actions = {
@@ -386,8 +388,8 @@ function getLinkColor(link, model) {
386388
switch (getLinkState(link, model)) {
387389
case 'regular':
388390
if (
389-
model.graph.nodeInfo[link.source.id].type === 'tag' &&
390-
model.graph.nodeInfo[link.target.id].type === 'tag'
391+
model.graph.nodeInfo[getLinkNodeId(link.source)].type === 'tag' &&
392+
model.graph.nodeInfo[getLinkNodeId(link.target)].type === 'tag'
391393
) {
392394
return getNodeTypeColor('tag', model);
393395
}
@@ -401,6 +403,16 @@ function getLinkColor(link, model) {
401403
}
402404
}
403405

406+
/**
407+
* Helper function to safely get node ID from a link's source or target
408+
* Handles both when the link endpoint is a string ID or a full node object
409+
* @param {string|Object} endpoint - Either a node ID string or a node object
410+
* @returns {string} The node ID
411+
*/
412+
function getLinkNodeId(endpoint) {
413+
return typeof endpoint === 'object' ? endpoint.id : endpoint;
414+
}
415+
404416
function getNodeState(nodeId, model) {
405417
return model.selectedNodes.has(nodeId) || model.hoverNode === nodeId
406418
? 'highlighted'
@@ -416,7 +428,8 @@ function getLinkState(link, model) {
416428
? 'regular'
417429
: Array.from(model.focusLinks).some(
418430
fLink =>
419-
fLink.source === link.source.id && fLink.target === link.target.id
431+
getLinkNodeId(fLink.source) === getLinkNodeId(link.source) &&
432+
getLinkNodeId(fLink.target) === getLinkNodeId(link.target)
420433
)
421434
? 'highlighted'
422435
: 'lessened';

0 commit comments

Comments
 (0)