add wallet create button

This commit is contained in:
hzrd149 2025-03-13 16:09:09 +00:00
parent 5982ce2a4e
commit 742c97c837

View File

@ -1,9 +1,11 @@
import { Tab, TabList, TabPanel, TabPanels, Tabs } from "@chakra-ui/react";
import { useState } from "react";
import { Button, Tab, TabList, TabPanel, TabPanels, Tabs, useToast } from "@chakra-ui/react";
import { kinds } from "nostr-tools";
import { WalletBalanceQuery } from "applesauce-wallet/queries";
import { CreateWallet } from "applesauce-wallet/actions";
import { WALLET_HISTORY_KIND, WALLET_TOKEN_KIND } from "applesauce-wallet/helpers";
import { useActiveAccount, useStoreQuery } from "applesauce-react/hooks";
import { useActionHub, useActiveAccount, useStoreQuery } from "applesauce-react/hooks";
import SimpleView from "../../components/layout/presets/simple-view";
import useTimelineLoader from "../../hooks/use-timeline-loader";
import useUserMailboxes from "../../hooks/use-user-mailboxes";
@ -18,8 +20,10 @@ import useUserWallet from "../../hooks/use-user-wallet";
import WalletUnlockButton from "./components/wallet-unlock-button";
export default function WalletHomeView() {
const toast = useToast();
const account = useActiveAccount()!;
const wallet = useUserWallet(account.pubkey);
const actions = useActionHub();
const mailboxes = useUserMailboxes(account.pubkey);
const readRelays = useReadRelays(mailboxes?.outboxes);
@ -32,6 +36,18 @@ export default function WalletHomeView() {
]);
const balance = useStoreQuery(WalletBalanceQuery, [account.pubkey]);
const [creating, setCreating] = useState(false);
const create = async () => {
try {
setCreating(true);
await actions.run(CreateWallet, []);
toast({ status: "success", description: "Created new wallet" });
} catch (error) {
if (error instanceof Error) toast({ status: "error", description: error.message });
}
setCreating(false);
};
const callback = useTimelineCurserIntersectionCallback(loader);
return (
@ -40,27 +56,35 @@ export default function WalletHomeView() {
title="Wallet"
actions={wallet?.locked && <WalletUnlockButton colorScheme="primary" ms="auto" size="sm" />}
>
<WalletBalanceCard pubkey={account.pubkey} w="full" maxW="2xl" mx="auto" />
{wallet ? (
<WalletBalanceCard pubkey={account.pubkey} w="full" maxW="2xl" mx="auto" />
) : (
<Button onClick={create} mx="auto" w="lg" size="lg" colorScheme="primary" my="10" isLoading={creating}>
Create Wallet
</Button>
)}
{wallet?.locked && <WalletUnlockButton colorScheme="primary" mx="auto" size="lg" w="sm" />}
<Tabs isFitted maxW="2xl" mx="auto" w="full" isLazy>
<TabList mb="1em">
<Tab>History ({events.filter((e) => e.kind === WALLET_HISTORY_KIND).length})</Tab>
<Tab>Tokens ({events.filter((e) => e.kind === WALLET_TOKEN_KIND).length})</Tab>
<Tab>Mints ({balance ? Object.keys(balance).length : 0})</Tab>
</TabList>
<TabPanels>
<TabPanel p="0">
<WalletHistoryTab />
</TabPanel>
<TabPanel p="0">
<WalletTokensTab />
</TabPanel>
<TabPanel p="0">
<WalletMintsTab />
</TabPanel>
</TabPanels>
</Tabs>
{wallet && (
<Tabs isFitted maxW="2xl" mx="auto" w="full" isLazy>
<TabList mb="1em">
<Tab>History ({events.filter((e) => e.kind === WALLET_HISTORY_KIND).length})</Tab>
<Tab>Tokens ({events.filter((e) => e.kind === WALLET_TOKEN_KIND).length})</Tab>
<Tab>Mints ({balance ? Object.keys(balance).length : 0})</Tab>
</TabList>
<TabPanels>
<TabPanel p="0">
<WalletHistoryTab />
</TabPanel>
<TabPanel p="0">
<WalletTokensTab />
</TabPanel>
<TabPanel p="0">
<WalletMintsTab />
</TabPanel>
</TabPanels>
</Tabs>
)}
</SimpleView>
</IntersectionObserverProvider>
);