2025-02-18 02:03:20 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
2025-02-13 16:52:36 +00:00
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
|
|
|
|
import List, { ListItem } from 'soapbox/components/list.tsx';
|
|
|
|
import { Card, CardBody, CardHeader, CardTitle } from 'soapbox/components/ui/card.tsx';
|
|
|
|
import { Column } from 'soapbox/components/ui/column.tsx';
|
2025-02-20 23:55:01 +00:00
|
|
|
import Spinner from 'soapbox/components/ui/spinner.tsx';
|
|
|
|
import Stack from 'soapbox/components/ui/stack.tsx';
|
2025-03-15 18:12:31 +00:00
|
|
|
import { SelectDropdown } from 'soapbox/features/forms/index.tsx';
|
2025-03-18 14:19:30 +00:00
|
|
|
import Balance from 'soapbox/features/wallet/components/balance.tsx';
|
|
|
|
import CreateWallet from 'soapbox/features/wallet/components/create-wallet.tsx';
|
|
|
|
import Transactions from 'soapbox/features/wallet/components/transactions.tsx';
|
2025-03-15 18:12:31 +00:00
|
|
|
import { usePaymentMethod } from 'soapbox/features/zap/usePaymentMethod.ts';
|
2025-02-18 02:03:20 +00:00
|
|
|
import { useApi } from 'soapbox/hooks/useApi.ts';
|
2025-02-13 16:52:36 +00:00
|
|
|
import { useOwnAccount } from 'soapbox/hooks/useOwnAccount.ts';
|
2025-02-18 02:03:20 +00:00
|
|
|
import { WalletData, baseWalletSchema } from 'soapbox/schemas/wallet.ts';
|
|
|
|
import toast from 'soapbox/toast.tsx';
|
2025-02-13 16:52:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
2025-03-18 14:19:30 +00:00
|
|
|
payment: { id: 'wallet.payment', defaultMessage: 'Payment Method' },
|
|
|
|
relays: { id: 'wallet.relays', defaultMessage: 'Relays' },
|
|
|
|
transactions: { id: 'wallet.transactions', defaultMessage: 'Transactions' },
|
|
|
|
wallet: { id: 'wallet', defaultMessage: 'Wallet' },
|
|
|
|
management: { id: 'wallet.management', defaultMessage: 'Wallet Management' },
|
|
|
|
mints: { id: 'wallet.mints', defaultMessage: 'Mints' },
|
2025-02-13 16:52:36 +00:00
|
|
|
});
|
|
|
|
|
2025-03-15 18:12:31 +00:00
|
|
|
const paymentMethods = {
|
|
|
|
zap: 'zap',
|
|
|
|
cashu: 'cashu',
|
|
|
|
};
|
|
|
|
|
2025-02-18 02:03:20 +00:00
|
|
|
/** User Wallet page. */
|
2025-03-18 14:19:30 +00:00
|
|
|
const Wallet = () => {
|
2025-02-18 02:03:20 +00:00
|
|
|
const api = useApi();
|
2025-02-13 16:52:36 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
const { account } = useOwnAccount();
|
2025-02-18 02:03:20 +00:00
|
|
|
const [walletData, setWalletData] = useState<WalletData | undefined>(undefined);
|
2025-02-20 23:55:01 +00:00
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(true);
|
2025-03-15 18:12:31 +00:00
|
|
|
const { method, changeMethod } = usePaymentMethod();
|
2025-02-18 02:03:20 +00:00
|
|
|
|
|
|
|
const fetchWallet = async () => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await api.get('/api/v1/ditto/cashu/wallet');
|
|
|
|
const data: WalletData = await response.json();
|
|
|
|
if (data) {
|
|
|
|
const normalizedData = baseWalletSchema.parse(data);
|
|
|
|
setWalletData(normalizedData);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
toast.error('Wallet not found');
|
2025-02-20 23:55:01 +00:00
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
2025-02-18 02:03:20 +00:00
|
|
|
}
|
|
|
|
};
|
2025-02-13 16:52:36 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-02-18 02:03:20 +00:00
|
|
|
fetchWallet();
|
|
|
|
}, []);
|
2025-02-13 16:52:36 +00:00
|
|
|
|
|
|
|
if (!account) return null;
|
|
|
|
|
|
|
|
return (
|
2025-02-20 23:55:01 +00:00
|
|
|
<>
|
|
|
|
{isLoading ?
|
|
|
|
<Stack className='h-screen justify-center'>
|
|
|
|
<Spinner size={40} withText={false} />
|
|
|
|
</Stack>
|
|
|
|
:
|
|
|
|
(
|
2025-03-18 14:19:30 +00:00
|
|
|
<Column label={intl.formatMessage(messages.wallet)} transparent withHeader={false} slim>
|
2025-02-20 23:55:01 +00:00
|
|
|
<Card className='space-y-4'>
|
|
|
|
<CardHeader>
|
2025-03-18 14:19:30 +00:00
|
|
|
<CardTitle title={intl.formatMessage(messages.wallet)} />
|
2025-02-20 23:55:01 +00:00
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
{walletData ? (
|
|
|
|
<>
|
|
|
|
<CardBody>
|
2025-03-12 18:31:48 +00:00
|
|
|
<Balance />
|
2025-02-20 23:55:01 +00:00
|
|
|
</CardBody>
|
|
|
|
|
|
|
|
<CardHeader>
|
|
|
|
<CardTitle title={intl.formatMessage(messages.transactions)} />
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
<CardBody>
|
|
|
|
<Transactions />
|
|
|
|
</CardBody>
|
|
|
|
|
|
|
|
<CardHeader>
|
|
|
|
<CardTitle title={intl.formatMessage(messages.management)} />
|
|
|
|
</CardHeader>
|
|
|
|
|
|
|
|
<CardBody>
|
|
|
|
<List>
|
2025-03-19 00:41:57 +00:00
|
|
|
<ListItem label={intl.formatMessage(messages.mints)} to='/wallet-mints' />
|
|
|
|
<ListItem label={intl.formatMessage(messages.relays)} to='/wallet-relays' />
|
2025-03-15 18:12:31 +00:00
|
|
|
<ListItem label={intl.formatMessage(messages.payment)} >
|
|
|
|
<SelectDropdown
|
|
|
|
className='max-w-[200px]'
|
|
|
|
items={paymentMethods}
|
|
|
|
defaultValue={method}
|
|
|
|
onChange={(event: React.ChangeEvent<HTMLSelectElement>) => {
|
|
|
|
changeMethod((event.target.value as 'cashu' | 'zap'));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</ListItem>
|
2025-02-20 23:55:01 +00:00
|
|
|
</List>
|
|
|
|
</CardBody>
|
|
|
|
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
:
|
|
|
|
<>
|
|
|
|
<CardBody>
|
|
|
|
<CreateWallet setWalletData={setWalletData} />
|
|
|
|
</CardBody>
|
|
|
|
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
</Card>
|
|
|
|
</Column>
|
2025-02-13 16:52:36 +00:00
|
|
|
)
|
2025-02-20 23:55:01 +00:00
|
|
|
}
|
|
|
|
</>
|
2025-02-13 16:52:36 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2025-03-18 14:19:30 +00:00
|
|
|
export default Wallet;
|