296 lines
9.6 KiB
TypeScript
296 lines
9.6 KiB
TypeScript
"use client";
|
|
|
|
import { toast } from "@heroui/react";
|
|
import { useEffect, useState, type ReactNode } from "react";
|
|
|
|
import { openLoginDialog } from "@/components/auth/login-dialog";
|
|
import { AdBanners, AdIcons } from "@/components/common/ads";
|
|
import { formatDay, formatDuration } from "@/components/common/media-meta";
|
|
import { PcPlayer } from "@/components/drama-play/pc-player";
|
|
import { FloatingActions } from "@/components/home/floating-actions";
|
|
import {
|
|
HlAction,
|
|
InfoList,
|
|
SectionTitle,
|
|
TagLinks,
|
|
VideoRecommend,
|
|
type InfoRow,
|
|
type TagLinkItem,
|
|
} from "@/components/media-page/parts";
|
|
import { PageBreadcrumb } from "@/components/seo/page-breadcrumb";
|
|
import { SeoBreadcrumb } from "@/components/seo/seo-breadcrumb";
|
|
import type { ShortDramaItem } from "@/components/short-drama/data";
|
|
import { tagPageHref } from "@/components/tag/href";
|
|
import { favoriteVideo, getVideoInfo, likeVideo } from "@/lib/api/video-actions";
|
|
import { handleUnauthorized } from "@/lib/auth/handle-unauthorized";
|
|
import { ensureGuestSession, getAuthToken } from "@/lib/auth/session";
|
|
|
|
import { formatCount, formatWatchNum, type VideoDetail } from "./data";
|
|
import { H5VideoPlay } from "./h5-video-play";
|
|
|
|
async function ensureVideoAuthToken() {
|
|
let token = getAuthToken();
|
|
if (!token) {
|
|
try {
|
|
await ensureGuestSession();
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
token = getAuthToken();
|
|
}
|
|
return token;
|
|
}
|
|
|
|
function resolveVideoLikeFavorite(data: Awaited<ReturnType<typeof getVideoInfo>> | null | undefined) {
|
|
const info = data?.videoInfo;
|
|
return {
|
|
isLike: Boolean(data?.isLike ?? info?.isLike),
|
|
isFavorite: Boolean(data?.isFavorite ?? info?.isFavorite),
|
|
likeNum: Number(info?.likeNum) || 0,
|
|
countCollect: Number(info?.countCollect) || 0,
|
|
};
|
|
}
|
|
|
|
export function VideoPlayPage({
|
|
detail,
|
|
src,
|
|
related = [],
|
|
promo,
|
|
adIcons,
|
|
adGrid,
|
|
bannerAds,
|
|
}: {
|
|
detail: VideoDetail;
|
|
src: string;
|
|
related?: ShortDramaItem[];
|
|
promo?: ReactNode;
|
|
adIcons?: ReactNode;
|
|
adGrid?: ReactNode;
|
|
bannerAds?: ReactNode;
|
|
}) {
|
|
const [liked, setLiked] = useState(Boolean(detail.isLike));
|
|
const [likeNum, setLikeNum] = useState(Number(detail.likeNum) || 0);
|
|
const [liking, setLiking] = useState(false);
|
|
const [collected, setCollected] = useState(Boolean(detail.isFavorite));
|
|
const [countCollect, setCountCollect] = useState(Number(detail.countCollect) || 0);
|
|
const [collecting, setCollecting] = useState(false);
|
|
|
|
const likeFavoriteKey = `${detail.id}:${detail.isLike}:${detail.likeNum}:${detail.isFavorite}:${detail.countCollect}`;
|
|
const [likeFavoriteSync, setLikeFavoriteSync] = useState(likeFavoriteKey);
|
|
if (likeFavoriteSync !== likeFavoriteKey) {
|
|
setLikeFavoriteSync(likeFavoriteKey);
|
|
setLiked(Boolean(detail.isLike));
|
|
setLikeNum(Number(detail.likeNum) || 0);
|
|
setCollected(Boolean(detail.isFavorite));
|
|
setCountCollect(Number(detail.countCollect) || 0);
|
|
}
|
|
|
|
// SSR 详情走 "use cache" 无登录态;客户端带 token 再拉一次,纠正已点赞 / 已收藏
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
void (async () => {
|
|
const token = await ensureVideoAuthToken();
|
|
if (!token || cancelled) return;
|
|
try {
|
|
const data = await getVideoInfo(
|
|
detail.slug?.trim() ? { slug: detail.slug.trim() } : { id: detail.id },
|
|
token,
|
|
);
|
|
if (cancelled) return;
|
|
const status = resolveVideoLikeFavorite(data);
|
|
setLiked(status.isLike);
|
|
setCollected(status.isFavorite);
|
|
setLikeNum(status.likeNum);
|
|
setCountCollect(status.countCollect);
|
|
} catch (error) {
|
|
console.warn("[video-play/status]", error);
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [detail.id, detail.slug]);
|
|
|
|
const share = async () => {
|
|
try {
|
|
await navigator.clipboard.writeText(window.location.href);
|
|
toast.success("链接已复制");
|
|
} catch {
|
|
toast.danger("复制失败,请稍后重试");
|
|
}
|
|
};
|
|
|
|
const toggleLike = async () => {
|
|
if (liking) return;
|
|
const videoId = Number(detail.id);
|
|
if (!Number.isInteger(videoId) || videoId < 1) return;
|
|
|
|
const token = await ensureVideoAuthToken();
|
|
if (!token) {
|
|
openLoginDialog();
|
|
return;
|
|
}
|
|
|
|
const next = !liked;
|
|
setLiked(next);
|
|
setLikeNum((value) => Math.max(0, value + (next ? 1 : -1)));
|
|
setLiking(true);
|
|
try {
|
|
const result = await likeVideo({ id: videoId, isLike: next }, token);
|
|
setLiked(Boolean(result?.isLike ?? next));
|
|
if (typeof result?.likeNum === "number") setLikeNum(result.likeNum);
|
|
} catch (error) {
|
|
setLiked(!next);
|
|
setLikeNum((value) => Math.max(0, value + (next ? -1 : 1)));
|
|
handleUnauthorized(error);
|
|
} finally {
|
|
setLiking(false);
|
|
}
|
|
};
|
|
|
|
const toggleCollect = async () => {
|
|
if (collecting) return;
|
|
const videoId = Number(detail.id);
|
|
if (!Number.isInteger(videoId) || videoId < 1) return;
|
|
|
|
const token = await ensureVideoAuthToken();
|
|
if (!token) {
|
|
openLoginDialog();
|
|
return;
|
|
}
|
|
|
|
const next = !collected;
|
|
setCollected(next);
|
|
setCountCollect((value) => Math.max(0, value + (next ? 1 : -1)));
|
|
setCollecting(true);
|
|
try {
|
|
const result = await favoriteVideo({ id: videoId, isFavorite: next }, token);
|
|
setCollected(Boolean(result?.isFavorite ?? next));
|
|
if (typeof result?.countCollect === "number") {
|
|
setCountCollect(result.countCollect);
|
|
} else {
|
|
// 响应无 countCollect 时再拉详情同步
|
|
try {
|
|
const data = await getVideoInfo(
|
|
detail.slug?.trim() ? { slug: detail.slug.trim() } : { id: detail.id },
|
|
token,
|
|
);
|
|
const num = Number(data?.videoInfo?.countCollect);
|
|
if (Number.isFinite(num) && num >= 0) setCountCollect(num);
|
|
} catch {
|
|
/* 保留乐观值 */
|
|
}
|
|
}
|
|
} catch (error) {
|
|
setCollected(!next);
|
|
setCountCollect((value) => Math.max(0, value + (next ? -1 : 1)));
|
|
handleUnauthorized(error);
|
|
} finally {
|
|
setCollecting(false);
|
|
}
|
|
};
|
|
|
|
const title = detail.title || "视频";
|
|
const desc = detail.desc?.trim() || "";
|
|
const day = formatDay(detail.addedTime);
|
|
const duration = formatDuration(detail.video?.videoTime);
|
|
const tags: TagLinkItem[] = (detail.tags ?? [])
|
|
.map((tag): TagLinkItem | null => {
|
|
const name = (tag.tagName || tag.name || "").trim();
|
|
if (!name) return null;
|
|
const canLink = Boolean(tag.slug?.trim() || tag.id != null);
|
|
return {
|
|
key: tag.slug || String(tag.id ?? name),
|
|
name,
|
|
href: canLink ? tagPageHref({ slug: tag.slug, id: tag.id }, "video") : undefined,
|
|
};
|
|
})
|
|
.filter((tag): tag is TagLinkItem => Boolean(tag));
|
|
const infoRows: InfoRow[] = [
|
|
{ label: "发布", value: day ? <time dateTime={day}>{day}</time> : "" },
|
|
{ label: "时长", value: duration },
|
|
{ label: "播放", value: formatWatchNum(detail.watchNum) },
|
|
{ label: "收藏", value: formatCount(countCollect) },
|
|
];
|
|
const crumbs = [
|
|
{ name: "首页", href: "/" },
|
|
{ name: "视频库", href: "/video/hot" },
|
|
{ name: title },
|
|
];
|
|
|
|
return (
|
|
<main data-component="hl-video-play" className="relative flex-1 bg-[#0C0B10]">
|
|
<SeoBreadcrumb items={crumbs} />
|
|
<div
|
|
data-pc-scale
|
|
className="relative mx-auto hidden w-[1700px] flex-col gap-8 pt-6 pb-14 [zoom:var(--page-scale,1)] lg:flex"
|
|
>
|
|
<PageBreadcrumb items={crumbs} />
|
|
|
|
<PcPlayer
|
|
src={src}
|
|
poster={detail.coverImg || ""}
|
|
title={title}
|
|
locked={false}
|
|
dramaTitle={title}
|
|
layout="wide"
|
|
/>
|
|
|
|
<article className="grid grid-cols-[minmax(0,1fr)_auto] items-start gap-x-10 gap-y-5 border-t-2 border-[#FFB020] pt-8">
|
|
<div className="flex min-w-0 flex-col gap-4">
|
|
<p className="text-sm leading-5 tracking-[0.2em] text-[#FFB020]">视频库</p>
|
|
<h1 className="text-4xl leading-[48px] font-bold text-white">{title}</h1>
|
|
<div className="max-w-[900px]">
|
|
<InfoList rows={infoRows} />
|
|
</div>
|
|
<TagLinks tags={tags} />
|
|
</div>
|
|
<div className="flex shrink-0 gap-3 pt-7">
|
|
<HlAction kind="like" label={`点赞 ${formatCount(likeNum)}`} active={liked} onClick={liking ? undefined : toggleLike} />
|
|
<HlAction
|
|
kind="collect"
|
|
label={collected ? "已收藏" : "收藏"}
|
|
active={collected}
|
|
onClick={collecting ? undefined : toggleCollect}
|
|
/>
|
|
<HlAction kind="share" label="分享" onClick={share} />
|
|
</div>
|
|
{desc ? (
|
|
<section className="col-span-2 flex flex-col gap-3">
|
|
<SectionTitle>视频介绍</SectionTitle>
|
|
<p className="max-w-[1100px] text-[15px] leading-7 whitespace-pre-line text-[#B5B3BE]">{desc}</p>
|
|
</section>
|
|
) : null}
|
|
</article>
|
|
|
|
{adIcons ?? <AdIcons variant="pc" labels={false} />}
|
|
|
|
<VideoRecommend items={related} />
|
|
|
|
{promo ?? <AdBanners variant="pc" />}
|
|
</div>
|
|
|
|
<H5VideoPlay
|
|
detail={detail}
|
|
src={src}
|
|
related={related}
|
|
infoRows={infoRows.slice(0, 3)}
|
|
tags={tags}
|
|
liked={liked}
|
|
likeNum={likeNum}
|
|
collected={collected}
|
|
countCollect={countCollect}
|
|
onLike={liking ? () => undefined : toggleLike}
|
|
onCollect={collecting ? () => undefined : toggleCollect}
|
|
onShare={share}
|
|
adGrid={adGrid}
|
|
bannerAds={bannerAds}
|
|
/>
|
|
|
|
<div className="max-lg:hidden">
|
|
<FloatingActions />
|
|
</div>
|
|
</main>
|
|
);
|
|
}
|