'use client' import React, {useActionState, useEffect, useState} from 'react' import {useSession} from 'next-auth/react' import Image from 'next/image' import Link from 'next/link' 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 fetcher from '@/lib/fetcher' import CommentFormProvider, {useCommentFormContext} from '@/app/context/CommentFormProvider' import useSWR from 'swr' import type {Comment} from '@/types' type ReplyButton = { comment: Comment } Comments.ReplyButton = function ReplyButton({comment}: ReplyButton) { const commentFormContext = useCommentFormContext() const {data: session} = useSession() const handleReplyButton = async (e: React.MouseEvent) => { e.preventDefault() commentFormContext?.setCommentLength(0) commentFormContext?.commentFormRef?.current?.form?.reset() commentFormContext?.setReplyTo(comment); commentFormContext?.focusCommentForm() } return ( ) } Comments.Comment = function Comment({comment, isReply = false, className}: { comment: Comment, isReply?: boolean className?: string }) { const {data: session} = useSession() return ( <>
{comment.user.name}

{comment.user.name}

{comment.content}

{session && }
) } type CommentsListProps = { comments: Comment[] } Comments.List = function List({comments}: CommentsListProps) { const commentCount: number = comments.reduce((acc, comment) => { if (!comment) return acc const replyCount = comment.replies?.length || 0 return (acc + 1) + replyCount }, 0) return (

{commentCount > 0 ? `${commentCount} comment${commentCount > 1 ? 's' : ''}` : 'No comments'}

{comments.map((comment) => ( {(typeof comment.replies !== 'undefined' && comment.replies.length > 0) ? comment.replies.map((reply, i) => ( )) : 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 commentFormContext = useCommentFormContext() const commentFormRef = commentFormContext?.commentFormRef useEffect(() => { if (commentFormContext?.replyTo?.parent_id !== null) { setParentId(commentFormContext?.replyTo?.parent_id ?? '') } else { setParentId(commentFormContext?.replyTo?.id) } }, [commentFormContext?.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' && commentFormContext?.replyTo !== null) { commentFormContext?.setReplyTo(null) commentFormRef?.current?.blur() } } const handleSubmit = async (e: React.FormEvent) => { if (!session) { e.preventDefault() await loginWithGitHub() } commentFormContext?.setReplyTo(null) commentFormContext?.setCommentLength(0) } const handleCancel = () => { commentFormContext?.setReplyTo(null) commentFormContext?.setCommentLength(0) commentFormRef?.current?.form?.reset() } return (
{session?.user?.image !== null && {session?.user?.name }