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 { CenterLayout } from '../components/layout'
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/Alert'
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(!window.localStorage.getItem('hideLnAddrAlert'))
}, [])
if (called && !error) {
return
}
return (
<>
{me && showAlert &&
{
window.localStorage.setItem('hideLnAddrAlert', 'yep')
setShowAlert(false)
}}
>
You can also fund your account via lightning address with {`${me.name}@stacker.news`}
}
>
)
}
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 (
<>
or via
>
)
}
function LnQRWith ({ k1, encodedUrl }) {
const router = useRouter()
const query = gql`
{
lnWith(k1: "${k1}") {
withdrawalId
k1
}
}`
const { data } = useQuery(query, { pollInterval: 1000, nextFetchPolicy: 'cache-and-network' })
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 (
<>
>
)
}