import { useState } from 'react' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { Markdown } from '@/components/content/Markdown' import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar' import { useAuth } from '@/lib/auth' import { Status } from '@/__generated__/graphql' import { useBugAddCommentMutation, useBugAddCommentAndCloseMutation, useBugAddCommentAndReopenMutation, useBugStatusCloseMutation, useBugStatusOpenMutation, BugDetailDocument, } from '@/__generated__/graphql' interface CommentBoxProps { bugPrefix: string bugStatus: Status /** Current repo slug, passed as `ref` in refetch query variables. */ ref_?: string | null } // Write/preview comment form at the bottom of BugDetailPage. Also contains the // Close / Reopen button. Hidden entirely in read-only mode (no logged-in user). export function CommentBox({ bugPrefix, bugStatus, ref_ }: CommentBoxProps) { const { user } = useAuth() const [message, setMessage] = useState('') const [preview, setPreview] = useState(false) const refetchVars = { variables: { ref: ref_, prefix: bugPrefix } } const refetch = { refetchQueries: [{ query: BugDetailDocument, ...refetchVars }] } const [addComment, { loading: addingComment }] = useBugAddCommentMutation(refetch) const [addAndClose, { loading: addingAndClosing }] = useBugAddCommentAndCloseMutation(refetch) const [addAndReopen, { loading: addingAndReopening }] = useBugAddCommentAndReopenMutation(refetch) const [statusClose, { loading: closing }] = useBugStatusCloseMutation(refetch) const [statusOpen, { loading: reopening }] = useBugStatusOpenMutation(refetch) const isOpen = bugStatus === Status.Open const busy = addingComment || addingAndClosing || addingAndReopening || closing || reopening const hasMessage = message.trim().length > 0 async function handleComment() { await addComment({ variables: { input: { prefix: bugPrefix, message: message.trim() } } }) setMessage('') setPreview(false) } async function handleToggleStatus() { if (isOpen) { if (hasMessage) { await addAndClose({ variables: { input: { prefix: bugPrefix, message: message.trim() } } }) } else { await statusClose({ variables: { input: { prefix: bugPrefix } } }) } } else { if (hasMessage) { await addAndReopen({ variables: { input: { prefix: bugPrefix, message: message.trim() } } }) } else { await statusOpen({ variables: { input: { prefix: bugPrefix } } }) } } setMessage('') setPreview(false) } if (!user) return null return (
{user.displayName.slice(0, 2).toUpperCase()}
{/* Write / Preview tabs */}
{preview ? (
) : (