// Generated, do not edit. sha256:dbfabff3706a8f2f1755ee61d42f71fcabb5b42dd0ab60143a9e2b568cf13043 import { listAssets, listBookableAssets } 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, flatLine } from '../wealth/return-view.js'; import { lineForData, drawLine } from '../wealth/return-dialog.js'; import { drawReturnChart } from '../wealth/return-chart.js'; import { localDateNl } from '../../shared/time.js'; const localTodayIso = () => new Date().toISOString().slice(0, 10); 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 empty = !reeks.dagen?.length; const draw = async () => { const g = ++gen; const btn = host.querySelector('.range-cycle-btn'); if (btn) btn.textContent = rangeLabel(range); if (empty) { const flat = flatLine(RANGES[range] || RANGES.month, localTodayIso()); amountEl.innerHTML = `${esc(fmtBaseSigned(0, colouredSigns()))}`; pctEl.innerHTML = `${esc(fmtPct(0, { signs: colouredSigns() }))}`; chart?.destroy(); const canvas = host.querySelector('[data-return-chart]'); const c = canvas ? await drawReturnChart(canvas, flat.points, flat.labels.map(d => localDateNl(`${d}T00:00:00Z`))).catch(() => null) : null; if (g !== gen || !host.isConnected) { c?.destroy(); return; } chart = c; return; } const venster = periods.find(p => p.key === PERIOD_OF[range])?.venster || null; const line = lineForData({ reeks, assets: held, data, venster }); const tone = line.amount > 0.005 ? ' t-pos' : line.amount < -0.005 ? ' t-neg' : ''; amountEl.innerHTML = line.amount == null ? '-' : `${esc(fmtBaseSigned(line.amount, colouredSigns()))}`; 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 = `