stacker.news/pages/settings/wallets/lightning-address.js

91 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-01-07 17:00:24 +00:00
import { getGetServerSideProps } from '../../../api/ssrApollo'
import { Form, Input } from '../../../components/form'
import { CenterLayout } from '../../../components/layout'
import { useMe } from '../../../components/me'
import { WalletButtonBar, WalletCard } from '../../../components/wallet-card'
import { useMutation } from '@apollo/client'
import { useToast } from '../../../components/toast'
import { lnAddrAutowithdrawSchema } from '../../../lib/validate'
2024-01-07 17:00:24 +00:00
import { useRouter } from 'next/router'
import { AutowithdrawSettings, autowithdrawInitial } from '../../../components/autowithdraw-shared'
import { REMOVE_WALLET, UPSERT_WALLET_LNADDR, WALLET_BY_TYPE } from '../../../fragments/wallet'
2024-01-07 17:00:24 +00:00
const variables = { type: 'LIGHTNING_ADDRESS' }
export const getServerSideProps = getGetServerSideProps({ query: WALLET_BY_TYPE, variables, authRequired: true })
2024-01-07 17:00:24 +00:00
export default function LightningAddress ({ ssrData }) {
2024-01-07 17:00:24 +00:00
const me = useMe()
const toaster = useToast()
const router = useRouter()
const [upsertWalletLNAddr] = useMutation(UPSERT_WALLET_LNADDR)
const [removeWallet] = useMutation(REMOVE_WALLET)
2024-01-07 17:00:24 +00:00
const { walletByType: wallet } = ssrData || {}
2024-01-07 17:00:24 +00:00
return (
<CenterLayout>
<h2 className='pb-2'>lightning address</h2>
<h6 className='text-muted text-center pb-3'>autowithdraw to a lightning address</h6>
2024-01-07 17:00:24 +00:00
<Form
initial={{
address: wallet?.wallet?.address || '',
...autowithdrawInitial({ me, priority: wallet?.priority })
2024-01-07 17:00:24 +00:00
}}
2024-01-12 15:37:50 +00:00
schema={lnAddrAutowithdrawSchema({ me })}
onSubmit={async ({ address, ...settings }) => {
2024-01-07 17:00:24 +00:00
try {
await upsertWalletLNAddr({
2024-01-07 17:00:24 +00:00
variables: {
id: wallet?.id,
address,
settings: {
...settings,
autoWithdrawThreshold: Number(settings.autoWithdrawThreshold),
autoWithdrawMaxFeePercent: Number(settings.autoWithdrawMaxFeePercent)
}
2024-01-07 17:00:24 +00:00
}
})
toaster.success('saved settings')
router.push('/settings/wallets')
} catch (err) {
console.error(err)
toaster.danger('failed to attach: ' + err.message || err.toString?.())
2024-01-07 17:00:24 +00:00
}
}}
>
<Input
label='lightning address'
name='address'
autoComplete='off'
2024-01-07 17:00:24 +00:00
required
autoFocus
/>
<AutowithdrawSettings />
2024-01-07 17:00:24 +00:00
<WalletButtonBar
enabled={!!wallet} onDelete={async () => {
2024-01-07 17:00:24 +00:00
try {
await removeWallet({ variables: { id: wallet?.id } })
2024-01-07 17:00:24 +00:00
toaster.success('saved settings')
router.push('/settings/wallets')
} catch (err) {
console.error(err)
toaster.danger('failed to unattach:' + err.message || err.toString?.())
}
}}
/>
</Form>
</CenterLayout>
)
}
export function LightningAddressWalletCard ({ wallet }) {
2024-01-07 17:00:24 +00:00
return (
<WalletCard
title='lightning address'
badges={['receive only', 'non-custodialish']}
provider='lightning-address'
enabled={wallet !== undefined || undefined}
2024-01-07 17:00:24 +00:00
/>
)
}