Răsfoiți Sursa

♻️ refactor(supply-chain): 优化产业默认值设置逻辑

- 移除defaultIndustry的硬编码默认值,改为通过函数动态获取
- 新增getDefaultIndustry函数,根据不同dashboard类型返回对应默认产业
- 修改handleSetDashboard方法,使用新的默认产业获取逻辑重置当前产业
- 支持dashboard类型与产业类型的关联映射:粮食-粮食、种业-种业、畜牧-畜牧、鲜食-鲜食
yourname 2 luni în urmă
părinte
comite
9a05bda9b1

+ 17 - 5
src/client/home/pages/SupplyChainDashboards/context/SupplyChainContext.tsx

@@ -990,10 +990,23 @@ const loadStaticData = async (dashboardType: DashboardType): Promise<SupplyChain
 export const SupplyChainProvider: React.FC<SupplyChainProviderProps> = ({
   children,
   defaultDashboard = 'grain-oil',
-  defaultIndustry = '粮食'
+  defaultIndustry
 }) => {
+  // 根据组合类型获取默认产业
+  const getDefaultIndustry = (dashboard: DashboardType): IndustryType => {
+    const defaultIndustries: Record<DashboardType, IndustryType> = {
+      'grain-oil': '粮食',
+      'seed-fruit': '种业',
+      'livestock-aquaculture': '畜牧',
+      'fresh-food-salt': '鲜食'
+    };
+    return defaultIndustries[dashboard];
+  };
+
   const [currentDashboard, setCurrentDashboard] = useState<DashboardType>(defaultDashboard);
-  const [currentIndustry, setCurrentIndustry] = useState<IndustryType>(defaultIndustry);
+  const [currentIndustry, setCurrentIndustry] = useState<IndustryType>(
+    defaultIndustry || getDefaultIndustry(defaultDashboard)
+  );
   const [currentData, setCurrentData] = useState<SupplyChainData | null>(null);
   const [isLoading, setIsLoading] = useState(false);
   const [error, setError] = useState<string | null>(null);
@@ -1034,9 +1047,8 @@ export const SupplyChainProvider: React.FC<SupplyChainProviderProps> = ({
   const handleSetDashboard = (dashboard: DashboardType) => {
     setCurrentDashboard(dashboard);
     // 重置当前产业为组合的第一个产业
-    if (currentData && currentData.industries.length > 0) {
-      setCurrentIndustry(currentData.industries[0]);
-    }
+    const newDefaultIndustry = getDefaultIndustry(dashboard);
+    setCurrentIndustry(newDefaultIndustry);
   };
 
   // 设置产业