2024-06-03 17:41:15 -05:00
|
|
|
import { getGetServerSideProps } from '@/api/ssrApollo'
|
|
|
|
import { Form, ClientInput, ClientCheckbox, PasswordInput } from '@/components/form'
|
|
|
|
import { CenterLayout } from '@/components/layout'
|
|
|
|
import { WalletButtonBar } from '@/components/wallet-card'
|
|
|
|
import { lnbitsSchema } from '@/lib/validate'
|
|
|
|
import { WalletSecurityBanner } from '@/components/banners'
|
2024-06-20 16:48:46 +02:00
|
|
|
import { WalletLogs } from '@/components/wallet-logger'
|
2024-06-03 17:41:15 -05:00
|
|
|
import { useToast } from '@/components/toast'
|
|
|
|
import { useRouter } from 'next/router'
|
2024-06-20 19:56:37 +02:00
|
|
|
import { useWallet, Status } from '@/components/wallet'
|
2024-06-03 17:41:15 -05:00
|
|
|
|
|
|
|
export const getServerSideProps = getGetServerSideProps({ authRequired: true })
|
|
|
|
|
|
|
|
export default function WalletSettings () {
|
|
|
|
const toaster = useToast()
|
|
|
|
const router = useRouter()
|
|
|
|
const { wallet: name } = router.query
|
|
|
|
const wallet = useWallet(name)
|
|
|
|
|
|
|
|
const initial = wallet.fields.reduce((acc, field) => {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[field.name]: wallet.config?.[field.name] || ''
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
isDefault: wallet.isDefault || false
|
|
|
|
})
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CenterLayout>
|
|
|
|
<h2 className='pb-2'>{wallet.card.title}</h2>
|
|
|
|
<h6 className='text-muted text-center pb-3'>use {wallet.card.title} for payments</h6>
|
|
|
|
<WalletSecurityBanner />
|
|
|
|
<Form
|
|
|
|
initial={initial}
|
|
|
|
schema={lnbitsSchema}
|
2024-06-20 19:56:37 +02:00
|
|
|
onSubmit={async ({ enabled, ...values }) => {
|
2024-06-03 17:41:15 -05:00
|
|
|
try {
|
2024-06-20 20:16:47 +02:00
|
|
|
const newConfig = !wallet.isConfigured
|
2024-06-03 17:41:15 -05:00
|
|
|
await wallet.validate(values)
|
|
|
|
wallet.saveConfig(values)
|
2024-06-20 20:16:47 +02:00
|
|
|
// enable wallet if checkbox was set or if wallet was just configured
|
|
|
|
if (enabled || newConfig) wallet.enable()
|
2024-06-20 19:56:37 +02:00
|
|
|
else wallet.disable()
|
2024-06-03 17:41:15 -05:00
|
|
|
toaster.success('saved settings')
|
|
|
|
router.push('/settings/wallets')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-06-20 19:56:37 +02:00
|
|
|
const message = 'failed to attach: ' + err.message || err.toString?.()
|
|
|
|
toaster.danger(message)
|
2024-06-03 17:41:15 -05:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<WalletFields wallet={wallet} />
|
|
|
|
<ClientCheckbox
|
|
|
|
disabled={false}
|
2024-06-20 19:56:37 +02:00
|
|
|
initialValue={wallet.status === Status.Enabled}
|
|
|
|
label='enabled'
|
|
|
|
name='enabled'
|
2024-06-03 17:41:15 -05:00
|
|
|
/>
|
|
|
|
<WalletButtonBar
|
|
|
|
wallet={wallet} onDelete={async () => {
|
|
|
|
try {
|
|
|
|
wallet.clearConfig()
|
|
|
|
toaster.success('saved settings')
|
|
|
|
router.push('/settings/wallets')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-06-20 19:56:37 +02:00
|
|
|
const message = 'failed to detach: ' + err.message || err.toString?.()
|
|
|
|
wallet.logger.error(message)
|
|
|
|
toaster.danger(message)
|
2024-06-03 17:41:15 -05:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
<div className='mt-3 w-100'>
|
|
|
|
<WalletLogs wallet={wallet} embedded />
|
|
|
|
</div>
|
|
|
|
</CenterLayout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function WalletFields ({ wallet: { config, fields } }) {
|
|
|
|
return fields.map(({ name, label, type }, i) => {
|
|
|
|
const props = {
|
|
|
|
initialValue: config?.[name],
|
|
|
|
label,
|
|
|
|
name,
|
|
|
|
required: true,
|
|
|
|
autoFocus: i === 0
|
|
|
|
}
|
|
|
|
if (type === 'text') {
|
|
|
|
return <ClientInput key={i} {...props} />
|
|
|
|
}
|
|
|
|
if (type === 'password') {
|
|
|
|
return <PasswordInput key={i} {...props} newPass />
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|