From b273e527785e720aed388b9f3c9831486d139685 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Thu, 9 Apr 2026 11:48:34 +0200 Subject: [PATCH] Fix Inspect Ledgers: stable checkbox, currency code, clean state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Checkbox for "own transactions only" now lives outside the data-dependent rendering block — visible whenever the selected account has children, regardless of query results - Selected account stored as ID (number), not object reference, eliminating React re-render reference-equality issues - isGroupAccount derived via useMemo from stable primitives - ownOnly reset moved to onChange handler, not effects - Autocomplete uses isOptionEqualToValue for stable comparison - Currency code (SEK/EUR) shown in ledger header from org API Made-with: Cursor --- frontend/src/pages/InspectLedgersPage.tsx | 57 ++++++++++++----------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/frontend/src/pages/InspectLedgersPage.tsx b/frontend/src/pages/InspectLedgersPage.tsx index cc9b17a0..a66e9778 100644 --- a/frontend/src/pages/InspectLedgersPage.tsx +++ b/frontend/src/pages/InspectLedgersPage.tsx @@ -43,14 +43,12 @@ const YEARS = Array.from({ length: 15 }, (_, i) => currentYear - i) export default function InspectLedgersPage() { const { activeOrg } = useOrg() const [accounts, setAccounts] = useState([]) - const [selectedAccount, setSelectedAccount] = useState(null) - // Type adapter for autocomplete which uses AccountWithDepth + const [selectedAccountId, setSelectedAccountId] = useState(null) const [year, setYear] = useState(currentYear) const [month, setMonth] = useState(0) const [data, setData] = useState(null) const [loading, setLoading] = useState(false) const [ownOnly, setOwnOnly] = useState(false) - const [isGroupAccount, setIsGroupAccount] = useState(false) useEffect(() => { if (!activeOrg) return @@ -59,23 +57,22 @@ export default function InspectLedgersPage() { .catch(() => {}) }, [activeOrg]) - useEffect(() => { - if (!activeOrg || !selectedAccount) return - setOwnOnly(false) - setIsGroupAccount(selectedAccount.hasChildren) - }, [activeOrg, selectedAccount]) + const isGroupAccount = useMemo(() => { + if (!selectedAccountId) return false + return accounts.some(a => a.parentId === selectedAccountId) + }, [selectedAccountId, accounts]) useEffect(() => { - if (!activeOrg || !selectedAccount) return + if (!activeOrg || !selectedAccountId) return setLoading(true) const ownParam = ownOnly ? '&ownOnly=true' : '' api.get( - `/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccount.id}&year=${year}&month=${month}${ownParam}` + `/organizations/${activeOrg.organizationId}/inspect/ledger?accountId=${selectedAccountId}&year=${year}&month=${month}${ownParam}` ) .then(setData) .catch(() => setData(null)) .finally(() => setLoading(false)) - }, [activeOrg, selectedAccount, year, month, ownOnly]) + }, [activeOrg, selectedAccountId, year, month, ownOnly]) interface AccountWithDepth extends AccountOption { depth: number @@ -123,8 +120,9 @@ export default function InspectLedgersPage() { options={accountsWithDepth} groupBy={a => a.accountType} getOptionLabel={a => a.name} - value={selectedAccount ? accountsWithDepth.find(a => a.id === selectedAccount.id) ?? null : null} - onChange={(_, v) => setSelectedAccount(v)} + value={accountsWithDepth.find(a => a.id === selectedAccountId) ?? null} + onChange={(_, v) => { setSelectedAccountId(v?.id ?? null); setOwnOnly(false) }} + isOptionEqualToValue={(opt, val) => opt.id === val.id} sx={{ minWidth: 350, flex: 1 }} renderInput={params => } renderOption={(props, option) => ( @@ -154,31 +152,36 @@ export default function InspectLedgersPage() { - {!selectedAccount ? ( + {!selectedAccountId ? ( Select an account to inspect its ledger entries. - ) : loading ? ( - - ) : data && data.entries.length > 0 ? ( + ) : ( + <> + {isGroupAccount && ( + + setOwnOnly(e.target.checked)} />} + label={ + Show only this account's own transactions (exclude sub-accounts) + } + /> + + )} + + {loading ? ( + + ) : data && data.entries.length > 0 ? ( <> {data.entryCount} entries, {data.from} to {data.to} - {isGroupAccount && !ownOnly && ( + {data.isGroup && !ownOnly && ( ({data.groupAccountCount} accounts) )} - {isGroupAccount && ( - setOwnOnly(e.target.checked)} />} - label={ - Show only "{data.accountName}" own transactions (exclude sub-accounts) - } - /> - )} Amounts in {activeOrg.currencyCode || 'presentation currency'} @@ -320,6 +323,8 @@ export default function InspectLedgersPage() { No entries for this period. ) : null} + + )} ) }