"use client";
import React, { useEffect, useRef, useMemo } from "react";
import { createPortal } from "react-dom";
import gsap from "gsap";
import {
Play,
Pause,
SkipForward,
SkipBack,
X,
Volume2,
VolumeX,
Repeat,
Repeat1,
Shuffle,
Music2,
ListMusic,
Radio,
Loader2,
Heart,
} from "lucide-react";
import type { TrackDetail } from "@/interface/music";
const DefaultVinylCover = () => (
{/* 唱片的同心圆纹路 */}
{/* 红心胶心 */}
);
export interface LyricLine {
time: number;
text: string;
translation?: string;
}
function parseLyric(lrc?: string, tlrc?: string): LyricLine[] {
if (!lrc && !tlrc) return [];
const parseSingle = (content?: string) => {
if (!content) return [];
const lines = content.split("\n");
const result: { time: number; text: string }[] = [];
const timeRegex = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/;
for (const line of lines) {
const match = timeRegex.exec(line);
if (match) {
const minutes = parseInt(match[1], 10);
const seconds = parseInt(match[2], 10);
const milliseconds = parseInt(match[3], 10);
const time = minutes * 60 + seconds + (milliseconds > 99 ? milliseconds / 1000 : milliseconds / 100);
const text = line.replace(timeRegex, "").trim();
if (text) {
result.push({ time, text });
}
}
}
return result;
};
const mainLines = parseSingle(lrc);
const transLines = parseSingle(tlrc);
const resultMap = new Map();
for (const item of mainLines) {
const key = Math.floor(item.time * 10) / 10;
resultMap.set(key, { time: item.time, text: item.text });
}
for (const item of transLines) {
const key = Math.floor(item.time * 10) / 10;
const existing = resultMap.get(key);
if (existing) {
existing.translation = item.text;
} else {
resultMap.set(key, { time: item.time, text: item.text, translation: item.text });
}
}
return Array.from(resultMap.values()).sort((a, b) => a.time - b.time);
}
interface ExpandedPlayerModalProps {
isOpen: boolean;
onClose: () => void;
currentTrack: TrackDetail;
isPlaying: boolean;
isResolvingTrack?: boolean;
isLoadingAudio?: boolean;
onTogglePlay: () => void;
onPlayNext: () => void;
onPlayPrev: () => void;
currentTime: number;
duration: number;
onSeek: (time: number) => void;
bgmVolume: number;
isMuted: boolean;
onVolumeChange: (val: number) => void;
onToggleMute: () => void;
playMode: "sequence" | "repeat" | "shuffle";
onCycleMode: () => void;
formatTime: (seconds: number) => string;
onOpenDrawer: () => void;
isFavorite?: boolean;
onToggleFavorite?: () => void;
brokenCovers?: Set;
playerMessage?: string;
onMarkBrokenCover?: (id: string) => void;
playlistLength?: number;
}
export default function ExpandedPlayerModal({
isOpen,
onClose,
currentTrack,
isPlaying,
isResolvingTrack = false,
isLoadingAudio = false,
onTogglePlay,
onPlayNext,
onPlayPrev,
currentTime,
duration,
onSeek,
bgmVolume,
isMuted,
onVolumeChange,
onToggleMute,
playMode,
onCycleMode,
formatTime,
onOpenDrawer,
isFavorite = false,
onToggleFavorite,
brokenCovers,
playerMessage,
onMarkBrokenCover,
playlistLength = 0,
}: ExpandedPlayerModalProps) {
const [mounted, setMounted] = React.useState(false);
const lyricScrollRef = useRef(null);
const activeLyricRef = useRef(null);
const backdropRef = useRef(null);
const cardRef = useRef(null);
useEffect(() => {
setMounted(true);
}, []);
const [localCoverFailed, setLocalCoverFailed] = React.useState(false);
useEffect(() => {
setLocalCoverFailed(false);
}, [currentTrack?.id]);
const lyricLines = useMemo(
() => parseLyric(currentTrack?.lyric, currentTrack?.tlyric),
[currentTrack?.lyric, currentTrack?.tlyric]
);
const activeIndex = useMemo(() => {
if (lyricLines.length === 0) return -1;
const index = lyricLines.findIndex((line) => line.time > currentTime);
if (index === -1) return lyricLines.length - 1;
return Math.max(0, index - 1);
}, [currentTime, lyricLines]);
// 滚动高亮歌词到视图中央
useEffect(() => {
if (activeLyricRef.current && lyricScrollRef.current) {
const activeEl = activeLyricRef.current;
const containerEl = lyricScrollRef.current;
const top = activeEl.offsetTop - containerEl.offsetTop - containerEl.clientHeight / 2 + activeEl.clientHeight / 2;
containerEl.scrollTo({ top: Math.max(0, top), behavior: "smooth" });
}
}, [activeIndex]);
// GSAP 打开/收起动画
useEffect(() => {
if (!mounted) return;
const backdrop = backdropRef.current;
const card = cardRef.current;
if (!backdrop || !card) return;
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const ctx = gsap.context(() => {
if (isOpen) {
gsap.set(backdrop, { display: "flex", autoAlpha: 0 });
gsap.set(card, { scale: 0.95, y: 20, autoAlpha: 0 });
gsap.to(backdrop, {
autoAlpha: 1,
duration: reduceMotion ? 0 : 0.3,
ease: "power2.out",
});
gsap.to(card, {
scale: 1,
y: 0,
autoAlpha: 1,
duration: reduceMotion ? 0 : 0.4,
ease: "back.out(1.2)",
});
} else {
gsap.to(card, {
scale: 0.95,
y: 20,
autoAlpha: 0,
duration: reduceMotion ? 0 : 0.25,
ease: "power2.in",
});
gsap.to(backdrop, {
autoAlpha: 0,
duration: reduceMotion ? 0 : 0.3,
ease: "power2.in",
onComplete: () => {
if (backdrop) gsap.set(backdrop, { display: "none" });
},
});
}
});
return () => ctx.revert();
}, [isOpen, mounted]);
if (!isOpen || !mounted || typeof document === "undefined") return null;
const isLoading = isResolvingTrack || isLoadingAudio;
const coverIsAvailable = Boolean(
playlistLength > 0 &&
currentTrack?.cover &&
!brokenCovers?.has(currentTrack.id) &&
!localCoverFailed
);
const modalContent = (
e.stopPropagation()}
className="w-full max-w-5xl h-[85vh] max-h-[750px] bg-(--bg-dark-color)/95 border border-(--border-line-color) rounded-3xl shadow-[0_25px_80px_rgba(0,0,0,0.5)] flex flex-col md:flex-row overflow-hidden relative"
>
{/* 1. 左侧:黑胶唱片与控制 */}
{currentTrack?.source || "netease"} 音源
{onToggleFavorite && (
)}
{/* 拟物黑胶唱片 */}
{coverIsAvailable ? (
// eslint-disable-next-line @next/next/no-img-element

{
onMarkBrokenCover?.(currentTrack.id);
setLocalCoverFailed(true);
}}
/>
) : (
)}
{currentTrack?.title || "暂无歌曲播放"}
{currentTrack?.artist || "未知歌手"} • {currentTrack?.album || "生活档案馆"}
{/* 2. 右侧:独立流动歌词面板 */}
e.stopPropagation()}
style={{ scrollBehavior: "smooth" }}
>
{lyricLines.length > 0 ? (
lyricLines.map((line, idx) => {
const isActive = idx === activeIndex;
return (
onSeek(line.time)}
className={`transition-all duration-300 text-center leading-relaxed cursor-pointer select-none hover:scale-105 active:scale-95 ${
isActive
? "text-xl md:text-2xl font-bold text-(--accent-color) scale-105 drop-shadow-[0_0_12px_rgba(224,122,95,0.4)]"
: "text-sm md:text-base text-(--fg-color)/40 hover:text-(--fg-color)/80"
}`}
title={`点击跳转至 ${formatTime(line.time)}`}
>
{line.text}
{line.translation && (
{line.translation}
)}
);
})
) : (
纯音乐 / 暂无歌词
{currentTrack?.album || "生活档案馆 • 治愈听觉体验"}
)}
{currentTrack?.title} - {currentTrack?.artist}
);
return createPortal(modalContent, document.body);
}