一分钟搭建影视站,支持Docker等部署方式
refactor(footer): 抽取统一的 SiteFooter 组件,首页与关于页共用版本更新检测
bestZwei3b15fa9af27f
diff --git a/src/app/about/page.tsx b/src/app/about/page.tsxindex 1fcf776..45bc91e 100644--- a/src/app/about/page.tsx+++ b/src/app/about/page.tsx@@ -1,10 +1,9 @@'use client';import { Header } from '@/components/header';-import { useAuth } from '@/components/auth';+import { SiteFooter } from '@/components/site-footer';export default function AboutPage() {- const { version } = useAuth();return (<div className="min-h-screen flex flex-col"><Header />@@ -45,20 +44,7 @@ export default function AboutPage() {</p></section></main>- <footer className="border-t border-line py-4">- <p className="text-center text-xs text-faint">- <a- href="https://github.com/LibreSpark/LibreTV"- target="_blank"- rel="noopener noreferrer"- className="hover:text-accent"- >- LibreTV- </a>- {version ? ` v${version} · ` : ' '}- AGPL-3.0 License- </p>- </footer>+ <SiteFooter /></div>);}diff --git a/src/app/page.tsx b/src/app/page.tsxindex 3a250ea..94e81c4 100644--- a/src/app/page.tsx+++ b/src/app/page.tsx@@ -13,9 +13,9 @@ import type { SearchResultItem, SourceSearchOutcome } from '@/lib/types';import { SearchHistoryDropdown, useSearchHistory } from '@/components/search-history';import { cn, formatDisableTtl, validateSourceUrl } from '@/lib/utils';import { useToast } from '@/components/toast';-import { useAuth } from '@/components/auth';import { EmptyState } from '@/components/states';import { Icon } from '@/components/icon';+import { SiteFooter } from '@/components/site-footer';/*** 首页:搜索(URL ?s= 驱动,可后退/分享)+ 豆瓣推荐。@@ -24,88 +24,6 @@ import { Icon } from '@/components/icon';/** 搜索结果分批渲染的批大小:一次挂载上千张卡片会明显掉帧 */const RESULT_PAGE_SIZE = 60;-/** 上游仓库最新 tag(即最新版本号),用于页脚的更新检测 */-const UPSTREAM_TAGS_API = 'https://api.github.com/repos/LibreSpark/LibreTV/tags?per_page=1';--/** 页脚版本号的更新检测结果;null 表示无需检测(本地 dev 构建) */-type UpstreamUpdate = { status: 'checking' | 'latest' | 'newer'; latest?: string };--/** 点分版本号比较:a 大于 b 返回正数 */-function compareVersions(a: string, b: string): number {- const pa = a.split('.').map((n) => parseInt(n, 10) || 0);- const pb = b.split('.').map((n) => parseInt(n, 10) || 0);- for (let i = 0; i < Math.max(pa.length, pb.length); i++) {- const diff = (pa[i] || 0) - (pb[i] || 0);- if (diff !== 0) return diff;- }- return 0;-}--/** 检测上游是否有更新的版本;检测失败或本地构建(dev)时静默返回 null */-function useUpstreamUpdate(current: string | null): UpstreamUpdate | null {- const [state, setState] = useState<UpstreamUpdate | null>({ status: 'checking' });- useEffect(() => {- if (!current || current === 'dev') {- setState(null);- return;- }- // 按当前版本隔离缓存到 sessionStorage:一次会话最多请求一次 GitHub API- const cacheKey = `upstream-latest-tag:${current}`;- const cached = sessionStorage.getItem(cacheKey);- if (cached) {- setState(- compareVersions(cached, current) > 0- ? { status: 'newer', latest: cached }- : { status: 'latest' },- );- return;- }- let alive = true;- fetch(UPSTREAM_TAGS_API)- .then((r) => (r.ok ? r.json() : []))- .then((tags: { name?: string }[]) => {- const name = tags[0]?.name?.replace(/^v/, '');- if (!name || !alive) return;- try {- sessionStorage.setItem(cacheKey, name);- } catch {- /* 隐私模式等场景下缓存不可写,忽略即可 */- }- setState(- compareVersions(name, current) > 0- ? { status: 'newer', latest: name }- : { status: 'latest' },- );- })- .catch(() => {- /* 网络受限 / 触发 GitHub 限流时静默跳过 */- if (alive) setState(null);- });- return () => {- alive = false;- };- }, [current]);- return state;-}--/** 页脚仓库链接上的悬停提示:显示更新检测结果 */-function UpdateTip({ update }: { update: UpstreamUpdate | null }) {- const tip =- update === null- ? null- : update.status === 'newer'- ? `发现新版本 v${update.latest}`- : update.status === 'latest'- ? '已是最新版本'- : '正在检测更新…';- if (!tip) return null;- return (- <span className="pointer-events-none absolute bottom-full left-1/2 z-10 mb-1.5 hidden -translate-x-1/2 whitespace-nowrap rounded-md border border-line bg-chip px-2 py-1 text-faint shadow-sm group-hover:block">- {tip}- </span>- );-}-export default function HomePage() {return (<Suspense>@@ -118,8 +36,6 @@ function HomeContent() {const searchParams = useSearchParams();const router = useRouter();const { toast } = useToast();- const { version } = useAuth();- const upstreamUpdate = useUpstreamUpdate(version);const urlQuery = searchParams.get('s') || '';const store = useAppStore();const [input, setInput] = useState(urlQuery);@@ -448,21 +364,7 @@ function HomeContent() {)}</main>- <footer className="border-t border-line py-4">- <p className="text-center text-xs text-faint">- <a- href="https://github.com/LibreSpark/LibreTV"- target="_blank"- rel="noopener noreferrer"- className="group relative inline-block cursor-help hover:text-accent"- >- LibreTV{version ? ` v${version}` : ''}- {version && <UpdateTip update={upstreamUpdate} />}- </a>- {' · '}- AGPL-3.0 License- </p>- </footer>+ <SiteFooter /><DetailModal item={detailItem} onClose={() => setDetailItem(null)} /></div>diff --git a/src/components/site-footer.tsx b/src/components/site-footer.tsxnew file mode 100644index 0000000..3665ce9--- /dev/null+++ b/src/components/site-footer.tsx@@ -0,0 +1,109 @@+'use client';++import { useEffect, useState } from 'react';+import { useAuth } from '@/components/auth';++/** 上游仓库最新 tag(即最新版本号),用于页脚的更新检测 */+const UPSTREAM_TAGS_API = 'https://api.github.com/repos/LibreSpark/LibreTV/tags?per_page=1';++/** 页脚版本号的更新检测结果;null 表示无需检测(本地 dev 构建) */+type UpstreamUpdate = { status: 'checking' | 'latest' | 'newer'; latest?: string };++/** 点分版本号比较:a 大于 b 返回正数 */+function compareVersions(a: string, b: string): number {+ const pa = a.split('.').map((n) => parseInt(n, 10) || 0);+ const pb = b.split('.').map((n) => parseInt(n, 10) || 0);+ for (let i = 0; i < Math.max(pa.length, pb.length); i++) {+ const diff = (pa[i] || 0) - (pb[i] || 0);+ if (diff !== 0) return diff;+ }+ return 0;+}++/** 检测上游是否有更新的版本;检测失败或本地构建(dev)时静默返回 null */+function useUpstreamUpdate(current: string | null): UpstreamUpdate | null {+ const [state, setState] = useState<UpstreamUpdate | null>({ status: 'checking' });+ useEffect(() => {+ if (!current || current === 'dev') {+ setState(null);+ return;+ }+ // 按当前版本隔离缓存到 sessionStorage:一次会话最多请求一次 GitHub API+ const cacheKey = `upstream-latest-tag:${current}`;+ const cached = sessionStorage.getItem(cacheKey);+ if (cached) {+ setState(+ compareVersions(cached, current) > 0+ ? { status: 'newer', latest: cached }+ : { status: 'latest' },+ );+ return;+ }+ let alive = true;+ fetch(UPSTREAM_TAGS_API)+ .then((r) => (r.ok ? r.json() : []))+ .then((tags: { name?: string }[]) => {+ const name = tags[0]?.name?.replace(/^v/, '');+ if (!name || !alive) return;+ try {+ sessionStorage.setItem(cacheKey, name);+ } catch {+ /* 隐私模式等场景下缓存不可写,忽略即可 */+ }+ setState(+ compareVersions(name, current) > 0+ ? { status: 'newer', latest: name }+ : { status: 'latest' },+ );+ })+ .catch(() => {+ /* 网络受限 / 触发 GitHub 限流时静默跳过 */+ if (alive) setState(null);+ });+ return () => {+ alive = false;+ };+ }, [current]);+ return state;+}++/** 页脚仓库链接上的悬停提示:显示更新检测结果 */+function UpdateTip({ update }: { update: UpstreamUpdate | null }) {+ const tip =+ update === null+ ? null+ : update.status === 'newer'+ ? `发现新版本 v${update.latest}`+ : update.status === 'latest'+ ? '已是最新版本'+ : '正在检测更新…';+ if (!tip) return null;+ return (+ <span className="pointer-events-none absolute bottom-full left-1/2 z-10 mb-1.5 hidden -translate-x-1/2 whitespace-nowrap rounded-md border border-line bg-chip px-2 py-1 text-faint shadow-sm group-hover:block">+ {tip}+ </span>+ );+}++/** 全站统一页脚:仓库链接 + 版本号(悬停显示上游更新检测结果) */+export function SiteFooter() {+ const { version } = useAuth();+ const upstreamUpdate = useUpstreamUpdate(version);+ return (+ <footer className="border-t border-line py-4">+ <p className="text-center text-xs text-faint">+ <a+ href="https://github.com/LibreSpark/LibreTV"+ target="_blank"+ rel="noopener noreferrer"+ className="group relative inline-block cursor-help hover:text-accent"+ >+ LibreTV{version ? ` v${version}` : ''}+ {version && <UpdateTip update={upstreamUpdate} />}+ </a>+ {' · '}+ AGPL-3.0 License+ </p>+ </footer>+ );+}