|
| 1 | +import { useEffect, useState, useCallback, useRef } from 'react'; |
| 2 | +import { |
| 3 | + Timer, Layers, Lightbulb, FileText, Flame, Trophy, Medal, |
| 4 | + type LucideIcon, |
| 5 | +} from 'lucide-react'; |
| 6 | +import type { Achievement } from '@/types/models'; |
| 7 | + |
| 8 | +const ICON_MAP: Record<string, LucideIcon> = { |
| 9 | + Timer, Layers, Lightbulb, FileText, Flame, Trophy, Medal, |
| 10 | +}; |
| 11 | + |
| 12 | +// 8 particle directions (angle in degrees) |
| 13 | +const PARTICLE_DIRS = [0, 45, 90, 135, 180, 225, 270, 315]; |
| 14 | +const PARTICLE_DIST = 28; // px |
| 15 | + |
| 16 | +interface ToastEntry extends Achievement { |
| 17 | + localId: number; |
| 18 | + exiting: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export default function AchievementToast() { |
| 22 | + const [toasts, setToasts] = useState<ToastEntry[]>([]); |
| 23 | + const idRef = useRef(0); |
| 24 | + |
| 25 | + const dismiss = useCallback((localId: number) => { |
| 26 | + setToasts((prev) => prev.map((t) => (t.localId === localId ? { ...t, exiting: true } : t))); |
| 27 | + setTimeout(() => { |
| 28 | + setToasts((prev) => prev.filter((t) => t.localId !== localId)); |
| 29 | + }, 300); |
| 30 | + }, []); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const handler = (e: Event) => { |
| 34 | + const a = (e as CustomEvent<Achievement>).detail; |
| 35 | + if (!a) return; |
| 36 | + const localId = ++idRef.current; |
| 37 | + setToasts((prev) => [...prev, { ...a, localId, exiting: false }]); |
| 38 | + // Auto-dismiss after 4s |
| 39 | + setTimeout(() => dismiss(localId), 4000); |
| 40 | + }; |
| 41 | + window.addEventListener('achievement-unlocked', handler); |
| 42 | + return () => window.removeEventListener('achievement-unlocked', handler); |
| 43 | + }, [dismiss]); |
| 44 | + |
| 45 | + if (toasts.length === 0) return null; |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className="fixed bottom-6 right-6 z-[60] flex flex-col gap-3 items-end pointer-events-none"> |
| 49 | + {toasts.map((t) => { |
| 50 | + const Icon = ICON_MAP[t.icon] ?? Trophy; |
| 51 | + return ( |
| 52 | + <div |
| 53 | + key={t.localId} |
| 54 | + className={[ |
| 55 | + 'pointer-events-auto relative flex items-center gap-3 px-4 py-3', |
| 56 | + 'bg-bg-elevated/95 backdrop-blur-md rounded-kb-lg shadow-kb-lg', |
| 57 | + 'border border-brand-500/30', |
| 58 | + 'min-w-[240px] max-w-xs', |
| 59 | + t.exiting |
| 60 | + ? 'opacity-0 translate-x-4 transition-all duration-300' |
| 61 | + : 'kb-achievement-toast', |
| 62 | + ].join(' ')} |
| 63 | + > |
| 64 | + {/* Icon with particles */} |
| 65 | + <div className="relative flex-shrink-0"> |
| 66 | + <div className="w-10 h-10 rounded-kb-lg bg-brand-500/15 flex items-center justify-center"> |
| 67 | + <Icon className="w-5 h-5 text-brand-500" strokeWidth={1.5} /> |
| 68 | + </div> |
| 69 | + {/* Particles */} |
| 70 | + {!t.exiting && |
| 71 | + PARTICLE_DIRS.map((deg, i) => { |
| 72 | + const rad = (deg * Math.PI) / 180; |
| 73 | + const tx = Math.round(Math.cos(rad) * PARTICLE_DIST); |
| 74 | + const ty = Math.round(Math.sin(rad) * PARTICLE_DIST); |
| 75 | + return ( |
| 76 | + <span |
| 77 | + key={i} |
| 78 | + className="kb-particle" |
| 79 | + style={{ |
| 80 | + left: '50%', |
| 81 | + top: '50%', |
| 82 | + marginLeft: -2, |
| 83 | + marginTop: -2, |
| 84 | + // @ts-expect-error custom CSS properties |
| 85 | + '--kb-tx': `${tx}px`, |
| 86 | + '--kb-ty': `${ty}px`, |
| 87 | + animationDelay: `${i * 60}ms`, |
| 88 | + }} |
| 89 | + /> |
| 90 | + ); |
| 91 | + })} |
| 92 | + </div> |
| 93 | + |
| 94 | + {/* Text */} |
| 95 | + <div className="flex flex-col min-w-0"> |
| 96 | + <span className="text-c1 text-brand-500 font-medium uppercase tracking-wide"> |
| 97 | + 成就解锁 |
| 98 | + </span> |
| 99 | + <span className="text-b2 font-semibold text-text-primary truncate"> |
| 100 | + {t.title} |
| 101 | + </span> |
| 102 | + <span className="text-c1 text-text-tertiary truncate"> |
| 103 | + {t.description} |
| 104 | + </span> |
| 105 | + </div> |
| 106 | + |
| 107 | + {/* Close */} |
| 108 | + <button |
| 109 | + onClick={() => dismiss(t.localId)} |
| 110 | + className="absolute top-1.5 right-1.5 p-1 rounded-kb-full text-text-tertiary hover:text-text-primary hover:bg-bg-tertiary transition-all" |
| 111 | + > |
| 112 | + <svg width="12" height="12" viewBox="0 0 12 12" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"> |
| 113 | + <path d="M3 3l6 6M9 3l-6 6" /> |
| 114 | + </svg> |
| 115 | + </button> |
| 116 | + </div> |
| 117 | + ); |
| 118 | + })} |
| 119 | + </div> |
| 120 | + ); |
| 121 | +} |
0 commit comments