RotNot / src /components /shared /xp-results.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
7.67 kB
"use client";
/**
* XpResults β€” slides in from right after an exercise.
*
* Props:
* category β€” used for accent color
* baseXp β€” raw base XP before bonuses
* qualityBonus β€” bonus XP from quality score
* isCriticalHit β€” whether 2Γ— crit multiplier applied
* totalXp β€” final XP awarded
* score? β€” 0-1 quality score (optional, shown as %)
* categoryCurrentXp β€” current XP in category after award
* categoryXpToNext β€” XP needed to reach next category level
* categoryLevel β€” current category level (after any level-up)
* levelUp? β€” if true, renders a "Level Up" badge inline
* onNext β€” callback for primary CTA (e.g. "Try Another")
* nextLabel β€” label for the CTA button (default: "Continue")
* children? β€” optional slot for extra detail rows above XP breakdown
*/
import { useEffect } from "react";
import { motion } from "framer-motion";
import { CATEGORY_COLORS, CATEGORY_LABELS, type Category } from "@/config/xp";
import { nativeHaptics } from "@/lib/native";
export interface XpResultsProps {
category: Category;
baseXp: number;
qualityBonus: number;
isCriticalHit: boolean;
totalXp: number;
score?: number; // 0-1
categoryCurrentXp: number;
categoryXpToNext: number;
categoryLevel: number;
levelUp?: boolean;
onNext: () => void;
nextLabel?: string;
children?: React.ReactNode;
/** Skill tree multiplier bonus XP (from +10%/+25% nodes) */
multiplierBonus?: number;
/** Multiplier percent active (e.g. 10 = +10%) β€” shown in the row label */
multiplierPercent?: number;
}
export function XpResults({
category,
baseXp,
qualityBonus,
isCriticalHit,
totalXp,
score,
categoryCurrentXp,
categoryXpToNext,
categoryLevel,
levelUp = false,
onNext,
nextLabel = "Continue",
children,
multiplierBonus,
multiplierPercent,
}: XpResultsProps) {
const accent = CATEGORY_COLORS[category];
const label = CATEGORY_LABELS[category];
const barPercent = Math.min(100, Math.round((categoryCurrentXp / categoryXpToNext) * 100));
// Native haptic feedback when the XP results panel mounts
useEffect(() => {
if (isCriticalHit) {
nativeHaptics.levelUp();
} else if (levelUp) {
nativeHaptics.levelUp();
} else {
nativeHaptics.xpGain();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<motion.div
initial={{ opacity: 0, x: 60 }}
animate={{ opacity: 1, x: 0 }}
exit={{ opacity: 0, x: 60 }}
transition={{ type: "spring", stiffness: 280, damping: 28 }}
className="glass rounded-2xl p-6"
style={{ borderColor: accent, borderWidth: 1 }}
>
{/* Header row */}
<div className="flex items-center justify-between mb-5">
<h3
className="text-lg font-bold"
style={{ fontFamily: "var(--font-mono)" }}
>
XP Earned
</h3>
{levelUp && (
<motion.span
initial={{ scale: 0.6, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
transition={{ type: "spring", stiffness: 400, damping: 20, delay: 0.3 }}
className="px-3 py-1 rounded-full text-xs font-bold uppercase tracking-wider"
style={{
background: `${accent}28`,
color: accent,
border: `1px solid ${accent}`,
}}
>
⬆ Level Up!
</motion.span>
)}
</div>
{/* Optional score / detail slot */}
{score !== undefined && (
<div className="mb-4 flex items-center gap-3">
<div
className="text-sm"
style={{ color: "var(--text-secondary)" }}
>
Quality Score
</div>
<QualityPill score={score} accent={accent} />
</div>
)}
{children && <div className="mb-4">{children}</div>}
{/* XP breakdown */}
<div
className="space-y-2 mb-5"
style={{ fontFamily: "var(--font-mono)", fontSize: "0.875rem" }}
>
<XpRow label="Base XP" value={`${baseXp}`} />
<XpRow
label="Quality Bonus"
value={`+${qualityBonus}`}
valueColor={qualityBonus > 0 ? "#22c55e" : "var(--text-muted)"}
/>
{isCriticalHit && (
<motion.div
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
transition={{ delay: 0.25 }}
>
<XpRow label="⚑ Critical Hit!" value="Γ—2" valueColor="#f59e0b" />
</motion.div>
)}
{typeof multiplierBonus === "number" && multiplierBonus > 0 && (
<motion.div
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
transition={{ delay: 0.35 }}
>
<XpRow
label={`🌟 Skill Tree Bonus${typeof multiplierPercent === "number" && multiplierPercent > 0 ? ` (+${multiplierPercent}%)` : ""}`}
value={`+${multiplierBonus}`}
valueColor="#a78bfa"
/>
</motion.div>
)}
<div
className="flex justify-between pt-2 text-base font-bold"
style={{ borderTop: "1px solid rgba(255,255,255,0.08)" }}
>
<span>Total</span>
<motion.span
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: 0.4 }}
style={{ color: accent }}
>
{totalXp} XP
</motion.span>
</div>
</div>
{/* Category skill progress bar */}
<div className="mb-6">
<div className="flex justify-between mb-1.5 text-xs" style={{ color: "var(--text-secondary)" }}>
<span>
{label} β€” Lvl {categoryLevel}
</span>
<span style={{ fontFamily: "var(--font-mono)" }}>
{categoryCurrentXp}/{categoryXpToNext}
</span>
</div>
<div
className="w-full h-2.5 rounded-full overflow-hidden"
style={{ background: "rgba(255,255,255,0.08)" }}
>
<motion.div
className="h-full rounded-full"
style={{ background: accent }}
initial={{ width: 0 }}
animate={{ width: `${barPercent}%` }}
transition={{ duration: 0.9, delay: 0.35, ease: "easeOut" }}
/>
</div>
</div>
{/* CTA */}
<button
onClick={onNext}
className="w-full py-3 rounded-xl font-bold text-base transition-transform hover:scale-[1.02] active:scale-[0.98]"
style={{ background: accent, color: "#000" }}
>
{nextLabel}
</button>
</motion.div>
);
}
/* ─── internal sub-components ─────────────────────────────────────── */
function XpRow({
label,
value,
valueColor,
}: {
label: string;
value: string;
valueColor?: string;
}) {
return (
<div className="flex justify-between">
<span style={{ color: "var(--text-secondary)" }}>{label}</span>
<span style={{ color: valueColor ?? "var(--text-primary)" }}>{value}</span>
</div>
);
}
function QualityPill({ score }: { score: number; accent?: string }) {
const pct = Math.round(score * 100);
const color =
pct >= 70 ? "#22c55e" : pct >= 40 ? "#eab308" : "#ef4444";
return (
<span
className="px-2 py-0.5 rounded-md text-xs font-bold"
style={{
background: `${color}20`,
color,
fontFamily: "var(--font-mono)",
}}
>
{pct}%
</span>
);
}