feat: show usd price

This commit is contained in:
Kieran 2024-12-18 09:47:35 +00:00
parent 3c4fe85694
commit 3e25743cdc
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
2 changed files with 34 additions and 12 deletions

View File

@ -3,6 +3,15 @@ export const DefaultLocale = "en-US";
export const getLocale = () => {
return (navigator.languages && navigator.languages[0]) ?? navigator.language ?? DefaultLocale;
};
export const getCurrency = () => {
const locale = navigator.language || navigator.languages[0];
const formatter = new Intl.NumberFormat(locale, {
style: "currency",
currency: "USD",
currencyDisplay: "code",
});
return formatter.formatToParts(1.2345).find(a => a.type === "currency")?.value ?? "USD";
};
export const AllLanguageCodes = [
"en",
"ja",

View File

@ -4,27 +4,23 @@ import { useNavigate } from "react-router-dom";
import Icon from "@/Components/Icons/Icon";
import { useRates } from "@/Hooks/useRates";
import { Sats, useWallet } from "@/Wallet";
import { useWallet } from "@/Wallet";
import { getCurrency, getLocale } from "@/Components/IntlProvider/IntlProviderUtils";
export const WalletBalance = () => {
const [balance, setBalance] = useState<Sats | null>(null);
const [balance, setBalance] = useState<number>();
const wallet = useWallet();
const rates = useRates("BTCUSD");
const localCurrency = getCurrency();
const rates = useRates(`BTC${localCurrency}`);
const navigate = useNavigate();
useEffect(() => {
setBalance(null);
setBalance(undefined);
if (wallet.wallet && wallet.wallet.canGetBalance()) {
wallet.wallet.getBalance().then(setBalance);
}
}, [wallet]);
const msgValues = useMemo(() => {
return {
amount: <FormattedNumber style="currency" currency="USD" value={(rates?.ask ?? 0) * (balance ?? 0) * 1e-8} />,
};
}, [balance, rates]);
return (
<div className="w-full flex flex-col max-xl:hidden pl-3 py-2 cursor-pointer" onClick={() => navigate("/wallet")}>
<div className="grow flex items-center justify-between">
@ -34,8 +30,25 @@ export const WalletBalance = () => {
</div>
<Icon name="dots" className="text-secondary" />
</div>
<div className="text-secondary text-sm">
<FormattedMessage defaultMessage="~{amount}" values={msgValues} />
<div className="text-secondary text-xs flex justify-between items-center">
<div>
~
<FormattedNumber
style="currency"
currency={localCurrency}
value={(rates?.ask ?? 0) * (balance ?? 0) * 1e-8}
currencyDisplay="narrowSymbol"
/>
</div>
<div>
<FormattedNumber
style="currency"
currency={localCurrency}
value={rates?.ask ?? 0}
maximumFractionDigits={0}
currencyDisplay="narrowSymbol"
/>
</div>
</div>
</div>
);