stacker.news/components/reply.js

167 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-07-01 23:51:58 +00:00
import { Form, MarkdownInput, SubmitButton } from '../components/form'
2021-04-14 23:56:29 +00:00
import { gql, useMutation } from '@apollo/client'
import styles from './reply.module.css'
2021-04-22 22:14:32 +00:00
import { COMMENTS } from '../fragments/comments'
2021-05-25 00:08:56 +00:00
import { useMe } from './me'
import { useEffect, useState, useRef, useCallback } from 'react'
2022-05-17 22:09:15 +00:00
import Link from 'next/link'
2022-08-10 15:06:31 +00:00
import FeeButton from './fee-button'
import { commentsViewedAfterComment } from '../lib/new-comments'
2023-02-08 19:38:04 +00:00
import { commentSchema } from '../lib/validate'
2023-06-20 17:55:45 +00:00
import Info from './info'
import { useAnonymous } from '../lib/anonymous'
2021-04-14 23:56:29 +00:00
2022-05-17 22:09:15 +00:00
export function ReplyOnAnotherPage ({ parentId }) {
return (
<Link href={`/items/${parentId}`} className={`${styles.replyButtons} text-muted`}>
reply on another page
2022-05-17 22:09:15 +00:00
</Link>
)
}
2023-06-20 17:55:45 +00:00
function FreebieDialog () {
return (
<div className='text-muted'>
you have no sats, so this one is on us
<Info>
2023-07-24 18:35:05 +00:00
<ul className='fw-bold'>
2023-06-20 17:55:45 +00:00
<li>Free comments have limited visibility and are listed at the bottom of the comment section until other stackers zap them.</li>
<li>Free comments will not cover comments that cost more than 1 sat.</li>
<li>To get fully visibile and unrestricted comments right away, fund your account with a few sats or earn some on Stacker News.</li>
</ul>
</Info>
</div>
)
}
2023-05-11 00:41:17 +00:00
export default function Reply ({ item, onSuccess, replyOpen, children, placeholder }) {
const [reply, setReply] = useState(replyOpen)
2021-05-25 00:08:56 +00:00
const me = useMe()
const parentId = item.id
2021-05-25 00:08:56 +00:00
useEffect(() => {
2023-07-25 14:14:45 +00:00
setReply(replyOpen || !!window.localStorage.getItem('reply-' + parentId + '-' + 'text'))
}, [])
2021-04-14 23:56:29 +00:00
const [createComment] = useMutation(
gql`
2021-04-17 18:15:18 +00:00
${COMMENTS}
mutation createComment($text: String!, $parentId: ID!, $invoiceHash: String) {
createComment(text: $text, parentId: $parentId, invoiceHash: $invoiceHash) {
2021-04-17 18:15:18 +00:00
...CommentFields
comments {
...CommentsRecursive
}
2021-04-14 23:56:29 +00:00
}
2021-04-17 18:15:18 +00:00
}`, {
update (cache, { data: { createComment } }) {
cache.modify({
2021-04-27 00:55:48 +00:00
id: `Item:${parentId}`,
2021-04-17 18:15:18 +00:00
fields: {
comments (existingCommentRefs = []) {
2021-04-17 18:15:18 +00:00
const newCommentRef = cache.writeFragment({
data: createComment,
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, createComment.createdAt)
2021-04-17 18:15:18 +00:00
}
}
2021-04-14 23:56:29 +00:00
)
const submitComment = useCallback(
async (_, values, parentId, resetForm, invoiceHash) => {
const { error } = await createComment({ variables: { ...values, parentId, invoiceHash } })
if (error) {
throw new Error({ message: error.toString() })
}
resetForm({ text: '' })
setReply(replyOpen || false)
}, [createComment, setReply])
const anonCreateComment = useAnonymous(submitComment)
2023-02-24 15:52:09 +00:00
const replyInput = useRef(null)
useEffect(() => {
2023-02-24 16:08:15 +00:00
if (replyInput.current && reply && !replyOpen) replyInput.current.focus()
2023-02-24 15:52:09 +00:00
}, [reply])
2021-04-14 23:56:29 +00:00
return (
2021-09-23 20:09:07 +00:00
<div>
{replyOpen
? <div className={styles.replyButtons} />
: (
2023-01-26 16:11:55 +00:00
<div className={styles.replyButtons}>
<div
onClick={() => setReply(!reply)}
>
{reply ? 'cancel' : 'reply'}
</div>
{/* HACK if we need more items, we should probably do a comment toolbar */}
{children}
</div>)}
{reply &&
<div className={styles.reply}>
<Form
initial={{
text: ''
}}
schema={commentSchema}
2023-07-22 23:50:06 +00:00
onSubmit={async ({ cost, ...values }, { resetForm }) => {
await anonCreateComment(cost, values, parentId, resetForm)
}}
storageKeyPrefix={'reply-' + parentId}
>
<MarkdownInput
name='text'
minRows={6}
autoFocus={!replyOpen}
required
placeholder={placeholder}
hint={me?.sats < 1 && <FreebieDialog />}
innerRef={replyInput}
/>
{reply &&
<div className='mt-1'>
<FeeButton
baseFee={1} parentId={parentId} text='reply'
ChildButton={SubmitButton} variant='secondary' alwaysShow
/>
</div>}
</Form>
</div>}
2021-04-14 23:56:29 +00:00
</div>
)
}
2021-04-27 00:55:48 +00:00
export function ReplySkeleton () {
return (
<div className={`${styles.reply} ${styles.skeleton}`}>
<div className={`${styles.input} clouds`} />
<div className={`${styles.button} clouds`} />
</div>
)
}