"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(); 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(); 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 = { 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 (
No skill tree data for this category.
); } 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 (
{/* 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 ( ); })} {/* 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 ( {/* Glow */} {isUnlocked && ( )} {/* Node background */} {/* Icon */} {icon} {/* Name */} {node.name.length > 18 ? node.name.slice(0, 17) + "…" : node.name} {/* Level requirement */} {isUnlocked ? "✓ unlocked" : `Lv ${node.levelRequired}`} ); })} {/* Legend */}
{[ { icon: "⚡", label: "Difficulty unlock" }, { icon: "🎯", label: "Exercise type" }, { icon: "✨", label: "XP multiplier" }, { icon: "🛡️", label: "Streak shield" }, { icon: "👑", label: "Cosmetic title" }, ].map(({ icon, label }) => (
{icon} {label}
))}
); }