From af96251ef86583e50b1688fe404f73766d1e5bb6 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Wed, 21 Sep 2022 14:37:00 -0700 Subject: [PATCH] Eliminate redundant showReply/replyTo state --- web/components/comment-input.tsx | 15 ++++--- .../feed/feed-answer-comment-group.tsx | 39 +++++++------------ web/components/feed/feed-comments.tsx | 30 +++++++------- web/posts/post-comments.tsx | 2 +- 4 files changed, 35 insertions(+), 51 deletions(-) diff --git a/web/components/comment-input.tsx b/web/components/comment-input.tsx index bf3730f3..d13ebf3b 100644 --- a/web/components/comment-input.tsx +++ b/web/components/comment-input.tsx @@ -11,7 +11,7 @@ import { Row } from './layout/row' import { LoadingIndicator } from './loading-indicator' export function CommentInput(props: { - replyToUser?: { id: string; username: string } + replyTo?: { id: string; username: string } // Reply to a free response answer parentAnswerOutcome?: string // Reply to another comment @@ -19,7 +19,7 @@ export function CommentInput(props: { onSubmitComment?: (editor: Editor) => void className?: string }) { - const { parentAnswerOutcome, parentCommentId, replyToUser, onSubmitComment } = + const { parentAnswerOutcome, parentCommentId, replyTo, onSubmitComment } = props const user = useUser() @@ -55,7 +55,7 @@ export function CommentInput(props: { [0]['upload'] submitComment: () => void isSubmitting: boolean }) { - const { user, editor, upload, submitComment, isSubmitting, replyToUser } = - props + const { user, editor, upload, submitComment, isSubmitting, replyTo } = props useEffect(() => { editor?.setEditable(!isSubmitting) }, [isSubmitting, editor]) @@ -108,12 +107,12 @@ export function CommentInputTextArea(props: { }, }) // insert at mention and focus - if (replyToUser) { + if (replyTo) { editor .chain() .setContent({ type: 'mention', - attrs: { label: replyToUser.username, id: replyToUser.id }, + attrs: { label: replyTo.username, id: replyTo.id }, }) .insertContent(' ') .focus() diff --git a/web/components/feed/feed-answer-comment-group.tsx b/web/components/feed/feed-answer-comment-group.tsx index 27f0f731..b4f822cb 100644 --- a/web/components/feed/feed-answer-comment-group.tsx +++ b/web/components/feed/feed-answer-comment-group.tsx @@ -10,11 +10,10 @@ import clsx from 'clsx' import { ContractCommentInput, FeedComment, + ReplyTo, } from 'web/components/feed/feed-comments' import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-time' import { useRouter } from 'next/router' -import { User } from 'common/user' -import { useEvent } from 'web/hooks/use-event' import { CommentTipMap } from 'web/hooks/use-tip-txns' import { UserLink } from 'web/components/user-link' @@ -27,27 +26,11 @@ export function FeedAnswerCommentGroup(props: { const { answer, contract, answerComments, tips } = props const { username, avatarUrl, name, text } = answer - const [replyToUser, setReplyToUser] = - useState>() - const [showReply, setShowReply] = useState(false) + const [replyTo, setReplyTo] = useState() const [highlighted, setHighlighted] = useState(false) const router = useRouter() - const answerElementId = `answer-${answer.id}` - const scrollAndOpenReplyInput = useEvent( - (comment?: ContractComment, answer?: Answer) => { - setReplyToUser( - comment - ? { id: comment.userId, username: comment.userUsername } - : answer - ? { id: answer.userId, username: answer.username } - : undefined - ) - setShowReply(true) - } - ) - useEffect(() => { if (router.asPath.endsWith(`#${answerElementId}`)) { setHighlighted(true) @@ -83,7 +66,9 @@ export function FeedAnswerCommentGroup(props: {
@@ -92,7 +77,9 @@ export function FeedAnswerCommentGroup(props: {
@@ -107,11 +94,13 @@ export function FeedAnswerCommentGroup(props: { contract={contract} comment={comment} tips={tips[comment.id] ?? {}} - onReplyClick={scrollAndOpenReplyInput} + onReplyClick={() => + setReplyTo({ id: comment.id, username: comment.userUsername }) + } /> ))} - {showReply && ( + {replyTo && (
setShowReply(false)} + replyTo={replyTo} + onSubmitComment={() => setReplyTo(undefined)} />
)} diff --git a/web/components/feed/feed-comments.tsx b/web/components/feed/feed-comments.tsx index acb48ec1..94cea452 100644 --- a/web/components/feed/feed-comments.tsx +++ b/web/components/feed/feed-comments.tsx @@ -20,6 +20,8 @@ import { Editor } from '@tiptap/react' import { UserLink } from 'web/components/user-link' import { CommentInput } from '../comment-input' +export type ReplyTo = { id: string; username: string } + export function FeedCommentThread(props: { contract: Contract threadComments: ContractComment[] @@ -27,13 +29,7 @@ export function FeedCommentThread(props: { parentComment: ContractComment }) { const { contract, threadComments, tips, parentComment } = props - const [showReply, setShowReply] = useState(false) - const [replyTo, setReplyTo] = useState<{ id: string; username: string }>() - - function scrollAndOpenReplyInput(comment: ContractComment) { - setReplyTo({ id: comment.userId, username: comment.userUsername }) - setShowReply(true) - } + const [replyTo, setReplyTo] = useState() return ( @@ -48,10 +44,12 @@ export function FeedCommentThread(props: { contract={contract} comment={comment} tips={tips[comment.id] ?? {}} - onReplyClick={scrollAndOpenReplyInput} + onReplyClick={() => + setReplyTo({ id: comment.id, username: comment.userUsername }) + } /> ))} - {showReply && ( + {replyTo && ( { - setShowReply(false) - }} + replyTo={replyTo} + onSubmitComment={() => setReplyTo(undefined)} /> )} @@ -76,7 +72,7 @@ export function FeedComment(props: { comment: ContractComment tips?: CommentTips indent?: boolean - onReplyClick?: (comment: ContractComment) => void + onReplyClick?: () => void }) { const { contract, comment, tips, indent, onReplyClick } = props const { @@ -174,7 +170,7 @@ export function FeedComment(props: { {onReplyClick && ( @@ -204,7 +200,7 @@ export function ContractCommentInput(props: { contract: Contract className?: string parentAnswerOutcome?: string | undefined - replyToUser?: { id: string; username: string } + replyTo?: ReplyTo parentCommentId?: string onSubmitComment?: () => void }) { @@ -226,7 +222,7 @@ export function ContractCommentInput(props: { return (