feat(Company): 集成股票对比功能
- 新增 currentStockInfo/compareStockInfo 状态管理 - 新增 handleCompare 处理对比数据加载 - StockQuoteCard 传入对比相关 props
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
// src/views/Company/index.js
|
// src/views/Company/index.js
|
||||||
// 公司详情页面入口 - 纯组合层
|
// 公司详情页面入口 - 纯组合层
|
||||||
|
|
||||||
import React, { useEffect, useRef } from 'react';
|
import React, { useEffect, useRef, useState, useCallback } from 'react';
|
||||||
import { Container, VStack } from '@chakra-ui/react';
|
import { Container, VStack, useToast } from '@chakra-ui/react';
|
||||||
import { useDispatch } from 'react-redux';
|
import { useDispatch } from 'react-redux';
|
||||||
import { loadAllStocks } from '@store/slices/stockSlice';
|
import { loadAllStocks } from '@store/slices/stockSlice';
|
||||||
|
import { financialService } from '@services/financialService';
|
||||||
|
import { logger } from '@utils/logger';
|
||||||
|
|
||||||
// 自定义 Hooks
|
// 自定义 Hooks
|
||||||
import { useCompanyStock } from './hooks/useCompanyStock';
|
import { useCompanyStock } from './hooks/useCompanyStock';
|
||||||
@@ -29,6 +31,7 @@ import CompanyTabs from './components/CompanyTabs';
|
|||||||
*/
|
*/
|
||||||
const CompanyIndex = () => {
|
const CompanyIndex = () => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
const toast = useToast();
|
||||||
|
|
||||||
// 1. 先获取股票代码(不带追踪回调)
|
// 1. 先获取股票代码(不带追踪回调)
|
||||||
const {
|
const {
|
||||||
@@ -50,6 +53,57 @@ const CompanyIndex = () => {
|
|||||||
// 2.1 获取公司基本信息
|
// 2.1 获取公司基本信息
|
||||||
const { basicInfo } = useBasicInfo(stockCode);
|
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)
|
// 3. 再初始化事件追踪(传入 stockCode)
|
||||||
const {
|
const {
|
||||||
trackStockSearched,
|
trackStockSearched,
|
||||||
@@ -92,7 +146,7 @@ const CompanyIndex = () => {
|
|||||||
bgColor="#1A202C"
|
bgColor="#1A202C"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* 股票行情卡片:价格、关键指标、主力动态、公司信息 */}
|
{/* 股票行情卡片:价格、关键指标、主力动态、公司信息、股票对比 */}
|
||||||
<StockQuoteCard
|
<StockQuoteCard
|
||||||
data={quoteData}
|
data={quoteData}
|
||||||
isLoading={isQuoteLoading}
|
isLoading={isQuoteLoading}
|
||||||
@@ -100,6 +154,12 @@ const CompanyIndex = () => {
|
|||||||
isWatchlistLoading={isWatchlistLoading}
|
isWatchlistLoading={isWatchlistLoading}
|
||||||
onWatchlistToggle={handleWatchlistToggle}
|
onWatchlistToggle={handleWatchlistToggle}
|
||||||
basicInfo={basicInfo}
|
basicInfo={basicInfo}
|
||||||
|
// 股票对比相关
|
||||||
|
currentStockInfo={currentStockInfo}
|
||||||
|
compareStockInfo={compareStockInfo}
|
||||||
|
isCompareLoading={isCompareLoading}
|
||||||
|
onCompare={handleCompare}
|
||||||
|
onCloseCompare={handleCloseCompare}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Tab 切换区域:概览、行情、财务、预测 */}
|
{/* Tab 切换区域:概览、行情、财务、预测 */}
|
||||||
|
|||||||
Reference in New Issue
Block a user