import { Checkbox, Form, Input, SubmitButton, Select, VariableInput } from '../components/form'
import * as Yup from 'yup'
import { Alert, Button, InputGroup, Modal } from 'react-bootstrap'
import LayoutCenter from '../components/layout-center'
import { useState } from 'react'
import { gql, useMutation, useQuery } from '@apollo/client'
import { getGetServerSideProps } from '../api/ssrApollo'
import LoginButton from '../components/login-button'
import { signIn } from 'next-auth/client'
import ModalButton from '../components/modal-button'
import { LightningAuth, SlashtagsAuth } from '../components/lightning-auth'
import { SETTINGS, SET_SETTINGS } from '../fragments/users'
import { useRouter } from 'next/router'
import Info from '../components/info'
import { CURRENCY_SYMBOLS } from '../components/price'
import Link from 'next/link'
import AccordianItem from '../components/accordian-item'
import { MAX_NOSTR_RELAY_NUM } from '../lib/constants'
import { WS_REGEXP } from '../lib/url'
import { bech32 } from 'bech32'
export const getServerSideProps = getGetServerSideProps(SETTINGS)
const supportedCurrencies = Object.keys(CURRENCY_SYMBOLS)
const HEX64 = /^[0-9a-fA-F]{64}$/
const NPUB = /^npub1[02-9ac-hj-np-z]+$/
Yup.addMethod(Yup.string, 'or', function (schemas, msg) {
return this.test({
name: 'or',
message: msg,
test: value => {
if (Array.isArray(schemas) && schemas.length > 1) {
const resee = schemas.map(schema => schema.isValidSync(value))
return resee.some(res => res)
} else {
throw new TypeError('Schemas is not correct array schema')
}
},
exclusive: false
})
})
export const SettingsSchema = Yup.object({
tipDefault: Yup.number().typeError('must be a number').required('required')
.positive('must be positive').integer('must be whole'),
fiatCurrency: Yup.string().required('required').oneOf(supportedCurrencies),
nostrPubkey: Yup.string()
.or([
Yup.string().matches(HEX64, 'must be 64 hex chars'),
Yup.string().matches(NPUB, 'invalid bech32 encoding')], 'invalid pubkey'),
nostrRelays: Yup.array().of(
Yup.string().matches(WS_REGEXP, 'invalid web socket address')
).max(MAX_NOSTR_RELAY_NUM,
({ max, value }) => `${Math.abs(max - value.length)} too many`)
})
const warningMessage = 'If I logout, even accidentally, I will never be able to access my account again'
export const WarningSchema = Yup.object({
warning: Yup.string().matches(warningMessage, 'does not match').required('required')
})
function bech32encode (hexString) {
return bech32.encode('npub', bech32.toWords(Buffer.from(hexString, 'hex')))
}
export default function Settings ({ data: { settings } }) {
const [success, setSuccess] = useState()
const [setSettings] = useMutation(SET_SETTINGS, {
update (cache, { data: { setSettings } }) {
cache.modify({
id: 'ROOT_QUERY',
fields: {
settings () {
return setSettings
}
}
})
}
}
)
const { data } = useQuery(SETTINGS)
if (data) {
({ settings } = data)
}
return (
settings