{parts}
+ ) +} diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-comments/comment-composer.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-comments/comment-composer.tsx new file mode 100644 index 00000000000..3593721231f --- /dev/null +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-comments/comment-composer.tsx @@ -0,0 +1,325 @@ +'use client' + +import { type KeyboardEvent, useLayoutEffect, useMemo, useRef, useState } from 'react' +import { + Button, + ComposerActionButton, + chipFieldSurfaceClass, + chipFieldTextClass, + cn, +} from '@sim/emcn' +import { ArrowUp } from '@sim/emcn/icons' +import { + FILE_COMMENT_BODY_MAX_CHARS, + formatPersonMention, + parseCommentBody, +} from '@/lib/workspace-files/comments/format' +import { UserAvatar } from '@/app/workspace/[workspaceId]/components/user-avatar/user-avatar' +import { + SUGGESTION_ITEM_CLASS, + SUGGESTION_SCROLL_CLASS, + SUGGESTION_SURFACE_CLASS, +} from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/suggestion-menu-chrome' +import { useSuggestionKeyboard } from '@/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/use-suggestion-keyboard' +import type { WorkspaceMember } from '@/hooks/queries/workspace' + +/** Most people listed under an `@` at once; typing more of a name narrows the rest in. */ +const MAX_SUGGESTIONS = 6 +/** An `@` that starts a word, and what has been typed after it, right before the caret. */ +const MENTION_QUERY_RE = /(^|\s)@([^\s@]{0,40})$/ +/** Tallest the box grows while typing before it scrolls instead. */ +const MAX_INPUT_HEIGHT_PX = 160 + +interface MentionQuery { + /** Index of the `@` in the text. */ + start: number + query: string +} + +/** A person picked from the list, at the `@Name` it occupies in the composer's text. */ +interface PlacedMention { + start: number + name: string + userId: string +} + +function mentionEnd(mention: PlacedMention): number { + return mention.start + mention.name.length + 1 +} + +/** A stored body as the composer shows it: each mention becomes `@Name` at a known position. */ +function toDraft(body: string): { text: string; mentions: PlacedMention[] } { + let text = '' + const mentions: PlacedMention[] = [] + for (const segment of parseCommentBody(body)) { + if (segment.kind === 'mention') { + mentions.push({ start: text.length, name: segment.text, userId: segment.userId }) + text += `@${segment.text}` + } else { + text += segment.text + } + } + return { text, mentions } +} + +/** + * Carries mentions across an edit from `before` to `after`: those wholly before or after the + * changed span keep their place (shifted by the change in length), and any the edit touched are + * dropped, so retyping part of a name turns it back into plain text instead of a stale link. + */ +function carryMentions( + mentions: readonly PlacedMention[], + before: string, + after: string +): PlacedMention[] { + let prefix = 0 + const shortest = Math.min(before.length, after.length) + while (prefix < shortest && before[prefix] === after[prefix]) prefix++ + let suffix = 0 + while ( + suffix < shortest - prefix && + before[before.length - 1 - suffix] === after[after.length - 1 - suffix] + ) { + suffix++ + } + const changedEnd = before.length - suffix + const delta = after.length - before.length + return mentions.flatMap((mention) => { + if (mentionEnd(mention) <= prefix) return [mention] + if (mention.start >= changedEnd) return [{ ...mention, start: mention.start + delta }] + return [] + }) +} + +/** The composer's text as a stored body, with each placed `@Name` written as its mention link. */ +function toBody(text: string, mentions: readonly PlacedMention[]): string { + let body = text + for (const mention of [...mentions].sort((a, b) => b.start - a.start)) { + if (body.slice(mention.start, mentionEnd(mention)) !== `@${mention.name}`) continue + body = + body.slice(0, mention.start) + + formatPersonMention(mention.userId, mention.name) + + body.slice(mentionEnd(mention)) + } + return body.trim() +} + +function findMentionQuery(text: string, caret: number): MentionQuery | null { + const match = MENTION_QUERY_RE.exec(text.slice(0, caret)) + if (!match) return null + return { start: caret - match[2].length - 1, query: match[2] } +} + +interface CommentComposerProps { + members: readonly WorkspaceMember[] + placeholder: string + /** Names the send button for assistive tech, e.g. "Comment" or "Reply". */ + submitLabel: string + initialBody?: string + autoFocus?: boolean + isPending?: boolean + onSubmit: (body: string) => void + onCancel?: () => void + /** Receives the unsent body after every change, so a caller can restore it into a new composer. */ + onDraftChange?: (body: string) => void +} + +/** + * Writes a comment. Typing `@` lists the workspace's members; picking one inserts `@Name`, which is + * saved as a mention of that exact person (two members may share a name) and notifies them. + * One line that grows with the text: Enter sends, Shift-Enter starts a new line, Escape cancels. + * Read-only while sending, since the box clears or closes once the send lands. + */ +export function CommentComposer({ + members, + placeholder, + submitLabel, + initialBody, + autoFocus, + isPending = false, + onSubmit, + onCancel, + onDraftChange, +}: CommentComposerProps) { + const [initial] = useState(() => toDraft(initialBody ?? '')) + const [text, setText] = useState(initial.text) + const [mentions, setMentions] = useState(initial.mentions) + const [query, setQuery] = useState+ Keep comments under {FILE_COMMENT_BODY_MAX_CHARS.toLocaleString()} characters +
+ )} ++ Resolved by {thread.resolvedBy.name} +
+ )} + {isActive && !isResolved && ( +