import { useEffect, useMemo, useState } from 'react'; import { currencyChoices, currencyLabel } from '../lib/currency'; import { tr } from '../lib/i18n'; import { useMoney } from '../lib/money'; import { setElectricity, useStore } from '../lib/store'; import { Btn, Select } from './ui'; /** * 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 * region, shown beside it. "Change currency" is a way to pick another one, never a step that has to be taken. */ export function ElectricityPrice() { const price = useStore(s => s.electricityPrice), override = useStore(s => s.electricityCurrency); const money = useMoney(); const [text, setText] = useState(price > 0 ? String(price) : ''), [picking, setPicking] = useState(false); useEffect(() => { setText(price > 0 ? String(price) : ''); }, [price]); const commit = () => setElectricity(Number(text.replace(',', '.')), override); const choices = useMemo(() => currencyChoices(money.locale), [money.locale]); return setText(e.target.value)} onBlur={commit} onKeyDown={e => { if (e.key === 'Enter') commit(); }}/> {money.currency ? tr("per kWh, in {0}", [currencyLabel(money.currency, money.locale)]) : tr("per kWh")} setPicking(v => !v)}>{tr("Change currency")} {picking ?