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(insights): stop other insights' comments leaking onto unsaved insights (#69923)

posthog[bot]59d711bcf216
diff --git a/frontend/src/scenes/comments/commentsLogic.ts b/frontend/src/scenes/comments/commentsLogic.ts
index 3877c17c9f48..29804a1cd3ba 100644
--- a/frontend/src/scenes/comments/commentsLogic.ts
+++ b/frontend/src/scenes/comments/commentsLogic.ts
@@ -148,6 +148,13 @@ export const commentsLogic = kea<commentsLogicType>([
null as CommentType[] | null,
{
loadComments: async () => {
+ if (!props.item_id) {
+ // A per-item discussion must target a specific item. Listing with only a
+ // scope makes the backend return every comment in that scope across the
+ // team, so fail closed rather than leak other items' comments.
+ return []
+ }
+
const response = await api.comments.list({
scope: props.scope,
item_id: props.item_id,
@@ -158,6 +165,11 @@ export const commentsLogic = kea<commentsLogicType>([
sendComposedContent: async ({ asTask }) => {
const existingComments = values.comments ?? []
+ if (!props.item_id) {
+ console.error('Failed to create a comment because no item_id was provided')
+ return existingComments
+ }
+
if (values.richContentEditor?.isEmpty()) {
console.error('Failed to create a comment because the content was empty')
return existingComments
diff --git a/frontend/src/scenes/insights/insightSceneLogic.test.ts b/frontend/src/scenes/insights/insightSceneLogic.test.ts
index 58968b5a97ff..3da4ff173ada 100644
--- a/frontend/src/scenes/insights/insightSceneLogic.test.ts
+++ b/frontend/src/scenes/insights/insightSceneLogic.test.ts
@@ -13,7 +13,7 @@ import { useMocks } from '~/mocks/jest'
import { examples } from '~/queries/examples'
import { InsightVizNode, NodeKind, ProductKey } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
-import { InsightShortId, InsightType, ItemMode } from '~/types'
+import { ActivityScope, InsightShortId, InsightType, ItemMode } from '~/types'
const Insight12 = '12' as InsightShortId
const Insight42 = '42' as InsightShortId
@@ -53,6 +53,34 @@ describe('insightSceneLogic', () => {
})
})
+ it('disables discussions for an unsaved insight so comments do not leak across the team', async () => {
+ // A new insight has no numeric id yet. The side panel context must still declare the Insight
+ // scope (so sidePanelContextLogic does not fall back to the URL guesser and drop item_id, which
+ // would list every Insight-scoped comment in the team) but mark discussions disabled.
+ router.actions.push(urls.insightNew())
+ logic = insightSceneLogic()
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(logic.values.insight?.id).toBeUndefined()
+ expect(logic.values.sidePanelContext).toEqual({
+ activity_scope: ActivityScope.INSIGHT,
+ discussions_disabled: true,
+ })
+
+ // Once saved, the context carries the concrete item_id and discussions become enabled.
+ logic.values.insightLogicRef?.logic.actions.setInsight(
+ { id: 42, short_id: Insight42, result: ['some result'] },
+ { fromPersistentApi: true, overrideQuery: true }
+ )
+
+ expect(logic.values.sidePanelContext).toMatchObject({
+ activity_scope: ActivityScope.INSIGHT,
+ activity_item_id: '42',
+ })
+ expect(logic.values.sidePanelContext?.discussions_disabled).toBeUndefined()
+ })
+
it('redirects maintaining url params when opening /insight/new with insight type in theurl', async () => {
router.actions.push(urls.insightNew({ type: InsightType.FUNNELS }))
logic = insightSceneLogic()
diff --git a/frontend/src/scenes/insights/insightSceneLogic.tsx b/frontend/src/scenes/insights/insightSceneLogic.tsx
index 2479becff3da..6ad9a3e9a7d2 100644
--- a/frontend/src/scenes/insights/insightSceneLogic.tsx
+++ b/frontend/src/scenes/insights/insightSceneLogic.tsx
@@ -354,19 +354,27 @@ export const insightSceneLogic = kea<insightSceneLogicType>([
[SIDE_PANEL_CONTEXT_KEY]: [
(s) => [s.insight],
(insight): SidePanelSceneContext | null => {
- return insight?.id
- ? {
- activity_scope: ActivityScope.INSIGHT,
- activity_item_id: `${insight.id}`,
- // when e.g. constructing URLs for an insight we don't use the id,
- // so we also store the short id
- activity_item_context: {
- short_id: `${insight.short_id}`,
- },
- access_control_resource: 'insight',
- access_control_resource_id: `${insight.id}`,
- }
- : null
+ if (!insight?.id) {
+ // An unsaved insight has no numeric id yet. Still declare the Insight scope so
+ // sidePanelContextLogic does not fall back to the URL-based guesser (which would
+ // drop the missing item_id and list every Insight-scoped comment in the team),
+ // but mark discussions disabled until the insight is saved.
+ return {
+ activity_scope: ActivityScope.INSIGHT,
+ discussions_disabled: true,
+ }
+ }
+ return {
+ activity_scope: ActivityScope.INSIGHT,
+ activity_item_id: `${insight.id}`,
+ // when e.g. constructing URLs for an insight we don't use the id,
+ // so we also store the short id
+ activity_item_context: {
+ short_id: `${insight.short_id}`,
+ },
+ access_control_resource: 'insight',
+ access_control_resource_id: `${insight.id}`,
+ }
},
],
maxContext: [