import { useRouter } from 'next/router' import { Form, Input, SubmitButton } from '../components/form' import Link from 'next/link' import Button from 'react-bootstrap/Button' import { gql, useMutation, useQuery } from '@apollo/client' import Qr, { QrSkeleton } from '../components/qr' import LayoutCenter from '../components/layout-center' import InputGroup from 'react-bootstrap/InputGroup' import { WithdrawlSkeleton } from './withdrawals/[id]' import { useMe } from '../components/me' import { useEffect, useState } from 'react' import { requestProvider } from 'webln' import { Alert } from 'react-bootstrap' import { CREATE_WITHDRAWL, SEND_TO_LNADDR } from '../fragments/wallet' import { getGetServerSideProps } from '../api/ssrApollo' import { amountSchema, lnAddrSchema, withdrawlSchema } from '../lib/validate' export const getServerSideProps = getGetServerSideProps() export default function Wallet () { return ( ) } function YouHaveSats () { const me = useMe() return (

you have {me && me.sats} sats

) } function WalletHistory () { return (
wallet history
) } export function WalletForm () { const router = useRouter() if (!router.query.type) { return (
or
) } if (router.query.type === 'fund') { return } else if (router.query.type === 'withdraw') { return } else if (router.query.type === 'lnurl-withdraw') { return } else { return } } export function FundForm () { const me = useMe() const [showAlert, setShowAlert] = useState(true) const router = useRouter() const [createInvoice, { called, error }] = useMutation(gql` mutation createInvoice($amount: Int!) { createInvoice(amount: $amount) { id } }`) useEffect(() => { setShowAlert(!localStorage.getItem('hideLnAddrAlert')) }, []) if (called && !error) { return } return ( <> {me && showAlert && { localStorage.setItem('hideLnAddrAlert', 'yep') setShowAlert(false) }} > You can also fund your account via lightning address with {`${me.name}@stacker.news`} }
{ const { data } = await createInvoice({ variables: { amount: Number(amount) } }) router.push(`/invoices/${data.createInvoice.id}`) }} > sats} /> generate invoice
) } const MAX_FEE_DEFAULT = 10 export function WithdrawlForm () { const router = useRouter() const me = useMe() const [createWithdrawl, { called, error }] = useMutation(CREATE_WITHDRAWL) useEffect(() => { async function effect () { try { const provider = await requestProvider() const { paymentRequest: invoice } = await provider.makeInvoice({ defaultMemo: `Withdrawal for @${me.name} on SN`, maximumAmount: Math.max(me.sats - MAX_FEE_DEFAULT, 0) }) const { data } = await createWithdrawl({ variables: { invoice, maxFee: MAX_FEE_DEFAULT } }) router.push(`/withdrawals/${data.createWithdrawl.id}`) } catch (e) { console.log(e.message) } } effect() }, []) if (called && !error) { return } return ( <>
{ const { data } = await createWithdrawl({ variables: { invoice, maxFee: Number(maxFee) } }) router.push(`/withdrawals/${data.createWithdrawl.id}`) }} > sats} /> withdraw
or via ) } function LnQRWith ({ k1, encodedUrl }) { const router = useRouter() const query = gql` { lnWith(k1: "${k1}") { withdrawalId k1 } }` const { data } = useQuery(query, { pollInterval: 1000, fetchPolicy: 'cache-first' }) if (data?.lnWith?.withdrawalId) { router.push(`/withdrawals/${data.lnWith.withdrawalId}`) } return } export function LnWithdrawal () { // query for challenge const [createWith, { data, error }] = useMutation(gql` mutation createWith { createWith { k1 encodedUrl } }`) useEffect(() => { createWith() }, []) if (error) return
error
if (!data) { return } return } export function LnAddrWithdrawal () { const router = useRouter() const [sendToLnAddr, { called, error }] = useMutation(SEND_TO_LNADDR) if (called && !error) { return } return ( <>
{ const { data } = await sendToLnAddr({ variables: { addr, amount: Number(amount), maxFee: Number(maxFee) } }) router.push(`/withdrawals/${data.sendToLnAddr.id}`) }} > sats} /> sats} /> send
) }