import { useCallback, useMemo, useState } from 'react'; import { MarkdownViewer } from '@renderer/components/chat/viewers/MarkdownViewer'; import { ReplyQuoteBlock } from '@renderer/components/team/activity/ReplyQuoteBlock'; import { MentionableTextarea } from '@renderer/components/ui/MentionableTextarea'; import { Tooltip, TooltipContent, TooltipTrigger } from '@renderer/components/ui/tooltip'; import { useDraftPersistence } from '@renderer/hooks/useDraftPersistence'; import { useMarkCommentsRead } from '@renderer/hooks/useMarkCommentsRead'; import { useStore } from '@renderer/store'; import { buildReplyBlock, parseMessageReply } from '@renderer/utils/agentMessageFormatting'; import { formatAgentRole } from '@renderer/utils/formatAgentRole'; import { getModifierKeyName } from '@renderer/utils/keyboardUtils'; import { formatDistanceToNow } from 'date-fns'; import { ChevronDown, ChevronUp, MessageSquare, Reply, Send, X } from 'lucide-react'; import type { MentionSuggestion } from '@renderer/types/mention'; import type { ResolvedTeamMember, TaskComment } from '@shared/types'; const MAX_COMMENT_LENGTH = 2000; interface TaskCommentsSectionProps { teamName: string; taskId: string; comments: TaskComment[]; members: ResolvedTeamMember[]; } export const TaskCommentsSection = ({ teamName, taskId, comments, members, }: TaskCommentsSectionProps): React.JSX.Element => { const addTaskComment = useStore((s) => s.addTaskComment); const addingComment = useStore((s) => s.addingComment); const commentsRef = useMarkCommentsRead(teamName, taskId, comments); const [replyTo, setReplyTo] = useState<{ author: string; text: string } | null>(null); const [expandedCommentIds, setExpandedCommentIds] = useState>(new Set()); const toggleCommentExpanded = useCallback((commentId: string) => { setExpandedCommentIds((prev) => { const next = new Set(prev); if (next.has(commentId)) next.delete(commentId); else next.add(commentId); return next; }); }, []); const draft = useDraftPersistence({ key: `taskComment:${teamName}:${taskId}` }); const mentionSuggestions = useMemo( () => members.map((m) => ({ id: m.name, name: m.name, subtitle: formatAgentRole(m.role) ?? formatAgentRole(m.agentType) ?? undefined, color: m.color, })), [members] ); const trimmed = draft.value.trim(); const remaining = MAX_COMMENT_LENGTH - trimmed.length; const canSubmit = trimmed.length > 0 && trimmed.length <= MAX_COMMENT_LENGTH && !addingComment; const handleSubmit = useCallback(async () => { if (!canSubmit) return; try { const text = replyTo ? buildReplyBlock(replyTo.author, replyTo.text, trimmed) : trimmed; await addTaskComment(teamName, taskId, text); draft.clearDraft(); setReplyTo(null); } catch { // Error is stored in addCommentError via store } }, [canSubmit, addTaskComment, teamName, taskId, trimmed, draft, replyTo]); return (
Comments {comments.length > 0 ? ( {comments.length} ) : null}
{comments.length > 0 ? (
{comments.map((comment) => (
m.name === comment.author)?.color ?? 'var(--color-text-secondary)'), }} > {comment.author} {(() => { const date = new Date(comment.createdAt); return isNaN(date.getTime()) ? 'unknown time' : formatDistanceToNow(date, { addSuffix: true }); })()}
{(() => { const reply = parseMessageReply(comment.text); const expanded = expandedCommentIds.has(comment.id); return reply ? ( ) : ( ); })()}
))}
) : null} {replyTo ? (
Replying to{' '} m.name === replyTo.author)?.color ?? 'var(--color-text-secondary)'), }} > @{replyTo.author}
{replyTo.text}
Cancel reply
) : null}
void handleSubmit()} > Comment } footerRight={
{remaining < 200 ? ( {remaining} chars left ) : null} {draft.isSaved ? ( Draft saved ) : null}
} />
); };