'use client' import type { FC, ReactNode } from 'react' import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useParams } from 'next/navigation' import { useReactFlow, useViewport } from 'reactflow' import { useTranslation } from 'react-i18next' 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 Tooltip from '@/app/components/base/tooltip' import InlineDeleteConfirm from '@/app/components/base/inline-delete-confirm' import { PortalToFollowElem, PortalToFollowElemContent, PortalToFollowElemTrigger } from '@/app/components/base/portal-to-follow-elem' import cn from '@/utils/classnames' import { useFormatTimeFromNow } from '@/hooks/use-format-time-from-now' import type { WorkflowCommentDetail, WorkflowCommentDetailReply } from '@/service/workflow-comment' import { useAppContext } from '@/context/app-context' import { MentionInput } from './mention-input' import { getUserColor } from '@/app/components/workflow/collaboration/utils/user-color' import { useStore } from '../store' type CommentThreadProps = { comment: WorkflowCommentDetail loading?: boolean replySubmitting?: boolean replyUpdating?: 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 onReplyDeleteDirect?: (replyId: string) => Promise | void } const ThreadMessage: FC<{ authorId: string authorName: string avatarUrl?: string | null createdAt: number content: string mentionableNames: string[] className?: string }> = ({ authorId, authorName, avatarUrl, createdAt, content, mentionableNames, className }) => { const { formatTimeFromNow } = useFormatTimeFromNow() const { userProfile } = useAppContext() const currentUserId = userProfile?.id const isCurrentUser = authorId === currentUserId const userColor = isCurrentUser ? undefined : getUserColor(authorId) const highlightedContent = useMemo(() => { if (!content) return '' // Extract valid user names from mentionableNames, sorted by length (longest first) const normalizedNames = Array.from(new Set(mentionableNames .map(name => name.trim()) .filter(Boolean))) normalizedNames.sort((a, b) => b.length - a.length) if (normalizedNames.length === 0) return content const segments: ReactNode[] = [] let hasMention = false let cursor = 0 while (cursor < content.length) { let nextMatchStart = -1 let matchedName = '' for (const name of normalizedNames) { const searchStart = content.indexOf(`@${name}`, cursor) if (searchStart === -1) continue const previousChar = searchStart > 0 ? content[searchStart - 1] : '' if (searchStart > 0 && !/\s/.test(previousChar)) continue if ( nextMatchStart === -1 || searchStart < nextMatchStart || (searchStart === nextMatchStart && name.length > matchedName.length) ) { nextMatchStart = searchStart matchedName = name } } if (nextMatchStart === -1) break if (nextMatchStart > cursor) segments.push({content.slice(cursor, nextMatchStart)}) const mentionEnd = nextMatchStart + matchedName.length + 1 segments.push( {content.slice(nextMatchStart, mentionEnd)} , ) hasMention = true cursor = mentionEnd } if (!hasMention) return content if (cursor < content.length) segments.push({content.slice(cursor)}) return segments }, [content, mentionableNames]) return (
{authorName} {formatTimeFromNow(createdAt * 1000)}
{highlightedContent}
) } export const CommentThread: FC = memo(({ comment, loading = false, replySubmitting = false, replyUpdating = false, onClose, onDelete, onResolve, onPrev, onNext, canGoPrev, canGoNext, onReply, onReplyEdit, onReplyDelete, onReplyDeleteDirect, }) => { const params = useParams() const appId = params.appId as string const { flowToScreenPosition } = useReactFlow() const viewport = useViewport() const { userProfile } = useAppContext() const { t } = useTranslation() const [replyContent, setReplyContent] = useState('') const [activeReplyMenuId, setActiveReplyMenuId] = useState(null) const [editingReply, setEditingReply] = useState<{ id: string; content: string }>({ id: '', content: '' }) const [deletingReplyId, setDeletingReplyId] = useState(null) const [isSubmittingEdit, setIsSubmittingEdit] = useState(false) // Focus management refs const replyInputRef = useRef(null) const threadRef = useRef(null) // Get mentionable users from store const mentionUsersFromStore = useStore(state => ( appId ? state.mentionableUsersCache[appId] : undefined )) const mentionUsers = mentionUsersFromStore ?? [] // Extract all mentionable names for highlighting const mentionableNames = useMemo(() => { const names = mentionUsers .map(user => user.name?.trim()) .filter((name): name is string => Boolean(name)) return Array.from(new Set(names)) }, [mentionUsers]) useEffect(() => { setReplyContent('') }, [comment.id]) // P0: Auto-focus reply input when thread opens or comment changes useEffect(() => { const timer = setTimeout(() => { if (replyInputRef.current && !editingReply.id && onReply) replyInputRef.current.focus() }, 100) return () => clearTimeout(timer) }, [comment.id, editingReply.id, onReply]) // P2: Handle Esc key to close thread useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { // Don't intercept if actively editing a reply if (editingReply.id) return // Don't intercept if mention dropdown is open (let MentionInput handle it) if (document.querySelector('[data-mention-dropdown]')) return if (e.key === 'Escape') { e.preventDefault() e.stopPropagation() onClose() } } document.addEventListener('keydown', handleKeyDown, true) return () => document.removeEventListener('keydown', handleKeyDown, true) }, [onClose, editingReply.id]) const handleReplySubmit = useCallback(async (content: string, mentionedUserIds: string[]) => { if (!onReply || replySubmitting) return setReplyContent('') try { await onReply(content, mentionedUserIds) // P0: Restore focus to reply input after successful submission setTimeout(() => { replyInputRef.current?.focus() }, 0) } catch (error) { console.error('Failed to send reply', error) setReplyContent(content) } }, [onReply, replySubmitting]) 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 workflowContainerRect = typeof document !== 'undefined' ? document.getElementById('workflow-container')?.getBoundingClientRect() : null const containerLeft = workflowContainerRect?.left ?? 0 const containerTop = workflowContainerRect?.top ?? 0 const canvasPosition = useMemo(() => ({ x: screenPosition.x - containerLeft, y: screenPosition.y - containerTop, }), [screenPosition.x, screenPosition.y, containerLeft, containerTop]) const handleStartEdit = useCallback((reply: WorkflowCommentDetailReply) => { setEditingReply({ id: reply.id, content: reply.content }) setActiveReplyMenuId(null) }, []) const handleCancelEdit = useCallback(() => { setEditingReply({ id: '', content: '' }) // P1: Restore focus to reply input after canceling edit setTimeout(() => { replyInputRef.current?.focus() }, 0) }, []) const handleEditSubmit = useCallback(async (content: string, mentionedUserIds: string[]) => { if (!onReplyEdit || !editingReply) return const trimmed = content.trim() if (!trimmed) return setIsSubmittingEdit(true) try { await onReplyEdit(editingReply.id, trimmed, mentionedUserIds) setEditingReply({ id: '', content: '' }) // P1: Restore focus to reply input after saving edit setTimeout(() => { replyInputRef.current?.focus() }, 0) } catch (error) { console.error('Failed to edit reply', error) } finally { setIsSubmittingEdit(false) } }, [editingReply, onReplyEdit]) const replies = comment.replies || [] const messageListRef = useRef(null) const previousReplyCountRef = useRef(undefined) const previousCommentIdRef = useRef(undefined) // Close dropdown when scrolling useEffect(() => { const container = messageListRef.current if (!container || !activeReplyMenuId) return const handleScroll = () => { setActiveReplyMenuId(null) } container.addEventListener('scroll', handleScroll) return () => container.removeEventListener('scroll', handleScroll) }, [activeReplyMenuId]) // Auto-scroll to bottom on new messages useEffect(() => { const container = messageListRef.current if (!container) return const isFirstRender = previousCommentIdRef.current === undefined const isNewComment = comment.id !== previousCommentIdRef.current const hasNewReply = previousReplyCountRef.current !== undefined && replies.length > previousReplyCountRef.current // Scroll on first render, new comment, or new reply if (isFirstRender || isNewComment || hasNewReply) { container.scrollTo({ top: container.scrollHeight, behavior: 'smooth', }) } previousCommentIdRef.current = comment.id previousReplyCountRef.current = replies.length }, [comment.id, replies.length]) return (
{t('workflow.comments.panelTitle')}
{replies.length > 0 && (
{replies.map((reply) => { const isReplyEditing = editingReply?.id === reply.id const isOwnReply = reply.created_by_account?.id === userProfile?.id return (
{isOwnReply && !isReplyEditing && ( { if (!open) { setDeletingReplyId(null) setActiveReplyMenuId(null) } }} >
{/* Menu buttons - hidden when showing delete confirm */}
{/* Delete confirmation - shown when deletingReplyId matches */}
{ setDeletingReplyId(null) setActiveReplyMenuId(null) onReplyDeleteDirect?.(reply.id) }} onCancel={() => { setDeletingReplyId(null) }} className='m-0 w-full border-0 shadow-none' />
)} {isReplyEditing ? (
setEditingReply(prev => prev ? { ...prev, content: newContent } : prev)} onSubmit={handleEditSubmit} onCancel={handleCancelEdit} placeholder={t('workflow.comments.placeholder.editReply')} disabled={loading} loading={replyUpdating || isSubmittingEdit} isEditing={true} className="system-sm-regular" autoFocus />
) : ( )}
) })}
)}
{loading && (
{t('workflow.comments.loading')}
)} {onReply && (
)}
) }) CommentThread.displayName = 'CommentThread'