From a711e0832893ade43dcb72ae15bfad08491ad36a Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Tue, 22 Apr 2025 18:58:23 +0800 Subject: [PATCH] refactor: update QuestMap component to use new node structure and simplify DAG class --- app/src/components/QuestMap.vue | 6 +++--- app/tsconfig.json | 5 +---- packages/utils/src/dag.ts | 23 +++++++++++++++-------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/components/QuestMap.vue b/app/src/components/QuestMap.vue index 4a0846a..e701b08 100644 --- a/app/src/components/QuestMap.vue +++ b/app/src/components/QuestMap.vue @@ -69,14 +69,14 @@ async function computeGraphData() { const nodesValues = dag.getNodes() const nodes = nodesValues.map((node) => ({ - id: node._id, + id: node.id, title: node.title, x: marginX * node.layer * 2, y: marginY * node.index - - (marginX * dag.getLayer(node._id)) / 2 + + (marginX * dag.getLayer(node.id)) / 2 + svgHeight.value / 2, - completed: store.progress[node._id]?.completed || false, + completed: store.progress[node.id]?.completed || false, unlocked: node.requirements.every( (req: string) => store.progress[req]?.completed ), diff --git a/app/tsconfig.json b/app/tsconfig.json index a5d598f..c15257a 100644 --- a/app/tsconfig.json +++ b/app/tsconfig.json @@ -15,10 +15,7 @@ "jsx": "preserve", /* Linting */ - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true + "strict": true }, "include": ["src/**/*.ts", "src/**/*.vue"], "references": [ diff --git a/packages/utils/src/dag.ts b/packages/utils/src/dag.ts index be85dac..f5fcb07 100644 --- a/packages/utils/src/dag.ts +++ b/packages/utils/src/dag.ts @@ -1,15 +1,22 @@ -interface Node { +export interface NodeInput { id: string requirements: string[] } -export default class DAG { - private nodes = new Map() +type Node = T & { + layer: number + index: number +} + +export default class DAG { + private nodes = new Map>() private edges = new Set<[string, string]>() private layers: number[] = [] - constructor(nodes: Node[]) { - this.nodes = new Map(nodes.map((node) => [node.id, node])) + constructor(nodes: T[]) { + this.nodes = new Map( + nodes.map((node) => [node.id, { ...node, layer: 0, index: 0 }]) + ) this.edges = new Set<[string, string]>() this.nodes.forEach((_, id) => { this.setLayer(id) @@ -38,7 +45,7 @@ export default class DAG { } getLayer(id: string) { - const node = this.getNode(id) + const node = this.nodes.get(id) if (!node) { throw new Error(`Node ${id} not found`) } @@ -46,7 +53,7 @@ export default class DAG { } private setLayer(id: string) { - const node = this.getNode(id) + const node = this.nodes.get(id) if (!node) { throw new Error(`Node ${id} not found`) } @@ -67,7 +74,7 @@ export default class DAG { } addEdges(id: string) { - const node = this.getNode(id) + const node = this.nodes.get(id) if (!node) { throw new Error(`Node ${id} not found`) }