'use client' import React, {useActionState, useEffect, useState} from 'react' import {useSession} from 'next-auth/react' import Image from 'next/image' import clsx from 'clsx' import {addComment, loginWithGitHub} from '@/app/actions/comments' import {Button} from '@/components/ui/Button' import {GitHubIcon} from '@/components/icons/SocialIcons' import {ArrowLeftIcon} from '@/components/icons/ArrowLeftIcon' import {StatusMessage} from '@/components/ui/StatusMessage' import {getShortDurationFromNow} from '@/lib/dateFns' import ReplyProvider, {useReplyContext} from '@/app/context/ReplyProvider' import CommentFormProvider, {useCommentFormContext} from '@/app/context/CommentFormProvider' import type {Comment} from '@/types' type ReplyButton = { comment: Comment } Comments.ReplyButton = function ReplyButton({comment}: ReplyButton) { const replyContext = useReplyContext() const commentFormContext = useCommentFormContext() const {data: session} = useSession() const handleReplyButton = async (e: React.MouseEvent) => { e.preventDefault() if (!session) { await loginWithGitHub() } commentFormContext?.setCommentLength(0) commentFormContext?.commentFormRef?.current?.form?.reset() replyContext?.setReplyTo(comment); commentFormContext?.focusCommentForm() } return ( ) } Comments.Comment = function Comment({comment, isReply = false}: { comment: Comment, isReply?: boolean }) { return ( <>
{comment.user.name}

{comment.user.name}

{comment.content}

) } type CommentsListProps = { comments: Comment[] } Comments.List = function List({comments}: CommentsListProps) { return ( <> {comments.length > 0 &&

Comments

{comments.map((comment) => ( {(typeof comment.replies !== 'undefined' && comment.replies.length > 0) ? comment.replies.map(reply => ( )) : null } ))}
} ) } type InitialState = { message: string } const initialState: InitialState = { message: '' } Comments.Form = function Form({slug}: { slug: string }) { const [parentId, setParentId] = useState('') const [state, formAction, pending] = useActionState(addComment, initialState) const {data: session} = useSession() const replyContext = useReplyContext() const commentFormContext = useCommentFormContext() const commentFormRef = commentFormContext?.commentFormRef useEffect(() => { if (replyContext?.replyTo?.parent_id !== null) { setParentId(replyContext?.replyTo?.parent_id ?? '') } else { setParentId(replyContext?.replyTo?.id) } }, [replyContext?.replyTo]) const handleKeyDown = (e: React.KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && (e.key === 'Enter' || e.key === 'NumpadEnter')) { e.preventDefault() e.currentTarget.form?.requestSubmit() } if (e.key === 'Escape' && replyContext?.replyTo !== null) { replyContext?.setReplyTo(null) commentFormRef?.current?.blur() } } const handleSubmit = () => { replyContext?.setReplyTo(null) 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 }