"use client" import { useState, useEffect } from "react" import { useSearchParams } from "next/navigation" import { cn } from "@/lib/utils" import { SearchStock } from "@/components/search-stock" import { HistoryList } from "@/components/history-list" import { DataSourceSelector } from "@/components/data-source-selector" import { DataStatusCard } from "@/components/data-status-card" import { FinancialTables } from "@/components/financial-tables" import { AnalysisTrigger } from "@/components/analysis-trigger" import { AnalysisReport } from "@/components/analysis-report" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { ArrowLeft, Building2, RefreshCw, Loader2, CheckCircle2, ChevronLeft, ChevronRight } from "lucide-react" import type { SearchResult } from "@/lib/types" import { useFinancialData } from "@/hooks/use-financial-data" import { Progress } from "@/components/ui/progress" import { formatTimestamp } from "@/lib/formatters" import { BloombergView } from "@/components/bloomberg-view" import { HeaderPortal } from "@/components/header-portal" import { AppSidebar } from "@/components/app-sidebar" import { StockChart } from "@/components/stock-chart" import { AiDiscussionView } from "@/components/ai-discussion-view" import { HistoryView } from "@/components/history-view" export default function Home() { const searchParams = useSearchParams() // 状态管理 const [selectedCompany, setSelectedCompany] = useState(null) const [selectedDataSource, setSelectedDataSource] = useState("iFinD") const [isSidebarOpen, setIsSidebarOpen] = useState(true) const [companyId, setCompanyId] = useState(null) const [analysisId, setAnalysisId] = useState(null) const [oneTimeModel, setOneTimeModel] = useState(undefined) const [currency, setCurrency] = useState("Auto") // View State (home, financial, chart) const [currentView, setCurrentView] = useState("home") // 处理公司选择 const handleCompanySelect = (company: SearchResult, dataSource?: string) => { setSelectedCompany(company) setCompanyId(null) setAnalysisId(null) setOneTimeModel(undefined) // Switch to financial view by default when company is selected setCurrentView("financial") // 如果没有传入数据源,则根据市场设置默认值 const targetDataSource = dataSource || (company.market === 'CN' ? 'Tushare' : 'iFinD') setSelectedDataSource(targetDataSource) } // 监听 URL 参数变化实现快速导航 useEffect(() => { const symbol = searchParams.get('symbol') const market = searchParams.get('market') const source = searchParams.get('source') const name = searchParams.get('name') if (symbol && market && source) { const company: SearchResult = { symbol, market, company_name: name || symbol } handleCompanySelect(company, source) } }, [searchParams]) // 数据准备就绪 const handleDataReady = (id: number) => { setCompanyId(id) } // AI分析完成 const handleAnalysisComplete = (id: number) => { setAnalysisId(id) } // 返回搜索 (Reset) const handleBackToSearch = () => { setSelectedCompany(null) setCompanyId(null) setAnalysisId(null) setOneTimeModel(undefined) setCurrentView("home") } // Navigation Handler const handleTabChange = (tab: string) => { if (tab === "home") { handleBackToSearch() } else { setCurrentView(tab) } } // Render Content based on View const renderContent = () => { // History View (Global, no company selection needed) if (currentView === "history") { return (
) } // Home View if (!selectedCompany || currentView === "home") { return (

股票分析

输入公司名称或股票代码,开始全面的AI驱动的分析。支持中国、香港、美国、日本、越南的公司分析。

{/* 搜索组件 */}
{/* 历史记录 */}
) } // Chart View if (currentView === "chart") { return (
{/* Top Navigation for Chart View */}

{selectedCompany.company_name}

{selectedCompany.symbol} {selectedCompany.market}
) } // AI Discussion View if (currentView === "ai-research") { return (
) } // Financial Data View (Default fallback) return (
{/* 顶部导航栏 (Portal Target) */}
{/* 数据源选择 */}
{/* 数据获取状态 */}
) } return (
{/* Sidebar */}
{/* Main Content Area Wrapper */}
{/* Sidebar Toggle Button */} {/* Scrollable Content */}
{renderContent()}
) } // ---------------------------------------------------------------------- // Sub-components // ---------------------------------------------------------------------- function CompanyAnalysisView({ company, dataSource, onDataReady, onAnalysisComplete, selectedModel, currency, setCurrency }: { company: SearchResult dataSource: string onDataReady: (id: number) => void onAnalysisComplete: (id: number) => void selectedModel?: string currency: string setCurrency: (c: string) => void }) { const { status, loading, fetching, error, fetchData, checkStatus, updateStatus } = useFinancialData(company, dataSource) const progress = updateStatus ? { percentage: updateStatus.progress_percentage || 0, message: updateStatus.progress_message || "" } : null useEffect(() => { if (status && status.has_data && status.company_id) { onDataReady(status.company_id) } }, [status, onDataReady]) return (
{/* Header Controls */}
{company.company_name} {company.symbol} {company.market}
{["Auto", "CNY", "USD"].map((opt) => ( ))}
{fetching && (
{progress?.message || "准备中..."}
)} {!fetching && !loading &&
已更新: {status?.last_update?.date ? formatTimestamp(status.last_update.date) : "无记录"}
}
{/* DataStatusCard usage removed because it duplicates logic and caused prop mismatch. Status is now handled by HeaderPortal controls. */} {error && (
{/* Reuse icon or AlertCircle */} {error}
)} {dataSource === 'Bloomberg' ? ( status?.company_id && ( ) ) : ( status?.company_id && ( ) )}
) } function SearchStockWithSelection({ onSelect }: { onSelect: (c: SearchResult, s?: string) => void }) { return ( 搜索股票 ) }