diff --git a/src/views/Company/index.js b/src/views/Company/index.js index b1e01435..c57f931e 100644 --- a/src/views/Company/index.js +++ b/src/views/Company/index.js @@ -1,10 +1,12 @@ // src/views/Company/index.js // 公司详情页面入口 - 纯组合层 -import React, { useEffect, useRef } from 'react'; -import { Container, VStack } from '@chakra-ui/react'; +import React, { useEffect, useRef, useState, useCallback } from 'react'; +import { Container, VStack, useToast } from '@chakra-ui/react'; import { useDispatch } from 'react-redux'; import { loadAllStocks } from '@store/slices/stockSlice'; +import { financialService } from '@services/financialService'; +import { logger } from '@utils/logger'; // 自定义 Hooks import { useCompanyStock } from './hooks/useCompanyStock'; @@ -29,6 +31,7 @@ import CompanyTabs from './components/CompanyTabs'; */ const CompanyIndex = () => { const dispatch = useDispatch(); + const toast = useToast(); // 1. 先获取股票代码(不带追踪回调) const { @@ -50,6 +53,57 @@ const CompanyIndex = () => { // 2.1 获取公司基本信息 const { basicInfo } = useBasicInfo(stockCode); + // 5. 股票对比状态管理 + const [currentStockInfo, setCurrentStockInfo] = useState(null); + const [compareStockInfo, setCompareStockInfo] = useState(null); + const [isCompareLoading, setIsCompareLoading] = useState(false); + + // 加载当前股票财务信息(用于对比) + useEffect(() => { + const loadCurrentStockInfo = async () => { + if (!stockCode) return; + try { + const res = await financialService.getStockInfo(stockCode); + setCurrentStockInfo(res.data); + } catch (error) { + logger.error('CompanyIndex', 'loadCurrentStockInfo', error, { stockCode }); + } + }; + loadCurrentStockInfo(); + // 清除对比数据 + setCompareStockInfo(null); + }, [stockCode]); + + // 处理股票对比 + const handleCompare = useCallback(async (compareCode) => { + if (!compareCode) return; + + logger.debug('CompanyIndex', '开始加载对比数据', { stockCode, compareCode }); + setIsCompareLoading(true); + + try { + const res = await financialService.getStockInfo(compareCode); + setCompareStockInfo(res.data); + logger.info('CompanyIndex', '对比数据加载成功', { stockCode, compareCode }); + } catch (error) { + logger.error('CompanyIndex', 'handleCompare', error, { stockCode, compareCode }); + toast({ + title: '加载对比数据失败', + description: '请检查股票代码是否正确', + status: 'error', + duration: 3000, + }); + } finally { + setIsCompareLoading(false); + } + }, [stockCode, toast]); + + // 关闭对比弹窗 + const handleCloseCompare = useCallback(() => { + // 可选:清除对比数据 + // setCompareStockInfo(null); + }, []); + // 3. 再初始化事件追踪(传入 stockCode) const { trackStockSearched, @@ -92,7 +146,7 @@ const CompanyIndex = () => { bgColor="#1A202C" /> - {/* 股票行情卡片:价格、关键指标、主力动态、公司信息 */} + {/* 股票行情卡片:价格、关键指标、主力动态、公司信息、股票对比 */} { isWatchlistLoading={isWatchlistLoading} onWatchlistToggle={handleWatchlistToggle} basicInfo={basicInfo} + // 股票对比相关 + currentStockInfo={currentStockInfo} + compareStockInfo={compareStockInfo} + isCompareLoading={isCompareLoading} + onCompare={handleCompare} + onCloseCompare={handleCloseCompare} /> {/* Tab 切换区域:概览、行情、财务、预测 */}