/* eslint-disable max-lines */ import { LNWallet,Sats, WalletInvoice } from "@snort/wallet"; import classNames from "classnames"; import { useEffect, useState } from "react"; import { FormattedMessage, FormattedNumber, useIntl } from "react-intl"; import { useNavigate } from "react-router-dom"; import AsyncButton from "@/Components/Button/AsyncButton"; import { AsyncIcon } from "@/Components/Button/AsyncIcon"; import NoteTime from "@/Components/Event/Note/NoteTime"; import Icon from "@/Components/Icons/Icon"; import { useRates } from "@/Hooks/useRates"; import { unwrap } from "@/Utils"; import { useWallet, Wallets } from "@/Wallet"; 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 === undefined && ( )} {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() { if (!wallet) return; return ( <>
{walletBalance()}
), }} />
{wallet?.canCreateInvoice() && ( navigate("/wallet/receive")}> )} {wallet?.canPayInvoice() && ( navigate("/wallet/send")} className="primary"> )}
{walletHistory()} ); } return (
{walletList()} {error && {error}} {unlockWallet()} {walletInfo()}
); }