Skip to content

: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.

fix(comments): stop discussion panel flickering on background poll (#87239)

Phill3538f3b62268
diff --git a/frontend/src/scenes/comments/Comment.tsx b/frontend/src/scenes/comments/Comment.tsx
index 7b9e1f74aba1..17d9c6a355bf 100644
--- a/frontend/src/scenes/comments/Comment.tsx
+++ b/frontend/src/scenes/comments/Comment.tsx
@@ -137,7 +137,7 @@ const CommentBottomRow = ({ comment }: { comment: CommentType }): JSX.Element |
}
const CommentEditingForm = ({ comment }: { comment: CommentType }): JSX.Element => {
- const { editingComment, commentsLoading, editingCommentRichContentEditor, isEditingCommentEmpty } =
+ const { editingComment, isSavingEditedComment, editingCommentRichContentEditor, isEditingCommentEmpty } =
useValues(commentsLogic)
const {
setEditingComment,
@@ -163,7 +163,7 @@ const CommentEditingForm = ({ comment }: { comment: CommentType }): JSX.Element
}
}}
onPressCmdEnter={persistEditedComment}
- disabled={commentsLoading}
+ disabled={isSavingEditedComment}
/>
<div className="flex justify-end items-center gap-2">
<LemonButton
@@ -173,7 +173,7 @@ const CommentEditingForm = ({ comment }: { comment: CommentType }): JSX.Element
setEditingComment(null)
setEditingCommentRichContentEditor(null)
}}
- disabled={commentsLoading}
+ disabled={isSavingEditedComment}
>
Cancel
</LemonButton>
@@ -181,7 +181,7 @@ const CommentEditingForm = ({ comment }: { comment: CommentType }): JSX.Element
type="primary"
size="small"
onClick={persistEditedComment}
- disabledReason={isEditingCommentEmpty ? 'No message' : commentsLoading ? 'Saving...' : null}
+ disabledReason={isEditingCommentEmpty ? 'No message' : isSavingEditedComment ? 'Saving...' : null}
sideIcon={<KeyboardShortcut command enter />}
>
Save
diff --git a/frontend/src/scenes/comments/CommentsList.test.tsx b/frontend/src/scenes/comments/CommentsList.test.tsx
new file mode 100644
index 000000000000..6a400d64a810
--- /dev/null
+++ b/frontend/src/scenes/comments/CommentsList.test.tsx
@@ -0,0 +1,65 @@
+import '@testing-library/jest-dom'
+
+import { act, render, screen, waitFor } from '@testing-library/react'
+import { Provider } from 'kea'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+import { ActivityScope } from '~/types'
+
+import { CommentsList } from './CommentsList'
+import { commentsLogic } from './commentsLogic'
+
+const PROPS = { scope: ActivityScope.TICKET, item_id: 'ticket-1' }
+
+describe('CommentsList', () => {
+ let logic: ReturnType<typeof commentsLogic.build>
+ let answerFirstLoad: () => void = () => {}
+
+ beforeEach(() => {
+ initKeaTests()
+
+ const firstLoadReached = new Promise<void>((resolve) => {
+ answerFirstLoad = resolve
+ })
+ useMocks({
+ get: {
+ '/api/projects/:team_id/comments': async () => {
+ await firstLoadReached
+ return [200, { results: [] }]
+ },
+ '/api/organizations/@current/members/': { results: [] },
+ },
+ })
+
+ logic = commentsLogic(PROPS)
+ logic.mount()
+ })
+
+ // The background poll shares one loading flag with the first fetch, so a skeleton keyed off
+ // that flag takes the empty state off screen every time the poll runs.
+ it('shows the empty state once loaded and keeps it through a background refresh', async () => {
+ render(
+ <Provider>
+ <CommentsList {...PROPS} />
+ </Provider>
+ )
+
+ // Nothing has answered yet, so there is no empty thread to claim
+ expect(screen.queryByText('Start the discussion!')).not.toBeInTheDocument()
+
+ await act(async () => {
+ answerFirstLoad()
+ })
+ await waitFor(() => expect(screen.getByText('Start the discussion!')).toBeInTheDocument())
+
+ act(() => {
+ logic.actions.refreshComments()
+ })
+ expect(logic.values.commentsLoading).toBe(true)
+ expect(screen.getByText('Start the discussion!')).toBeInTheDocument()
+
+ // Let the refresh land, so its request doesn't settle after teardown
+ await waitFor(() => expect(logic.values.commentsLoading).toBe(false))
+ })
+})
diff --git a/frontend/src/scenes/comments/CommentsList.tsx b/frontend/src/scenes/comments/CommentsList.tsx
index c2348457fbec..1c51dd4b6ca4 100644
--- a/frontend/src/scenes/comments/CommentsList.tsx
+++ b/frontend/src/scenes/comments/CommentsList.tsx
@@ -17,7 +17,7 @@ export interface CommentsListProps extends CommentsLogicProps {
}
export const CommentsList = ({ noun = 'page', ...props }: CommentsListProps): JSX.Element => {
- const { key, commentsWithReplies, commentsLoading } = useValues(commentsLogic(props))
+ const { key, commentsWithReplies, commentsInitialLoading } = useValues(commentsLogic(props))
const { loadComments, setReplyingComment, clearRichContentEditor } = useActions(commentsLogic(props))
// If the comment list focus changes we should load the comments
@@ -38,7 +38,7 @@ export const CommentsList = ({ noun = 'page', ...props }: CommentsListProps): JS
return (
<BindLogic logic={commentsLogic} props={props}>
<div className="flex flex-col">
- {!commentsWithReplies?.length && commentsLoading ? (
+ {commentsInitialLoading ? (
<div className="deprecated-space-y-2">
<LemonSkeleton className="h-10 w-full" />
</div>
diff --git a/frontend/src/scenes/comments/commentsLogic.ts b/frontend/src/scenes/comments/commentsLogic.ts
index 4dba9f417c2e..199be98621c8 100644
--- a/frontend/src/scenes/comments/commentsLogic.ts
+++ b/frontend/src/scenes/comments/commentsLogic.ts
@@ -68,6 +68,7 @@ export interface commentsLogicValues {
user: UserType | null // userLogic
commentContexts: Record<string, string>
comments: CommentType[] | null
+ commentsInitialLoading: boolean
commentsLoading: boolean
commentsWithReplies: CommentWithRepliesType[]
composerDrafts: Record<string, JSONContent | null>
@@ -84,6 +85,7 @@ export interface commentsLogicValues {
isEditingCommentEmpty: boolean
isEmpty: boolean
isMyComment: (comment: CommentType) => boolean
+ isSavingEditedComment: boolean
isSendingComment: boolean
itemContext: CommentContext | null
key: string
@@ -357,6 +359,7 @@ export interface commentsLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
key: (arg: any) => string
+ commentsInitialLoading: (comments: CommentType[] | null, commentsLoading: boolean) => boolean
sortedComments: (comments: CommentType[] | null) => CommentType[]
commentsWithReplies: (sortedComments: CommentType[]) => CommentWithRepliesType[]
emojiReactionsByComment: (sortedComments: CommentType[]) => Record<string, Record<string, CommentType[]>>
@@ -546,6 +549,14 @@ export const commentsLogic = kea<commentsLogicType>([
persistEditedCommentSuccess: () => false,
},
],
+ isSavingEditedComment: [
+ false,
+ {
+ persistEditedComment: () => true,
+ persistEditedCommentSuccess: () => false,
+ persistEditedCommentFailure: () => false,
+ },
+ ],
}),
loaders(({ props, values, actions, cache }) => ({
comments: [
@@ -873,6 +884,15 @@ export const commentsLogic = kea<commentsLogicType>([
selectors({
key: [() => [(_, props) => props], (props): string => `${props.scope}-${props.item_id || ''}`],
+
+ // `commentsLoading` is shared by every handler on the `comments` loader, including the
+ // background poll's `refreshComments`, so a surface that swaps in a skeleton on it would
+ // flash away a thread that has already loaded.
+ commentsInitialLoading: [
+ (s) => [s.comments, s.commentsLoading],
+ (comments: CommentType[] | null, commentsLoading: boolean): boolean => !comments && commentsLoading,
+ ],
+
sortedComments: [
(s) => [s.comments],
(comments: CommentType[] | null) => {