Files
2026FIFAWorldCup/platform/web/app/odds/page.tsx
QuantBot aa7e3bba76
Some checks failed
2026 World Cup Quant Platform - Production Deployment / Code Quality & Testing (push) Failing after 1m49s
2026 World Cup Quant Platform - Production Deployment / Deploy to Production VM via Rsync (push) Has been skipped
chore: migrate deployment to Gitea Actions with zero-trust rsync
2026-06-16 19:06:50 +08:00

115 lines
5.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useEffect, useState } from 'react';
import { OddsLineMovementChart } from '@/components/OddsLineMovementChart';
import { getAllMatches, getMatchById, type MatchListItem, type MatchDetail } from '@/lib/analytics-api';
export default function OddsPage() {
const [rows, setRows] = useState<{ match: MatchListItem; detail: MatchDetail | null }[]>([]);
const [selectedMatch, setSelectedMatch] = useState<MatchDetail | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function load() {
try {
const list = await getAllMatches();
const now = Date.now();
const upcoming = list
.filter((match) => new Date(match.kickoff_utc).getTime() >= now - 120 * 60 * 1000 && match.status !== '已結束')
.slice(0, 5);
const hydrated = await Promise.all(
upcoming.map(async (match) => {
try {
return { match, detail: await getMatchById(match.match_id) };
} catch {
return { match, detail: null };
}
}),
);
setRows(hydrated);
setSelectedMatch(hydrated.find((row) => row.detail?.odds_series.length)?.detail ?? hydrated[0]?.detail ?? null);
} catch (err) {
console.error(err);
} finally {
setLoading(false);
}
}
load();
const interval = window.setInterval(load, 60_000);
return () => window.clearInterval(interval);
}, []);
const chartData = selectedMatch?.odds_series.map(point => ({
time: new Date(point.recorded_at).toLocaleTimeString('zh-TW', { hour: '2-digit', minute: '2-digit' }),
bookmaker: point.bookmaker,
odds: point.decimal_odds,
})) || [];
function latestBookOdds(detail: MatchDetail | null): string[] {
if (!detail?.odds_series.length) return [];
const latest = [...detail.odds_series].sort((a, b) => new Date(b.recorded_at).getTime() - new Date(a.recorded_at).getTime());
const seen = new Set<string>();
const values: string[] = [];
for (const point of latest) {
const key = `${point.bookmaker}-${point.market_type}-${point.selection}`;
if (seen.has(key)) continue;
seen.add(key);
values.push(`${point.bookmaker}${point.market_type} ${point.selection} ${point.decimal_odds.toFixed(2)}`);
if (values.length === 3) break;
}
return values;
}
return (
<div className="space-y-6">
<h2 className="dot-matrix text-2xl font-bold text-cyan-400"></h2>
<section className="panel-glow rounded-2xl p-6">
<p className="text-sm text-slate-300 mb-4"></p>
{loading ? (
<p className="text-sm text-slate-400 dot-matrix dot-matrix-loading">...</p>
) : (
<div className="overflow-auto rounded-xl border border-slate-700 bg-slate-900/50">
<table className="min-w-full text-sm text-left">
<thead>
<tr className="bg-slate-800 text-slate-200">
<th className="px-4 py-3 border-b border-slate-700"></th>
<th className="px-4 py-3 border-b border-slate-700"> 1</th>
<th className="px-4 py-3 border-b border-slate-700"> 2</th>
<th className="px-4 py-3 border-b border-slate-700"> 3</th>
<th className="px-4 py-3 border-b border-slate-700"></th>
</tr>
</thead>
<tbody className="divide-y divide-slate-700">
{rows.map(({ match, detail }) => {
const odds = latestBookOdds(detail);
return (
<tr key={match.match_id} className="hover:bg-slate-800/50 transition">
<td className="px-4 py-3 text-slate-100">{match.home_team} vs {match.away_team}</td>
<td className="px-4 py-3 text-cyan-400">{odds[0] ?? '等待盤口'}</td>
<td className="px-4 py-3 text-cyan-400">{odds[1] ?? '等待盤口'}</td>
<td className="px-4 py-3 text-cyan-400">{odds[2] ?? '等待盤口'}</td>
<td className={odds.length ? 'px-4 py-3 text-emerald-400' : 'px-4 py-3 text-amber-300'}>
{odds.length ? '已有可檢查盤口' : '等待資料源'}
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)}
</section>
{selectedMatch && chartData.length > 0 && (
<OddsLineMovementChart data={chartData} teamName={selectedMatch.home_team} />
)}
{!loading && !rows.some((row) => row.detail?.odds_series.length) ? (
<section className="panel-glow rounded-2xl border-dashed p-5 text-sm text-slate-400">
1.90
</section>
) : null}
</div>
);
}