stacker.news/components/reply.js

120 lines
3.7 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 * as Yup from 'yup'
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'
2021-07-08 18:42:57 +00:00
import ActionTooltip from './action-tooltip'
2021-07-16 17:32:54 +00:00
import TextareaAutosize from 'react-textarea-autosize'
import { useEffect, useState } from 'react'
2022-04-18 16:08:58 +00:00
import Info from './info'
2021-04-14 23:56:29 +00:00
export const CommentSchema = Yup.object({
text: Yup.string().required('required').trim()
})
export default function Reply ({ parentId, meComments, onSuccess, replyOpen }) {
const [reply, setReply] = useState(replyOpen)
2021-05-25 00:08:56 +00:00
const me = useMe()
useEffect(() => {
setReply(replyOpen || !!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}
2021-04-14 23:56:29 +00:00
mutation createComment($text: String!, $parentId: ID!) {
createComment(text: $text, parentId: $parentId) {
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]
2021-04-27 21:30:58 +00:00
},
ncomments (existingNComments = 0) {
return existingNComments + 1
},
meComments (existingMeComments = 0) {
return existingMeComments + 1
2021-04-17 18:15:18 +00:00
}
}
})
}
}
2021-04-14 23:56:29 +00:00
)
const cost = me?.freeComments ? 0 : Math.pow(10, meComments)
2021-04-14 23:56:29 +00:00
return (
2021-09-23 20:09:07 +00:00
<div>
{replyOpen
? <div className={styles.replyButtons} />
: (
<div
className={styles.replyButtons}
onClick={() => setReply(!reply)}
>
{reply ? 'cancel' : 'reply'}
</div>)}
2021-09-23 20:09:07 +00:00
<div className={reply ? `${styles.reply}` : 'd-none'}>
<Form
initial={{
text: ''
}}
schema={CommentSchema}
onSubmit={async (values, { resetForm }) => {
const { error } = await createComment({ variables: { ...values, parentId } })
if (error) {
throw new Error({ message: error.toString() })
}
resetForm({ text: '' })
setReply(replyOpen || false)
}}
2022-01-07 18:55:40 +00:00
storageKeyPrefix={'reply-' + parentId}
2021-09-23 20:09:07 +00:00
>
<MarkdownInput
name='text'
as={TextareaAutosize}
2021-11-12 22:39:52 +00:00
minRows={6}
2021-09-23 20:09:07 +00:00
autoFocus={!replyOpen}
required
hint={me?.freeComments ? <span className='text-success'>{me.freeComments} free comments left</span> : null}
/>
2022-04-18 16:08:58 +00:00
<div className='d-flex align-items-center mt-1'>
<ActionTooltip overlayText={`${cost} sats`}>
<SubmitButton variant='secondary'>reply{cost > 1 && <small> {cost} sats</small>}</SubmitButton>
</ActionTooltip>
{cost > 1 && (
<Info>
<div className='font-weight-bold'>Multiple replies on the same level get pricier, but we still love your thoughts!</div>
</Info>
)}
</div>
2021-09-23 20:09:07 +00:00
</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>
)
}