Open-source personal finance manager. Self-hosted, privacy-first.
fix(assets): show portfolio share for holdings in the primary currency (#994)
Tássio Noronha74d4fe41a22a
diff --git a/frontend/src/lib/asset-portfolio-share.test.ts b/frontend/src/lib/asset-portfolio-share.test.tsnew file mode 100644index 00000000..2eb1676d--- /dev/null+++ b/frontend/src/lib/asset-portfolio-share.test.ts@@ -0,0 +1,32 @@+import { describe, expect, it } from 'vitest'++import {+ getAssetValuePrimary,+ getPortfolioShare,+ getPortfolioTotalPrimary,+} from '@/lib/asset-portfolio-share'++describe('asset portfolio share', () => {+ const inPrimary = { current_value: 600, current_value_primary: null }+ const foreign = { current_value: 100, current_value_primary: 400 }+ const unvalued = { current_value: null, current_value_primary: null }++ it('falls back to current_value for assets in the primary currency', () => {+ expect(getAssetValuePrimary(inPrimary)).toBe(600)+ expect(getAssetValuePrimary(foreign)).toBe(400)+ expect(getAssetValuePrimary(unvalued)).toBeNull()+ })++ it('computes shares on the same basis as the total so they sum to 100', () => {+ const assets = [inPrimary, foreign, unvalued]+ const total = getPortfolioTotalPrimary(assets)+ expect(total).toBe(1000)+ expect(getPortfolioShare(inPrimary, total)).toBe(60)+ expect(getPortfolioShare(foreign, total)).toBe(40)+ expect(getPortfolioShare(unvalued, total)).toBeNull()+ })++ it('returns null when the portfolio total is zero', () => {+ expect(getPortfolioShare(inPrimary, 0)).toBeNull()+ })+})diff --git a/frontend/src/lib/asset-portfolio-share.ts b/frontend/src/lib/asset-portfolio-share.tsnew file mode 100644index 00000000..9ad583bf--- /dev/null+++ b/frontend/src/lib/asset-portfolio-share.ts@@ -0,0 +1,21 @@+import type { Asset } from '@/types'++type ValuedAsset = Pick<Asset, 'current_value' | 'current_value_primary'>++// The API only fills current_value_primary when the asset is held in a+// currency other than the user's primary one, so fall back to current_value+// for assets already in the primary currency.+export function getAssetValuePrimary(asset: ValuedAsset): number | null {+ const value = asset.current_value_primary ?? asset.current_value+ return value == null ? null : Number(value)+}++export function getPortfolioTotalPrimary(assets: ValuedAsset[]): number {+ return assets.reduce((acc, a) => acc + (getAssetValuePrimary(a) ?? 0), 0)+}++export function getPortfolioShare(asset: ValuedAsset, totalPrimary: number): number | null {+ const value = getAssetValuePrimary(asset)+ if (value == null || !(totalPrimary > 0)) return null+ return (value / totalPrimary) * 100+}diff --git a/frontend/src/pages/assets.tsx b/frontend/src/pages/assets.tsxindex 54afddba..87f5131e 100644--- a/frontend/src/pages/assets.tsx+++ b/frontend/src/pages/assets.tsx@@ -58,6 +58,7 @@ import { useAuth } from '@/contexts/auth-context'import { useWorkspace } from '@/contexts/workspace-context'import { useCollectionFilter } from '@/contexts/collection-filter-context'import { getAssetProfit } from '@/lib/asset-profit'+import { getPortfolioShare, getPortfolioTotalPrimary } from '@/lib/asset-portfolio-share'import { formatCurrency } from '@/lib/format'// Renders a logo image when one is available, falling back to the asset's@@ -305,12 +306,6 @@ export default function AssetsPage() {(acc: number, a: { current_value?: number | null }) => acc + Number(a.current_value || 0),0,)- // Portfolio total in the user's primary currency — denominator for the- // "% da carteira" column in the holdings table.- const portfolioTotalPrimary = (assetsList ?? []).reduce(- (acc, a) => acc + Number(a.current_value_primary ?? a.current_value ?? 0),- 0,- )const byType: Record<string, number> = {}for (const a of (assetsList ?? []) as Array<{ type?: string; current_value?: number | null }>) {if (!a.type) continue@@ -497,6 +492,9 @@ export default function AssetsPage() {const activeAssets = useMemo(() => assetsList?.filter(a => !a.sell_date && !a.is_archived) ?? [], [assetsList])const soldAssets = assetsList?.filter(a => a.sell_date) ?? []+ // Denominator for the "% of portfolio" column: current holdings only, in the+ // user's primary currency, so the active rows add up to 100%.+ const portfolioTotalPrimary = getPortfolioTotalPrimary(activeAssets)// Debounced ticker search. Runs only when the market-price method is// selected and the query is non-trivial — keeps the autocomplete snappy@@ -717,10 +715,7 @@ export default function AssetsPage() {const isProviderOwned = isSynced && !isMarketPricedconst hasCost = asset.average_price != null && asset.total_invested != nullconst profit = getAssetProfit(asset)- const pctOfPortfolio =- portfolioTotalPrimary > 0 && asset.current_value_primary != null- ? (asset.current_value_primary / portfolioTotalPrimary) * 100- : null+ const pctOfPortfolio = asset.sell_date ? null : getPortfolioShare(asset, portfolioTotalPrimary)const needsBuys = isMarketPriced && !hasCost && !asset.sell_datereturn (