2024-06-03 22:41:15 +00: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 { WalletSecurityBanner } from '@/components/banners'
|
2024-06-20 14:48:46 +00:00
|
|
|
import { WalletLogs } from '@/components/wallet-logger'
|
2024-06-03 22:41:15 +00:00
|
|
|
import { useToast } from '@/components/toast'
|
|
|
|
import { useRouter } from 'next/router'
|
2024-06-20 17:56:37 +00:00
|
|
|
import { useWallet, Status } from '@/components/wallet'
|
2024-06-24 10:53:59 +00:00
|
|
|
import Info from '@/components/info'
|
|
|
|
import Text from '@/components/text'
|
2024-06-03 22:41:15 +00: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] || ''
|
|
|
|
}
|
2024-06-21 20:12:10 +00:00
|
|
|
}, {})
|
2024-06-03 22:41:15 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<CenterLayout>
|
|
|
|
<h2 className='pb-2'>{wallet.card.title}</h2>
|
2024-06-20 19:52:07 +00:00
|
|
|
<h6 className='text-muted text-center pb-3'>{wallet.card.subtitle}</h6>
|
2024-06-03 22:41:15 +00:00
|
|
|
<WalletSecurityBanner />
|
|
|
|
<Form
|
|
|
|
initial={initial}
|
2024-06-20 18:43:55 +00:00
|
|
|
schema={wallet.schema}
|
2024-06-20 17:56:37 +00:00
|
|
|
onSubmit={async ({ enabled, ...values }) => {
|
2024-06-03 22:41:15 +00:00
|
|
|
try {
|
2024-06-20 18:16:47 +00:00
|
|
|
const newConfig = !wallet.isConfigured
|
2024-06-21 20:32:06 +00:00
|
|
|
await wallet.save(values)
|
2024-06-20 18:16:47 +00:00
|
|
|
// enable wallet if checkbox was set or if wallet was just configured
|
|
|
|
if (enabled || newConfig) wallet.enable()
|
2024-06-20 17:56:37 +00:00
|
|
|
else wallet.disable()
|
2024-06-03 22:41:15 +00:00
|
|
|
toaster.success('saved settings')
|
|
|
|
router.push('/settings/wallets')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-06-20 17:56:37 +00:00
|
|
|
const message = 'failed to attach: ' + err.message || err.toString?.()
|
|
|
|
toaster.danger(message)
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<WalletFields wallet={wallet} />
|
|
|
|
<ClientCheckbox
|
|
|
|
disabled={false}
|
2024-06-20 17:56:37 +00:00
|
|
|
initialValue={wallet.status === Status.Enabled}
|
|
|
|
label='enabled'
|
|
|
|
name='enabled'
|
2024-06-03 22:41:15 +00:00
|
|
|
/>
|
|
|
|
<WalletButtonBar
|
|
|
|
wallet={wallet} onDelete={async () => {
|
|
|
|
try {
|
2024-06-20 18:34:09 +00:00
|
|
|
wallet.delete()
|
2024-06-03 22:41:15 +00:00
|
|
|
toaster.success('saved settings')
|
|
|
|
router.push('/settings/wallets')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-06-20 17:56:37 +00:00
|
|
|
const message = 'failed to detach: ' + err.message || err.toString?.()
|
|
|
|
toaster.danger(message)
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
<div className='mt-3 w-100'>
|
|
|
|
<WalletLogs wallet={wallet} embedded />
|
|
|
|
</div>
|
|
|
|
</CenterLayout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function WalletFields ({ wallet: { config, fields } }) {
|
2024-06-24 10:53:59 +00:00
|
|
|
return fields.map(({ name, label, type, help, optional, hint }, i) => {
|
2024-06-03 22:41:15 +00:00
|
|
|
const props = {
|
|
|
|
initialValue: config?.[name],
|
2024-06-24 10:53:59 +00:00
|
|
|
label: (
|
|
|
|
<div className='d-flex align-items-center'>
|
|
|
|
{label}
|
|
|
|
{help && (
|
|
|
|
<Info label='help'>
|
|
|
|
<Text>{help}</Text>
|
|
|
|
</Info>
|
|
|
|
)}
|
|
|
|
{optional && <small className='text-muted ms-2'>optional</small>}
|
|
|
|
</div>
|
|
|
|
),
|
2024-06-03 22:41:15 +00:00
|
|
|
name,
|
2024-06-24 10:53:59 +00:00
|
|
|
required: !optional,
|
|
|
|
autoFocus: i === 0,
|
|
|
|
hint
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
if (type === 'text') {
|
|
|
|
return <ClientInput key={i} {...props} />
|
|
|
|
}
|
|
|
|
if (type === 'password') {
|
|
|
|
return <PasswordInput key={i} {...props} newPass />
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|