import { Form, MarkdownInput, SubmitButton } from '../components/form'
import { gql, useMutation } from '@apollo/client'
import styles from './reply.module.css'
import { COMMENTS } from '../fragments/comments'
import { useMe } from './me'
import { useEffect, useState, useRef, useCallback } from 'react'
import Link from 'next/link'
import FeeButton from './fee-button'
import { commentsViewedAfterComment } from '../lib/new-comments'
import { commentSchema } from '../lib/validate'
import Info from './info'
import { useInvoiceable } from './invoice'
export function ReplyOnAnotherPage ({ parentId }) {
return (
reply on another page
)
}
function FreebieDialog () {
return (
you have no sats, so this one is on us
- Free comments have limited visibility and are listed at the bottom of the comment section until other stackers zap them.
- Free comments will not cover comments that cost more than 1 sat.
- To get fully visibile and unrestricted comments right away, fund your account with a few sats or earn some on Stacker News.
)
}
export default function Reply ({ item, onSuccess, replyOpen, children, placeholder }) {
const [reply, setReply] = useState(replyOpen)
const me = useMe()
const parentId = item.id
useEffect(() => {
setReply(replyOpen || !!window.localStorage.getItem('reply-' + parentId + '-' + 'text'))
}, [])
const [upsertComment] = useMutation(
gql`
${COMMENTS}
mutation upsertComment($text: String!, $parentId: ID!, $invoiceHash: String, $invoiceHmac: String) {
upsertComment(text: $text, parentId: $parentId, invoiceHash: $invoiceHash, invoiceHmac: $invoiceHmac) {
...CommentFields
comments {
...CommentsRecursive
}
}
}`, {
update (cache, { data: { upsertComment } }) {
cache.modify({
id: `Item:${parentId}`,
fields: {
comments (existingCommentRefs = []) {
const newCommentRef = cache.writeFragment({
data: upsertComment,
fragment: COMMENTS,
fragmentName: 'CommentsRecursive'
})
return [newCommentRef, ...existingCommentRefs]
}
}
})
const ancestors = item.path.split('.')
// update all ancestors
ancestors.forEach(id => {
cache.modify({
id: `Item:${id}`,
fields: {
ncomments (existingNComments = 0) {
return existingNComments + 1
}
}
})
})
// so that we don't see indicator for our own comments, we record this comments as the latest time
// but we also have record num comments, in case someone else commented when we did
const root = ancestors[0]
commentsViewedAfterComment(root, upsertComment.createdAt)
}
}
)
const submitComment = useCallback(
async (_, values, parentId, resetForm, invoiceHash, invoiceHmac) => {
const { error } = await upsertComment({ variables: { ...values, parentId, invoiceHash, invoiceHmac } })
if (error) {
throw new Error({ message: error.toString() })
}
resetForm({ text: '' })
setReply(replyOpen || false)
}, [upsertComment, setReply])
const invoiceableCreateComment = useInvoiceable(submitComment)
const replyInput = useRef(null)
useEffect(() => {
if (replyInput.current && reply && !replyOpen) replyInput.current.focus()
}, [reply])
return (
{replyOpen
?
: (
setReply(!reply)}
>
{reply ? 'cancel' : 'reply'}
{/* HACK if we need more items, we should probably do a comment toolbar */}
{children}
)}
{reply &&
}
)
}
export function ReplySkeleton () {
return (
)
}