:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import clsx from 'clsx'
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
import { useEffect, useMemo, useRef } from 'react'
import { IconChevronRight, IconEllipsis, IconEye, IconPencil, IconShare, IconTrash } from '@posthog/icons'
import { LemonButton, LemonCheckbox, LemonMenu, LemonTag, ProfilePicture, Tooltip } from '@posthog/lemon-ui'
import { SentenceList } from 'lib/components/ActivityLog/SentenceList'
import { EmojiPickerPopover } from 'lib/components/EmojiPicker/EmojiPickerPopover'
import { LemonRichContentEditor } from 'lib/components/RichContentEditor/LemonRichContentEditor'
import { TZLabel } from 'lib/components/TZLabel'
import { FEATURE_FLAGS } from 'lib/constants'
import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { IconSlack } from 'lib/lemon-ui/icons'
import { KeyboardShortcut } from 'lib/lemon-ui/KeyboardShortcut/KeyboardShortcut'
import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
import { LemonMarkdown } from 'lib/lemon-ui/LemonMarkdown'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { colonDelimitedDuration } from 'lib/utils/durations'
import { pluralize } from 'lib/utils/strings'
import { CommentType } from '~/types'
import { CommentComposer } from './CommentComposer'
import { CommentsLogicProps, CommentWithRepliesType, commentsLogic } from './commentsLogic'
import { getCommentText, getRecordingLinkInfo, isViewingRecording, scopeSyncsToSlack } from './commentUtils'
import { sendCommentToSlackLogic } from './sendCommentToSlackLogic'
// Comments that came in from a synced Slack thread have no PostHog author; their Slack identity
// rides in item_context.
export function getCommentAuthorName(comment: CommentType): string {
if (comment.created_by) {
return comment.created_by.first_name ?? 'Unknown user'
}
if (comment.item_context?.from_slack) {
return comment.item_context.slack_author_name ?? 'Slack user'
}
return 'Unknown user'
}
export type CommentProps = {
commentWithReplies: CommentWithRepliesType
/** Mounts the inline reply composer at the thread's bottom while it is the reply target */
composerLogicProps: CommentsLogicProps
}
const CommentBottomRow = ({ comment }: { comment: CommentType }): JSX.Element | null => {
const { emojiReactionsByComment, isMyComment } = useValues(commentsLogic)
const { deleteComment, sendEmojiReaction } = useActions(commentsLogic)
const reactions = emojiReactionsByComment[comment.id] || {}
const recordingLinkInfo = getRecordingLinkInfo(comment)
const handleViewInRecording = (): void => {
if (!recordingLinkInfo) {
return
}
if (isViewingRecording(recordingLinkInfo.recordingId) && recordingLinkInfo.unixTimestampMillis) {
router.actions.push(recordingLinkInfo.url)
} else {
window.location.href = recordingLinkInfo.url
}
}
let timeInRecordingLabel: string | null = null
if (comment.item_context?.milliseconds_into_recording !== undefined) {
timeInRecordingLabel = colonDelimitedDuration(comment.item_context?.milliseconds_into_recording / 1000, null)
}
// Keep comment cards slim: skip the whole row when there is nothing to show in it
if (!recordingLinkInfo && !comment.version && !Object.keys(reactions).length) {
return null
}
return (
<div className="flex flex-row items-center justify-between">
<div className="flex flex-row items-center gap-1">
{recordingLinkInfo ? (
<LemonButton
icon={<IconEye />}
size="xsmall"
type="tertiary"
onClick={handleViewInRecording}
tooltip="View in recording"
data-attr="view-comment-in-recording-at-timestamp"
>
{timeInRecordingLabel}
</LemonButton>
) : null}
<span className="text-xs text-secondary italic">{comment.version ? <span>(edited)</span> : null}</span>
</div>
<div className="flex items-center">
<div data-attr="comment-reactions" className="flex items-center">
{Object.entries(reactions).map(([emoji, commentList]) => (
<LemonButton
key={emoji}
type="tertiary"
onClick={() => {
const existingCurrentUserReaction = commentList.find((emojiReaction) =>
isMyComment(emojiReaction)
)
if (existingCurrentUserReaction) {
deleteComment(existingCurrentUserReaction)
} else {
sendEmojiReaction(emoji, comment.id)
}
}}
size="small"
data-attr={`comment-reaction-${emoji}`}
tooltip={
<div className="flex flex-col gap-">
<div className="text-2xl">{emoji}</div>
<SentenceList
listParts={commentList.map((c) =>
isMyComment(c) ? 'you' : (c.created_by?.first_name ?? 'Unknown user')
)}
/>
</div>
}
>
<div className="flex flex-row gap-1 items-center">
<span>{emoji}</span>
<span className="text-xs font-semibold">{commentList.length}</span>
</div>
</LemonButton>
))}
</div>
</div>
</div>
)
}
const CommentEditingForm = ({ comment }: { comment: CommentType }): JSX.Element => {
const { editingComment, isSavingEditedComment, editingCommentRichContentEditor, isEditingCommentEmpty } =
useValues(commentsLogic)
const {
setEditingComment,
persistEditedComment,
setEditingCommentRichContentEditor,
onEditingCommentRichContentEditorUpdate,
} = useActions(commentsLogic)
return (
<div className="deprecated-space-y-2">
<LemonRichContentEditor
placeholder="Edit comment"
// Seed from the in-progress edit so collapsing/expanding the thread mid-edit loses nothing
initialContent={editingComment?.rich_content ?? comment.rich_content}
onCreate={setEditingCommentRichContentEditor}
onUpdate={(isEmpty) => {
if (editingCommentRichContentEditor && editingComment) {
setEditingComment({
...editingComment,
rich_content: editingCommentRichContentEditor.getJSON(),
})
onEditingCommentRichContentEditorUpdate(isEmpty)
}
}}
onPressCmdEnter={persistEditedComment}
disabled={isSavingEditedComment}
/>
<div className="flex justify-end items-center gap-2">
<LemonButton
type="secondary"
size="small"
onClick={() => {
setEditingComment(null)
setEditingCommentRichContentEditor(null)
}}
disabled={isSavingEditedComment}
>
Cancel
</LemonButton>
<LemonButton
type="primary"
size="small"
onClick={persistEditedComment}
disabledReason={isEditingCommentEmpty ? 'No message' : isSavingEditedComment ? 'Saving...' : null}
sideIcon={<KeyboardShortcut command enter />}
>
Save
</LemonButton>
</div>
</div>
)
}
const CommentTopRow = ({ comment }: { comment: CommentType }): JSX.Element => {
const { disabledReasonFor, sendToSlackDisabledReasonFor } = useValues(commentsLogic)
const { deleteComment, setEditingComment, sendEmojiReaction, setReplyingComment } = useActions(commentsLogic)
const { featureFlags } = useValues(featureFlagLogic)
const { openModal } = useActions(sendCommentToSlackLogic)
const isCompleted = !!comment.completed_at
const fromSlack = !!comment.item_context?.from_slack
const slackThread = comment.slack_thread
// Only an untracked top-level comment (a thread root) can be sent to Slack; replies follow their parent.
const canSendToSlack =
featureFlags[FEATURE_FLAGS.DISCUSSIONS_SLACK_SYNC] &&
scopeSyncsToSlack(comment.scope) &&
!comment.source_comment &&
!fromSlack &&
!slackThread
return (
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2">
<span className="ph-no-capture flex-1 font-semibold">{getCommentAuthorName(comment)}</span>
{fromSlack ? (
<LemonTag size="small" type="muted">
via Slack
</LemonTag>
) : null}
{comment.is_task ? (
<LemonTag size="small" type={isCompleted ? 'success' : 'warning'}>
{isCompleted ? 'Completed' : 'Task'}
</LemonTag>
) : null}
</div>
<div className="flex items-center gap-1">
{slackThread ? (
<LemonButton
icon={<IconSlack />}
size="xsmall"
to={slackThread.url}
targetBlank
tooltip="Open synced Slack discussion"
data-attr="discussions-comment-open-in-slack"
>
{slackThread.channel_name ? `#${slackThread.channel_name}` : 'Open in Slack'}
</LemonButton>
) : null}
{comment.created_at ? (
<span className="text-xs">
<TZLabel time={comment.created_at} />
</span>
) : null}
{/* has-[…active] holds the trigger visible while its picker is open - focus sits in the portal, so focus-within can't */}
<span className="opacity-0 transition-opacity group-hover:opacity-100 focus-within:opacity-100 has-[.LemonButton--active]:opacity-100">
<EmojiPickerPopover
size="xsmall"
onSelect={(emoji: string): void => {
sendEmojiReaction(emoji, comment.id)
}}
data-attr="comment-react-button"
/>
</span>
<LemonMenu
items={[
{
icon: <IconShare />,
label: 'Reply',
onClick: () => setReplyingComment(comment.source_comment ?? comment.id),
},
...(canSendToSlack
? [
{
icon: <IconSlack />,
label: 'Send to Slack',
onClick: () => openModal(comment),
disabledReason: sendToSlackDisabledReasonFor(comment),
},
]
: []),
{
icon: <IconPencil />,
label: 'Edit',
onClick: () => setEditingComment(comment),
},
{
icon: <IconTrash />,
label: 'Delete',
onClick: () => deleteComment(comment),
disabledReason: disabledReasonFor(comment),
},
]}
>
<LemonButton icon={<IconEllipsis />} size="xsmall" />
</LemonMenu>
</div>
</div>
)
}
const Comment = ({ comment }: { comment: CommentType }): JSX.Element => {
const { editingComment, selectedCommentId, commentContexts } = useValues(commentsLogic)
const { setSelectedComment, completeComment, reopenComment } = useActions(commentsLogic)
const contextText = commentContexts[comment.id]
const isInlineComment = comment.item_context?.type === 'mark'
const ref = useRef<HTMLDivElement | null>(null)
const isEditing = editingComment?.id === comment.id
// Selection-driven so deep links can highlight their target even while a reply is open;
// the reply target already shows via the card's accent border
const isHighlighted = selectedCommentId === comment.id || isEditing
const threadId = comment.source_comment ?? comment.id
// Rendering markdown from tiptap JSON is not free - skip it on unrelated re-renders
const text = useMemo(() => getCommentText(comment), [comment])
useEffect(() => {
if (isHighlighted) {
ref.current?.scrollIntoView({ block: 'nearest' })
}
}, [isHighlighted])
return (
<div
ref={ref}
className={clsx('Comment group px-2 py-1', isHighlighted && 'bg-fill-highlight-50')}
data-comment-id={comment.id}
// Selection is not a visual focus: it drives the notebook mark highlight and deep links
onClick={isEditing ? undefined : () => setSelectedComment(threadId)}
>
<div className="flex items-center gap-3">
{comment.is_task ? (
<>
<Tooltip
title={
comment.completed_at
? `Completed by ${comment.completed_by?.first_name ?? 'Unknown user'}`
: 'Mark as complete'
}
>
<span className="flex items-center scale-125 ml-1">
<LemonCheckbox
checked={!!comment.completed_at}
onChange={() =>
comment.completed_at ? reopenComment(comment) : completeComment(comment)
}
data-attr="comment-task-checkbox"
/>
</span>
</Tooltip>
<LemonDivider vertical className="self-stretch" />
</>
) : null}
<div className="flex flex-col justify-start gap-2 flex-1 min-w-0">
<div className="flex-1 flex justify-start items-start gap-2">
<ProfilePicture size={comment.source_comment ? 'md' : 'xl'} user={comment.created_by} />
<div className="flex flex-col flex-1 min-w-0">
<CommentTopRow comment={comment} />
{contextText && !isEditing && isInlineComment && (
<div className="border-l-2 border-border pl-2 my-2">
<span className="block text-muted truncate text-sm">{contextText}</span>
</div>
)}
{isEditing ? (
<CommentEditingForm comment={comment} />
) : (
<div className={clsx(comment.completed_at && 'line-through text-secondary')}>
<LemonMarkdown lowKeyHeadings>{text}</LemonMarkdown>
</div>
)}
</div>
</div>
{!isEditing && <CommentBottomRow comment={comment} />}
</div>
</div>
</div>
)
}
const InlineReplyComposer = ({ logicProps }: { logicProps: CommentsLogicProps }): JSX.Element => {
const ref = useRef<HTMLDivElement | null>(null)
useOnMountEffect(() => {
// In long threads the composer mounts below the fold at the thread's bottom
ref.current?.scrollIntoView({ block: 'nearest' })
})
return (
<div ref={ref}>
<CommentComposer {...logicProps} variant="inline-reply" />
</div>
)
}
export const CommentWithReplies = ({ commentWithReplies, composerLogicProps }: CommentProps): JSX.Element => {
const { comment, replies } = commentWithReplies
const { replyingCommentId, expandedThreadIds, editingComment, itemContext } = useValues(commentsLogic)
const { setReplyingComment, setThreadExpanded } = useActions(commentsLogic)
// replyingCommentId always resolves to the thread root, so this only matches top-level threads
const isReplyTarget = replyingCommentId === commentWithReplies.id
// expandedThreadIds always includes the reply target, so the composer is never collapsed away
const isExpanded = expandedThreadIds.has(commentWithReplies.id)
const canToggle = editingComment?.id !== commentWithReplies.id
// Hidden only while the composer is open - the composer takes over as the reply affordance
const replyButton = !isReplyTarget ? (
<LemonButton
size="xsmall"
onClick={() => setReplyingComment(commentWithReplies.id)}
data-attr="comment-reply-button"
>
Reply
</LemonButton>
) : null
// TODO: Permissions
return (
<div className={clsx('border rounded-lg bg-surface-primary overflow-hidden', isReplyTarget && 'border-accent')}>
<div
className={canToggle ? 'cursor-pointer' : undefined}
data-attr={canToggle ? 'comment-thread-toggle' : undefined}
onClick={
canToggle
? (e) => {
const target = e.target as HTMLElement
// Popover content (emoji picker etc.) bubbles clicks here through its
// portal without being a DOM descendant of the card - never toggle on those
if (!e.currentTarget.contains(target)) {
return
}
// Leave clicks on inner controls and text selections alone
if (target.closest('button, a, label, input, textarea, [contenteditable="true"]')) {
return
}
if (window.getSelection()?.toString()) {
return
}
// Open = replies visible with a focused composer; closing puts both away
if (isExpanded) {
if (isReplyTarget) {
setReplyingComment(null)
}
setThreadExpanded(commentWithReplies.id, false)
} else if (itemContext) {
// An in-progress anchored comment survives a peek - only the
// explicit Reply button trades the anchor for a reply
setThreadExpanded(commentWithReplies.id, true)
} else {
setReplyingComment(commentWithReplies.id)
}
}
: undefined
}
>
{comment ? (
<Comment comment={comment} />
) : (
<div className="px-2 py-1 font-semibold italic text-secondary">Deleted comment</div>
)}
{replies.length > 0 ? (
<>
<LemonDivider className="my-0" />
<div className="flex items-center px-2 py-1 text-xs text-secondary">
{/* Keyboard path to the toggle - Enter/Space expands or collapses without composing */}
<div
className="flex flex-1 items-center gap-1"
role="button"
tabIndex={0}
aria-expanded={isExpanded}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
if (isExpanded && isReplyTarget) {
// The reply pin keeps the thread open - end the reply so collapse takes effect
setReplyingComment(null)
}
setThreadExpanded(commentWithReplies.id, !isExpanded)
}
}}
>
<IconChevronRight
className={clsx('size-3 shrink-0 transition-transform', isExpanded && 'rotate-90')}
/>
<span>{pluralize(replies.length, 'reply', 'replies')}</span>
</div>
{/* A sibling of the keyboard toggle, never a descendant - interactive content must not
nest. Once expanded, the reply affordance moves to the thread's bottom */}
{!isExpanded ? replyButton : null}
</div>
</>
) : null}
</div>
{isExpanded ? replies.map((reply) => <Comment key={reply.id} comment={reply} />) : null}
{isReplyTarget ? (
<>
<LemonDivider className="my-0" />
<div className="p-2">
<InlineReplyComposer logicProps={composerLogicProps} />
</div>
</>
) : isExpanded || replies.length === 0 ? (
<>
<LemonDivider className="my-0" />
<div className="flex justify-end px-2 py-1">{replyButton}</div>
</>
) : null}
</div>
)
}