feat: AI分析页支持按日期查看历史报告(?date=,兼容?data=)
This commit is contained in:
@@ -35,6 +35,13 @@ export async function fetchAiReports(): Promise<AiReport[]> {
|
||||
return result.data || [];
|
||||
}
|
||||
|
||||
export async function fetchAiReportByDate(tradeDate: string): Promise<AiReport> {
|
||||
const resp = await fetch(`${API_BASE}/api/ai-analysis/by-date/${tradeDate}`);
|
||||
if (!resp.ok) throw new Error(`请求失败 (${resp.status})`);
|
||||
const result = await resp.json();
|
||||
return result.data;
|
||||
}
|
||||
|
||||
export async function fetchAiReport(id: number): Promise<AiReport> {
|
||||
const resp = await fetch(`${API_BASE}/api/ai-analysis/${id}`);
|
||||
if (!resp.ok) throw new Error(`请求失败 (${resp.status})`);
|
||||
|
||||
+70
-17
@@ -1,15 +1,22 @@
|
||||
import * as React from "react";
|
||||
import { Link, createFileRoute } from "@tanstack/react-router";
|
||||
import { Link, createFileRoute, useNavigate } from "@tanstack/react-router";
|
||||
import { ArrowLeft, Clock, Loader2, AlertCircle, Wrench, Calendar } from "lucide-react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import Markdown from "react-markdown";
|
||||
import remarkGfm from "remark-gfm";
|
||||
import type { Components } from "react-markdown";
|
||||
import { Mermaid } from "../components/Mermaid";
|
||||
import { fetchAiLatestReport } from "../lib/ai-analysis-api";
|
||||
import { fetchAiLatestReport, fetchAiReportByDate, fetchAiReports } from "../lib/ai-analysis-api";
|
||||
import { Card, CardContent } from "../components/ui/card";
|
||||
|
||||
export const Route = createFileRoute("/ai-analysis")({
|
||||
validateSearch: (search: Record<string, unknown>) => {
|
||||
return {
|
||||
date: typeof search.date === "string" ? search.date : undefined,
|
||||
// 兼容 ?data=YYYY-MM-DD 写法
|
||||
data: typeof search.data === "string" ? search.data : undefined,
|
||||
};
|
||||
},
|
||||
component: AiAnalysisPage,
|
||||
});
|
||||
|
||||
@@ -62,12 +69,29 @@ function splitReport(content: string): { intro: string; sections: { title: strin
|
||||
}
|
||||
|
||||
function AiAnalysisPage() {
|
||||
const navigate = useNavigate();
|
||||
const { date, data } = Route.useSearch();
|
||||
const tradeDate = date ?? data ?? undefined;
|
||||
const isHistorical = !!tradeDate;
|
||||
|
||||
const { data: report, isLoading, isError } = useQuery({
|
||||
queryKey: ["ai-report-latest"],
|
||||
queryFn: fetchAiLatestReport,
|
||||
queryKey: isHistorical ? ["ai-report", tradeDate] : ["ai-report-latest"],
|
||||
queryFn: () => (isHistorical ? fetchAiReportByDate(tradeDate!) : fetchAiLatestReport()),
|
||||
retry: false,
|
||||
});
|
||||
|
||||
const { data: history = [] } = useQuery({
|
||||
queryKey: ["ai-reports"],
|
||||
queryFn: fetchAiReports,
|
||||
retry: false,
|
||||
});
|
||||
|
||||
const goDate = (d?: string) => {
|
||||
navigate({ to: "/ai-analysis", search: d ? { date: d } : {} });
|
||||
};
|
||||
|
||||
const selectedDate = tradeDate ?? report?.trade_date ?? "";
|
||||
|
||||
const mdComponents: Components = {
|
||||
table({ children, ...props }) {
|
||||
return (
|
||||
@@ -96,17 +120,34 @@ function AiAnalysisPage() {
|
||||
<div className="min-h-screen bg-background text-foreground">
|
||||
<div className="max-w-4xl mx-auto px-3 sm:px-4 py-4 sm:py-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-2 sm:gap-3 mb-4 sm:mb-6">
|
||||
<Link to="/" className="text-muted-foreground hover:text-foreground transition-colors">
|
||||
<ArrowLeft className="h-5 w-5" />
|
||||
</Link>
|
||||
<h1 className="text-lg sm:text-xl md:text-2xl font-bold flex items-center gap-2">
|
||||
<Calendar className="h-5 w-5 text-primary" />
|
||||
AI 收盘分析
|
||||
{report?.issue_number ? (
|
||||
<span className="text-xs font-normal text-muted-foreground">总第 {report.issue_number} 期</span>
|
||||
) : null}
|
||||
</h1>
|
||||
<div className="flex items-center justify-between gap-2 sm:gap-3 mb-4 sm:mb-6">
|
||||
<div className="flex items-center gap-2 sm:gap-3 min-w-0">
|
||||
<Link to="/" className="text-muted-foreground hover:text-foreground transition-colors shrink-0">
|
||||
<ArrowLeft className="h-5 w-5" />
|
||||
</Link>
|
||||
<h1 className="text-lg sm:text-xl md:text-2xl font-bold flex items-center gap-2 truncate">
|
||||
<Calendar className="h-5 w-5 text-primary shrink-0" />
|
||||
AI 收盘分析
|
||||
{report?.issue_number ? (
|
||||
<span className="text-xs font-normal text-muted-foreground whitespace-nowrap">总第 {report.issue_number} 期</span>
|
||||
) : null}
|
||||
</h1>
|
||||
</div>
|
||||
{history.length > 0 && (
|
||||
<select
|
||||
value={history.some((h) => h.trade_date === selectedDate) ? selectedDate : ""}
|
||||
onChange={(e) => goDate(e.target.value || undefined)}
|
||||
className="text-xs sm:text-sm border border-border rounded-md bg-background px-2 py-1.5 text-foreground shrink-0"
|
||||
aria-label="选择历史报告日期"
|
||||
>
|
||||
<option value="">最新</option>
|
||||
{history.map((h) => (
|
||||
<option key={h.id} value={h.trade_date}>
|
||||
{h.trade_date}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Content */}
|
||||
@@ -118,8 +159,20 @@ function AiAnalysisPage() {
|
||||
) : isError || !report ? (
|
||||
<div className="flex flex-col items-center gap-3 py-20">
|
||||
<AlertCircle className="h-6 w-6 text-destructive" />
|
||||
<p className="text-sm text-muted-foreground">暂无分析报告</p>
|
||||
<p className="text-xs text-muted-foreground/60">请通过管理面板触发 AI 分析</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{isHistorical ? `${tradeDate} 暂无分析报告` : "暂无分析报告"}
|
||||
</p>
|
||||
{isHistorical ? (
|
||||
<Link
|
||||
to="/ai-analysis"
|
||||
search={{}}
|
||||
className="text-xs text-primary hover:underline"
|
||||
>
|
||||
查看最新报告
|
||||
</Link>
|
||||
) : (
|
||||
<p className="text-xs text-muted-foreground/60">请通过管理面板触发 AI 分析</p>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
|
||||
Reference in New Issue
Block a user