refactor(DeepAnalysis): TypeScript 重构,提取 useDeepAnalysisData Hook
- 新增 types.ts:API 类型定义、状态接口、Tab 映射常量 - 新增 hooks/useDeepAnalysisData.ts:提取数据获取逻辑 - 懒加载:按 Tab 按需请求 - 数据缓存:已加载数据不重复请求 - 竞态处理:stockCode 变更时防止旧请求覆盖 - 重写 index.tsx:memo 优化,代码行数 229 → 81 - 新增 README.md:组件文档 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
81
src/views/Company/components/DeepAnalysis/index.tsx
Normal file
81
src/views/Company/components/DeepAnalysis/index.tsx
Normal file
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* DeepAnalysis - 深度分析组件
|
||||
*
|
||||
* 独立一级 Tab 组件,支持:
|
||||
* - 按 Tab 懒加载数据
|
||||
* - 已加载数据缓存
|
||||
* - 业务板块展开状态管理
|
||||
*/
|
||||
|
||||
import React, { useState, useCallback, useEffect, memo } from 'react';
|
||||
import DeepAnalysisTab from '../CompanyOverview/DeepAnalysisTab';
|
||||
import type { DeepAnalysisTabKey } from '../CompanyOverview/DeepAnalysisTab/types';
|
||||
import { useDeepAnalysisData } from './hooks';
|
||||
import { TAB_API_MAP } from './types';
|
||||
import type { DeepAnalysisProps } from './types';
|
||||
|
||||
/**
|
||||
* 深度分析组件
|
||||
*
|
||||
* @param stockCode 股票代码
|
||||
*/
|
||||
const DeepAnalysis: React.FC<DeepAnalysisProps> = memo(({ stockCode }) => {
|
||||
// 当前 Tab
|
||||
const [activeTab, setActiveTab] = useState<DeepAnalysisTabKey>('strategy');
|
||||
|
||||
// 业务板块展开状态
|
||||
const [expandedSegments, setExpandedSegments] = useState<Record<number, boolean>>({});
|
||||
|
||||
// 数据获取 Hook
|
||||
const { data, loading, loadTabData } = useDeepAnalysisData(stockCode);
|
||||
|
||||
// stockCode 变更时重置 UI 状态
|
||||
useEffect(() => {
|
||||
if (stockCode) {
|
||||
setActiveTab('strategy');
|
||||
setExpandedSegments({});
|
||||
}
|
||||
}, [stockCode]);
|
||||
|
||||
// 切换业务板块展开状态
|
||||
const toggleSegmentExpansion = useCallback((segmentIndex: number) => {
|
||||
setExpandedSegments((prev) => ({
|
||||
...prev,
|
||||
[segmentIndex]: !prev[segmentIndex],
|
||||
}));
|
||||
}, []);
|
||||
|
||||
// Tab 切换回调
|
||||
const handleTabChange = useCallback(
|
||||
(index: number, tabKey: DeepAnalysisTabKey) => {
|
||||
setActiveTab(tabKey);
|
||||
loadTabData(tabKey);
|
||||
},
|
||||
[loadTabData]
|
||||
);
|
||||
|
||||
// 获取当前 Tab 的 loading 状态
|
||||
const currentLoading = (() => {
|
||||
const apiKey = TAB_API_MAP[activeTab];
|
||||
return apiKey ? loading[apiKey] : false;
|
||||
})();
|
||||
|
||||
return (
|
||||
<DeepAnalysisTab
|
||||
comprehensiveData={data.comprehensive}
|
||||
valueChainData={data.valueChain}
|
||||
keyFactorsData={data.keyFactors}
|
||||
industryRankData={data.industryRank}
|
||||
loading={currentLoading}
|
||||
cardBg="white"
|
||||
expandedSegments={expandedSegments}
|
||||
onToggleSegment={toggleSegmentExpansion}
|
||||
activeTab={activeTab}
|
||||
onTabChange={handleTabChange}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
DeepAnalysis.displayName = 'DeepAnalysis';
|
||||
|
||||
export default DeepAnalysis;
|
||||
Reference in New Issue
Block a user