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.

feat(dashboards): show full insight display options on dashboard cards (#65238)

Sam Pennington4d7dd922de53
diff --git a/frontend/src/lib/components/Cards/InsightCard/DashboardInsightActions.tsx b/frontend/src/lib/components/Cards/InsightCard/DashboardInsightActions.tsx
deleted file mode 100644
index de2cee7d235f..000000000000
--- a/frontend/src/lib/components/Cards/InsightCard/DashboardInsightActions.tsx
+++ /dev/null
@@ -1,113 +0,0 @@
-import { useActions, useValues } from 'kea'
-
-import { LemonButton } from 'lib/lemon-ui/LemonButton'
-import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
-import { DashboardEventSource, eventUsageLogic } from 'lib/utils/eventUsageLogic'
-import { insightLogic } from 'scenes/insights/insightLogic'
-
-import { insightsModel } from '~/models/insightsModel'
-import { InsightLogicProps, QueryBasedInsightModel } from '~/types'
-
-import { toggleAnnotationsInInsightQuery } from './annotationsToggle'
-import { toggleDisplayLabelsInInsightQuery } from './displayLabelsToggle'
-import { toggleLegendInInsightQuery } from './legendToggle'
-
-type DashboardInsightActionsProps = {
- insight: QueryBasedInsightModel
- insightLogicProps: InsightLogicProps
- dashboardId: number | undefined
- canToggleDisplayLabels: boolean
- canToggleLegend: boolean
- canToggleAnnotations: boolean
-}
-
-/** Quick visualization toggles in the insight card ⋯ menu on dashboard (and similar placements). */
-export function DashboardInsightActions({
- insight,
- insightLogicProps,
- dashboardId,
- canToggleDisplayLabels,
- canToggleLegend,
- canToggleAnnotations,
-}: DashboardInsightActionsProps): JSX.Element | null {
- const {
- displayLabelsToggleTextForInsight,
- legendToggleTextForInsight,
- annotationsToggleTextForInsight,
- query: activeQuery,
- } = useValues(insightLogic(insightLogicProps))
- const { updateInsightDirect } = useActions(insightsModel)
- const {
- reportDashboardInsightValuesOnSeriesToggled,
- reportDashboardInsightLegendToggled,
- reportDashboardInsightAnnotationsToggled,
- } = useActions(eventUsageLogic)
-
- if (!canToggleDisplayLabels && !canToggleLegend && !canToggleAnnotations) {
- return null
- }
-
- return (
- <>
- <LemonDivider />
- {canToggleDisplayLabels && (
- <LemonButton
- onClick={() => {
- const currentQuery = activeQuery ?? insight.query
- const query = toggleDisplayLabelsInInsightQuery(currentQuery)
- if (query !== currentQuery) {
- updateInsightDirect(insight, { query })
- reportDashboardInsightValuesOnSeriesToggled(
- dashboardId,
- insight.id,
- DashboardEventSource.MoreDropdown
- )
- }
- }}
- fullWidth
- >
- {displayLabelsToggleTextForInsight}
- </LemonButton>
- )}
- {canToggleLegend && (
- <LemonButton
- onClick={() => {
- const currentQuery = activeQuery ?? insight.query
- const query = toggleLegendInInsightQuery(currentQuery)
- if (query !== currentQuery) {
- updateInsightDirect(insight, { query })
- reportDashboardInsightLegendToggled(
- dashboardId,
- insight.id,
- DashboardEventSource.MoreDropdown
- )
- }
- }}
- fullWidth
- >
- {legendToggleTextForInsight}
- </LemonButton>
- )}
- {canToggleAnnotations && (
- <LemonButton
- onClick={() => {
- const currentQuery = activeQuery ?? insight.query
- const query = toggleAnnotationsInInsightQuery(currentQuery)
- if (query !== currentQuery) {
- updateInsightDirect(insight, { query })
- reportDashboardInsightAnnotationsToggled(
- dashboardId,
- insight.id,
- DashboardEventSource.MoreDropdown
- )
- }
- }}
- fullWidth
- >
- {annotationsToggleTextForInsight}
- </LemonButton>
- )}
- <LemonDivider />
- </>
- )
-}
diff --git a/frontend/src/lib/components/Cards/InsightCard/DashboardInsightDisplayOptions.tsx b/frontend/src/lib/components/Cards/InsightCard/DashboardInsightDisplayOptions.tsx
new file mode 100644
index 000000000000..49c17bcf5b9d
--- /dev/null
+++ b/frontend/src/lib/components/Cards/InsightCard/DashboardInsightDisplayOptions.tsx
@@ -0,0 +1,29 @@
+import { LemonButton } from 'lib/lemon-ui/LemonButton'
+import { LemonDivider } from 'lib/lemon-ui/LemonDivider'
+import { LemonMenu } from 'lib/lemon-ui/LemonMenu'
+
+import { useInsightDisplayOptions } from '~/queries/nodes/InsightViz/insightDisplayOptions'
+
+// The insight editor's "Options" menu, surfaced as a submenu in the dashboard card ⋯ menu. Edits
+// persist to the saved insight via the card's `setQuery` wiring (see InsightCard).
+export function DashboardInsightDisplayOptions(): JSX.Element | null {
+ const { items } = useInsightDisplayOptions()
+
+ if (items.length === 0) {
+ return null
+ }
+
+ return (
+ <>
+ <LemonDivider />
+ <LemonMenu
+ items={items}
+ closeOnClickInside={false}
+ placement="right-start"
+ fallbackPlacements={['left-start']}
+ >
+ <LemonButton fullWidth>Display options</LemonButton>
+ </LemonMenu>
+ </>
+ )
+}
diff --git a/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx b/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx
index 3ca460d9763c..05e1ad842c65 100644
--- a/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx
+++ b/frontend/src/lib/components/Cards/InsightCard/InsightCard.tsx
@@ -2,10 +2,11 @@ import './InsightCard.scss'
import { useMergeRefs } from '@floating-ui/react'
import clsx from 'clsx'
-import { BindLogic, useValues } from 'kea'
-import React, { useMemo, useState } from 'react'
+import { BindLogic, useActions, useValues } from 'kea'
+import React, { useMemo, useRef, useState } from 'react'
import { LayoutItem } from 'react-grid-layout'
import { useInView } from 'react-intersection-observer'
+import { useDebouncedCallback } from 'use-debounce'
import { ApiError } from 'lib/api'
import { Resizeable } from 'lib/components/Cards/CardMeta'
@@ -26,9 +27,10 @@ import { insightLogic } from 'scenes/insights/insightLogic'
import { ErrorBoundary } from '~/layout/ErrorBoundary'
import { themeLogic } from '~/layout/navigation-3000/themeLogic'
+import { insightsModel } from '~/models/insightsModel'
import { extractValidationError } from '~/queries/nodes/InsightViz/utils'
import { Query } from '~/queries/Query/Query'
-import { DashboardFilter, HogQLVariable } from '~/queries/schema/schema-general'
+import { DashboardFilter, HogQLVariable, Node } from '~/queries/schema/schema-general'
import {
AccessControlLevel,
AccessControlResourceType,
@@ -46,6 +48,7 @@ import { EditModeEdge, EditModeEdgeOverlay } from './EditModeEdgeOverlay'
import { InsightMeta } from './InsightMeta'
const IS_STORYBOOK = inStorybook() || inStorybookTestRunner()
+const DISPLAY_OPTIONS_PERSIST_DEBOUNCE_MS = 700
export interface InsightCardProps extends Resizeable {
/** Insight to display. */
@@ -173,6 +176,21 @@ function InsightCardInternal(
const mergedRefs = useMergeRefs([ref, inViewRef])
const { theme } = useValues(themeLogic)
+ const { updateInsightDirect } = useActions(insightsModel)
+
+ // Display options edited from the card ⋯ menu mutate the saved insight directly (no per-tile
+ // override), mirroring the legend/labels/annotations quick toggles. Debounced so rapid edits
+ // (e.g. typing an axis label) collapse into a single PATCH.
+ const insightRef = useRef(insight)
+ insightRef.current = insight
+ const canEditInsight = insight.user_access_level
+ ? accessLevelSatisfied(AccessControlResourceType.Insight, insight.user_access_level, AccessControlLevel.Editor)
+ : true
+ const canPersistDisplayOptions = !!dashboardId && canEditInsight
+ const persistDisplayOptions = useDebouncedCallback((node: Node) => {
+ updateInsightDirect(insightRef.current, { query: node })
+ }, DISPLAY_OPTIONS_PERSIST_DEBOUNCE_MS)
+
// Stable reference so the memoized viz below isn't invalidated on every grid re-render.
const insightLogicProps: InsightLogicProps = useMemo(
() => ({
@@ -181,8 +199,9 @@ function InsightCardInternal(
cachedInsight: insight,
loadPriority,
doNotLoad,
+ setQuery: canPersistDisplayOptions ? persistDisplayOptions : undefined,
}),
- [insight, dashboardId, loadPriority, doNotLoad]
+ [insight, dashboardId, loadPriority, doNotLoad, canPersistDisplayOptions, persistDisplayOptions]
)
const { insightLoading } = useValues(insightLogic(insightLogicProps))
@@ -299,6 +318,7 @@ function InsightCardInternal(
insight={insight}
ribbonColor={ribbonColor}
dashboardId={dashboardId}
+ persistDisplayOptions={canPersistDisplayOptions ? persistDisplayOptions : undefined}
updateColor={updateColor}
toggleShowDescription={toggleShowDescription}
removeFromDashboard={removeFromDashboard}
diff --git a/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx b/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx
index 38f8125afbfc..57658faccb9b 100644
--- a/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx
+++ b/frontend/src/lib/components/Cards/InsightCard/InsightMeta.tsx
@@ -41,7 +41,7 @@ import { urls } from 'scenes/urls'
import { dashboardsModel } from '~/models/dashboardsModel'
import { insightsModel } from '~/models/insightsModel'
-import { ProductKey } from '~/queries/schema/schema-general'
+import { Node, ProductKey } from '~/queries/schema/schema-general'
import { isDataVisualizationNode } from '~/queries/utils'
import {
AccessControlLevel,
@@ -55,7 +55,7 @@ import {
QueryBasedInsightModel,
} from '~/types'
-import { DashboardInsightActions } from './DashboardInsightActions'
+import { DashboardInsightDisplayOptions } from './DashboardInsightDisplayOptions'
import { dashboardWidgetMenusLogic } from './dashboardWidgetMenusLogic'
import { DashboardWidgetPlacementMenus } from './DashboardWidgetPlacementMenus'
import { InsightCardProps } from './InsightCard'
@@ -91,6 +91,7 @@ interface InsightMetaProps extends Pick<
insight: QueryBasedInsightModel
areDetailsShown?: boolean
setAreDetailsShown?: React.Dispatch<React.SetStateAction<boolean>>
+ persistDisplayOptions?: (node: Node) => void
}
export function InsightMeta({
@@ -120,6 +121,7 @@ export function InsightMeta({
placement,
surveyOpportunity,
onDragHandleMouseDown,
+ persistDisplayOptions,
}: InsightMetaProps): JSX.Element {
const { short_id, name, next_allowed_client_refresh: nextAllowedClientRefresh } = insight
const tileFiltersOverride = tile?.filters_overrides
@@ -130,13 +132,9 @@ export function InsightMeta({
filtersOverride: filtersOverride ?? null,
variablesOverride: variablesOverride ?? null,
tileFiltersOverride: tileFiltersOverride ?? null,
+ setQuery: persistDisplayOptions,
}
- const {
- insightFeedback,
- canToggleDisplayLabelsForInsight,
- canToggleLegendForInsight,
- canToggleAnnotationsForInsight,
- } = useValues(insightLogic(insightLogicProps))
+ const { insightFeedback } = useValues(insightLogic(insightLogicProps))
const { setInsightFeedback } = useActions(insightLogic(insightLogicProps))
const { exportContext, insightData, query } = useValues(insightDataLogic(insightLogicProps))
const [isManageAlertsModalOpen, setIsManageAlertsModalOpen] = useState(false)
@@ -200,9 +198,7 @@ export function InsightMeta({
hogqlAlertsEnabled: !!featureFlags[FEATURE_FLAGS.HOGQL_INSIGHT_ALERTS],
})
- const canToggleDisplayLabels = isUsedAsDashboardTile && canEditInsight && canToggleDisplayLabelsForInsight
- const canToggleLegend = isUsedAsDashboardTile && canEditInsight && canToggleLegendForInsight
- const canToggleAnnotations = isUsedAsDashboardTile && canEditInsight && canToggleAnnotationsForInsight
+ const showDisplayOptionsMenu = isUsedAsDashboardTile && canEditInsight && !!persistDisplayOptions
const hasTileStyleActions = !!(showCompactTile && toggleShowDescription && insight.description) || !!updateColor
const canShowCopyToDashboardTile = showCompactTile && !!copyToDashboard && canViewInsight
@@ -464,20 +460,11 @@ export function InsightMeta({
Alerts
</LemonButton>
) : null}
- <DashboardInsightActions
- insight={insight}
- insightLogicProps={insightLogicProps}
- dashboardId={dashboardId}
- canToggleDisplayLabels={canToggleDisplayLabels}
- canToggleLegend={canToggleLegend}
- canToggleAnnotations={canToggleAnnotations}
- />
+ {showDisplayOptionsMenu && <DashboardInsightDisplayOptions />}
{canShowCopyToDashboardTile && !canEditDashboard && (
<>
- {!canToggleDisplayLabels && !canToggleLegend && !canToggleAnnotations && (
- <LemonDivider />
- )}
+ <LemonDivider />
<h5 className="mx-2 my-1">Dashboard</h5>
<DashboardWidgetPlacementMenus
placementDestinations={copyToDestinations}
@@ -489,9 +476,7 @@ export function InsightMeta({
{/* Dashboard related */}
{canEditDashboard && (
<>
- {!canToggleDisplayLabels && !canToggleLegend && !canToggleAnnotations && (
- <LemonDivider />
- )}
+ <LemonDivider />
{showCompactTile && toggleShowDescription && !!insight.description && (
<LemonButton onClick={toggleShowDescription} fullWidth>
{tile?.show_description === false ? 'Show description' : 'Hide description'}
diff --git a/frontend/src/lib/components/Cards/InsightCard/annotationsToggle.ts b/frontend/src/lib/components/Cards/InsightCard/annotationsToggle.ts
deleted file mode 100644
index f9f543fba027..000000000000
--- a/frontend/src/lib/components/Cards/InsightCard/annotationsToggle.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { NON_TIME_SERIES_DISPLAY_TYPES } from 'lib/constants'
-
-import { InsightVizNode } from '~/queries/schema/schema-general'
-import { getShowAnnotations, isFunnelsQuery, isInsightVizNode, isTrendsQuery } from '~/queries/utils'
-import { ChartDisplayType, FunnelVizType, QueryBasedInsightModel } from '~/types'
-
-// Annotations only render for trends time-series charts and funnels with the historical-trends viz type.
-export function canToggleAnnotationsInInsightQuery(query: QueryBasedInsightModel['query']): boolean {
- if (!isInsightVizNode(query)) {
- return false
- }
-
- const source = query.source
- if (isTrendsQuery(source)) {
- const display = source.trendsFilter?.display || ChartDisplayType.ActionsLineGraph
- return display !== ChartDisplayType.Metric && !NON_TIME_SERIES_DISPLAY_TYPES.includes(display)
- }
-
- if (isFunnelsQuery(source)) {
- return source.funnelsFilter?.funnelVizType === FunnelVizType.Trends
- }
-
- return false
-}
-
-export function isAnnotationsEnabledInInsightQuery(query: QueryBasedInsightModel['query']): boolean {
- if (!canToggleAnnotationsInInsightQuery(query) || !isInsightVizNode(query)) {
- return false
- }
- // Default true — treat undefined as enabled.
- return getShowAnnotations(query.source) !== false
-}
-
-export function getAnnotationsToggleText(query: QueryBasedInsightModel['query']): string {
- return isAnnotationsEnabledInInsightQuery(query) ? 'Hide annotations' : 'Show annotations'
-}
-
-export function toggleAnnotationsInInsightQuery(
- query: QueryBasedInsightModel['query']
-): QueryBasedInsightModel['query'] {
- if (!isInsightVizNode(query)) {
- return query
- }
-
- const viz: InsightVizNode = query
- const source = viz.source
- const nextShowAnnotations = getShowAnnotations(source) === false
-
- if (isTrendsQuery(source)) {
- return {
- ...viz,
- source: {
- ...source,
- trendsFilter: {
- ...source.trendsFilter,
- showAnnotations: nextShowAnnotations,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isFunnelsQuery(source)) {
- return {
- ...viz,
- source: {
- ...source,
- funnelsFilter: {
- ...source.funnelsFilter,
- showAnnotations: nextShowAnnotations,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- return query
-}
diff --git a/frontend/src/lib/components/Cards/InsightCard/displayLabelsToggle.test.ts b/frontend/src/lib/components/Cards/InsightCard/displayLabelsToggle.test.ts
deleted file mode 100644
index e08ff0c3e1bd..000000000000
--- a/frontend/src/lib/components/Cards/InsightCard/displayLabelsToggle.test.ts
+++ /dev/null
@@ -1,235 +0,0 @@
-import { NodeKind } from '~/queries/schema/schema-general'
-import { ChartDisplayType } from '~/types'
-
-import {
- canToggleDisplayLabelsInInsightQuery,
- getDisplayLabelsToggleText,
- getDisplayLabelsToggleMode,
- isDisplayLabelsEnabledInInsightQuery,
- toggleDisplayLabelsInInsightQuery,
-} from './displayLabelsToggle'
-
-describe('displayLabelsToggle', () => {
- describe('canToggleDisplayLabelsInInsightQuery', () => {
- it('returns true for trends pie', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsPie },
- },
- } as any
-
- expect(canToggleDisplayLabelsInInsightQuery(query)).toBe(true)
- })
-
- it('returns false for trends bold number', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.BoldNumber },
- },
- } as any
-
- expect(canToggleDisplayLabelsInInsightQuery(query)).toBe(false)
- })
-
- it('returns false for funnels steps visualization', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.FunnelsQuery,
- funnelsFilter: { funnelVizType: 'steps' },
- },
- } as any
-
- expect(canToggleDisplayLabelsInInsightQuery(query)).toBe(false)
- })
-
- it('returns true for funnels trends visualization', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.FunnelsQuery,
- funnelsFilter: { funnelVizType: 'trends' },
- },
- } as any
-
- expect(canToggleDisplayLabelsInInsightQuery(query)).toBe(true)
- })
-
- it('returns true for lifecycle insights', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.LifecycleQuery,
- },
- } as any
-
- expect(canToggleDisplayLabelsInInsightQuery(query)).toBe(true)
- })
- })
-
- describe('isDisplayLabelsEnabledInInsightQuery', () => {
- it('reads trends pie label toggle', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsPie, showLabelsOnSeries: true },
- },
- } as any
-
- expect(isDisplayLabelsEnabledInInsightQuery(query)).toBe(true)
- })
-
- it('reads trends line values toggle', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, showValuesOnSeries: true },
- },
- } as any
-
- expect(isDisplayLabelsEnabledInInsightQuery(query)).toBe(true)
- })
- })
-
- describe('getDisplayLabelsToggleMode', () => {
- it('returns pie_labels for trends pie', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsPie },
- },
- } as any
-
- expect(getDisplayLabelsToggleMode(query)).toBe('pie_labels')
- })
-
- it('returns series_values for trends line', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph },
- },
- } as any
-
- expect(getDisplayLabelsToggleMode(query)).toBe('series_values')
- })
- })
-
- describe('getDisplayLabelsToggleText', () => {
- it('returns labels wording for pie insights', () => {
- const hiddenLabelsQuery = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsPie, showLabelsOnSeries: false },
- },
- } as any
-
- const shownLabelsQuery = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsPie, showLabelsOnSeries: true },
- },
- } as any
-
- expect(getDisplayLabelsToggleText(hiddenLabelsQuery)).toBe('Show labels on series')
- expect(getDisplayLabelsToggleText(shownLabelsQuery)).toBe('Hide labels on series')
- })
-
- it('returns values wording for non-pie insights', () => {
- const hiddenValuesQuery = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, showValuesOnSeries: false },
- },
- } as any
-
- const shownValuesQuery = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, showValuesOnSeries: true },
- },
- } as any
-
- expect(getDisplayLabelsToggleText(hiddenValuesQuery)).toBe('Show values on series')
- expect(getDisplayLabelsToggleText(shownValuesQuery)).toBe('Hide values on series')
- })
- })
-
- describe('toggleDisplayLabelsInInsightQuery', () => {
- it.each([
- {
- kind: NodeKind.TrendsQuery,
- filter: 'trendsFilter',
- display: ChartDisplayType.ActionsLineGraph,
- field: 'showValuesOnSeries',
- },
- {
- kind: NodeKind.TrendsQuery,
- filter: 'trendsFilter',
- display: ChartDisplayType.ActionsPie,
- field: 'showLabelsOnSeries',
- },
- {
- kind: NodeKind.StickinessQuery,
- filter: 'stickinessFilter',
- display: ChartDisplayType.ActionsLineGraph,
- field: 'showValuesOnSeries',
- },
- {
- kind: NodeKind.FunnelsQuery,
- filter: 'funnelsFilter',
- field: 'showValuesOnSeries',
- },
- {
- kind: NodeKind.LifecycleQuery,
- filter: 'lifecycleFilter',
- field: 'showValuesOnSeries',
- },
- ])('toggles $field for $kind', ({ kind, filter, display, field }) => {
- const makeQuery = (enabled: boolean): any => ({
- kind: NodeKind.InsightVizNode,
- source: {
- kind,
- [filter]: {
- ...(display ? { display } : {}),
- [field]: enabled,
- },
- },
- })
-
- const enabledQuery = toggleDisplayLabelsInInsightQuery(makeQuery(false)) as any
- expect(enabledQuery.source[filter]?.[field]).toBe(true)
-
- const disabledQuery = toggleDisplayLabelsInInsightQuery(makeQuery(true)) as any
- expect(disabledQuery.source[filter]?.[field]).toBe(false)
- })
-
- it('keeps query unchanged for trends displays without series values', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: {
- display: ChartDisplayType.BoldNumber,
- showValuesOnSeries: false,
- },
- },
- } as any
-
- const updatedQuery = toggleDisplayLabelsInInsightQuery(query)
- expect(updatedQuery).toBe(query)
- })
- })
-})
diff --git a/frontend/src/lib/components/Cards/InsightCard/displayLabelsToggle.ts b/frontend/src/lib/components/Cards/InsightCard/displayLabelsToggle.ts
deleted file mode 100644
index f5e518e6ba03..000000000000
--- a/frontend/src/lib/components/Cards/InsightCard/displayLabelsToggle.ts
+++ /dev/null
@@ -1,171 +0,0 @@
-import { NON_VALUES_ON_SERIES_DISPLAY_TYPES } from 'lib/constants'
-
-import { isFunnelsQuery, isInsightVizNode, isLifecycleQuery, isStickinessQuery, isTrendsQuery } from '~/queries/utils'
-import { ChartDisplayType, FunnelVizType, QueryBasedInsightModel } from '~/types'
-
-export type DisplayLabelsToggleMode = 'pie_labels' | 'series_values'
-
-export function canToggleDisplayLabelsInInsightQuery(query: QueryBasedInsightModel['query']): boolean {
- if (!isInsightVizNode(query)) {
- return false
- }
-
- if (isTrendsQuery(query.source)) {
- const display = query.source.trendsFilter?.display || ChartDisplayType.ActionsLineGraph
- return display === ChartDisplayType.ActionsPie || !NON_VALUES_ON_SERIES_DISPLAY_TYPES.includes(display)
- }
-
- if (isStickinessQuery(query.source)) {
- const display = query.source.stickinessFilter?.display || ChartDisplayType.ActionsLineGraph
- return !NON_VALUES_ON_SERIES_DISPLAY_TYPES.includes(display)
- }
-
- if (isFunnelsQuery(query.source)) {
- return query.source.funnelsFilter?.funnelVizType === FunnelVizType.Trends
- }
-
- if (isLifecycleQuery(query.source)) {
- return true
- }
-
- return false
-}
-
-export function isDisplayLabelsEnabledInInsightQuery(query: QueryBasedInsightModel['query']): boolean {
- if (!canToggleDisplayLabelsInInsightQuery(query) || !isInsightVizNode(query)) {
- return false
- }
-
- if (isTrendsQuery(query.source)) {
- const display = query.source.trendsFilter?.display || ChartDisplayType.ActionsLineGraph
- return display === ChartDisplayType.ActionsPie
- ? !!query.source.trendsFilter?.showLabelsOnSeries
- : !!query.source.trendsFilter?.showValuesOnSeries
- }
-
- if (isStickinessQuery(query.source)) {
- return !!query.source.stickinessFilter?.showValuesOnSeries
- }
-
- if (isFunnelsQuery(query.source)) {
- return !!query.source.funnelsFilter?.showValuesOnSeries
- }
-
- return isLifecycleQuery(query.source) && !!query.source.lifecycleFilter?.showValuesOnSeries
-}
-
-export function getDisplayLabelsToggleMode(query: QueryBasedInsightModel['query']): DisplayLabelsToggleMode | null {
- if (!isInsightVizNode(query)) {
- return null
- }
-
- if (isTrendsQuery(query.source)) {
- const display = query.source.trendsFilter?.display || ChartDisplayType.ActionsLineGraph
- return display === ChartDisplayType.ActionsPie ? 'pie_labels' : 'series_values'
- }
-
- if (isStickinessQuery(query.source) || isFunnelsQuery(query.source) || isLifecycleQuery(query.source)) {
- return 'series_values'
- }
-
- return null
-}
-
-export function getDisplayLabelsToggleText(query: QueryBasedInsightModel['query']): string {
- const displayLabelsShown = isDisplayLabelsEnabledInInsightQuery(query)
- const displayLabelsToggleMode = getDisplayLabelsToggleMode(query)
-
- if (displayLabelsToggleMode === 'pie_labels') {
- return displayLabelsShown ? 'Hide labels on series' : 'Show labels on series'
- }
-
- return displayLabelsShown ? 'Hide values on series' : 'Show values on series'
-}
-
-export function toggleDisplayLabelsInInsightQuery(
- query: QueryBasedInsightModel['query']
-): QueryBasedInsightModel['query'] {
- if (!isInsightVizNode(query)) {
- return query
- }
-
- const queryWithSource = query as any
- const source = queryWithSource.source
-
- if (isTrendsQuery(source)) {
- const display = source.trendsFilter?.display || ChartDisplayType.ActionsLineGraph
- if (display === ChartDisplayType.ActionsPie) {
- return {
- ...queryWithSource,
- source: {
- ...source,
- trendsFilter: {
- ...source.trendsFilter,
- showLabelsOnSeries: !source.trendsFilter?.showLabelsOnSeries,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (NON_VALUES_ON_SERIES_DISPLAY_TYPES.includes(display)) {
- return query
- }
-
- return {
- ...queryWithSource,
- source: {
- ...source,
- trendsFilter: {
- ...source.trendsFilter,
- showValuesOnSeries: !source.trendsFilter?.showValuesOnSeries,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isStickinessQuery(source)) {
- const display = source.stickinessFilter?.display || ChartDisplayType.ActionsLineGraph
- if (NON_VALUES_ON_SERIES_DISPLAY_TYPES.includes(display)) {
- return query
- }
-
- return {
- ...queryWithSource,
- source: {
- ...source,
- stickinessFilter: {
- ...source.stickinessFilter,
- showValuesOnSeries: !source.stickinessFilter?.showValuesOnSeries,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isFunnelsQuery(source)) {
- return {
- ...queryWithSource,
- source: {
- ...source,
- funnelsFilter: {
- ...source.funnelsFilter,
- showValuesOnSeries: !source.funnelsFilter?.showValuesOnSeries,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isLifecycleQuery(source)) {
- return {
- ...queryWithSource,
- source: {
- ...source,
- lifecycleFilter: {
- ...source.lifecycleFilter,
- showValuesOnSeries: !source.lifecycleFilter?.showValuesOnSeries,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- return query
-}
diff --git a/frontend/src/lib/components/Cards/InsightCard/legendToggle.test.ts b/frontend/src/lib/components/Cards/InsightCard/legendToggle.test.ts
deleted file mode 100644
index dbc2426f666b..000000000000
--- a/frontend/src/lib/components/Cards/InsightCard/legendToggle.test.ts
+++ /dev/null
@@ -1,208 +0,0 @@
-import { InsightVizNode, NodeKind } from '~/queries/schema/schema-general'
-import { ChartDisplayType } from '~/types'
-
-import {
- canToggleLegendInInsightQuery,
- getLegendToggleText,
- isLegendEnabledInInsightQuery,
- toggleLegendInInsightQuery,
-} from './legendToggle'
-
-describe('legendToggle', () => {
- describe('canToggleLegendInInsightQuery', () => {
- it.each([
- {
- title: 'trends line graph',
- query: {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph },
- },
- },
- expected: true,
- },
- {
- title: 'world map',
- query: {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.WorldMap },
- },
- },
- expected: false,
- },
- {
- title: 'funnels steps',
- query: {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.FunnelsQuery,
- funnelsFilter: { funnelVizType: 'steps' },
- },
- },
- expected: false,
- },
- {
- title: 'funnels historical trends with breakdown',
- query: {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.FunnelsQuery,
- funnelsFilter: { funnelVizType: 'trends' },
- breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },
- },
- },
- expected: true,
- },
- {
- title: 'funnels historical trends without breakdown',
- query: {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.FunnelsQuery,
- funnelsFilter: { funnelVizType: 'trends' },
- },
- },
- expected: false,
- },
- {
- title: 'lifecycle',
- query: {
- kind: NodeKind.InsightVizNode,
- source: { kind: NodeKind.LifecycleQuery },
- },
- expected: true,
- },
- {
- title: 'trends table',
- query: {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsTable },
- },
- },
- expected: false,
- },
- {
- title: 'non-insight-viz (SQL)',
- query: { kind: NodeKind.DataVisualizationNode },
- expected: false,
- },
- ])('returns $expected for $title', ({ query, expected }) => {
- expect(canToggleLegendInInsightQuery(query as any)).toBe(expected)
- })
- })
-
- describe('toggleLegendInInsightQuery', () => {
- it.each([
- {
- kind: NodeKind.TrendsQuery,
- filterKey: 'trendsFilter',
- display: ChartDisplayType.ActionsLineGraph,
- },
- {
- kind: NodeKind.StickinessQuery,
- filterKey: 'stickinessFilter',
- display: ChartDisplayType.ActionsLineGraph,
- },
- {
- kind: NodeKind.LifecycleQuery,
- filterKey: 'lifecycleFilter',
- display: undefined,
- },
- {
- kind: NodeKind.FunnelsQuery,
- filterKey: 'funnelsFilter',
- display: undefined,
- },
- ])('sets showLegend true when unset ($kind)', ({ kind, filterKey, display }) => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind,
- [filterKey]: display ? { display } : {},
- },
- } as any
-
- const next = toggleLegendInInsightQuery(query) as any
- expect(next.source[filterKey]?.showLegend).toBe(true)
- })
-
- it('toggles showLegend off for trends', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, showLegend: true },
- },
- } as any
-
- const next = toggleLegendInInsightQuery(query) as InsightVizNode
- const src = next.source
- expect(src.kind).toBe(NodeKind.TrendsQuery)
- if (src.kind === NodeKind.TrendsQuery) {
- expect(src.trendsFilter?.showLegend).toBe(false)
- }
- })
- })
-
- describe('getLegendToggleText', () => {
- it('returns hide when legend on', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, showLegend: true },
- },
- } as any
-
- expect(getLegendToggleText(query)).toBe('Hide legend')
- expect(isLegendEnabledInInsightQuery(query)).toBe(true)
- })
-
- it('returns show when legend off', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph, showLegend: false },
- },
- } as any
-
- expect(getLegendToggleText(query)).toBe('Show legend')
- })
-
- it('reflects toggle state for funnels historical trends', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.FunnelsQuery,
- funnelsFilter: { funnelVizType: 'trends' },
- breakdownFilter: { breakdown: '$browser', breakdown_type: 'event' },
- },
- } as any
-
- expect(getLegendToggleText(query)).toBe('Show legend')
-
- const next = toggleLegendInInsightQuery(query) as any
- expect(next.source.funnelsFilter?.showLegend).toBe(true)
- expect(getLegendToggleText(next)).toBe('Hide legend')
- })
-
- it('after toggling unset legend, label reads hide', () => {
- const query = {
- kind: NodeKind.InsightVizNode,
- source: {
- kind: NodeKind.TrendsQuery,
- trendsFilter: { display: ChartDisplayType.ActionsLineGraph },
- },
- } as any
-
- const next = toggleLegendInInsightQuery(query)
- expect(getLegendToggleText(next)).toBe('Hide legend')
- })
- })
-})
diff --git a/frontend/src/lib/components/Cards/InsightCard/legendToggle.ts b/frontend/src/lib/components/Cards/InsightCard/legendToggle.ts
deleted file mode 100644
index 545902919022..000000000000
--- a/frontend/src/lib/components/Cards/InsightCard/legendToggle.ts
+++ /dev/null
@@ -1,120 +0,0 @@
-import { DISPLAY_TYPES_WITHOUT_LEGEND } from 'lib/components/InsightLegend/utils'
-
-import { InsightVizNode } from '~/queries/schema/schema-general'
-import {
- getShowLegend,
- hasBreakdownFilter,
- isFunnelsQuery,
- isInsightVizNode,
- isLifecycleQuery,
- isStickinessQuery,
- isTrendsQuery,
-} from '~/queries/utils'
-import { FunnelVizType, QueryBasedInsightModel } from '~/types'
-
-// Eligibility matches insightVizDataLogic `hasLegend` (trends / stickiness / lifecycle, excluding DISPLAY_TYPES_WITHOUT_LEGEND).
-
-export function canToggleLegendInInsightQuery(query: QueryBasedInsightModel['query']): boolean {
- if (!isInsightVizNode(query)) {
- return false
- }
-
- const source = query.source
-
- if (isFunnelsQuery(source)) {
- return (
- source.funnelsFilter?.funnelVizType === FunnelVizType.Trends && hasBreakdownFilter(source.breakdownFilter)
- )
- }
-
- const isTrends = isTrendsQuery(source)
- const isStickiness = isStickinessQuery(source)
- const isLifecycle = isLifecycleQuery(source)
- if (!isTrends && !isStickiness && !isLifecycle) {
- return false
- }
-
- const display = isTrends
- ? source.trendsFilter?.display
- : isStickiness
- ? source.stickinessFilter?.display
- : undefined
-
- return !(display && DISPLAY_TYPES_WITHOUT_LEGEND.includes(display))
-}
-
-export function isLegendEnabledInInsightQuery(query: QueryBasedInsightModel['query']): boolean {
- if (!canToggleLegendInInsightQuery(query) || !isInsightVizNode(query)) {
- return false
- }
-
- return !!getShowLegend(query.source)
-}
-
-export function getLegendToggleText(query: QueryBasedInsightModel['query']): string {
- return isLegendEnabledInInsightQuery(query) ? 'Hide legend' : 'Show legend'
-}
-
-export function toggleLegendInInsightQuery(query: QueryBasedInsightModel['query']): QueryBasedInsightModel['query'] {
- if (!isInsightVizNode(query)) {
- return query
- }
-
- const viz: InsightVizNode = query
- const source = viz.source
- const nextShowLegend = !getShowLegend(source)
-
- if (isTrendsQuery(source)) {
- return {
- ...viz,
- source: {
- ...source,
- trendsFilter: {
- ...source.trendsFilter,
- showLegend: nextShowLegend,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isStickinessQuery(source)) {
- return {
- ...viz,
- source: {
- ...source,
- stickinessFilter: {
- ...source.stickinessFilter,
- showLegend: nextShowLegend,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isLifecycleQuery(source)) {
- return {
- ...viz,
- source: {
- ...source,
- lifecycleFilter: {
- ...source.lifecycleFilter,
- showLegend: nextShowLegend,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- if (isFunnelsQuery(source)) {
- return {
- ...viz,
- source: {
- ...source,
- funnelsFilter: {
- ...source.funnelsFilter,
- showLegend: nextShowLegend,
- },
- },
- } as QueryBasedInsightModel['query']
- }
-
- return query
-}
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index abf26b11f6de..27d012fd1149 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -1490,6 +1490,46 @@ describe('dashboardLogic', () => {
expect(logic.values.textTiles[0].text!.body).toEqual('I AM A TEXT')
})
+ it('preserves cached chart data when a bare PATCH returns null result', async () => {
+ // insight800() has non-null result and last_refresh from the fixture; a bare PATCH
+ // (rename, display-option save) responds with result: null. The tile must keep its
+ // previously-computed chart data rather than blanking to "Chart data didn't load".
+ const originalInsight = logic.values.insightTiles[0].insight!
+ const originalResult = originalInsight.result
+ const originalLastRefresh = originalInsight.last_refresh
+
+ insightsModel.actions.renameInsightSuccess({
+ ...insight800(),
+ name: 'renamed via bare patch',
+ result: null,
+ last_refresh: null,
+ })
+
+ await expectLogic(logic).toFinishAllListeners()
+ const updated = logic.values.insightTiles[0].insight!
+ expect(updated.name).toEqual('renamed via bare patch')
+ expect(updated.result).toEqual(originalResult)
+ expect(updated.last_refresh).toEqual(originalLastRefresh)
+ })
+
+ it('replaces cached chart data when a full refresh returns non-null result', async () => {
+ const newResult = [{ data: 'fresh' }]
+ const newLastRefresh = '2024-01-01T00:00:00Z'
+
+ insightsModel.actions.renameInsightSuccess({
+ ...insight800(),
+ name: 'refreshed',
+ result: newResult,
+ last_refresh: newLastRefresh,
+ })
+
+ await expectLogic(logic).toFinishAllListeners()
+ const updated = logic.values.insightTiles[0].insight!
+ expect(updated.name).toEqual('refreshed')
+ expect(updated.result).toEqual(newResult)
+ expect(updated.last_refresh).toEqual(newLastRefresh)
+ })
+
it('can respond to external insight update for an insight tile that is new on this dashboard', async () => {
await expectLogic(logic, () => {
dashboardsModel.actions.updateDashboardInsight({
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index 0499ac39feb9..5f76ebce44f8 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -961,11 +961,17 @@ export const dashboardLogic = kea<dashboardLogicType>([
return state
}
+ // A bare PATCH (rename, display-option persist) doesn't recompute the insight, so
+ // its response carries `result: null` and stale-but-empty cache metadata. Keep the
+ // tile's already-computed chart data instead of blanking it into "Chart data didn't load".
+ const existing = tiles[tileIndex].insight as QueryBasedInsightModel
tiles[tileIndex] = {
...tiles[tileIndex],
insight: {
- ...(tiles[tileIndex].insight as QueryBasedInsightModel),
+ ...existing,
...item,
+ result: item.result ?? existing.result,
+ last_refresh: item.last_refresh ?? existing.last_refresh,
},
}
diff --git a/frontend/src/scenes/insights/insightLogic.tsx b/frontend/src/scenes/insights/insightLogic.tsx
index 18f58480b263..13a8903d4b58 100644
--- a/frontend/src/scenes/insights/insightLogic.tsx
+++ b/frontend/src/scenes/insights/insightLogic.tsx
@@ -7,16 +7,6 @@ import { LemonDialog, LemonInput } from '@posthog/lemon-ui'
import { ApiError } from 'lib/api'
import { insightAlertsLogic } from 'lib/components/Alerts/insightAlertsLogic'
-import {
- canToggleAnnotationsInInsightQuery,
- getAnnotationsToggleText,
-} from 'lib/components/Cards/InsightCard/annotationsToggle'
-import {
- canToggleDisplayLabelsInInsightQuery,
- getDisplayLabelsToggleText,
- isDisplayLabelsEnabledInInsightQuery,
-} from 'lib/components/Cards/InsightCard/displayLabelsToggle'
-import { canToggleLegendInInsightQuery, getLegendToggleText } from 'lib/components/Cards/InsightCard/legendToggle'
import { tryShowMCPHint } from 'lib/components/MCPHint/mcpHintLogic'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { FEATURE_FLAGS } from 'lib/constants'
@@ -471,28 +461,6 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType
)
: true,
],
- canToggleDisplayLabelsForInsight: [
- (s) => [s.query],
- (query) => !!query && canToggleDisplayLabelsInInsightQuery(query),
- ],
- canToggleLegendForInsight: [(s) => [s.query], (query) => !!query && canToggleLegendInInsightQuery(query)],
- canToggleAnnotationsForInsight: [
- (s) => [s.query],
- (query) => !!query && canToggleAnnotationsInInsightQuery(query),
- ],
- displayLabelsShownForInsight: [
- (s) => [s.query],
- (query) => !!query && isDisplayLabelsEnabledInInsightQuery(query),
- ],
- displayLabelsToggleTextForInsight: [
- (s) => [s.query],
- (query) => (query ? getDisplayLabelsToggleText(query) : 'Show values on series'),
- ],
- legendToggleTextForInsight: [(s) => [s.query], (query) => (query ? getLegendToggleText(query) : 'Show legend')],
- annotationsToggleTextForInsight: [
- (s) => [s.query],
- (query) => (query ? getAnnotationsToggleText(query) : 'Hide annotations'),
- ],
insightChanged: [
(s) => [s.insight, s.savedInsight],
(insight, savedInsight): boolean => {