RotNot / src /components /profile /skill-tree.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
7.75 kB
"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>
);
}