File size: 7,753 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"use client";

import { useMemo } from "react";
import { motion } from "framer-motion";
import { CATEGORY_COLORS } from "@/config/xp";
import type { Category } from "@/config/xp";

interface SkillNode {
  id: number;
  category: string;
  name: string;
  description: string;
  levelRequired: number;
  parentNodeId: number | null;
  nodeType: string;
  unlocked: boolean;
}

interface SkillTreeProps {
  category: Category;
  nodes: SkillNode[];
  categoryLevel: number;
}

const NODE_W = 140;
const NODE_H = 52;
const H_GAP = 36;
const V_GAP = 56;

interface LayoutNode extends SkillNode {
  x: number;
  y: number;
  col: number;
  row: number;
}

function buildLayout(nodes: SkillNode[]): LayoutNode[] {
  const childrenOf = new Map<number | null, SkillNode[]>();
  for (const n of nodes) {
    const key = n.parentNodeId ?? null;
    if (!childrenOf.has(key)) childrenOf.set(key, []);
    childrenOf.get(key)!.push(n);
  }

  // BFS level assignment
  const levels: SkillNode[][] = [];
  const queue: Array<{ node: SkillNode; level: number }> = (
    childrenOf.get(null) ?? []
  ).map((n) => ({ node: n, level: 0 }));

  const nodeLevel = new Map<number, number>();
  while (queue.length > 0) {
    const { node, level } = queue.shift()!;
    nodeLevel.set(node.id, level);
    if (!levels[level]) levels[level] = [];
    levels[level].push(node);
    const children = childrenOf.get(node.id) ?? [];
    for (const child of children) {
      queue.push({ node: child, level: level + 1 });
    }
  }

  // Assign x positions per level, centered
  const result: LayoutNode[] = [];
  for (let row = 0; row < levels.length; row++) {
    const rowNodes = levels[row];
    const totalW = rowNodes.length * NODE_W + (rowNodes.length - 1) * H_GAP;
    let x = -totalW / 2 + NODE_W / 2;
    for (let col = 0; col < rowNodes.length; col++) {
      result.push({
        ...rowNodes[col],
        x,
        y: row * (NODE_H + V_GAP),
        col,
        row,
      });
      x += NODE_W + H_GAP;
    }
  }

  return result;
}

const NODE_TYPE_ICONS: Record<string, string> = {
  difficulty: "⚑",
  "exercise-type": "🎯",
  multiplier: "✨",
  "streak-shield": "πŸ›‘οΈ",
  cosmetic: "πŸ‘‘",
};

