diff --git a/app/context/CommentFormProvider.tsx b/app/context/CommentFormProvider.tsx index 77ada15..36d8277 100644 --- a/app/context/CommentFormProvider.tsx +++ b/app/context/CommentFormProvider.tsx @@ -1,8 +1,11 @@ -import {createContext, ReactNode, RefObject, useContext, useRef} from 'react' +import {createContext, ReactNode, RefObject, useContext, useRef, useState} from 'react' type CommentFormContext = { commentFormRef?: RefObject focusCommentForm: () => void + commentLength: number + setCommentLength: (commentLength: number) => void + commentMaxLength: number } export const CommentFormContext = createContext(null) @@ -10,14 +13,17 @@ export const CommentFormContext = createContext(null) export const useCommentFormContext = () => useContext(CommentFormContext) export default function CommentFormProvider({children}: { children: ReactNode }) { + const [commentLength, setCommentLength] = useState(0) const commentFormRef = useRef(null) + const commentMaxLength = 300 const focusCommentForm = () => { commentFormRef.current?.focus() } return ( - + {children} ) diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index d74b76b..2b29404 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -29,6 +29,8 @@ Comments.ReplyButton = function ReplyButton({comment}: ReplyButton) { if (!session) { await loginWithGitHub() } + commentFormContext?.setCommentLength(0) + commentFormContext?.commentFormRef?.current?.form?.reset() replyContext?.setReplyTo(comment); commentFormContext?.focusCommentForm() } @@ -51,7 +53,7 @@ Comments.Comment = function Comment({comment, isReply = false}: { return ( <>
+ className={clsx('flex gap-x-4 py-5', isReply && 'ml-[66px] border-l border-zinc-100 pl-6 dark:border-zinc-700/40')}> {comment.user.name}
@@ -114,8 +116,6 @@ Comments.Form = function Form({slug}: { slug: string }) { const replyContext = useReplyContext() const commentFormContext = useCommentFormContext() const commentFormRef = commentFormContext?.commentFormRef - const [commentLength, setCommentLength] = useState(0) - const commentMaxLength = 300 useEffect(() => { if (replyContext?.replyTo?.parent_id !== null) { @@ -139,11 +139,17 @@ Comments.Form = function Form({slug}: { slug: string }) { const handleSubmit = () => { replyContext?.setReplyTo(null) - setCommentLength(0) + commentFormContext?.setCommentLength(0) + } + + const handleCancel = () => { + replyContext?.setReplyTo(null) + commentFormContext?.setCommentLength(0) + commentFormRef?.current?.form?.reset() } return ( -
+
{!session ?
await loginWithGitHub()}>
:
- -
+
+ {session?.user?.image !== null && + {session?.user?.name + } +