import { useState, useEffect } from 'react' import AccordianItem from './accordian-item' import { Input, InputUserSuggest, VariableInput, Checkbox } from './form' import InputGroup from 'react-bootstrap/InputGroup' import { BOOST_MIN, BOOST_MULT, MAX_FORWARDS } from '@/lib/constants' import { DEFAULT_CROSSPOSTING_RELAYS } from '@/lib/nostr' import Info from './info' import { numWithUnits } from '@/lib/format' import styles from './adv-post-form.module.css' import { useMe } from './me' import { useFeeButton } from './fee-button' import { useRouter } from 'next/router' import { useFormikContext } from 'formik' const EMPTY_FORWARD = { nym: '', pct: '' } export function AdvPostInitial ({ forward, boost }) { return { boost: boost || '', forward: forward?.length ? forward : [EMPTY_FORWARD] } } const FormStatus = { DIRTY: 'dirty', ERROR: 'error' } export default function AdvPostForm ({ children, item, storageKeyPrefix }) { const me = useMe() const { merge } = useFeeButton() const router = useRouter() const [itemType, setItemType] = useState() const formik = useFormikContext() const [show, setShow] = useState(false) useEffect(() => { const isDirty = formik?.values.forward?.[0].nym !== '' || formik?.values.forward?.[0].pct !== '' || formik?.values.boost !== '' || (router.query?.type === 'link' && formik?.values.text !== '') // if the adv post form is dirty on first render, show the accordian if (isDirty) { setShow(FormStatus.DIRTY) } // HACK ... TODO: we should generically handle this kind of local storage stuff // in the form component, overriding the initial values if (storageKeyPrefix) { for (let i = 0; i < MAX_FORWARDS; i++) { ['nym', 'pct'].forEach(key => { const value = window.localStorage.getItem(`${storageKeyPrefix}-forward[${i}].${key}`) if (value) { formik?.setFieldValue(`forward[${i}].${key}`, value) } }) } } }, [formik?.values, storageKeyPrefix]) useEffect(() => { // force show the accordian if there is an error and the form is submitting const hasError = !!formik?.errors?.boost || formik?.errors?.forward?.length > 0 // if it's open we don't want to collapse on submit setShow(show => hasError && formik?.isSubmitting ? FormStatus.ERROR : show) }, [formik?.isSubmitting]) useEffect(() => { const determineItemType = () => { if (router && router.query.type) { return router.query.type } else if (item) { const typeMap = { url: 'link', bounty: 'bounty', pollCost: 'poll' } for (const [key, type] of Object.entries(typeMap)) { if (item[key]) { return type } } return 'discussion' } } const type = determineItemType() setItemType(type) }, [item, router]) function renderCrosspostDetails (itemType) { switch (itemType) { case 'discussion': return