|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Image from "next/image"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { useCallback, useEffect, useMemo, useState } from "react"; |
| 6 | +import { ChevronLeft, ChevronRight } from "lucide-react"; |
| 7 | +import eventsData from "@/data/event.json"; |
| 8 | +import type { ActivityEvent, ActivityEventsConfig } from "@/app/types/event"; |
| 9 | +import { cn } from "@/lib/utils"; |
| 10 | + |
| 11 | +const { |
| 12 | + events: rawEvents, |
| 13 | + settings: { |
| 14 | + maxItems: configuredMaxItems = 3, |
| 15 | + rotationIntervalMs: configuredRotationIntervalMs = 8000, |
| 16 | + }, |
| 17 | +} = eventsData as ActivityEventsConfig; |
| 18 | + |
| 19 | +// 默认配置,从data/event.json中读取配置 |
| 20 | +const MAX_ITEMS = configuredMaxItems; |
| 21 | +const ROTATION_INTERVAL_MS = configuredRotationIntervalMs; |
| 22 | + |
| 23 | +/** ActivityTicker 外部传入的样式配置 */ |
| 24 | +type ActivityTickerProps = { |
| 25 | + /** 容器额外类名,用于控制宽度与定位 */ |
| 26 | + className?: string; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * 首页活动轮播组件: |
| 31 | + * - 读取 event.json 配置的活动数量 |
| 32 | + * - 自动轮播封面图,顶部指示器支持手动切换 |
| 33 | + * - 底部两个毛玻璃按钮:Discord 永远可见,Playback 仅在 deprecated=true 时显示 |
| 34 | + */ |
| 35 | +export function ActivityTicker({ className }: ActivityTickerProps) { |
| 36 | + // 预处理活动列表,保持初次渲染后的引用稳定 |
| 37 | + const events = useMemo<ActivityEvent[]>(() => { |
| 38 | + return rawEvents.slice(0, MAX_ITEMS); |
| 39 | + }, []); |
| 40 | + |
| 41 | + // 当前展示的活动索引 |
| 42 | + const [activeIndex, setActiveIndex] = useState(0); |
| 43 | + const totalEvents = events.length; |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + if (totalEvents <= 1) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // 定时轮播,间隔 ROTATION_INTERVAL_MS |
| 51 | + const timer = window.setInterval(() => { |
| 52 | + setActiveIndex((prev) => (prev + 1) % totalEvents); |
| 53 | + }, ROTATION_INTERVAL_MS); |
| 54 | + |
| 55 | + return () => window.clearInterval(timer); |
| 56 | + }, [totalEvents, activeIndex]); |
| 57 | + |
| 58 | + const handlePrev = useCallback(() => { |
| 59 | + if (totalEvents <= 1) { |
| 60 | + return; |
| 61 | + } |
| 62 | + setActiveIndex((prev) => (prev - 1 + totalEvents) % totalEvents); |
| 63 | + }, [totalEvents]); |
| 64 | + |
| 65 | + const handleNext = useCallback(() => { |
| 66 | + if (totalEvents <= 1) { |
| 67 | + return; |
| 68 | + } |
| 69 | + setActiveIndex((prev) => (prev + 1) % totalEvents); |
| 70 | + }, [totalEvents]); |
| 71 | + |
| 72 | + if (totalEvents === 0) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + const activeEvent = events[activeIndex]; |
| 77 | + const coverSrc = activeEvent.coverUrl; |
| 78 | + const showPlayback = activeEvent.deprecated && Boolean(activeEvent.playback); |
| 79 | + |
| 80 | + return ( |
| 81 | + <aside |
| 82 | + className={cn( |
| 83 | + "relative w-full overflow-hidden rounded-2xl border border-border bg-background/70 text-left shadow-md backdrop-blur supports-[backdrop-filter]:bg-background/50", |
| 84 | + className, |
| 85 | + )} |
| 86 | + > |
| 87 | + <div className="group relative aspect-[5/4] w-full overflow-hidden"> |
| 88 | + <Image |
| 89 | + src={coverSrc} |
| 90 | + alt={activeEvent.name} |
| 91 | + fill |
| 92 | + sizes="(min-width: 1024px) 320px, (min-width: 640px) 288px, 90vw" |
| 93 | + priority |
| 94 | + className="object-contain object-top" |
| 95 | + /> |
| 96 | + {/* 下半透明渐变,用于保证文字与按钮对比度 */} |
| 97 | + <div className="absolute inset-x-0 bottom-0 h-1/2 bg-gradient-to-t from-black/70 via-black/30 to-transparent" /> |
| 98 | + {events.length > 1 && ( |
| 99 | + <> |
| 100 | + {/* 多条活动时显示手动切换指示器 */} |
| 101 | + <div className="absolute inset-x-0 top-0 flex justify-end gap-1 p-3"> |
| 102 | + {events.map((event, idx) => ( |
| 103 | + <button |
| 104 | + key={`${event.name}-${idx}`} |
| 105 | + type="button" |
| 106 | + onClick={() => setActiveIndex(idx)} |
| 107 | + aria-label={`切换到 ${event.name}`} |
| 108 | + className={cn( |
| 109 | + "h-1.5 w-6 rounded-full transition-opacity", |
| 110 | + idx === activeIndex |
| 111 | + ? "bg-white/90 opacity-100" |
| 112 | + : "bg-white/40 opacity-60 hover:opacity-85", |
| 113 | + )} |
| 114 | + /> |
| 115 | + ))} |
| 116 | + </div> |
| 117 | + <button |
| 118 | + type="button" |
| 119 | + aria-label="上一条活动" |
| 120 | + onClick={handlePrev} |
| 121 | + className="absolute left-3 top-1/2 z-30 flex h-9 w-9 -translate-y-1/2 items-center justify-center rounded-full bg-black/35 text-white shadow-sm transition hover:bg-black/55 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/70" |
| 122 | + > |
| 123 | + <ChevronLeft className="h-4 w-4" /> |
| 124 | + </button> |
| 125 | + <button |
| 126 | + type="button" |
| 127 | + aria-label="下一条活动" |
| 128 | + onClick={handleNext} |
| 129 | + className="absolute right-3 top-1/2 z-30 flex h-9 w-9 -translate-y-1/2 items-center justify-center rounded-full bg-black/35 text-white shadow-sm transition hover:bg-black/55 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white/70" |
| 130 | + > |
| 131 | + <ChevronRight className="h-4 w-4" /> |
| 132 | + </button> |
| 133 | + </> |
| 134 | + )} |
| 135 | + {/* 底部毛玻璃按钮,根据 deprecated 控制回放按钮可见性 */} |
| 136 | + <div |
| 137 | + className={cn( |
| 138 | + "absolute inset-x-0 bottom-0 top-3/4 z-10 grid border-t border-white/15 bg-white/20 text-sm font-medium text-white shadow-lg backdrop-blur-md", |
| 139 | + showPlayback ? "grid-cols-2" : "grid-cols-1", |
| 140 | + )} |
| 141 | + > |
| 142 | + <Link |
| 143 | + href={activeEvent.discord} |
| 144 | + prefetch={false} |
| 145 | + className="flex h-full items-center justify-center px-3 text-white transition-colors hover:bg-white/25 hover:text-white" |
| 146 | + > |
| 147 | + Discord |
| 148 | + </Link> |
| 149 | + {showPlayback && ( |
| 150 | + <Link |
| 151 | + href={activeEvent.playback as string} |
| 152 | + prefetch={false} |
| 153 | + className="flex h-full items-center justify-center border-l border-white/15 px-3 text-white transition-colors hover:bg-white/25 hover:text-white" |
| 154 | + > |
| 155 | + Playback |
| 156 | + </Link> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + </aside> |
| 161 | + ); |
| 162 | +} |
0 commit comments