diff --git a/frontend/admin-web/src/components/features/system-account-report/SystemAccountsTab.tsx b/frontend/admin-web/src/components/features/system-account-report/SystemAccountsTab.tsx index 4c7c516f..ccc25696 100644 --- a/frontend/admin-web/src/components/features/system-account-report/SystemAccountsTab.tsx +++ b/frontend/admin-web/src/components/features/system-account-report/SystemAccountsTab.tsx @@ -300,11 +300,37 @@ export default function SystemAccountsTab() { * [2026-01-07] 更新:添加查看分类账明细按钮,在卡片下方公共区域显示明细 */ function FixedAccountsSection({ data }: { data: SystemAccountReportResponse['fixedAccounts'] }) { - // [2026-01-07] 新增:选中账户和分类账数据(hooks 必须在条件返回之前) + // [2026-01-07] 所有 hooks 必须在条件返回之前调用 const [selectedAccount, setSelectedAccount] = useState(null); const [allLedgerData, setAllLedgerData] = useState(null); const [ledgerLoading, setLedgerLoading] = useState(false); + // 加载所有固定账户的分类账明细(useCallback 必须在条件返回之前) + const loadAllLedger = useCallback(async () => { + if (allLedgerData) return; // 已加载过,不重复加载 + + setLedgerLoading(true); + try { + const response = await systemAccountReportService.getAllLedger({ pageSize: 100 }); + if (response.data) { + setAllLedgerData(response.data); + } + } catch (err) { + console.error('Failed to load ledger data:', err); + } finally { + setLedgerLoading(false); + } + }, [allLedgerData]); + + // 获取指定账户的分类账数据 + const getAccountLedger = useCallback((accountSequence: string): LedgerEntryDTO[] => { + if (!allLedgerData?.fixedAccountsLedger) return []; + const accountLedger = allLedgerData.fixedAccountsLedger.find( + (item) => item.accountSequence === accountSequence + ); + return accountLedger?.ledger || []; + }, [allLedgerData]); + // [2026-01-07] 修复:添加空值检查,防止 data 为 undefined 时报错 if (!data) { return
固定账户数据加载中...
; @@ -325,23 +351,6 @@ function FixedAccountsSection({ data }: { data: SystemAccountReportResponse['fix sequence: item.data?.accountSequence || '', })); - // 加载所有固定账户的分类账明细 - const loadAllLedger = useCallback(async () => { - if (allLedgerData) return; // 已加载过,不重复加载 - - setLedgerLoading(true); - try { - const response = await systemAccountReportService.getAllLedger({ pageSize: 100 }); - if (response.data) { - setAllLedgerData(response.data); - } - } catch (err) { - console.error('Failed to load ledger data:', err); - } finally { - setLedgerLoading(false); - } - }, [allLedgerData]); - // 选择账户查看明细 const handleSelectAccount = async (accountSequence: string) => { if (selectedAccount === accountSequence) { @@ -356,15 +365,6 @@ function FixedAccountsSection({ data }: { data: SystemAccountReportResponse['fix setSelectedAccount(accountSequence); }; - // 获取指定账户的分类账数据 - const getAccountLedger = (accountSequence: string): LedgerEntryDTO[] => { - if (!allLedgerData?.fixedAccountsLedger) return []; - const accountLedger = allLedgerData.fixedAccountsLedger.find( - (item) => item.accountSequence === accountSequence - ); - return accountLedger?.ledger || []; - }; - // 当前选中账户的明细数据 const currentLedger = selectedAccount ? getAccountLedger(selectedAccount) : [];