Skip to content

一分钟搭建影视站,支持Docker等部署方式

feat: 页脚仓库链接悬停显示上游版本检测提示(已是最新/发现新版本)

bestZwei76cb6f0bf2a5
diff --git a/src/app/page.tsx b/src/app/page.tsx
index fb823fc..3a250ea 100644
--- a/src/app/page.tsx
+++ b/src/app/page.tsx
@@ -24,6 +24,88 @@ 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>
@@ -37,6 +119,7 @@ function HomeContent() {
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);
@@ -371,11 +454,12 @@ function HomeContent() {
href="https://github.com/LibreSpark/LibreTV"
target="_blank"
rel="noopener noreferrer"
- className="hover:text-accent"
+ className="group relative inline-block cursor-help hover:text-accent"
>
- LibreTV
+ LibreTV{version ? ` v${version}` : ''}
+ {version && <UpdateTip update={upstreamUpdate} />}
</a>
- {version ? ` v${version} · ` : ' '}
+ {' · '}
AGPL-3.0 License
</p>
</footer>