: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
feat(notebooks): show comment previews in side-panel (#57339)
Alex Liderf7064746e235
diff --git a/frontend/src/scenes/comments/Comment.tsx b/frontend/src/scenes/comments/Comment.tsxindex b77c9adc8b12..8b8eb747f382 100644--- a/frontend/src/scenes/comments/Comment.tsx+++ b/frontend/src/scenes/comments/Comment.tsx@@ -223,8 +223,10 @@ const CommentTopRow = ({ comment }: { comment: CommentType }): JSX.Element => {}const Comment = ({ comment }: { comment: CommentType }): JSX.Element => {- const { editingComment, replyingCommentId, selectedCommentId } = useValues(commentsLogic)+ const { editingComment, replyingCommentId, selectedCommentId, commentContexts } = useValues(commentsLogic)const { setSelectedComment } = useActions(commentsLogic)+ const contextText = commentContexts[comment.id]+ const isInlineComment = comment.item_context?.type === 'mark'const ref = useRef<HTMLDivElement | null>(null)@@ -255,6 +257,11 @@ const Comment = ({ comment }: { comment: CommentType }): JSX.Element => {<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} />) : (diff --git a/frontend/src/scenes/comments/commentsLogic.ts b/frontend/src/scenes/comments/commentsLogic.tsindex 178eb35bc8d5..100edddd1394 100644--- a/frontend/src/scenes/comments/commentsLogic.ts+++ b/frontend/src/scenes/comments/commentsLogic.ts@@ -73,6 +73,7 @@ export const commentsLogic = kea<commentsLogicType>([setEditingComment: (comment: CommentType | null) => ({ comment }),setReplyingComment: (commentId: string | null) => ({ commentId }),setSelectedComment: (commentId: string | null) => ({ commentId }),+ setCommentContexts: (contexts: Record<string, string>) => ({ contexts }),setItemContext: (context: Record<string, any> | null, callback?: (event: { sent: boolean }) => void) => ({context,callback,@@ -96,6 +97,12 @@ export const commentsLogic = kea<commentsLogicType>([sendComposedContentSuccess: () => null,},],+ commentContexts: [+ {} as Record<string, string>,+ {+ setCommentContexts: (_, { contexts }) => contexts,+ },+ ],itemContext: [null as CommentContext | null,{diff --git a/frontend/src/scenes/notebooks/Notebook/Editor.tsx b/frontend/src/scenes/notebooks/Notebook/Editor.tsxindex 7a8431b7c3b5..d55676aafbb0 100644--- a/frontend/src/scenes/notebooks/Notebook/Editor.tsx+++ b/frontend/src/scenes/notebooks/Notebook/Editor.tsx@@ -205,6 +205,7 @@ export function Editor(): JSX.Element {const notebookEditor: NotebookEditor = {...createEditor(editor),findCommentPosition: (markId: string) => findCommentPosition(editor, markId),+ getAllCommentTexts: () => getAllCommentTexts(editor),removeComment: (pos: number) => removeCommentMark(editor, pos),getText: () => textContent(editor.state.doc),}@@ -261,6 +262,21 @@ function findCommentPosition(editor: TTEditor, markId: string): number | null {return result}+function getAllCommentTexts(editor: TTEditor): Record<string, string> {+ const result: Record<string, string> = {}+ editor.state.doc.descendants((node) => {+ if (!node.isText) {+ return+ }+ for (const m of node.marks) {+ if (m.type.name === 'comment' && m.attrs.id) {+ result[m.attrs.id] = (result[m.attrs.id] ?? '') + (node.text ?? '')+ }+ }+ })+ return result+}+function removeCommentMark(editor: TTEditor, pos: number): void {editor.chain()diff --git a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts b/frontend/src/scenes/notebooks/Notebook/notebookLogic.tsindex 1d1cf4c6423b..5cea341d5b91 100644--- a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts+++ b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts@@ -101,6 +101,21 @@ async function runWhenEditorIsReady(waitForEditor: () => boolean, fn: () => any)return fn()}+function buildCommentContexts(editor: NotebookEditor, comments: CommentType[]): Record<string, string> {+ const markTexts = editor.getAllCommentTexts()+ const contexts: Record<string, string> = {}+ for (const comment of comments) {+ if (comment.source_comment || comment.item_context?.type !== 'mark') {+ continue+ }+ const text = markTexts[comment.item_context.id]+ if (text) {+ contexts[comment.id] = text+ }+ }+ return contexts+}+export const notebookLogic = kea<notebookLogicType>([props({} as NotebookLogicProps),path((key) => ['scenes', 'notebooks', 'Notebook', 'notebookLogic', key]),@@ -116,7 +131,7 @@ export const notebookLogic = kea<notebookLogicType>([scope: ActivityScope.NOTEBOOK,item_id: props.shortId,}),- ['comments', 'itemContext', 'selectedCommentId'],+ ['comments', 'itemContext', 'selectedCommentId', 'commentContexts'],notebookKernelInfoLogic({ shortId: props.shortId }),['kernelInfo'],notebookSettingsLogic,@@ -133,7 +148,7 @@ export const notebookLogic = kea<notebookLogicType>([scope: ActivityScope.NOTEBOOK,item_id: props.shortId,}),- ['setItemContext', 'maybeLoadComments', 'setSelectedComment'],+ ['setItemContext', 'maybeLoadComments', 'setSelectedComment', 'setCommentContexts'],notebookCollabLogic({ shortId: props.shortId }),['ackLocalSteps', 'applyRemoteSteps'],],@@ -884,7 +899,26 @@ export const notebookLogic = kea<notebookLogicType>([cache.throttledOnUpdateEditorTimeout = null}, 16) // ~60fps throttling},- setEditor: () => {},+ setEditor: () => {+ // Compute contexts immediately if comments are already loaded when the editor mounts+ if (values.editor && values.comments) {+ actions.setCommentContexts(buildCommentContexts(values.editor, values.comments))+ }+ },+ onUpdateEditor: () => {+ // Re-sync previews so they track edits to text under comment marks.+ // Skip the dispatch when nothing changed to avoid re-rendering every Comment per keystroke.+ if (!values.editor || !values.comments) {+ return+ }+ const next = buildCommentContexts(values.editor, values.comments)+ const prev = values.commentContexts+ const nextKeys = Object.keys(next)+ if (nextKeys.length === Object.keys(prev).length && nextKeys.every((k) => prev[k] === next[k])) {+ return+ }+ actions.setCommentContexts(next)+ },saveNotebookSuccess: actions.scheduleNotebookRefresh,loadNotebookSuccess: () => {@@ -1014,6 +1048,8 @@ export const notebookLogic = kea<notebookLogicType>([editor.removeComment(mark.pos)}})++ actions.setCommentContexts(buildCommentContexts(editor, comments))}},activeCommentMarkId: (markId: string | null) => {diff --git a/frontend/src/scenes/notebooks/types.ts b/frontend/src/scenes/notebooks/types.tsindex bc045684c6f3..d05291729dc3 100644--- a/frontend/src/scenes/notebooks/types.ts+++ b/frontend/src/scenes/notebooks/types.ts@@ -155,6 +155,7 @@ export type NotebookNodeAction = Pick<LemonButtonProps, 'icon'> & {export interface NotebookEditor extends RichContentEditorType {findCommentPosition: (markId: string) => number | null+ getAllCommentTexts: () => Record<string, string>removeComment: (pos: number) => voidgetText: () => string}