Skip to content

Commit 4da62d2

Browse files
committed
fix: attempted self healing fix
1 parent 9cdfe48 commit 4da62d2

2 files changed

Lines changed: 78 additions & 15 deletions

File tree

src/Node.jsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,42 @@ const Node = ({
228228
}
229229
}, [isPreviewing]);
230230

231+
// Self-healing effect: when previewing starts but no definitions found, search for orphan graphs
232+
useEffect(() => {
233+
if (!isPreviewing || !prototypeId || !storeActions) return;
234+
235+
// Check if we already have definitions (from prototype spread)
236+
if (definitionGraphIds.length > 0) return;
237+
238+
// No definitions on the hydrated node - check if there are orphan graphs
239+
const currentState = useGraphStore.getState();
240+
const graphs = currentState.graphs;
241+
242+
let orphanGraphId = null;
243+
try {
244+
for (const [gId, g] of graphs.entries()) {
245+
if (Array.isArray(g.definingNodeIds) && g.definingNodeIds.includes(prototypeId)) {
246+
orphanGraphId = gId;
247+
break;
248+
}
249+
}
250+
} catch (_) { }
251+
252+
if (orphanGraphId) {
253+
console.log('[Node] Self-healing: Found orphan definition graph. Repairing link.', {
254+
prototypeId,
255+
orphanGraphId
256+
});
257+
// Repair the link
258+
storeActions.updateNodePrototype(prototypeId, draft => {
259+
draft.definitionGraphIds = Array.isArray(draft.definitionGraphIds) ? draft.definitionGraphIds : [];
260+
if (!draft.definitionGraphIds.includes(orphanGraphId)) {
261+
draft.definitionGraphIds.push(orphanGraphId);
262+
}
263+
});
264+
}
265+
}, [isPreviewing, prototypeId, definitionGraphIds.length, storeActions]);
266+
231267
// Navigation functions
232268
const navigateToPreviousDefinition = () => {
233269
if (!hasMultipleDefinitions || !onNavigateDefinition) return;

src/NodeCanvas.jsx

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4562,25 +4562,52 @@ function NodeCanvas() {
45624562
const nodeData = nodes.find(n => n.id === instanceId);
45634563
if (nodeData) {
45644564
const prototypeId = nodeData.prototypeId;
4565-
if (nodeData.definitionGraphIds && nodeData.definitionGraphIds.length > 0) {
4565+
const currentState = useGraphStore.getState();
4566+
const prototypeData = currentState.nodePrototypes.get(prototypeId);
4567+
4568+
if (prototypeData?.definitionGraphIds && prototypeData.definitionGraphIds.length > 0) {
45664569
// Node has definitions - start hurtle animation to first one
4567-
const graphIdToOpen = nodeData.definitionGraphIds[0];
4570+
const graphIdToOpen = prototypeData.definitionGraphIds[0];
45684571
startHurtleAnimation(instanceId, graphIdToOpen, prototypeId);
45694572
} else {
4570-
// Node has no definitions - create one first, then start hurtle animation
4571-
const sourceGraphId = activeGraphId; // Capture current graph before it changes
4572-
storeActions.createAndAssignGraphDefinitionWithoutActivation(prototypeId);
4573-
4574-
setTimeout(() => {
4575-
const currentState = useGraphStore.getState();
4576-
const updatedNodeData = currentState.nodePrototypes.get(prototypeId);
4577-
if (updatedNodeData?.definitionGraphIds?.length > 0) {
4578-
const newGraphId = updatedNodeData.definitionGraphIds[updatedNodeData.definitionGraphIds.length - 1];
4579-
startHurtleAnimation(instanceId, newGraphId, prototypeId, sourceGraphId);
4580-
} else {
4581-
4573+
// No definitions recorded. Self-heal: find any existing graph that defines this prototype
4574+
const sourceGraphId = activeGraphId;
4575+
let orphanGraphId = null;
4576+
try {
4577+
for (const [gId, g] of currentState.graphs.entries()) {
4578+
if (Array.isArray(g.definingNodeIds) && g.definingNodeIds.includes(prototypeId)) {
4579+
orphanGraphId = gId;
4580+
break;
4581+
}
45824582
}
4583-
}, 50);
4583+
} catch (_) { }
4584+
4585+
if (orphanGraphId) {
4586+
console.log('[Expand] Found orphan definition graph. Repairing and opening.', {
4587+
prototypeId,
4588+
orphanGraphId
4589+
});
4590+
// Self-heal: add to prototype.definitionGraphIds
4591+
storeActions.updateNodePrototype(prototypeId, draft => {
4592+
draft.definitionGraphIds = Array.isArray(draft.definitionGraphIds) ? draft.definitionGraphIds : [];
4593+
if (!draft.definitionGraphIds.includes(orphanGraphId)) {
4594+
draft.definitionGraphIds.push(orphanGraphId);
4595+
}
4596+
});
4597+
startHurtleAnimation(instanceId, orphanGraphId, prototypeId, sourceGraphId);
4598+
} else {
4599+
// No existing definition anywhere - create one
4600+
storeActions.createAndAssignGraphDefinitionWithoutActivation(prototypeId);
4601+
4602+
setTimeout(() => {
4603+
const updatedState = useGraphStore.getState();
4604+
const updatedNodeData = updatedState.nodePrototypes.get(prototypeId);
4605+
if (updatedNodeData?.definitionGraphIds?.length > 0) {
4606+
const newGraphId = updatedNodeData.definitionGraphIds[updatedNodeData.definitionGraphIds.length - 1];
4607+
startHurtleAnimation(instanceId, newGraphId, prototypeId, sourceGraphId);
4608+
}
4609+
}, 50);
4610+
}
45844611
}
45854612
}
45864613
}

0 commit comments

Comments
 (0)