'use client' import type { FC } from 'react' import { memo, useCallback, useEffect, useMemo, useState } from 'react' import { useReactFlow, useViewport } from 'reactflow' import { RiArrowDownSLine, RiArrowUpSLine, RiCheckboxCircleFill, RiCheckboxCircleLine, RiCloseLine, RiDeleteBinLine, RiMoreFill } from '@remixicon/react' import Avatar from '@/app/components/base/avatar' import Divider from '@/app/components/base/divider' import cn from '@/utils/classnames' import { useFormatTimeFromNow } from '@/app/components/workflow/hooks' import type { WorkflowCommentDetail, WorkflowCommentDetailReply } from '@/service/workflow-comment' import { useAppContext } from '@/context/app-context' import { MentionInput } from './mention-input' type CommentThreadProps = { comment: WorkflowCommentDetail loading?: boolean onClose: () => void onDelete?: () => void onResolve?: () => void onPrev?: () => void onNext?: () => void canGoPrev?: boolean canGoNext?: boolean onReply?: (content: string, mentionedUserIds?: string[]) => Promise | void onReplyEdit?: (replyId: string, content: string, mentionedUserIds?: string[]) => Promise | void onReplyDelete?: (replyId: string) => void } const ThreadMessage: FC<{ authorName: string avatarUrl?: string | null createdAt: number content: string }> = ({ authorName, avatarUrl, createdAt, content }) => { const { formatTimeFromNow } = useFormatTimeFromNow() return (
{authorName} {formatTimeFromNow(createdAt * 1000)}
{content}
) } export const CommentThread: FC = memo(({ comment, loading = false, onClose, onDelete, onResolve, onPrev, onNext, canGoPrev, canGoNext, onReply, onReplyEdit, onReplyDelete, }) => { const { flowToScreenPosition } = useReactFlow() const viewport = useViewport() const { userProfile } = useAppContext() const [replyContent, setReplyContent] = useState('') const [activeReplyMenuId, setActiveReplyMenuId] = useState(null) const [editingReply, setEditingReply] = useState<{ id: string; content: string }>({ id: '', content: '' }) useEffect(() => { setReplyContent('') }, [comment.id]) const handleReplySubmit = useCallback(async (content: string, mentionedUserIds: string[]) => { if (!onReply || loading) return try { await onReply(content, mentionedUserIds) setReplyContent('') } catch (error) { console.error('Failed to send reply', error) } }, [onReply, loading]) const screenPosition = useMemo(() => { return flowToScreenPosition({ x: comment.position_x, y: comment.position_y, }) }, [comment.position_x, comment.position_y, viewport.x, viewport.y, viewport.zoom, flowToScreenPosition]) const handleStartEdit = useCallback((reply: WorkflowCommentDetailReply) => { setEditingReply({ id: reply.id, content: reply.content }) setActiveReplyMenuId(null) }, []) const handleCancelEdit = useCallback(() => { setEditingReply({ id: '', content: '' }) }, []) const handleEditSubmit = useCallback(async (content: string, mentionedUserIds: string[]) => { if (!onReplyEdit || !editingReply) return const trimmed = content.trim() if (!trimmed) return await onReplyEdit(editingReply.id, trimmed, mentionedUserIds) setEditingReply({ id: '', content: '' }) }, [editingReply, onReplyEdit]) const replies = comment.replies || [] return (
Comment
{replies.length > 0 && (
{replies.map((reply) => { const isReplyEditing = editingReply?.id === reply.id return (
{!isReplyEditing && (
{activeReplyMenuId === reply.id && (
)}
)} {isReplyEditing ? (
setEditingReply(prev => prev ? { ...prev, content: newContent } : prev)} onSubmit={handleEditSubmit} onCancel={handleCancelEdit} placeholder="Edit reply" disabled={loading} loading={loading} isEditing={true} className="system-sm-regular" autoFocus />
) : ( )}
) })}
)}
{loading && (
Loading…
)} {onReply && (
)}
) }) CommentThread.displayName = 'CommentThread'