Skip to content

Commit

Permalink
Refactor to use useImperativeHandle for Reply
Browse files Browse the repository at this point in the history
  • Loading branch information
SatsAllDay committed Sep 27, 2023
1 parent 8ae0d9a commit 9f14021
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion components/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default function Comment ({
: (
<div className={styles.children}>
{!noReply &&
<Reply depth={depth + 1} item={item} replyOpen={replyOpen} innerRef={replyRef}>
<Reply depth={depth + 1} item={item} replyOpen={replyOpen} ref={replyRef}>
{root.bounty && !bountyPaid && <PayBounty item={item} />}
</Reply>}
{children}
Expand Down
2 changes: 1 addition & 1 deletion components/item-full.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function TopLevelItem ({ item, noReply, ...props }) {
</div>
{!noReply &&
<>
<Reply item={item} replyOpen placeholder={item.ncomments ? undefined : 'start the conversation ...'} innerRef={replyRef} />
<Reply item={item} replyOpen placeholder={item.ncomments ? undefined : 'start the conversation ...'} ref={replyRef} />
{!item.position && !item.isJob && !item.parentId && !item.bounty > 0 && <Related title={item.title} itemId={item.id} />}
{item.bounty > 0 && <PastBounties item={item} />}
</>}
Expand Down
43 changes: 21 additions & 22 deletions components/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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 { forwardRef, useCallback, useEffect, useState, useRef, useImperativeHandle } from 'react'
import Link from 'next/link'
import FeeButton from './fee-button'
import { commentsViewedAfterComment } from '../lib/new-comments'
Expand Down Expand Up @@ -34,32 +34,31 @@ function FreebieDialog () {
)
}

export default function Reply ({ item, onSuccess, replyOpen, children, placeholder, innerRef }) {
export default forwardRef(function Reply ({ item, onSuccess, replyOpen, children, placeholder }, ref) {
const [reply, setReply] = useState(replyOpen)
const me = useMe()
const parentId = item.id
const replyInput = useRef(null)
const formInnerRef = useRef()
const quoteReply = useCallback(() => {
if (!reply) {
setReply(true)
}
let updatedValue
if (formInnerRef.current && formInnerRef.current.values && !formInnerRef.current.values.text) {
updatedValue = quote(item.text)
} else if (formInnerRef.current?.values?.text) {
// append quote reply text if the input already has content
updatedValue = `${replyInput.current.value}\n${quote(item.text)}`
}
if (updatedValue) {
replyInput.current.value = updatedValue
formInnerRef.current.setValues({ text: updatedValue })
window.localStorage.setItem(`reply-${parentId}-text`, updatedValue)
useImperativeHandle(ref, () => ({
quoteReply: () => {
if (!reply) {
setReply(true)
}
let updatedValue
if (formInnerRef.current && formInnerRef.current.values && !formInnerRef.current.values.text) {
updatedValue = quote(item.text)
} else if (formInnerRef.current?.values?.text) {
// append quote reply text if the input already has content
updatedValue = `${replyInput.current.value}\n${quote(item.text)}`
}
if (updatedValue) {
replyInput.current.value = updatedValue
formInnerRef.current.setValues({ text: updatedValue })
window.localStorage.setItem(`reply-${parentId}-text`, updatedValue)
}
}
}, [reply, item])
if (innerRef) {
innerRef.current = { quoteReply }
}
}), [reply, item])

useEffect(() => {
setReply(replyOpen || !!window.localStorage.getItem('reply-' + parentId + '-' + 'text'))
Expand Down Expand Up @@ -168,7 +167,7 @@ export default function Reply ({ item, onSuccess, replyOpen, children, placehold
</div>
</div>
)
}
})

export function ReplySkeleton () {
return (
Expand Down

0 comments on commit 9f14021

Please sign in to comment.