From 7fb8d8951d1b105e190a95c701b5fdc595565893 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Sun, 1 Sep 2024 20:58:28 +0700 Subject: [PATCH] feat: save wallet to local storage --- app/page.tsx | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/app/page.tsx b/app/page.tsx index 80ec7ac..65ddea1 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -10,9 +10,25 @@ import { ScanQR } from "./components/ScanQR"; export default function Home() { const [wallet, setWallet] = React.useState(); + const [loadedSavedWallet, setLoadedSavedWallet] = React.useState(false); const [loading, setLoading] = React.useState(false); const [copied, setCopied] = React.useState<"nwcUrl" | "lightningAddress">(); const [balance, setBalance] = React.useState(0); + React.useEffect(() => { + const savedWalletJSON = window.localStorage.getItem("wallet"); + if (savedWalletJSON) { + const _wallet = JSON.parse(savedWalletJSON); + setWallet(_wallet); + setLoadedSavedWallet(true); + (async () => { + const client = new nwc.NWCClient({ + nostrWalletConnectUrl: _wallet.connectionSecret, + }); + const _balance = await client.getBalance(); + setBalance(_balance.balance); + })(); + } + }, []); async function onSubmit() { setLoading(true); try { @@ -25,6 +41,9 @@ export default function Home() { throw new Error(error); } setWallet(wallet); + if (wallet) { + window.localStorage.setItem("wallet", JSON.stringify(wallet)); + } } catch (error) { console.error(error); alert("Something went wrong: " + error); @@ -91,7 +110,35 @@ export default function Home() { )} {wallet && ( <> -

New Wallet Created!

+

+ {loadedSavedWallet ? ( + <> + Welcome back,{" "} + + {wallet.lightningAddress} + + + ) : ( + "New Wallet Created!" + )} +

+ {loadedSavedWallet && ( + + )}

{"Make sure to copy it and save it somewhere safe."}