import { Form, Input, MarkdownInput, SubmitButton } from '../components/form' import { useRouter } from 'next/router' import { gql, useApolloClient, useLazyQuery, useMutation } from '@apollo/client' import Countdown from './countdown' import AdvPostForm, { AdvPostInitial } from './adv-post-form' import FeeButton, { EditFeeButton } from './fee-button' import { ITEM_FIELDS } from '../fragments/items' import AccordianItem from './accordian-item' import Item from './item' import Delete from './delete' import Button from 'react-bootstrap/Button' import { discussionSchema } from '../lib/validate' import { SubSelectInitial } from './sub-select-form' import CancelButton from './cancel-button' import { useCallback } from 'react' import { normalizeForwards } from '../lib/form' import { MAX_TITLE_LENGTH } from '../lib/constants' import { useMe } from './me' import useCrossposter from './use-crossposter' export function DiscussionForm ({ item, sub, editThreshold, titleLabel = 'title', textLabel = 'text', buttonText = 'post', handleSubmit, children }) { const router = useRouter() const client = useApolloClient() const me = useMe() const schema = discussionSchema({ client, me, existingBoost: item?.boost }) // if Web Share Target API was used const shareTitle = router.query.title const crossposter = useCrossposter() const [upsertDiscussion] = useMutation( gql` mutation upsertDiscussion($sub: String, $id: ID, $title: String!, $text: String, $boost: Int, $forward: [ItemForwardInput], $hash: String, $hmac: String) { upsertDiscussion(sub: $sub, id: $id, title: $title, text: $text, boost: $boost, forward: $forward, hash: $hash, hmac: $hmac) { id } }` ) const onSubmit = useCallback( async ({ boost, crosspost, ...values }) => { try { if (crosspost && !(await window.nostr.getPublicKey())) { throw new Error('not available') } } catch (e) { throw new Error(`Nostr extension error: ${e.message}`) } const { data, error } = await upsertDiscussion({ variables: { sub: item?.subName || sub?.name, id: item?.id, boost: boost ? Number(boost) : undefined, ...values, forward: normalizeForwards(values.forward) } }) if (error) { throw new Error({ message: error.toString() }) } try { if (crosspost && data?.upsertDiscussion?.id) { await crossposter({ ...values, id: data.upsertDiscussion.id }) } } catch (e) { console.error(e) } if (item) { await router.push(`/items/${item.id}`) } else { const prefix = sub?.name ? `/~${sub.name}` : '' await router.push(prefix + '/recent') } }, [upsertDiscussion, router, item, sub, crossposter] ) const [getRelated, { data: relatedData }] = useLazyQuery(gql` ${ITEM_FIELDS} query related($title: String!) { related(title: $title, minMatch: "75%", limit: 3) { items { ...ItemFields } } }`) const related = relatedData?.related?.items || [] // const cost = linkOrImg ? 10 : me?.freePosts ? 0 : 1 return (
{children} { if (e.target.value) { getRelated({ variables: { title: e.target.value } }) } }} maxLength={MAX_TITLE_LENGTH} /> {textLabel} optional} name='text' minRows={6} hint={editThreshold ?
: null} />
{item ? (
router.push(`/items/${item.id}`)}>
) : }
{!item &&
0 ? '' : 'invisible'}`}> similar
} body={
{related.map((item, i) => ( ))}
} /> } ) }