import { useRouter } from 'next/router' import { Form, Input, SubmitButton } from '../components/form' import Link from 'next/link' import Button from 'react-bootstrap/Button' import * as Yup from 'yup' import { gql, useMutation, useQuery } from '@apollo/client' import LnQR, { LnQRSkeleton } from '../components/lnqr' 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' 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 const FundSchema = Yup.object({ amount: Yup.number().typeError('must be a number').required('required') .positive('must be positive').integer('must be whole') }) 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
) } export const WithdrawlSchema = Yup.object({ invoice: Yup.string().required('required'), maxFee: Yup.number().typeError('must be a number').required('required') .min(0, 'must be positive').integer('must be whole') }) const MAX_FEE_DEFAULT = 10 export function WithdrawlForm () { const router = useRouter() const me = useMe() const [createWithdrawl, { called, error }] = useMutation(CREATE_WITHDRAWL) useEffect(async () => { 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) } }, []) 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 [createAuth, { data, error }] = useMutation(gql` mutation createAuth { createWith { k1 encodedUrl } }`) useEffect(createAuth, []) if (error) return
error
if (!data) { return } return } export const LnAddrSchema = Yup.object({ // addr: Yup.string().email('address is no good').required('required'), amount: Yup.number().typeError('must be a number').required('required') .positive('must be positive').integer('must be whole'), maxFee: Yup.number().typeError('must be a number').required('required') .min(0, 'must be positive').integer('must be whole') }) 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
) }