// Generated, do not edit. sha256:ae0f941ec7a830f9f94ae85e37de1157b6e15f16cc2e9dc3f0c5c7243d3eaafd import { listAssets } from '../data/assets.js'; import { listAccounts } from '../data/accounts.js'; import { getPref, setPref } from '../data/preferences.js'; import { t } from '../i18n/index.js'; import { esc } from '../ui/escape.js'; import { fmtBaseSigned, fmtPct, pctTone, colouredSigns } from '../ui/format.js'; import { assetLabel, assetLabelHtml, byKindAndLabel } from '../ui/asset-label.js'; import { CHEVRON_RIGHT } from '../ui/icons.js'; import { rangeLabel, rangeCycleButton, wireRangeCycle } from '../ui/range-cycle.js'; import { isOwnWealth } from '../../shared/wealth-scope.js'; import { ENUMS, TARGETS, targetHash } from '../../shared/config.js'; import { dagreeksVanNu } from '../wealth/series.js'; import { periodChoices } from '../wealth/return-view.js'; import { lineForData, drawLine } from '../wealth/return-dialog.js'; const PREF_SCOPE = 'widget.return'; const RANGES = Object.freeze({ day: 1, week: 7, month: 30, year: 365, all: 0 }); const PERIOD_OF = Object.freeze({ day: 'd1', week: 'd7', month: 'd30', year: 'd365', all: 'start' }); const DEFAULT_RANGE = 'month'; const dataOf = instance => (typeof instance?.params?.data === 'string' ? instance.params.data : 'total'); function titleHtml(data, assets) { const base = esc(t('dashboard.widget.return')); if (data.startsWith('asset:')) { const a = assets.find(x => x.id === Number(data.slice(6))); return a ? `${base}: ${assetLabelHtml(a)}` : base; } if (data.startsWith('kind:')) return `${base}: ${esc(t('enums.asset_kind.' + data.slice(5)))}`; return base; } export async function renderReturnWidget(host, instance) { const data = dataOf(instance); const key = instance?.instId || 'return'; const [accounts, assets, stored] = await Promise.all([ listAccounts(), listAssets(), getPref(PREF_SCOPE, key, DEFAULT_RANGE).catch(() => DEFAULT_RANGE), ]); let range = RANGES[stored] != null ? stored : DEFAULT_RANGE; host.innerHTML = `
`; const amountEl = host.querySelector('[data-return-amount]'); const pctEl = host.querySelector('[data-return-pct]'); let chart = null; let gen = 0; try { const excludedAccounts = new Set(accounts.filter(ac => !isOwnWealth(ac)).map(ac => ac.id)); const reeks = await dagreeksVanNu({ excludedAccounts }); if (!host.isConnected) return; const held = assets.filter(a => (reeks.perAsset?.get(a.id) || []).some(v => v > 0)); const periods = periodChoices(reeks.dagen?.length || 0, null); const draw = async () => { const g = ++gen; const btn = host.querySelector('.range-cycle-btn'); if (btn) btn.textContent = rangeLabel(range); const venster = periods.find(p => p.key === PERIOD_OF[range])?.venster || null; const line = lineForData({ reeks, assets: held, data, venster }); amountEl.innerHTML = line.amount == null ? '-' : `${esc(fmtBaseSigned(line.amount, 'both'))}`; pctEl.innerHTML = line.pct == null ? '' : `${esc(fmtPct(line.pct, { fine: range === 'day', signs: colouredSigns() }))}`; chart?.destroy(); chart = null; let canvas = host.querySelector('[data-return-chart]'); const plot = host.querySelector('.return-plot'); if (line.points.length < 2) { plot.innerHTML = `