okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-027-gh-app: GitHub app and Terminal items for 0.2.73cabb9e3f

main
1.9 KiB29 linesraw
1import { useEffect, useMemo, useState } from 'react';
2import { currencyChoices, currencyLabel } from '../lib/currency';
3import { tr } from '../lib/i18n';
4import { useMoney } from '../lib/money';
5import { setElectricity, useStore } from '../lib/store';
6import { Btn, Select } from './ui';
7
8/**
9 * The price of one kWh for the Monitor's cost line (#3628). The person types only the number: the currency is the one of the computer's
10 * region, shown beside it. "Change currency" is a way to pick another one, never a step that has to be taken.
11 */
12export function ElectricityPrice() {
13    const price = useStore(s => s.electricityPrice), override = useStore(s => s.electricityCurrency);
14    const money = useMoney();
15    const [text, setText] = useState(price > 0 ? String(price) : ''), [picking, setPicking] = useState(false);
16    useEffect(() => { setText(price > 0 ? String(price) : ''); }, [price]);
17    const commit = () => setElectricity(Number(text.replace(',', '.')), override);
18    const choices = useMemo(() => currencyChoices(money.locale), [money.locale]);
19    return <span className="stack">
20        <span className="row">
21            <input className="st-input" inputMode="decimal" size={7} aria-label={tr("Price per kWh")} placeholder="0.30" value={text} onChange={e => setText(e.target.value)} onBlur={commit} onKeyDown={e => { if (e.key === 'Enter') commit(); }}/>
22            <span className="small muted">{money.currency ? tr("per kWh, in {0}", [currencyLabel(money.currency, money.locale)]) : tr("per kWh")}</span>
23            <Btn size="sm" onClick={() => setPicking(v => !v)}>{tr("Change currency")}</Btn>
24        </span>
25        {picking ? <Select ariaLabel={tr("Currency")} translateLabels={false} value={override}
26            options={[{ value: '', label: tr("Automatic ({0})", [money.detected ?? '—']) }, ...choices]}
27            onChange={value => { setElectricity(price, String(value)); setPicking(false); }}/> : null}
28    </span>;
29}