'use client' import React, {ReactNode, useActionState} 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 {formatDistanceToNow} from 'date-fns' type Comment = { id: number content: string created_at: string user: { id: number name: string image: string } } type CommentsListProps = { comments: Comment[] } Comments.List = function List({comments}: CommentsListProps) { return ( <> {comments.length > 0 &&

Comments

{comments.map((comment) => (
{comment.user.name}

{comment.user.name}

{comment.content}

))}
} ) } type CommentsStatusProps = { children: ReactNode } Comments.Status = function Status({children}: CommentsStatusProps) { const conditions = ['error', 'problem'] const isError = conditions.some(condition => children?.toString().toLowerCase().includes(condition)) return (

{children}

) } type InitialState = { message: string } const initialState: InitialState = { message: '' } Comments.Form = function Form({slug}: CommentsProps) { const [state, formAction, pending] = useActionState(addComment, initialState) const {data: session} = useSession() const handleKeyDown = (e: React.KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && (e.key === 'Enter' || e.key === 'NumpadEnter')) { e.preventDefault() e.currentTarget.form?.requestSubmit() } } return (
{!session ?
await loginWithGitHub()}>
: