44 lines
2.2 KiB
JavaScript
44 lines
2.2 KiB
JavaScript
// Generated, do not edit. sha256:9910b171f8780710e55bbf6e2270b3758af0e766b12a3590b21420b1bc45cfe8
|
|
import { listAssets } from '../data/assets.js';
|
|
import { baseCurrency } from '../app-settings.js';
|
|
import { currentRate } from '../price/providers.js';
|
|
import { fmtMoney } from '../ui/format.js';
|
|
import { esc } from '../ui/escape.js';
|
|
import { t } from '../i18n/index.js';
|
|
|
|
export function secondCurrencyOf(instance, fallback = null) {
|
|
const p = instance && instance.params;
|
|
const v = p && Object.prototype.hasOwnProperty.call(p, 'second') ? p.second : fallback;
|
|
return typeof v === 'string' && v !== '' && v !== baseCurrency() ? v : null;
|
|
}
|
|
|
|
export async function secondCurrencyChoices() {
|
|
const base = baseCurrency();
|
|
const fiat = (await listAssets()).filter(a => a.kind === 'fiat' && a.symbol !== base);
|
|
const rates = await Promise.all(fiat.map(a => currentRate(a.symbol).catch(() => null)));
|
|
return fiat.filter((_, i) => rates[i] != null).map(a => a.symbol).sort();
|
|
}
|
|
|
|
export async function secondAmount(baseAmount, code, fmtOpts = {}) {
|
|
if (!code || baseAmount == null || !Number.isFinite(baseAmount)) return '';
|
|
const rate = await currentRate(code).catch(() => null);
|
|
return rate ? fmtMoney(baseAmount / rate, code, fmtOpts) : '';
|
|
}
|
|
|
|
export async function renderSecondCurrencySetting(host, instance, commit, fallback = null) {
|
|
const current = secondCurrencyOf(instance, fallback);
|
|
const choices = await secondCurrencyChoices();
|
|
if (current && !choices.includes(current)) choices.unshift(current);
|
|
const opties = [`<option value=""${current ? '' : ' selected'}>${esc(t('dashboard.secondNone'))}</option>`]
|
|
.concat(choices.map(c => `<option value="${esc(c)}"${c === current ? ' selected' : ''}>${esc(c)}</option>`)).join('');
|
|
const block = document.createElement('label');
|
|
block.className = 'dash-set-row';
|
|
block.innerHTML = `<span class="t-label t-label--ter">${esc(t('dashboard.secondCurrency'))}</span>
|
|
<select class="input dash-set-select" data-second>${opties}</select>`;
|
|
host.appendChild(block);
|
|
block.querySelector('select').addEventListener('change', e => {
|
|
instance.params = { ...(instance.params || {}), second: e.target.value || null };
|
|
commit();
|
|
});
|
|
}
|