feat(ui): 完成 Phase 2 页面与组件开发
- 完成 T-011: TanStack Query 数据获取 hooks - 完成 T-012: 内容卡片组件 ContentCard - 完成 T-013: 响应式网格布局 + 骨架屏 - 完成 T-014: 平台 Tab 切换 - 完成 T-015: 排序工具栏 - 完成 T-016: 内容详情页 - 完成 T-017: 自动刷新机制 - 完成 T-018: 手动刷新 + 刷新时间 - 完成 T-019: 首页组装 - 完成 T-020: 收藏 store - 完成 T-021: 收藏按钮组件 - 完成 T-022: 收藏页面 - 完成 T-023: 设置页面 - API Key - 完成 T-024: 设置页面 - 刷新间隔 - 完成 T-025: Toast 通知 (sonner) - 完成 T-026: 错误/空状态组件 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
1fb288986a
commit
ce736f197d
67
src/app/detail/[platform]/[id]/page.tsx
Normal file
67
src/app/detail/[platform]/[id]/page.tsx
Normal file
@ -0,0 +1,67 @@
|
||||
"use client";
|
||||
|
||||
import { use } from "react";
|
||||
import { useDetailQuery } from "@/hooks/useDetailQuery";
|
||||
import { DetailPanel } from "@/components/detail/DetailPanel";
|
||||
import { DetailSkeleton } from "@/components/detail/DetailSkeleton";
|
||||
import { ArrowLeft, RefreshCw } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
|
||||
interface DetailPageProps {
|
||||
params: Promise<{ platform: string; id: string }>;
|
||||
}
|
||||
|
||||
export default function DetailPage({ params }: DetailPageProps) {
|
||||
const { platform, id } = use(params);
|
||||
const decodedId = decodeURIComponent(id);
|
||||
const { data, isLoading, isError, error, refetch } = useDetailQuery(
|
||||
platform,
|
||||
decodedId
|
||||
);
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="px-4 py-6">
|
||||
<DetailSkeleton />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (isError || !data) {
|
||||
return (
|
||||
<div className="px-4 py-6">
|
||||
<div className="max-w-3xl mx-auto text-center py-20">
|
||||
<p className="text-4xl mb-4">😵</p>
|
||||
<h2 className="text-lg font-medium text-slate-700 mb-2">
|
||||
加载失败
|
||||
</h2>
|
||||
<p className="text-sm text-slate-500 mb-6">
|
||||
{error?.message || "无法获取内容详情"}
|
||||
</p>
|
||||
<div className="flex items-center justify-center gap-3">
|
||||
<button
|
||||
onClick={() => refetch()}
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
重试
|
||||
</button>
|
||||
<Link
|
||||
href="/"
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-slate-600 border border-slate-200 rounded-lg hover:bg-slate-50 transition-colors"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
返回首页
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="px-4 py-6">
|
||||
<DetailPanel item={data} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
50
src/app/favorites/page.tsx
Normal file
50
src/app/favorites/page.tsx
Normal file
@ -0,0 +1,50 @@
|
||||
"use client";
|
||||
|
||||
import { useFavoritesStore } from "@/stores/favorites";
|
||||
import { ContentGrid } from "@/components/card/ContentGrid";
|
||||
import Link from "next/link";
|
||||
import { ArrowLeft } from "lucide-react";
|
||||
|
||||
export default function FavoritesPage() {
|
||||
const favorites = useFavoritesStore((s) => s.items);
|
||||
|
||||
return (
|
||||
<div className="px-4 py-4">
|
||||
<div className="flex items-center gap-3 mb-4">
|
||||
<Link
|
||||
href="/"
|
||||
className="inline-flex items-center gap-1 text-sm text-slate-500 hover:text-slate-700 transition-colors"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
返回
|
||||
</Link>
|
||||
<h1 className="text-lg font-semibold text-slate-800">
|
||||
我的收藏
|
||||
</h1>
|
||||
<span className="text-sm text-slate-400">
|
||||
{favorites.length} 个内容
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{favorites.length === 0 ? (
|
||||
<div className="text-center py-20">
|
||||
<p className="text-4xl mb-4">💝</p>
|
||||
<h2 className="text-lg font-medium text-slate-700 mb-2">
|
||||
还没有收藏
|
||||
</h2>
|
||||
<p className="text-sm text-slate-500 mb-4">
|
||||
浏览热门内容,点击心形按钮收藏
|
||||
</p>
|
||||
<Link
|
||||
href="/"
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
去发现
|
||||
</Link>
|
||||
</div>
|
||||
) : (
|
||||
<ContentGrid items={favorites} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
136
src/app/page.tsx
136
src/app/page.tsx
@ -1,65 +1,87 @@
|
||||
import Image from "next/image";
|
||||
"use client";
|
||||
|
||||
import { useState, useMemo, useCallback } from "react";
|
||||
import { useContentQuery, useRefreshContent } from "@/hooks/useContentQuery";
|
||||
import { PlatformTabs } from "@/components/layout/PlatformTabs";
|
||||
import { SortToolbar, type SortField, type SortOrder } from "@/components/layout/SortToolbar";
|
||||
import { ContentGrid } from "@/components/card/ContentGrid";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
|
||||
function sortItems(
|
||||
items: ContentItem[],
|
||||
sortBy: SortField,
|
||||
sortOrder: SortOrder
|
||||
): ContentItem[] {
|
||||
return [...items].sort((a, b) => {
|
||||
let valA: number;
|
||||
let valB: number;
|
||||
|
||||
if (sortBy === "publish_time") {
|
||||
valA = new Date(a.publish_time).getTime() || 0;
|
||||
valB = new Date(b.publish_time).getTime() || 0;
|
||||
} else {
|
||||
valA = (a[sortBy] as number) ?? 0;
|
||||
valB = (b[sortBy] as number) ?? 0;
|
||||
}
|
||||
|
||||
return sortOrder === "desc" ? valB - valA : valA - valB;
|
||||
});
|
||||
}
|
||||
|
||||
export default function Home() {
|
||||
const [platform, setPlatform] = useState("all");
|
||||
const [sortBy, setSortBy] = useState<SortField>("play_count");
|
||||
const [sortOrder, setSortOrder] = useState<SortOrder>("desc");
|
||||
const [lastRefreshTime, setLastRefreshTime] = useState<string | null>(null);
|
||||
|
||||
const { data, isLoading, isFetching } = useContentQuery(platform);
|
||||
const { refresh } = useRefreshContent();
|
||||
|
||||
const sortedItems = useMemo(() => {
|
||||
if (!data) return [];
|
||||
return sortItems(data, sortBy, sortOrder);
|
||||
}, [data, sortBy, sortOrder]);
|
||||
|
||||
const handleRefresh = useCallback(() => {
|
||||
refresh(platform);
|
||||
setLastRefreshTime(
|
||||
new Date().toLocaleTimeString("zh-CN", {
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
})
|
||||
);
|
||||
}, [refresh, platform]);
|
||||
|
||||
const handleSortOrderChange = useCallback(() => {
|
||||
setSortOrder((prev) => (prev === "desc" ? "asc" : "desc"));
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="flex min-h-screen items-center justify-center bg-zinc-50 font-sans dark:bg-black">
|
||||
<main className="flex min-h-screen w-full max-w-3xl flex-col items-center justify-between py-32 px-16 bg-white dark:bg-black sm:items-start">
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/next.svg"
|
||||
alt="Next.js logo"
|
||||
width={100}
|
||||
height={20}
|
||||
priority
|
||||
/>
|
||||
<div className="flex flex-col items-center gap-6 text-center sm:items-start sm:text-left">
|
||||
<h1 className="max-w-xs text-3xl font-semibold leading-10 tracking-tight text-black dark:text-zinc-50">
|
||||
To get started, edit the page.tsx file.
|
||||
</h1>
|
||||
<p className="max-w-md text-lg leading-8 text-zinc-600 dark:text-zinc-400">
|
||||
Looking for a starting point or more instructions? Head over to{" "}
|
||||
<a
|
||||
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||
>
|
||||
Templates
|
||||
</a>{" "}
|
||||
or the{" "}
|
||||
<a
|
||||
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
className="font-medium text-zinc-950 dark:text-zinc-50"
|
||||
>
|
||||
Learning
|
||||
</a>{" "}
|
||||
center.
|
||||
<div className="px-4 py-4">
|
||||
<PlatformTabs active={platform} onChange={setPlatform} />
|
||||
<SortToolbar
|
||||
sortBy={sortBy}
|
||||
sortOrder={sortOrder}
|
||||
onSortByChange={setSortBy}
|
||||
onSortOrderChange={handleSortOrderChange}
|
||||
onRefresh={handleRefresh}
|
||||
isRefreshing={isFetching}
|
||||
lastRefreshTime={lastRefreshTime}
|
||||
/>
|
||||
|
||||
{!isLoading && sortedItems.length === 0 ? (
|
||||
<div className="text-center py-20">
|
||||
<p className="text-4xl mb-4">📭</p>
|
||||
<h2 className="text-lg font-medium text-slate-700 mb-2">
|
||||
暂无内容
|
||||
</h2>
|
||||
<p className="text-sm text-slate-500">
|
||||
请先在设置页配置 API Key,或稍后重试
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex flex-col gap-4 text-base font-medium sm:flex-row">
|
||||
<a
|
||||
className="flex h-12 w-full items-center justify-center gap-2 rounded-full bg-foreground px-5 text-background transition-colors hover:bg-[#383838] dark:hover:bg-[#ccc] md:w-[158px]"
|
||||
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
<Image
|
||||
className="dark:invert"
|
||||
src="/vercel.svg"
|
||||
alt="Vercel logomark"
|
||||
width={16}
|
||||
height={16}
|
||||
/>
|
||||
Deploy Now
|
||||
</a>
|
||||
<a
|
||||
className="flex h-12 w-full items-center justify-center rounded-full border border-solid border-black/[.08] px-5 transition-colors hover:border-transparent hover:bg-black/[.04] dark:border-white/[.145] dark:hover:bg-[#1a1a1a] md:w-[158px]"
|
||||
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
>
|
||||
Documentation
|
||||
</a>
|
||||
</div>
|
||||
</main>
|
||||
) : (
|
||||
<ContentGrid items={sortedItems} loading={isLoading} />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
143
src/app/settings/page.tsx
Normal file
143
src/app/settings/page.tsx
Normal file
@ -0,0 +1,143 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { ArrowLeft, Eye, EyeOff, Check } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { useSettingsStore } from "@/stores/settings";
|
||||
import { toast } from "sonner";
|
||||
|
||||
const REFRESH_OPTIONS: { value: 5 | 10 | 15 | 30 | 60; label: string }[] = [
|
||||
{ value: 5, label: "5 分钟" },
|
||||
{ value: 10, label: "10 分钟" },
|
||||
{ value: 15, label: "15 分钟" },
|
||||
{ value: 30, label: "30 分钟" },
|
||||
{ value: 60, label: "60 分钟" },
|
||||
];
|
||||
|
||||
export default function SettingsPage() {
|
||||
const { apiKey, setApiKey, refreshInterval, setRefreshInterval } =
|
||||
useSettingsStore();
|
||||
|
||||
const [inputKey, setInputKey] = useState(apiKey);
|
||||
const [showKey, setShowKey] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const handleSaveApiKey = async () => {
|
||||
const trimmed = inputKey.trim();
|
||||
if (!trimmed) {
|
||||
toast.error("请输入 API Key");
|
||||
return;
|
||||
}
|
||||
|
||||
setSaving(true);
|
||||
try {
|
||||
const res = await fetch("/api/settings", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ apiKey: trimmed }),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error("保存失败");
|
||||
}
|
||||
|
||||
setApiKey(trimmed);
|
||||
toast.success("API Key 已保存");
|
||||
} catch {
|
||||
toast.error("保存失败,请重试");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="px-4 py-4 max-w-2xl mx-auto">
|
||||
<div className="flex items-center gap-3 mb-6">
|
||||
<Link
|
||||
href="/"
|
||||
className="inline-flex items-center gap-1 text-sm text-slate-500 hover:text-slate-700 transition-colors"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
返回
|
||||
</Link>
|
||||
<h1 className="text-lg font-semibold text-slate-800">设置</h1>
|
||||
</div>
|
||||
|
||||
{/* API Key Section */}
|
||||
<section className="mb-8">
|
||||
<h2 className="text-sm font-medium text-slate-700 mb-1">
|
||||
TikHub API Key
|
||||
</h2>
|
||||
<p className="text-xs text-slate-400 mb-3">
|
||||
请前往{" "}
|
||||
<a
|
||||
href="https://tikhub.io"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-blue-500 hover:underline"
|
||||
>
|
||||
tikhub.io
|
||||
</a>{" "}
|
||||
获取 API Key
|
||||
</p>
|
||||
<div className="flex gap-2">
|
||||
<div className="relative flex-1">
|
||||
<input
|
||||
type={showKey ? "text" : "password"}
|
||||
value={inputKey}
|
||||
onChange={(e) => setInputKey(e.target.value)}
|
||||
placeholder="输入你的 API Key"
|
||||
className="w-full text-sm border border-slate-200 rounded-lg px-3 py-2 pr-10 bg-white text-slate-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowKey(!showKey)}
|
||||
className="absolute right-2 top-1/2 -translate-y-1/2 text-slate-400 hover:text-slate-600"
|
||||
>
|
||||
{showKey ? (
|
||||
<EyeOff className="w-4 h-4" />
|
||||
) : (
|
||||
<Eye className="w-4 h-4" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
onClick={handleSaveApiKey}
|
||||
disabled={saving}
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{saving ? "保存中..." : "保存"}
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Refresh Interval Section */}
|
||||
<section className="mb-8">
|
||||
<h2 className="text-sm font-medium text-slate-700 mb-1">
|
||||
自动刷新间隔
|
||||
</h2>
|
||||
<p className="text-xs text-slate-400 mb-3">
|
||||
设置热门内容的自动刷新频率
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{REFRESH_OPTIONS.map((opt) => (
|
||||
<button
|
||||
key={opt.value}
|
||||
onClick={() => setRefreshInterval(opt.value)}
|
||||
className={`inline-flex items-center gap-1 px-3 py-1.5 text-sm rounded-lg border transition-colors ${
|
||||
refreshInterval === opt.value
|
||||
? "border-blue-500 bg-blue-50 text-blue-700"
|
||||
: "border-slate-200 bg-white text-slate-600 hover:bg-slate-50"
|
||||
}`}
|
||||
>
|
||||
{refreshInterval === opt.value && (
|
||||
<Check className="w-3.5 h-3.5" />
|
||||
)}
|
||||
{opt.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
27
src/components/card/CardSkeleton.tsx
Normal file
27
src/components/card/CardSkeleton.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
export function CardSkeleton() {
|
||||
return (
|
||||
<div className="rounded-lg border border-slate-200 bg-white overflow-hidden animate-pulse">
|
||||
{/* Cover placeholder */}
|
||||
<div className="aspect-[4/3] bg-slate-200" />
|
||||
|
||||
<div className="p-3 space-y-2">
|
||||
{/* Platform tag */}
|
||||
<div className="h-4 w-16 bg-slate-200 rounded" />
|
||||
{/* Title */}
|
||||
<div className="h-4 w-full bg-slate-200 rounded" />
|
||||
<div className="h-4 w-2/3 bg-slate-200 rounded" />
|
||||
{/* Author */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<div className="w-5 h-5 rounded-full bg-slate-200" />
|
||||
<div className="h-3 w-20 bg-slate-200 rounded" />
|
||||
</div>
|
||||
{/* Stats */}
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="h-3 w-10 bg-slate-200 rounded" />
|
||||
<div className="h-3 w-10 bg-slate-200 rounded" />
|
||||
<div className="h-3 w-10 bg-slate-200 rounded" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
118
src/components/card/ContentCard.tsx
Normal file
118
src/components/card/ContentCard.tsx
Normal file
@ -0,0 +1,118 @@
|
||||
"use client";
|
||||
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { Play, Heart, MessageCircle } from "lucide-react";
|
||||
import { getPlatformConfig } from "@/lib/platforms";
|
||||
import { formatCount } from "@/lib/format";
|
||||
import { FavoriteButton } from "@/components/common/FavoriteButton";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
import { useState } from "react";
|
||||
|
||||
interface ContentCardProps {
|
||||
item: ContentItem;
|
||||
}
|
||||
|
||||
export function ContentCard({ item }: ContentCardProps) {
|
||||
const platform = getPlatformConfig(item.platform);
|
||||
const [imgError, setImgError] = useState(false);
|
||||
|
||||
const playCount = formatCount(item.play_count);
|
||||
const likeCount = formatCount(item.like_count);
|
||||
const commentCount = formatCount(item.comment_count);
|
||||
|
||||
return (
|
||||
<Link
|
||||
href={`/detail/${item.platform}/${encodeURIComponent(item.id)}`}
|
||||
className="group block rounded-lg border border-slate-200 bg-white shadow-sm overflow-hidden transition-all hover:-translate-y-0.5 hover:shadow-md"
|
||||
>
|
||||
{/* Cover image */}
|
||||
<div className="relative aspect-[4/3] bg-slate-100 overflow-hidden">
|
||||
{item.cover_url && !imgError ? (
|
||||
<Image
|
||||
src={item.cover_url}
|
||||
alt={item.title}
|
||||
fill
|
||||
className="object-cover"
|
||||
loading="lazy"
|
||||
sizes="(max-width: 640px) 100vw, (max-width: 960px) 50vw, (max-width: 1240px) 33vw, 25vw"
|
||||
onError={() => setImgError(true)}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex items-center justify-center h-full text-3xl text-slate-300">
|
||||
{platform?.icon || "📄"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="p-3">
|
||||
{/* Platform tag */}
|
||||
{platform && (
|
||||
<span
|
||||
className="inline-block text-xs px-1.5 py-0.5 rounded mb-1.5"
|
||||
style={{
|
||||
backgroundColor: `${platform.color}15`,
|
||||
color: platform.color,
|
||||
}}
|
||||
>
|
||||
{platform.icon} {platform.name}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* Title */}
|
||||
<h3 className="text-sm font-medium text-slate-800 line-clamp-2 mb-2 leading-snug">
|
||||
{item.title}
|
||||
</h3>
|
||||
|
||||
{/* Author */}
|
||||
<div className="flex items-center gap-1.5 mb-2">
|
||||
{item.author_avatar ? (
|
||||
<Image
|
||||
src={item.author_avatar}
|
||||
alt={item.author_name}
|
||||
width={20}
|
||||
height={20}
|
||||
className="rounded-full object-cover"
|
||||
onError={(e) => {
|
||||
(e.target as HTMLImageElement).style.display = "none";
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<div className="w-5 h-5 rounded-full bg-slate-200 flex items-center justify-center text-[10px] text-slate-500">
|
||||
👤
|
||||
</div>
|
||||
)}
|
||||
<span className="text-xs text-slate-500 truncate">
|
||||
{item.author_name}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* Stats + Favorite */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3 text-xs text-slate-400">
|
||||
{playCount && (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<Play className="w-3 h-3" />
|
||||
{playCount}
|
||||
</span>
|
||||
)}
|
||||
{likeCount && (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<Heart className="w-3 h-3" />
|
||||
{likeCount}
|
||||
</span>
|
||||
)}
|
||||
{commentCount && (
|
||||
<span className="flex items-center gap-0.5">
|
||||
<MessageCircle className="w-3 h-3" />
|
||||
{commentCount}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<FavoriteButton item={item} />
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
35
src/components/card/ContentGrid.tsx
Normal file
35
src/components/card/ContentGrid.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { ContentCard } from "./ContentCard";
|
||||
import { CardSkeleton } from "./CardSkeleton";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
|
||||
interface ContentGridProps {
|
||||
items: ContentItem[];
|
||||
loading?: boolean;
|
||||
skeletonCount?: number;
|
||||
}
|
||||
|
||||
export function ContentGrid({
|
||||
items,
|
||||
loading,
|
||||
skeletonCount = 12,
|
||||
}: ContentGridProps) {
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(280px,1fr))] gap-4">
|
||||
{Array.from({ length: skeletonCount }).map((_, i) => (
|
||||
<CardSkeleton key={i} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-[repeat(auto-fill,minmax(280px,1fr))] gap-4">
|
||||
{items.map((item) => (
|
||||
<ContentCard key={`${item.platform}-${item.id}`} item={item} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
35
src/components/common/EmptyState.tsx
Normal file
35
src/components/common/EmptyState.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
import Link from "next/link";
|
||||
|
||||
interface EmptyStateProps {
|
||||
icon?: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
actionLabel?: string;
|
||||
actionHref?: string;
|
||||
}
|
||||
|
||||
export function EmptyState({
|
||||
icon = "📭",
|
||||
title,
|
||||
description,
|
||||
actionLabel,
|
||||
actionHref,
|
||||
}: EmptyStateProps) {
|
||||
return (
|
||||
<div className="text-center py-20">
|
||||
<p className="text-4xl mb-4">{icon}</p>
|
||||
<h2 className="text-lg font-medium text-slate-700 mb-2">{title}</h2>
|
||||
{description && (
|
||||
<p className="text-sm text-slate-500 mb-4">{description}</p>
|
||||
)}
|
||||
{actionLabel && actionHref && (
|
||||
<Link
|
||||
href={actionHref}
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
{actionLabel}
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
41
src/components/common/ErrorState.tsx
Normal file
41
src/components/common/ErrorState.tsx
Normal file
@ -0,0 +1,41 @@
|
||||
import { RefreshCw } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
|
||||
interface ErrorStateProps {
|
||||
message?: string;
|
||||
onRetry?: () => void;
|
||||
showHomeLink?: boolean;
|
||||
}
|
||||
|
||||
export function ErrorState({
|
||||
message = "加载失败,请稍后重试",
|
||||
onRetry,
|
||||
showHomeLink = true,
|
||||
}: ErrorStateProps) {
|
||||
return (
|
||||
<div className="text-center py-20">
|
||||
<p className="text-4xl mb-4">😵</p>
|
||||
<h2 className="text-lg font-medium text-slate-700 mb-2">出错了</h2>
|
||||
<p className="text-sm text-slate-500 mb-6">{message}</p>
|
||||
<div className="flex items-center justify-center gap-3">
|
||||
{onRetry && (
|
||||
<button
|
||||
onClick={onRetry}
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
<RefreshCw className="w-4 h-4" />
|
||||
重试
|
||||
</button>
|
||||
)}
|
||||
{showHomeLink && (
|
||||
<Link
|
||||
href="/"
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-slate-600 border border-slate-200 rounded-lg hover:bg-slate-50 transition-colors"
|
||||
>
|
||||
返回首页
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
54
src/components/common/FavoriteButton.tsx
Normal file
54
src/components/common/FavoriteButton.tsx
Normal file
@ -0,0 +1,54 @@
|
||||
"use client";
|
||||
|
||||
import { Heart } from "lucide-react";
|
||||
import { useFavoritesStore } from "@/stores/favorites";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useState } from "react";
|
||||
|
||||
interface FavoriteButtonProps {
|
||||
item: ContentItem;
|
||||
size?: "sm" | "md";
|
||||
}
|
||||
|
||||
export function FavoriteButton({ item, size = "sm" }: FavoriteButtonProps) {
|
||||
const { isFavorited, addFavorite, removeFavorite } = useFavoritesStore();
|
||||
const favorited = isFavorited(item.id, item.platform);
|
||||
const [animating, setAnimating] = useState(false);
|
||||
|
||||
const handleClick = (e: React.MouseEvent) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
setAnimating(true);
|
||||
setTimeout(() => setAnimating(false), 300);
|
||||
|
||||
if (favorited) {
|
||||
removeFavorite(item.id, item.platform);
|
||||
} else {
|
||||
addFavorite(item);
|
||||
}
|
||||
};
|
||||
|
||||
const iconSize = size === "sm" ? "w-4 h-4" : "w-5 h-5";
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={handleClick}
|
||||
className={cn(
|
||||
"inline-flex items-center justify-center rounded-md transition-colors",
|
||||
size === "sm" ? "w-8 h-8" : "w-11 h-11",
|
||||
favorited
|
||||
? "text-red-500 hover:text-red-600"
|
||||
: "text-slate-400 hover:text-red-400",
|
||||
animating && "scale-110"
|
||||
)}
|
||||
style={{ transition: "transform 0.2s ease" }}
|
||||
aria-label={favorited ? "取消收藏" : "收藏"}
|
||||
>
|
||||
<Heart
|
||||
className={cn(iconSize, favorited && "fill-current")}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
155
src/components/detail/DetailPanel.tsx
Normal file
155
src/components/detail/DetailPanel.tsx
Normal file
@ -0,0 +1,155 @@
|
||||
"use client";
|
||||
|
||||
import Image from "next/image";
|
||||
import { ArrowLeft, Play, Heart, MessageCircle, Share2, ExternalLink } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { getPlatformConfig } from "@/lib/platforms";
|
||||
import { formatCount, formatTime } from "@/lib/format";
|
||||
import { FavoriteButton } from "@/components/common/FavoriteButton";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
import { useState } from "react";
|
||||
|
||||
interface DetailPanelProps {
|
||||
item: ContentItem;
|
||||
}
|
||||
|
||||
const STAT_ITEMS = [
|
||||
{ key: "play_count" as const, label: "播放", icon: Play },
|
||||
{ key: "like_count" as const, label: "点赞", icon: Heart },
|
||||
{ key: "comment_count" as const, label: "评论", icon: MessageCircle },
|
||||
{ key: "share_count" as const, label: "分享", icon: Share2 },
|
||||
];
|
||||
|
||||
export function DetailPanel({ item }: DetailPanelProps) {
|
||||
const router = useRouter();
|
||||
const platform = getPlatformConfig(item.platform);
|
||||
const [imgError, setImgError] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="max-w-3xl mx-auto">
|
||||
{/* Back button */}
|
||||
<button
|
||||
onClick={() => router.back()}
|
||||
className="inline-flex items-center gap-1.5 text-sm text-slate-500 hover:text-slate-700 mb-4 transition-colors"
|
||||
>
|
||||
<ArrowLeft className="w-4 h-4" />
|
||||
返回
|
||||
</button>
|
||||
|
||||
{/* Cover image */}
|
||||
<div className="relative aspect-video bg-slate-100 rounded-lg overflow-hidden">
|
||||
{item.cover_url && !imgError ? (
|
||||
<Image
|
||||
src={item.cover_url}
|
||||
alt={item.title}
|
||||
fill
|
||||
className="object-cover"
|
||||
priority
|
||||
sizes="(max-width: 768px) 100vw, 768px"
|
||||
onError={() => setImgError(true)}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex items-center justify-center h-full text-5xl text-slate-300">
|
||||
{platform?.icon || "📄"}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="mt-6 space-y-5">
|
||||
{/* Platform tag */}
|
||||
{platform && (
|
||||
<span
|
||||
className="inline-block text-xs px-2 py-0.5 rounded"
|
||||
style={{
|
||||
backgroundColor: `${platform.color}15`,
|
||||
color: platform.color,
|
||||
}}
|
||||
>
|
||||
{platform.icon} {platform.name}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* Title */}
|
||||
<h1 className="text-xl font-semibold text-slate-900 leading-relaxed">
|
||||
{item.title}
|
||||
</h1>
|
||||
|
||||
{/* Author */}
|
||||
<div className="flex items-center gap-3">
|
||||
{item.author_avatar ? (
|
||||
<Image
|
||||
src={item.author_avatar}
|
||||
alt={item.author_name}
|
||||
width={40}
|
||||
height={40}
|
||||
className="rounded-full object-cover"
|
||||
onError={(e) => {
|
||||
(e.target as HTMLImageElement).style.display = "none";
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<div className="w-10 h-10 rounded-full bg-slate-200 flex items-center justify-center text-lg text-slate-500">
|
||||
👤
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<p className="text-sm font-medium text-slate-700">
|
||||
{item.author_name}
|
||||
</p>
|
||||
{item.publish_time && (
|
||||
<p className="text-xs text-slate-400">
|
||||
{formatTime(item.publish_time)}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stats panel */}
|
||||
<div className="grid grid-cols-4 gap-3">
|
||||
{STAT_ITEMS.map(({ key, label, icon: Icon }) => {
|
||||
const value = item[key];
|
||||
return (
|
||||
<div
|
||||
key={key}
|
||||
className="flex flex-col items-center gap-1 py-3 bg-slate-50 rounded-lg"
|
||||
>
|
||||
<Icon className="w-4 h-4 text-slate-400" />
|
||||
<span className="text-lg font-semibold text-slate-800">
|
||||
{formatCount(value) || "0"}
|
||||
</span>
|
||||
<span className="text-xs text-slate-400">{label}</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Tags */}
|
||||
{item.tags && item.tags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{item.tags.map((tag) => (
|
||||
<span
|
||||
key={tag}
|
||||
className="text-xs px-2.5 py-1 bg-slate-100 text-slate-600 rounded-full"
|
||||
>
|
||||
#{tag}
|
||||
</span>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Actions */}
|
||||
<div className="flex items-center gap-3 pt-2">
|
||||
<button
|
||||
onClick={() => window.open(item.original_url, "_blank")}
|
||||
className="inline-flex items-center gap-1.5 px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700 transition-colors"
|
||||
>
|
||||
<ExternalLink className="w-4 h-4" />
|
||||
查看原文
|
||||
</button>
|
||||
<FavoriteButton item={item} size="md" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
34
src/components/detail/DetailSkeleton.tsx
Normal file
34
src/components/detail/DetailSkeleton.tsx
Normal file
@ -0,0 +1,34 @@
|
||||
export function DetailSkeleton() {
|
||||
return (
|
||||
<div className="max-w-3xl mx-auto animate-pulse">
|
||||
{/* Cover */}
|
||||
<div className="aspect-video bg-slate-200 rounded-lg" />
|
||||
|
||||
<div className="mt-6 space-y-4">
|
||||
{/* Title */}
|
||||
<div className="h-7 w-3/4 bg-slate-200 rounded" />
|
||||
<div className="h-7 w-1/2 bg-slate-200 rounded" />
|
||||
|
||||
{/* Author */}
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 rounded-full bg-slate-200" />
|
||||
<div className="h-4 w-24 bg-slate-200 rounded" />
|
||||
</div>
|
||||
|
||||
{/* Stats */}
|
||||
<div className="flex gap-6">
|
||||
{Array.from({ length: 4 }).map((_, i) => (
|
||||
<div key={i} className="h-16 w-24 bg-slate-200 rounded-lg" />
|
||||
))}
|
||||
</div>
|
||||
|
||||
{/* Tags */}
|
||||
<div className="flex gap-2">
|
||||
{Array.from({ length: 3 }).map((_, i) => (
|
||||
<div key={i} className="h-6 w-16 bg-slate-200 rounded-full" />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
77
src/components/layout/SortToolbar.tsx
Normal file
77
src/components/layout/SortToolbar.tsx
Normal file
@ -0,0 +1,77 @@
|
||||
"use client";
|
||||
|
||||
import { RefreshCw } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export type SortField = "play_count" | "like_count" | "comment_count" | "publish_time";
|
||||
export type SortOrder = "asc" | "desc";
|
||||
|
||||
const SORT_OPTIONS: { value: SortField; label: string }[] = [
|
||||
{ value: "play_count", label: "播放量" },
|
||||
{ value: "like_count", label: "点赞数" },
|
||||
{ value: "comment_count", label: "评论数" },
|
||||
{ value: "publish_time", label: "发布时间" },
|
||||
];
|
||||
|
||||
interface SortToolbarProps {
|
||||
sortBy: SortField;
|
||||
sortOrder: SortOrder;
|
||||
onSortByChange: (field: SortField) => void;
|
||||
onSortOrderChange: () => void;
|
||||
onRefresh: () => void;
|
||||
isRefreshing: boolean;
|
||||
lastRefreshTime: string | null;
|
||||
}
|
||||
|
||||
export function SortToolbar({
|
||||
sortBy,
|
||||
sortOrder,
|
||||
onSortByChange,
|
||||
onSortOrderChange,
|
||||
onRefresh,
|
||||
isRefreshing,
|
||||
lastRefreshTime,
|
||||
}: SortToolbarProps) {
|
||||
return (
|
||||
<div className="flex items-center justify-between py-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<span className="text-sm text-slate-500">排序:</span>
|
||||
<select
|
||||
value={sortBy}
|
||||
onChange={(e) => onSortByChange(e.target.value as SortField)}
|
||||
className="text-sm border border-slate-200 rounded-md px-2 py-1 bg-white text-slate-700 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
>
|
||||
{SORT_OPTIONS.map((opt) => (
|
||||
<option key={opt.value} value={opt.value}>
|
||||
{opt.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
onClick={onSortOrderChange}
|
||||
className="text-sm border border-slate-200 rounded-md px-2 py-1 bg-white text-slate-700 hover:bg-slate-50 transition-colors"
|
||||
>
|
||||
{sortOrder === "desc" ? "↓ 降序" : "↑ 升序"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={onRefresh}
|
||||
disabled={isRefreshing}
|
||||
className="inline-flex items-center gap-1 text-sm text-slate-500 hover:text-slate-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
<RefreshCw
|
||||
className={cn("w-4 h-4", isRefreshing && "animate-spin")}
|
||||
/>
|
||||
刷新
|
||||
</button>
|
||||
{lastRefreshTime && (
|
||||
<span className="text-xs text-slate-400">
|
||||
上次: {lastRefreshTime}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
47
src/hooks/useContentQuery.ts
Normal file
47
src/hooks/useContentQuery.ts
Normal file
@ -0,0 +1,47 @@
|
||||
"use client";
|
||||
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useSettingsStore } from "@/stores/settings";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
import { MVP_PLATFORMS } from "@/lib/platforms";
|
||||
|
||||
async function fetchPlatformContent(platform: string): Promise<ContentItem[]> {
|
||||
const res = await fetch(`/api/tikhub/${platform}?count=20`);
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data.error || `请求失败: ${res.status}`);
|
||||
}
|
||||
const data = await res.json();
|
||||
return data.data || [];
|
||||
}
|
||||
|
||||
async function fetchAllPlatforms(): Promise<ContentItem[]> {
|
||||
const results = await Promise.allSettled(
|
||||
MVP_PLATFORMS.filter((p) => p.enabled).map((p) => fetchPlatformContent(p.id))
|
||||
);
|
||||
|
||||
return results.flatMap((r) => (r.status === "fulfilled" ? r.value : []));
|
||||
}
|
||||
|
||||
export function useContentQuery(platform: string) {
|
||||
const refreshInterval = useSettingsStore((s) => s.refreshInterval);
|
||||
|
||||
return useQuery<ContentItem[]>({
|
||||
queryKey: ["content", platform],
|
||||
queryFn: () =>
|
||||
platform === "all"
|
||||
? fetchAllPlatforms()
|
||||
: fetchPlatformContent(platform),
|
||||
refetchInterval: refreshInterval * 60 * 1000,
|
||||
refetchIntervalInBackground: false,
|
||||
});
|
||||
}
|
||||
|
||||
export function useRefreshContent() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return {
|
||||
refresh: (platform: string) =>
|
||||
queryClient.invalidateQueries({ queryKey: ["content", platform] }),
|
||||
};
|
||||
}
|
||||
27
src/hooks/useDetailQuery.ts
Normal file
27
src/hooks/useDetailQuery.ts
Normal file
@ -0,0 +1,27 @@
|
||||
"use client";
|
||||
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { ContentItem } from "@/types/content";
|
||||
|
||||
async function fetchDetail(
|
||||
platform: string,
|
||||
id: string
|
||||
): Promise<ContentItem> {
|
||||
const res = await fetch(
|
||||
`/api/tikhub/${platform}/detail?id=${encodeURIComponent(id)}`
|
||||
);
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data.error || `请求失败: ${res.status}`);
|
||||
}
|
||||
const data = await res.json();
|
||||
return data.data;
|
||||
}
|
||||
|
||||
export function useDetailQuery(platform: string, id: string) {
|
||||
return useQuery<ContentItem>({
|
||||
queryKey: ["detail", platform, id],
|
||||
queryFn: () => fetchDetail(platform, id),
|
||||
enabled: !!platform && !!id,
|
||||
});
|
||||
}
|
||||
29
src/lib/format.ts
Normal file
29
src/lib/format.ts
Normal file
@ -0,0 +1,29 @@
|
||||
export function formatCount(count: number | undefined): string | null {
|
||||
if (count === undefined || count === null) return null;
|
||||
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`;
|
||||
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}K`;
|
||||
return String(count);
|
||||
}
|
||||
|
||||
export function formatTime(isoString: string): string {
|
||||
try {
|
||||
const date = new Date(isoString);
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - date.getTime();
|
||||
const diffMin = Math.floor(diffMs / 60_000);
|
||||
const diffHour = Math.floor(diffMs / 3_600_000);
|
||||
const diffDay = Math.floor(diffMs / 86_400_000);
|
||||
|
||||
if (diffMin < 1) return "刚刚";
|
||||
if (diffMin < 60) return `${diffMin}分钟前`;
|
||||
if (diffHour < 24) return `${diffHour}小时前`;
|
||||
if (diffDay < 30) return `${diffDay}天前`;
|
||||
|
||||
return date.toLocaleDateString("zh-CN", {
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
});
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user