snort/packages/app/src/Pages/settings/wallet/Alby.tsx

58 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-04-11 12:26:50 +00:00
import { AlbyWallet, WalletKind } from "@snort/wallet";
2024-01-03 16:46:18 +00:00
import { useEffect, useState } from "react";
2024-01-04 12:05:13 +00:00
import { useLocation, useNavigate } from "react-router-dom";
import { v4 as uuid } from "uuid";
2024-01-03 16:46:18 +00:00
2024-01-04 17:01:18 +00:00
import PageSpinner from "@/Components/PageSpinner";
import { getAlbyOAuth } from "@/Pages/settings/wallet/utils";
2024-04-11 12:26:50 +00:00
import { WalletConfig, Wallets } from "@/Wallet";
2024-01-04 17:01:18 +00:00
2024-01-03 16:46:18 +00:00
export default function AlbyOAuth() {
2024-01-04 12:05:13 +00:00
const navigate = useNavigate();
2024-01-03 16:49:44 +00:00
const location = useLocation();
const alby = getAlbyOAuth();
const [error, setError] = useState("");
2024-01-03 16:46:18 +00:00
2024-01-03 16:49:44 +00:00
async function setupWallet(token: string) {
2024-01-04 12:05:13 +00:00
try {
const auth = await alby.getToken(token);
2024-04-11 12:26:50 +00:00
const connection = new AlbyWallet(auth);
2024-01-04 12:05:13 +00:00
const info = await connection.getInfo();
const newWallet = {
id: uuid(),
kind: WalletKind.Alby,
active: true,
info,
data: JSON.stringify(auth),
} as WalletConfig;
Wallets.add(newWallet);
navigate("/settings/wallet");
} catch (e) {
setError((e as Error).message);
}
2024-01-03 16:49:44 +00:00
}
2024-01-03 16:46:18 +00:00
2024-01-03 16:49:44 +00:00
useEffect(() => {
if (location.search) {
const params = new URLSearchParams(location.search);
const token = params.get("code");
if (token) {
setupWallet(token).catch(e => {
setError((e as Error).message);
});
}
}
}, [location]);
2024-01-03 16:46:18 +00:00
2024-01-03 16:49:44 +00:00
if (!location.search) return;
return (
<>
<h1>Alby Wallet Connection</h1>
{!error && <PageSpinner />}
{error && <b className="warning">{error}</b>}
2024-01-03 16:46:18 +00:00
</>
2024-01-03 16:49:44 +00:00
);
2024-01-03 16:46:18 +00:00
}