refactor(Company): 简化 CompanyHeader,添加详细代码注释
- CompanyHeader: 移除冗余的股票信息展示(已在 StockQuoteCard 中) - index.tsx: 添加完整的 JSDoc 注释和架构说明 - types.ts: 简化 CompanyHeaderProps,移除不再需要的属性 - useStockQuoteData: 优化数据获取逻辑 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -74,45 +74,46 @@ export const useStockQuoteData = (stockCode?: string): UseStockQuoteDataResult =
|
||||
const [basicLoading, setBasicLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// 用于手动刷新的 ref
|
||||
// 用于手动刷新的 ref(并行请求)
|
||||
const refetchRef = useCallback(async () => {
|
||||
if (!stockCode) return;
|
||||
|
||||
// 标准化股票代码(去除后缀)
|
||||
const baseCode = stockCode.split('.')[0];
|
||||
|
||||
// 获取行情详情数据(使用新的 quote-detail 接口)
|
||||
// 并行获取行情详情和基本信息
|
||||
setQuoteLoading(true);
|
||||
setBasicLoading(true);
|
||||
setError(null);
|
||||
|
||||
logger.debug('useStockQuoteData', '刷新股票数据', { stockCode, baseCode });
|
||||
|
||||
try {
|
||||
logger.debug('useStockQuoteData', '获取股票行情详情', { stockCode, baseCode });
|
||||
const { data: result } = await axios.get(`/api/stock/${baseCode}/quote-detail`);
|
||||
if (result.success && result.data) {
|
||||
const transformedData = transformQuoteData(result.data, stockCode);
|
||||
const [quoteResult, basicResult] = await Promise.all([
|
||||
axios.get(`/api/stock/${baseCode}/quote-detail`),
|
||||
axios.get(`/api/stock/${baseCode}/basic-info`),
|
||||
]);
|
||||
|
||||
// 处理行情数据
|
||||
if (quoteResult.data.success && quoteResult.data.data) {
|
||||
const transformedData = transformQuoteData(quoteResult.data.data, stockCode);
|
||||
logger.debug('useStockQuoteData', '行情数据转换完成', { stockCode, hasData: !!transformedData });
|
||||
setQuoteData(transformedData);
|
||||
} else {
|
||||
setError('获取行情数据失败');
|
||||
setQuoteData(null);
|
||||
}
|
||||
|
||||
// 处理基本信息
|
||||
if (basicResult.data.success) {
|
||||
setBasicInfo(basicResult.data.data);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('useStockQuoteData', '获取行情失败', err);
|
||||
setError('获取行情数据失败');
|
||||
logger.error('useStockQuoteData', '刷新数据失败', err);
|
||||
setError('刷新数据失败');
|
||||
setQuoteData(null);
|
||||
} finally {
|
||||
setQuoteLoading(false);
|
||||
}
|
||||
|
||||
// 获取基本信息(公司简介等)
|
||||
setBasicLoading(true);
|
||||
try {
|
||||
const { data: result } = await axios.get(`/api/stock/${baseCode}/basic-info`);
|
||||
if (result.success) {
|
||||
setBasicInfo(result.data);
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error('useStockQuoteData', '获取基本信息失败', err);
|
||||
} finally {
|
||||
setBasicLoading(false);
|
||||
}
|
||||
}, [stockCode]);
|
||||
@@ -132,47 +133,45 @@ export const useStockQuoteData = (stockCode?: string): UseStockQuoteDataResult =
|
||||
const baseCode = stockCode.split('.')[0];
|
||||
|
||||
const fetchData = async () => {
|
||||
// 获取行情详情数据(使用新的 quote-detail 接口)
|
||||
// 并行获取行情详情和基本信息(优化:原串行改为并行,节省 ~120ms)
|
||||
setQuoteLoading(true);
|
||||
setBasicLoading(true);
|
||||
setError(null);
|
||||
|
||||
logger.debug('useStockQuoteData', '并行获取股票数据', { stockCode, baseCode });
|
||||
|
||||
try {
|
||||
logger.debug('useStockQuoteData', '获取股票行情详情', { stockCode, baseCode });
|
||||
const { data: result } = await axios.get(`/api/stock/${baseCode}/quote-detail`, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
const [quoteResult, basicResult] = await Promise.all([
|
||||
axios.get(`/api/stock/${baseCode}/quote-detail`, { signal: controller.signal }),
|
||||
axios.get(`/api/stock/${baseCode}/basic-info`, { signal: controller.signal }),
|
||||
]);
|
||||
|
||||
if (isCancelled) return;
|
||||
if (result.success && result.data) {
|
||||
const transformedData = transformQuoteData(result.data, stockCode);
|
||||
|
||||
// 处理行情数据
|
||||
if (quoteResult.data.success && quoteResult.data.data) {
|
||||
const transformedData = transformQuoteData(quoteResult.data.data, stockCode);
|
||||
logger.debug('useStockQuoteData', '行情数据转换完成', { stockCode, hasData: !!transformedData });
|
||||
setQuoteData(transformedData);
|
||||
} else {
|
||||
setError('获取行情数据失败');
|
||||
setQuoteData(null);
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (isCancelled || err.name === 'CanceledError') return;
|
||||
logger.error('useStockQuoteData', '获取行情失败', err);
|
||||
setError('获取行情数据失败');
|
||||
setQuoteData(null);
|
||||
} finally {
|
||||
if (!isCancelled) setQuoteLoading(false);
|
||||
}
|
||||
|
||||
// 获取基本信息(公司简介等)
|
||||
setBasicLoading(true);
|
||||
try {
|
||||
const { data: result } = await axios.get(`/api/stock/${baseCode}/basic-info`, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (isCancelled) return;
|
||||
if (result.success) {
|
||||
setBasicInfo(result.data);
|
||||
// 处理基本信息
|
||||
if (basicResult.data.success) {
|
||||
setBasicInfo(basicResult.data.data);
|
||||
}
|
||||
} catch (err: any) {
|
||||
if (isCancelled || err.name === 'CanceledError') return;
|
||||
logger.error('useStockQuoteData', '获取基本信息失败', err);
|
||||
logger.error('useStockQuoteData', '获取数据失败', err);
|
||||
setError('获取数据失败');
|
||||
setQuoteData(null);
|
||||
} finally {
|
||||
if (!isCancelled) setBasicLoading(false);
|
||||
if (!isCancelled) {
|
||||
setQuoteLoading(false);
|
||||
setBasicLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user