import "./WalletPage.css"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { FormattedMessage, FormattedNumber, useIntl } from "react-intl"; import NoteTime from "@/Element/Event/NoteTime"; import { WalletInvoice, Sats, useWallet, LNWallet, Wallets } from "@/Wallet"; import AsyncButton from "@/Element/Button/AsyncButton"; import { unwrap } from "@/SnortUtils"; import Icon from "@/Icons/Icon"; import { useRates } from "@/Hooks/useRates"; import { AsyncIcon } from "@/Element/Button/AsyncIcon"; import classNames from "classnames"; export default function WalletPage(props: { showHistory: boolean }) { const navigate = useNavigate(); const { formatMessage } = useIntl(); const [balance, setBalance] = useState(); const [history, setHistory] = useState(); const [walletPassword, setWalletPassword] = useState(); const [error, setError] = useState(); const walletState = useWallet(); const wallet = walletState.wallet; const rates = useRates("BTCUSD"); async function loadWallet(wallet: LNWallet) { try { setError(undefined); setBalance(0); setHistory(undefined); if (wallet.canGetBalance()) { const b = await wallet.getBalance(); setBalance(b as Sats); } if (wallet.canGetInvoices() && (props.showHistory ?? true)) { const h = await wallet.getInvoices(); setHistory((h as WalletInvoice[]).sort((a, b) => b.timestamp - a.timestamp)); } } catch (e) { if (e instanceof Error) { setError((e as Error).message); } else { setError(formatMessage({ defaultMessage: "Unknown error", id: "qDwvZ4" })); } } } useEffect(() => { if (wallet && wallet.isReady()) { loadWallet(wallet).catch(console.warn); } }, [wallet]); async function loginWallet(pw: string) { if (wallet) { await wallet.login(pw); await loadWallet(wallet); setWalletPassword(undefined); } } function unlockWallet() { if (!wallet || wallet.isReady()) return null; return ( <>

setWalletPassword(e.target.value)} />
loginWallet(unwrap(walletPassword))} disabled={(walletPassword?.length ?? 0) < 8}>
); } function walletList() { if (walletState.configs.length === 0) { return ( ); } return (

); } function walletHistory() { if (!wallet?.canGetInvoices() || !(props.showHistory ?? true)) return; return (

{history?.map(a => { const dirClassname = { "text-[--success]": a.direction === "in", "text-[--error]": a.direction === "out", }; return (
{a.memo?.length === 0 ? CONFIG.appNameCapitalized : a.memo}
, }} />
), }} />
); })}
); } function walletBalance() { if (!wallet?.canGetBalance()) return; return (
{c}, small: c => {c}, amount: , }} />
); } function walletInfo() { return ( <>
{walletBalance()}
), }} />
{walletHistory()} ); } return (
{walletList()} {error && {error}} {unlockWallet()} {walletInfo()}
); }