import "./WalletPage.css"; import { useEffect, useState } from "react"; import { RouteObject, useNavigate } from "react-router-dom"; import { FormattedMessage, FormattedNumber, useIntl } from "react-intl"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faCheck, faClock, faXmark } from "@fortawesome/free-solid-svg-icons"; import NoteTime from "Element/NoteTime"; import { WalletInvoice, Sats, WalletInfo, WalletInvoiceState, useWallet, LNWallet, Wallets, WalletKind } from "Wallet"; import AsyncButton from "Element/AsyncButton"; import { unwrap } from "Util"; import { WebLNWallet } from "Wallet/WebLN"; export const WalletRoutes: RouteObject[] = [ { path: "/wallet", element: , }, ]; export default function WalletPage() { const navigate = useNavigate(); const { formatMessage } = useIntl(); const [info, setInfo] = useState(); const [balance, setBalance] = useState(); const [history, setHistory] = useState(); const [walletPassword, setWalletPassword] = useState(); const [error, setError] = useState(); const walletState = useWallet(); const wallet = walletState.wallet; async function loadWallet(wallet: LNWallet) { try { const i = await wallet.getInfo(); setInfo(i); const b = await wallet.getBalance(); setBalance(b as Sats); 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" })); } } } useEffect(() => { if (wallet) { if (wallet.isReady()) { loadWallet(wallet).catch(console.warn); } else if (walletState.config?.kind !== WalletKind.LNC) { wallet .login() .then(async () => await loadWallet(wallet)) .catch(console.warn); } } }, [wallet]); function stateIcon(s: WalletInvoiceState) { switch (s) { case WalletInvoiceState.Pending: return ; case WalletInvoiceState.Paid: return ; case WalletInvoiceState.Expired: return ; } } 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 instanceof WebLNWallet) return null; return ( <>

{history?.map(a => (
{(a.memo ?? "").length === 0 ? <>  : a.memo}
{ switch (a.state) { case WalletInvoiceState.Paid: return "success"; case WalletInvoiceState.Expired: return "expired"; case WalletInvoiceState.Failed: return "failed"; default: return "pending"; } })()}`}> {stateIcon(a.state)} , }} />
))} ); } function walletBalance() { if (wallet instanceof WebLNWallet) return null; return ( , }} /> ); } function walletInfo() { if (!wallet?.isReady()) return null; return ( <>

{info?.alias}

{walletBalance()}
{/*
*/} {walletHistory()} ); } return (
{error && {error}} {walletList()} {unlockWallet()} {walletInfo()}
); }