export function SkillTree({ category, nodes, categoryLevel }: SkillTreeProps) {
  const color = CATEGORY_COLORS[category] ?? "#6366f1";
  const catNodes = nodes.filter((n) => n.category === category);

  const layout = useMemo(() => buildLayout(catNodes), [catNodes]);

  if (layout.length === 0) {
    return (
      <div className="text-center py-12" style={{ color: "var(--text-muted)" }}>
        No skill tree data for this category.
      </div>
    );
  }

  const maxX = Math.max(...layout.map((n) => n.x)) + NODE_W / 2;
  const minX = Math.min(...layout.map((n) => n.x)) - NODE_W / 2;
  const maxY = Math.max(...layout.map((n) => n.y)) + NODE_H;

  const svgW = Math.max(maxX - minX + 80, 400);
  const svgH = maxY + 40;
  const offsetX = -minX + 40;

  // Build id β†’ layout node map
  const byId = new Map(layout.map((n) => [n.id, n]));

  return (
    <div className="w-full overflow-x-auto">
      <svg
        width={svgW}
        height={svgH}
        style={{ display: "block", margin: "0 auto" }}
        aria-label={`${category} skill tree`}
      >
        {/* Draw edges first (behind nodes) */}
        {layout.map((node) => {
          if (node.parentNodeId == null) return null;
          const parent = byId.get(node.parentNodeId);
          if (!parent) return null;
          const x1 = parent.x + offsetX;
          const y1 = parent.y + NODE_H;
          const x2 = node.x + offsetX;
          const y2 = node.y;
          const midY = (y1 + y2) / 2;
          const bothUnlocked = node.unlocked && parent.unlocked;
          return (
            <path
              key={`edge-${node.id}`}
              d={`M ${x1} ${y1} C ${x1} ${midY}, ${x2} ${midY}, ${x2} ${y2}`}
              fill="none"
              stroke={bothUnlocked ? color : "rgba(255,255,255,0.1)"}
              strokeWidth={bothUnlocked ? 2 : 1}
              strokeDasharray={bothUnlocked ? undefined : "4 4"}
              opacity={bothUnlocked ? 0.7 : 0.35}
            />
          );
        })}

        {/* Draw nodes */}
        {layout.map((node, i) => {
          const cx = node.x + offsetX;
          const cy = node.y;
          const available = categoryLevel >= node.levelRequired;
          const isUnlocked = node.unlocked;
          const icon = NODE_TYPE_ICONS[node.nodeType] ?? "β—†";

          let fillOpacity = 0.08;
          let strokeColor = "rgba(255,255,255,0.12)";
          let textOpacity = 0.35;
          let glowOpacity = 0;

          if (isUnlocked) {
            fillOpacity = 0.2;
            strokeColor = color;
            textOpacity = 1;
            glowOpacity = 0.4;
          } else if (available) {
            fillOpacity = 0.1;
            strokeColor = `${color}80`;
            textOpacity = 0.7;
            glowOpacity = 0;
          }

          return (
            <g key={node.id} role="img" aria-label={node.name}>
              {/* Glow */}
              {isUnlocked && (
                <rect
                  x={cx - NODE_W / 2 - 4}
                  y={cy - 4}
                  width={NODE_W + 8}
                  height={NODE_H + 8}
                  rx={10}
                  fill={color}
                  opacity={glowOpacity}
                  style={{ filter: "blur(6px)" }}
                />
              )}
              {/* Node background */}
              <motion.rect
                x={cx - NODE_W / 2}
                y={cy}
                width={NODE_W}
                height={NODE_H}
                rx={7}
                fill={color}
                fillOpacity={fillOpacity}
                stroke={strokeColor}
                strokeWidth={isUnlocked ? 1.5 : 1}
                initial={{ opacity: 0, scale: 0.85 }}
                animate={{ opacity: 1, scale: 1 }}
                transition={{ delay: i * 0.04, duration: 0.3 }}
              />
              {/* Icon */}
              <text
                x={cx - NODE_W / 2 + 12}
                y={cy + 20}
                fontSize={13}
                textAnchor="middle"
                opacity={textOpacity}
              >
                {icon}
              </text>
              {/* Name */}
              <text
                x={cx}
                y={cy + 19}
                fontSize={10}
                fontWeight={600}
                textAnchor="middle"
                fill="white"
                opacity={textOpacity}
              >
                {node.name.length > 18 ? node.name.slice(0, 17) + "…" : node.name}
              </text>
              {/* Level requirement */}
              <text
                x={cx}
                y={cy + 35}
                fontSize={9}
                textAnchor="middle"
                fill="white"
                opacity={textOpacity * 0.7}
              >
                {isUnlocked ? "βœ“ unlocked" : `Lv ${node.levelRequired}`}
              </text>
            </g>
          );
        })}
      </svg>

      {/* Legend */}
      <div className="flex gap-4 justify-center mt-3 flex-wrap">
        {[
          { icon: "⚑", label: "Difficulty unlock" },
          { icon: "🎯", label: "Exercise type" },
          { icon: "✨", label: "XP multiplier" },
          { icon: "πŸ›‘οΈ", label: "Streak shield" },
          { icon: "πŸ‘‘", label: "Cosmetic title" },
        ].map(({ icon, label }) => (
          <div key={label} className="flex items-center gap-1 text-xs" style={{ color: "var(--text-muted)" }}>
            <span>{icon}</span>
            <span>{label}</span>
          </div>
        ))}
      </div>
    </div>
  );
}