File size: 7,671 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
"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>
  );
}