From 8ea83d6088d2a54cd9d97b84466435b07033ad6c Mon Sep 17 00:00:00 2001 From: defiQUG Date: Fri, 23 Jan 2026 17:12:14 -0800 Subject: [PATCH] Enhance Treasury page: add state management and account hierarchy visualization - Added refreshKey state to force re-renders when accounts change - Enhanced account hierarchy display with clickable subledger cards - Improved transfer tab with validation messages - Enhanced report generation with better UX - Auto-select newly created accounts - Update selected account after transfers --- apps/web/src/pages/TreasuryPage.tsx | 102 ++++++++++++++++++++++------ 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/apps/web/src/pages/TreasuryPage.tsx b/apps/web/src/pages/TreasuryPage.tsx index 9f7f333..f741fd0 100644 --- a/apps/web/src/pages/TreasuryPage.tsx +++ b/apps/web/src/pages/TreasuryPage.tsx @@ -1,4 +1,4 @@ -import React, { useState, useMemo } from 'react'; +import React, { useState, useMemo, useEffect } from 'react'; import { getAccountStore, createTreasuryAccount, @@ -23,15 +23,16 @@ export default function TreasuryPage() { const [reportData, setReportData] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const [error, setError] = useState(null); + const [refreshKey, setRefreshKey] = useState(0); // Force re-render when accounts change - // Get all accounts + // Get all accounts - refresh when refreshKey changes const treasuryAccounts = useMemo(() => { return accountStore.getAll().filter((acc) => acc.type === 'treasury') as TreasuryAccount[]; - }, [accountStore]); + }, [refreshKey]); const subledgerAccounts = useMemo(() => { return accountStore.getAll().filter((acc) => acc.type === 'subledger') as SubledgerAccount[]; - }, [accountStore]); + }, [refreshKey]); // Get subledgers for selected treasury account const subledgersForSelected = useMemo(() => { @@ -51,6 +52,8 @@ export default function TreasuryPage() { accountStore.add(account); setShowCreateTreasury(false); setError(null); + setRefreshKey((k) => k + 1); // Force re-render + setSelectedAccount(account); // Select the newly created account } catch (err) { setError(err instanceof Error ? err.message : 'Failed to create treasury account'); } @@ -62,6 +65,8 @@ export default function TreasuryPage() { accountStore.add(account); setShowCreateSubledger(false); setError(null); + setRefreshKey((k) => k + 1); // Force re-render + setSelectedAccount(account); // Select the newly created account } catch (err) { setError(err instanceof Error ? err.message : 'Failed to create subledger account'); } @@ -73,6 +78,12 @@ export default function TreasuryPage() { try { executeSubledgerTransfer(fromId, toId, amount, currency, description); setShowTransfer(false); + setRefreshKey((k) => k + 1); // Force re-render to update balances + // Update selected account if it was involved in the transfer + if (selectedAccount && (selectedAccount.id === fromId || selectedAccount.id === toId)) { + const updated = accountStore.get(selectedAccount.id); + if (updated) setSelectedAccount(updated); + } } catch (err) { setError(err instanceof Error ? err.message : 'Transfer failed'); } finally { @@ -281,21 +292,53 @@ export default function TreasuryPage() { {activeTab === 'transfers' && (
-

Inter-Subledger Transfers

- - {/* Transfer history would go here */} +
+

Inter-Subledger Transfers

+ +
+ {subledgerAccounts.length < 2 ? ( +

You need at least 2 subledger accounts to make transfers

+ ) : ( +
+

Recent transfers will appear here

+
+ )}
)} {activeTab === 'reports' && (
-

Subledger Reports

- {reportData && ( +
+

Subledger Reports

+ {subledgerAccounts.length > 0 && ( + + )} +
+ {!reportData ? ( +

+ {selectedAccount && selectedAccount.type === 'subledger' + ? 'Click "Generate Report" to create a report for the selected subledger account' + : 'Select a subledger account and generate a report'} +

+ ) : (

Report Summary

@@ -494,16 +537,31 @@ export default function TreasuryPage() {

)} - {selectedAccount.type === 'treasury' && subledgersForSelected.length > 0 && ( + {selectedAccount.type === 'treasury' && (

Subledgers ({subledgersForSelected.length})

-
    - {subledgersForSelected.map((sub) => ( -
  • - {sub.name} ({sub.accountNumber}) -
  • - ))} -
+ {subledgersForSelected.length > 0 ? ( +
+ {subledgersForSelected.map((sub) => ( +
setSelectedAccount(sub)} + > +

{sub.name}

+

+ {sub.accountNumber} | Balance: {sub.balance.toLocaleString('en-US', { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + })}{' '} + {sub.currency} +

+
+ ))} +
+ ) : ( +

No subledgers yet

+ )}
)} {selectedAccount.type === 'subledger' && (