Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ddfa75d

Browse files
authoredJan 16, 2025
Rebuild history (#87)
2 parents 5d8a92b + 353c873 commit ddfa75d

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed
 

‎src/index.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,17 @@ Open up the challenge in your favorite code editor and follow the instructions i
290290
return existsSync(targetDir);
291291
}
292292

293+
rebuildHistory():void {
294+
// Rebuild Global tree
295+
this.globalTree = this.buildTree();
296+
// For each node in the current history, find the new node in the newly created tree
297+
const newHistory = this.history.map(({ node, selection }) => {
298+
const newNode = this.findNode(this.globalTree, node.name);
299+
return { node: newNode as TreeNode, selection };
300+
});
301+
this.history = newHistory;
302+
}
303+
293304
getChallengeActions(challenge: TreeNode): Actions {
294305
const actions: Actions = {};
295306
const { address, installLocation } = this.userState;
@@ -298,14 +309,12 @@ Open up the challenge in your favorite code editor and follow the instructions i
298309
actions["Setup Challenge Repository"] = async () => {
299310
this.clearView();
300311
await setupChallenge(name, installLocation);
301-
// Rebuild the tree
302-
this.globalTree = this.buildTree();
312+
// Rebuild the tree and history
313+
this.rebuildHistory();
303314
// Wait for enter key
304315
await this.pressEnterToContinue();
305-
this.history.pop(); // Remove the old node from history since it has different actions
306316
// Return to challenge menu
307-
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
308-
await this.navigate(challengeNode);
317+
await this.goBack();
309318
};
310319
} else {
311320
actions["Reset Challenge"] = async () => {
@@ -319,12 +328,11 @@ Open up the challenge in your favorite code editor and follow the instructions i
319328
rmSync(targetDir, { recursive: true, force: true });
320329
console.log(`Installing fresh copy of challenge...`);
321330
await setupChallenge(name, installLocation);
322-
this.globalTree = this.buildTree();
331+
// Rebuild the tree and history
332+
this.rebuildHistory();
323333
await this.pressEnterToContinue();
324-
this.history.pop(); // Remove the old node from history since it has different actions
325334
// Return to challenge menu
326-
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
327-
await this.navigate(challengeNode);
335+
await this.goBack();
328336
}
329337
};
330338
actions["Submit Completed Challenge"] = async () => {
@@ -336,14 +344,12 @@ Open up the challenge in your favorite code editor and follow the instructions i
336344
this.userState.challenges = newUserState.challenges;
337345
// Save the new user state locally
338346
await saveUserState(this.userState);
339-
// Rebuild the tree
340-
this.globalTree = this.buildTree();
347+
// Rebuild the tree and history
348+
this.rebuildHistory();
341349
// Wait for enter key
342350
await this.pressEnterToContinue();
343-
this.history.pop(); // Remove the old node from history since it has different actions
344351
// Return to challenge menu
345-
const challengeNode = this.findNode(this.globalTree, name) as TreeNode;
346-
await this.navigate(challengeNode);
352+
await this.goBack();
347353
};
348354
}
349355
return actions;

0 commit comments

Comments
 (0)
Please sign in to comment.