// 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 = `
${titleHtml(data, assets)}
${CHEVRON_RIGHT}
…
${rangeCycleButton(range)}
`; 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 = `
${esc(t('wealth.noChart'))}
`; return; } if (!canvas) { plot.innerHTML = ''; canvas = plot.firstElementChild; } const c = await drawLine(canvas, reeks, line.points).catch(() => null); if (g !== gen || !host.isConnected) { c?.destroy(); return; } chart = c; }; await draw(); wireRangeCycle(host.querySelector('[data-return-range]'), () => range, next => { range = next; setPref(PREF_SCOPE, key, range).catch(() => { }); draw(); }, RANGES); } catch (err) { console.error('[return-widget]', err); amountEl.textContent = '⚠ ' + err.message; } } export async function renderReturnSettings(host, instance, commit) { const assets = (await listAssets()).sort(byKindAndLabel); const current = dataOf(instance); const opt = (value, label) => ``; const kinds = ENUMS.ASSET_KIND.filter(k => assets.some(a => a.kind === k)); const row = document.createElement('div'); row.className = 'dash-set-row'; row.innerHTML = `${esc(t('wealth.returnData'))} `; host.appendChild(row); row.querySelector('[data-return-setting]').addEventListener('change', e => { instance.params = { ...(instance.params || {}), data: e.target.value }; commit(); }); }