import { useCallback, useEffect, useMemo, useState } from 'react'; import { MarkdownViewer } from '@renderer/components/chat/viewers/MarkdownViewer'; import { ReplyQuoteBlock } from '@renderer/components/team/activity/ReplyQuoteBlock'; import { ImageLightbox } from '@renderer/components/team/attachments/ImageLightbox'; import { MemberBadge } from '@renderer/components/team/MemberBadge'; import { ExpandableContent } from '@renderer/components/ui/ExpandableContent'; 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 { isImageMimeType } from '@renderer/utils/attachmentUtils'; import { formatAgentRole } from '@renderer/utils/formatAgentRole'; import { getModifierKeyName } from '@renderer/utils/keyboardUtils'; import { buildMemberColorMap } from '@renderer/utils/memberHelpers'; import { stripAgentBlocks } from '@shared/constants/agentBlocks'; import { formatDistanceToNow } from 'date-fns'; import { CheckCircle2, Eye, File, Loader2, MessageSquare, Reply, Send, X } from 'lucide-react'; import type { MentionSuggestion } from '@renderer/types/mention'; import type { ResolvedTeamMember, TaskAttachmentMeta, TaskComment } from '@shared/types'; /** * Convert literal backslash-n sequences to real newlines. * CLI tools (teamctl.js) may store `\n` as literal text when * shell double-quotes don't interpret escape sequences. */ function normalizeLiteralNewlines(text: string): string { return text.replace(/\\n/g, '\n').replace(/\\t/g, '\t'); } const MAX_COMMENT_LENGTH = 2000; const INITIAL_VISIBLE_COMMENTS = 30; const VISIBLE_COMMENTS_STEP = 50; const MAX_COMMENTS_TO_RENDER = 2000; interface TaskCommentsSectionProps { teamName: string; taskId: string; comments: TaskComment[]; members: ResolvedTeamMember[]; /** When true, the "Comments" header is not rendered (e.g. inside a collapsible section). */ hideHeader?: boolean; /** When true, the comment input area is not rendered (useful when input is rendered externally). */ hideInput?: boolean; /** Called when the user clicks Reply on a comment (used when input is rendered externally). */ onReply?: (author: string, text: string) => void; /** Called when a task ID link (e.g. #10) is clicked in comment text. */ onTaskIdClick?: (taskId: string) => void; /** Extra className on the outer comments container (e.g. negative margins for edge-to-edge). */ containerClassName?: string; } /** Convert `#` in plain text to markdown links with task:// protocol. */ function linkifyTaskIdsInMarkdown(text: string): string { return text.replace(/#(\d+)/g, '[#$1](task://$1)'); } /** Convert `@memberName` to markdown links with mention:// protocol for colored badge rendering. */ function linkifyMentionsInMarkdown(text: string, memberColorMap: Map): string { if (memberColorMap.size === 0) return text; const names = [...memberColorMap.keys()].sort((a, b) => b.length - a.length); const escaped = names.map((n) => n.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); const pattern = new RegExp(`(^|\\s)@(${escaped.join('|')})(?=[\\s,.:;!?)\\]}-]|$)`, 'gi'); return text.replace(pattern, (_match, prefix: string, name: string) => { const canonical = names.find((n) => n.toLowerCase() === name.toLowerCase()) ?? name; const color = memberColorMap.get(canonical) ?? ''; return `${prefix}[@${canonical}](mention://${encodeURIComponent(color)}/${encodeURIComponent(canonical)})`; }); } export const TaskCommentsSection = ({ teamName, taskId, comments, members, hideHeader = false, hideInput = false, onReply, onTaskIdClick, containerClassName, }: 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 [visibleCount, setVisibleCount] = useState(INITIAL_VISIBLE_COMMENTS); const [previewImageUrl, setPreviewImageUrl] = useState(null); // Reset local UI state when team/task changes. useEffect(() => { // eslint-disable-next-line react-hooks/set-state-in-effect -- intentional sync on prop change setVisibleCount(INITIAL_VISIBLE_COMMENTS); setReplyTo(null); setPreviewImageUrl(null); }, [teamName, taskId]); const draft = useDraftPersistence({ key: `taskComment:${teamName}:${taskId}` }); const colorMap = useMemo(() => buildMemberColorMap(members), [members]); const cappedComments = useMemo(() => { if (comments.length <= MAX_COMMENTS_TO_RENDER) return comments; // In extreme cases, rendering thousands of markdown blocks can freeze the renderer. // Keep the UI responsive by showing only the most recent subset. return comments.slice(-MAX_COMMENTS_TO_RENDER); }, [comments]); const sortedComments = useMemo(() => { const list = [...cappedComments]; list.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); return list; }, [cappedComments]); const visibleComments = useMemo( () => sortedComments.slice(0, Math.min(visibleCount, sortedComments.length)), [sortedComments, visibleCount] ); const mentionSuggestions = useMemo( () => members.map((m) => ({ id: m.name, name: m.name, subtitle: formatAgentRole(m.role) ?? formatAgentRole(m.agentType) ?? undefined, color: colorMap.get(m.name), })), [members, colorMap] ); 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 (
{!hideHeader ? (
Comments {comments.length > 0 ? ( {comments.length} ) : null}
) : null} {comments.length > 0 ? (
{comments.length > MAX_COMMENTS_TO_RENDER ? (
Showing the most recent {MAX_COMMENTS_TO_RENDER.toLocaleString()} comments to keep the UI responsive.
) : null}
{visibleComments.map((comment, index) => (
{comment.type === 'review_approved' ? ( Approved ) : comment.type === 'review_request' ? ( Review requested ) : null} {(() => { const date = new Date(comment.createdAt); return isNaN(date.getTime()) ? 'unknown time' : formatDistanceToNow(date, { addSuffix: true }); })()} Reply to comment
{(() => { const reply = parseMessageReply(comment.text); const rawForDisplay = reply ? reply.replyText : comment.text; const displayText = normalizeLiteralNewlines(stripAgentBlocks(rawForDisplay)); return ( {reply ? ( ) : ( { const link = (e.target as HTMLElement).closest( 'a[href^="task://"]' ); if (link) { e.preventDefault(); e.stopPropagation(); const id = link.getAttribute('href')?.replace('task://', ''); if (id) onTaskIdClick(id); } } : undefined } > { let t = linkifyTaskIdsInMarkdown(displayText); if (colorMap.size > 0) t = linkifyMentionsInMarkdown(t, colorMap); return t; })()} maxHeight="max-h-none" bare /> )} ); })()} {comment.attachments && comment.attachments.length > 0 ? ( ) : null}
))}
{sortedComments.length > visibleComments.length ? (
) : null}
) : null} {/* Image lightbox */} {previewImageUrl ? ( setPreviewImageUrl(null)} src={previewImageUrl} alt="Attachment preview" /> ) : null} {!hideInput && ( <> {replyTo ? (
Replying to
{replyTo.text}
Cancel reply
) : null}
void handleSubmit()} minRows={2} maxRows={8} maxLength={MAX_COMMENT_LENGTH} disabled={addingComment} cornerAction={ } footerRight={
{remaining < 200 ? ( {remaining} chars left ) : null} {draft.isSaved ? ( Draft saved ) : null}
} />
)}
); }; // --------------------------------------------------------------------------- // Comment attachment thumbnail (read-only, no delete) // --------------------------------------------------------------------------- interface CommentAttachmentThumbnailProps { attachment: TaskAttachmentMeta; teamName: string; taskId: string; onPreview: (dataUrl: string) => void; } const CommentAttachmentThumbnail = ({ attachment, teamName, taskId, onPreview, }: CommentAttachmentThumbnailProps): React.JSX.Element => { const getTaskAttachmentData = useStore((s) => s.getTaskAttachmentData); const [thumbUrl, setThumbUrl] = useState(null); const [downloading, setDownloading] = useState(false); const [downloadError, setDownloadError] = useState(null); useEffect(() => { let cancelled = false; void (async () => { try { if (!isImageMimeType(attachment.mimeType)) return; const base64 = await getTaskAttachmentData( teamName, taskId, attachment.id, attachment.mimeType ); if (!cancelled && base64) { setThumbUrl(`data:${attachment.mimeType};base64,${base64}`); } } catch { // ignore — thumbnail simply won't render } })(); return () => { cancelled = true; }; }, [teamName, taskId, attachment.id, attachment.mimeType, getTaskAttachmentData]); return (
{ if (isImageMimeType(attachment.mimeType)) { if (thumbUrl) onPreview(thumbUrl); return; } void (async () => { setDownloading(true); setDownloadError(null); try { const base64 = await getTaskAttachmentData( teamName, taskId, attachment.id, attachment.mimeType ); if (!base64) return; const mime = attachment.mimeType && typeof attachment.mimeType === 'string' ? attachment.mimeType : 'application/octet-stream'; const dataUrl = `data:${mime};base64,${base64}`; const blob = await fetch(dataUrl).then((r) => r.blob()); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = attachment.filename || 'attachment'; document.body.appendChild(a); a.click(); a.remove(); URL.revokeObjectURL(url); } catch (err) { setDownloadError(err instanceof Error ? err.message : 'Download failed'); } finally { setDownloading(false); } })(); }} > {isImageMimeType(attachment.mimeType) ? ( thumbUrl ? ( {attachment.filename} ) : ( ) ) : downloading ? ( ) : ( )}
{attachment.filename}
{downloadError ? ( {downloadError} ) : ( {attachment.filename} )}
); }; // --------------------------------------------------------------------------- // Comment attachments grid // --------------------------------------------------------------------------- interface CommentAttachmentsProps { attachments: TaskAttachmentMeta[]; teamName: string; taskId: string; onPreview: (dataUrl: string) => void; } const CommentAttachments = ({ attachments, teamName, taskId, onPreview, }: CommentAttachmentsProps): React.JSX.Element => (
{attachments.map((att) => ( ))}
);