- useStockQuoteData: 合并行情数据和基本信息获取 - useStockCompare: 股票对比逻辑封装 - 为数据下沉优化做准备 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
92 lines
2.6 KiB
TypeScript
92 lines
2.6 KiB
TypeScript
/**
|
|
* useStockCompare - 股票对比逻辑 Hook
|
|
*
|
|
* 管理股票对比所需的数据获取和状态
|
|
*/
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
import { useToast } from '@chakra-ui/react';
|
|
import { financialService } from '@services/financialService';
|
|
import { logger } from '@utils/logger';
|
|
import type { StockInfo } from '../../FinancialPanorama/types';
|
|
|
|
interface UseStockCompareResult {
|
|
currentStockInfo: StockInfo | null;
|
|
compareStockInfo: StockInfo | null;
|
|
isCompareLoading: boolean;
|
|
handleCompare: (compareCode: string) => Promise<void>;
|
|
clearCompare: () => void;
|
|
}
|
|
|
|
/**
|
|
* 股票对比 Hook
|
|
*
|
|
* @param stockCode - 当前股票代码
|
|
*/
|
|
export const useStockCompare = (stockCode?: string): UseStockCompareResult => {
|
|
const toast = useToast();
|
|
const [currentStockInfo, setCurrentStockInfo] = useState<StockInfo | null>(null);
|
|
const [compareStockInfo, setCompareStockInfo] = useState<StockInfo | null>(null);
|
|
const [isCompareLoading, setIsCompareLoading] = useState(false);
|
|
|
|
// 加载当前股票财务信息(用于对比)
|
|
useEffect(() => {
|
|
const loadCurrentStockInfo = async () => {
|
|
if (!stockCode) {
|
|
setCurrentStockInfo(null);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await financialService.getStockInfo(stockCode);
|
|
setCurrentStockInfo(res.data);
|
|
} catch (error) {
|
|
logger.error('useStockCompare', 'loadCurrentStockInfo', error, { stockCode });
|
|
}
|
|
};
|
|
|
|
loadCurrentStockInfo();
|
|
// 股票代码变化时清除对比数据
|
|
setCompareStockInfo(null);
|
|
}, [stockCode]);
|
|
|
|
// 处理股票对比
|
|
const handleCompare = useCallback(async (compareCode: string) => {
|
|
if (!compareCode) return;
|
|
|
|
logger.debug('useStockCompare', '开始加载对比数据', { stockCode, compareCode });
|
|
setIsCompareLoading(true);
|
|
|
|
try {
|
|
const res = await financialService.getStockInfo(compareCode);
|
|
setCompareStockInfo(res.data);
|
|
logger.info('useStockCompare', '对比数据加载成功', { stockCode, compareCode });
|
|
} catch (error) {
|
|
logger.error('useStockCompare', 'handleCompare', error, { stockCode, compareCode });
|
|
toast({
|
|
title: '加载对比数据失败',
|
|
description: '请检查股票代码是否正确',
|
|
status: 'error',
|
|
duration: 3000,
|
|
});
|
|
} finally {
|
|
setIsCompareLoading(false);
|
|
}
|
|
}, [stockCode, toast]);
|
|
|
|
// 清除对比数据
|
|
const clearCompare = useCallback(() => {
|
|
setCompareStockInfo(null);
|
|
}, []);
|
|
|
|
return {
|
|
currentStockInfo,
|
|
compareStockInfo,
|
|
isCompareLoading,
|
|
handleCompare,
|
|
clearCompare,
|
|
};
|
|
};
|
|
|
|
export default useStockCompare;
|