File size: 6,535 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
"use client";

/**
 * LevelUpOverlay — full-screen celebration overlay that auto-dismisses.
 *
 * Usage:
 *   <LevelUpOverlay
 *     show={showLevelUp}
 *     level={newLevel}
 *     title={newTitle}          // optional — only shown if changed
 *     category={category}       // for accent color
 *     onDismiss={() => setShowLevelUp(false)}
 *   />
 *
 * Auto-dismisses after 3 seconds. Click anywhere to dismiss early.
 */

import { useEffect, useCallback } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { CATEGORY_COLORS, CATEGORY_LABELS, type Category } from "@/config/xp";
import { nativeHaptics } from "@/lib/native";

export interface LevelUpOverlayProps {
  show: boolean;
  level: number;
  title?: string;          // new title if unlocked at this level
  category: Category;
  onDismiss: () => void;
  autoDismissMs?: number;  // default 3000
}

/** One CSS-only particle — absolutely positioned, randomised via inline style */
function Particle({ accent, delay, i }: { accent: string; delay: number; i: number }) {
  // Distribute particles around the screen using index
  const angle = (i / 20) * 360;
  const distance = 120 + (i % 5) * 40; // 120–280 px
  const dx = Math.round(Math.cos((angle * Math.PI) / 180) * distance);
  const dy = Math.round(Math.sin((angle * Math.PI) / 180) * distance);
  const size = 6 + (i % 4) * 3; // 6–15 px
  const colors = [accent, "#ffffff", "#f59e0b", "#22c55e", "#8b5cf6"];
  const color = colors[i % colors.length];

  return (
    <motion.span
      aria-hidden
      className="absolute rounded-full"
      style={{
        width: size,
        height: size,
        background: color,
        top: "50%",
        left: "50%",
        marginLeft: -size / 2,
        marginTop: -size / 2,
        opacity: 0,
      }}
      animate={{
        x: [0, dx],
        y: [0, dy],
        opacity: [0, 1, 0],
        scale: [0.3, 1, 0.4],
      }}
      transition={{
        duration: 1.2,
        delay,
        ease: "easeOut",
        times: [0, 0.4, 1],
      }}
    />
  );
}

export function LevelUpOverlay({
  show,
  level,
  title,
  category,
  onDismiss,
  autoDismissMs = 3000,
}: LevelUpOverlayProps) {
  const dismiss = useCallback(() => onDismiss(), [onDismiss]);

  useEffect(() => {
    if (!show) return;
    // Trigger heavy haptic on level-up
    nativeHaptics.levelUp();
    const t = setTimeout(dismiss, autoDismissMs);
    return () => clearTimeout(t);
  }, [show, dismiss, autoDismissMs]);

  const accent = CATEGORY_COLORS[category];
  const catLabel = CATEGORY_LABELS[category];

  return (
    <AnimatePresence>
      {show && (
        <motion.div
          key="level-up-overlay"
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          exit={{ opacity: 0 }}
          transition={{ duration: 0.25 }}
          className="fixed inset-0 z-50 flex items-center justify-center"
          style={{ background: "rgba(0,0,0,0.75)", backdropFilter: "blur(6px)" }}
          onClick={dismiss}
          role="dialog"
          aria-modal="true"
          aria-label={`Level up! You reached level ${level}`}
        >
          {/* Particle field */}
          <div className="relative" aria-hidden>
            {Array.from({ length: 20 }).map((_, i) => (
              <Particle key={i} i={i} accent={accent} delay={i * 0.03} />
            ))}
          </div>

          {/* Content card */}
          <motion.div
            initial={{ scale: 0.6, opacity: 0, y: 30 }}
            animate={{ scale: 1, opacity: 1, y: 0 }}
            exit={{ scale: 0.8, opacity: 0, y: -20 }}
            transition={{ type: "spring", stiffness: 360, damping: 26, delay: 0.05 }}
            className="absolute glass rounded-3xl px-12 py-10 text-center select-none"
            style={{
              borderColor: accent,
              borderWidth: 2,
              boxShadow: `0 0 60px ${accent}55, 0 0 120px ${accent}22`,
              minWidth: 320,
            }}
            onClick={(e) => e.stopPropagation()}
          >
            {/* Glow ring */}
            <motion.div
              className="absolute inset-0 rounded-3xl pointer-events-none"
              style={{ background: `radial-gradient(ellipse at center, ${accent}18 0%, transparent 70%)` }}
              animate={{ opacity: [0.6, 1, 0.6] }}
              transition={{ duration: 2, repeat: Infinity, ease: "easeInOut" }}
            />

            {/* Category badge */}
            <p
              className="text-xs uppercase tracking-widest mb-3 font-semibold"
              style={{ color: accent }}
            >
              {catLabel}
            </p>

            {/* ⬆ icon */}
            <motion.div
              className="text-5xl mb-2"
              animate={{ y: [0, -8, 0] }}
              transition={{ duration: 1.2, repeat: Infinity, ease: "easeInOut" }}
            >
              ⬆
            </motion.div>

            {/* Level number */}
            <motion.p
              className="font-bold mb-1"
              style={{
                fontFamily: "var(--font-mono)",
                fontSize: "5rem",
                lineHeight: 1,
                color: accent,
                textShadow: `0 0 30px ${accent}`,
              }}
              animate={{ textShadow: [`0 0 20px ${accent}88`, `0 0 50px ${accent}`, `0 0 20px ${accent}88`] }}
              transition={{ duration: 1.6, repeat: Infinity, ease: "easeInOut" }}
            >
              {level}
            </motion.p>

            <p
              className="text-xl font-bold mb-1"
              style={{ color: "var(--text-primary)" }}
            >
              Level Up!
            </p>

            {title && (
              <motion.p
                initial={{ opacity: 0, y: 8 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ delay: 0.5 }}
                className="mt-3 px-4 py-1.5 rounded-full text-sm font-semibold"
                style={{
                  background: `${accent}22`,
                  color: accent,
                  border: `1px solid ${accent}66`,
                  display: "inline-block",
                }}
              >
                New Title: {title}
              </motion.p>
            )}

            <p
              className="mt-5 text-xs"
              style={{ color: "var(--text-muted)" }}
            >
              Click anywhere to continue
            </p>
          </motion.div>
        </motion.div>
      )}
    </AnimatePresence>
  );
}