"use client";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState, type ReactNode } from "react";
import { BackButton } from "@/components/common/back-button";
import { pickPlayletCardTags } from "@/components/common/card-tags";
import { DecryptedImage } from "@/components/common/decrypted-image";
import {
dramaDetailHref,
playHref,
totalEpisodeOf,
updateStatusLabel,
type DramaDetail,
type RelatedDrama,
} from "@/components/drama-detail/data";
import { openAppDownload } from "@/components/download/download-dialog";
import { EpisodeDirectory } from "@/components/media-page/episode-directory";
import { DramaRecommend, HlAction, TagLinks, tagLinkItems } from "@/components/media-page/parts";
import { playbackRates, XgPlayer, type XgPlayerInstance } from "@/components/player/xg-player";
import { CheckIcon, ChevronIcon } from "./icons";
type Sheet = "speed" | null;
/** 播完自动下一集倒计时(秒) */
const AUTO_NEXT_SECONDS = 3;
/** 切集后自动起播标记(sessionStorage) */
const AUTOPLAY_NEXT_KEY = "hjsq-drama-autoplay-next";
function consumeAutoplayFlag() {
if (typeof window === "undefined") return false;
try {
if (sessionStorage.getItem(AUTOPLAY_NEXT_KEY) !== "1") return false;
sessionStorage.removeItem(AUTOPLAY_NEXT_KEY);
return true;
} catch {
return false;
}
}
function markAutoplayNext() {
try {
sessionStorage.setItem(AUTOPLAY_NEXT_KEY, "1");
} catch {
/* ignore */
}
}
type H5PlayProps = {
detail: DramaDetail;
episode: number;
src: string;
locked: boolean;
liked: boolean;
/** 点赞数 displayCountLike */
displayCountLike?: number;
collected: boolean;
/** 收藏数 countCollect */
countCollect?: number;
related?: RelatedDrama[];
onLike: () => void;
onCollect: () => void;
onShare: () => void;
onPlaySuccess?: () => void;
};
function LockedNotice() {
return (
);
}
function formatCount(value: number) {
return value >= 10000 ? `${(value / 10000).toFixed(1)}w` : String(value);
}
function BottomSheet({
open,
onClose,
label,
className = "",
children,
}: {
open: boolean;
onClose: () => void;
label: string;
className?: string;
children: ReactNode;
}) {
return (
<>
{children}
>
);
}
function SheetHead({ title, onClose, children }: { title: ReactNode; onClose: () => void; children?: ReactNode }) {
return (
{title}
{children ?? }
);
}
export function H5Play({
detail,
episode,
src,
locked,
liked,
displayCountLike,
collected,
countCollect,
related = [],
onLike,
onCollect,
onShare,
onPlaySuccess,
}: H5PlayProps) {
const router = useRouter();
const playerRef = useRef(null);
const [rate, setRate] = useState(1);
const [sheet, setSheet] = useState(null);
const [introOpen, setIntroOpen] = useState(false);
/** null=未进入自动下一集;数字=剩余秒数 */
const [autoNextSeconds, setAutoNextSeconds] = useState(null);
/** 自动切集进入本集时消费一次,用于起播 */
const [shouldAutoplay] = useState(consumeAutoplayFlag);
const closeSheet = () => setSheet(null);
const title = detail.title || "短剧";
const totalEpisode = totalEpisodeOf(detail);
const nextEpisode = episode + 1;
const hasNext = nextEpisode <= totalEpisode;
const nextHref = hasNext ? playHref(detail.id, nextEpisode, detail.slug) : null;
const prevHref = episode > 1 ? playHref(detail.id, episode - 1, detail.slug) : null;
const tags = tagLinkItems(pickPlayletCardTags(detail), "playlet");
const desc = detail.desc?.trim() || "";
useEffect(() => {
if (!sheet) return;
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") setSheet(null);
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [sheet]);
// 换集后清倒计时
useEffect(() => {
setAutoNextSeconds(null);
}, [episode, src]);
// 倒计时结束跳转下一集并标记自动播放
useEffect(() => {
if (autoNextSeconds == null || !nextHref) return;
if (autoNextSeconds <= 0) {
markAutoplayNext();
router.push(nextHref);
return;
}
const timer = window.setTimeout(() => {
setAutoNextSeconds((value) => (value == null ? null : value - 1));
}, 1000);
return () => window.clearTimeout(timer);
}, [autoNextSeconds, nextHref, router]);
const goNextEpisode = () => {
if (!nextHref) return;
setAutoNextSeconds(null);
markAutoplayNext();
router.push(nextHref);
};
const handleEnded = () => {
if (!hasNext || locked) return;
setAutoNextSeconds(AUTO_NEXT_SECONDS);
};
const likeCount = displayCountLike ?? (Number(detail.displayCountLike) || 0);
const collectCount = countCollect ?? (Number(detail.countCollect) || 0);
return (
{locked ? (
) : (
{
player.on("ratechange", () => setRate(player.playbackRate));
// 用户重新起播则取消自动下一集
player.on("playing", () => setAutoNextSeconds(null));
}}
onPlaySuccess={onPlaySuccess}
onEnded={handleEnded}
clickToPlay={!shouldAutoplay}
className="absolute inset-0"
options={{ ignores: ["playbackrate", "cssfullscreen"] }}
/>
)}
第 {episode} / {totalEpisode} 集
{locked ? : null}
{autoNextSeconds != null && hasNext ? (
) : null}
短剧 · {updateStatusLabel(detail.updateStatus)} · 共{totalEpisode}集
{title}
第{episode}集
{desc ? (
{desc}
) : null}
{playbackRates.map((item) => (
))}
);
}