Skip to content

Commit

Permalink
Move chartRenderingMetadata out of InsightLogicProps
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Nov 9, 2023
1 parent 8c2aded commit 39a9ce1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 42 deletions.
32 changes: 21 additions & 11 deletions frontend/src/queries/nodes/InsightViz/InsightContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ import { FunnelCorrelation } from 'scenes/insights/views/Funnels/FunnelCorrelati
import { InsightResultMetadata } from './InsightResultMetadata'
import { Link } from '@posthog/lemon-ui'

const VIEW_MAP = {
[`${InsightType.TRENDS}`]: <TrendInsight view={InsightType.TRENDS} />,
[`${InsightType.STICKINESS}`]: <TrendInsight view={InsightType.STICKINESS} />,
[`${InsightType.LIFECYCLE}`]: <TrendInsight view={InsightType.LIFECYCLE} />,
[`${InsightType.FUNNELS}`]: <FunnelInsight />,
[`${InsightType.RETENTION}`]: <RetentionContainer />,
[`${InsightType.PATHS}`]: <Paths />,
}

export function InsightContainer({
disableHeader,
disableTable,
Expand Down Expand Up @@ -135,6 +126,25 @@ export function InsightContainer({
return null
})()

function renderActiveView(): JSX.Element | null {
switch (activeView) {
case InsightType.TRENDS:
return <TrendInsight view={InsightType.TRENDS} context={context} />
case InsightType.STICKINESS:
return <TrendInsight view={InsightType.STICKINESS} context={context} />
case InsightType.LIFECYCLE:
return <TrendInsight view={InsightType.LIFECYCLE} context={context} />
case InsightType.FUNNELS:
return <FunnelInsight />
case InsightType.RETENTION:
return <RetentionContainer />
case InsightType.PATHS:
return <Paths />
default:
return null
}
}

function renderTable(): JSX.Element | null {
if (
isFunnels &&
Expand Down Expand Up @@ -255,13 +265,13 @@ export function InsightContainer({
BlockingEmptyState
) : supportsDisplay && showLegend ? (
<div className="insights-graph-container-row flex flex-nowrap">
<div className="insights-graph-container-row-left">{VIEW_MAP[activeView]}</div>
<div className="insights-graph-container-row-left">{renderActiveView()}</div>
<div className="insights-graph-container-row-right">
<InsightLegend />
</div>
</div>
) : (
VIEW_MAP[activeView]
renderActiveView()
)}
</div>
)}
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/queries/nodes/InsightViz/InsightViz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ let uniqueNode = 0

export function InsightViz({ uniqueKey, query, setQuery, context, readOnly }: InsightVizProps): JSX.Element {
const [key] = useState(() => `InsightViz.${uniqueKey || uniqueNode++}`)
const insightProps: InsightLogicProps = {
const insightProps: InsightLogicProps = context?.insightProps || {
dashboardItemId: `new-AdHoc.${key}`,
query,
setQuery,
...(context?.insightProps || {}),
}

if (!insightProps.setQuery && setQuery) {
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/queries/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InsightLogicProps } from '~/types'
import { ChartDisplayType, InsightLogicProps, TrendResult } from '~/types'
import { ComponentType, HTMLProps } from 'react'
import { DataTableNode } from '~/queries/schema'

Expand All @@ -15,6 +15,15 @@ export interface QueryContext {
emptyStateHeading?: string
emptyStateDetail?: string
rowProps?: (record: unknown) => Omit<HTMLProps<HTMLTableRowElement>, 'key'>
/** chart-specific rendering context **/
chartRenderingMetadata?: ChartRenderingMetadata
}

/** Pass custom rendering metadata to specific kinds of charts **/
export interface ChartRenderingMetadata {
[ChartDisplayType.WorldMap]?: {
countryProps?: (countryCode: string, countryData: TrendResult | undefined) => Omit<HTMLProps<SVGElement>, 'key'>
}
}

export type QueryContextColumnTitleComponent = ComponentType<{
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/insights/insightLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export const insightLogic = kea<insightLogicType>([
)
},
],
showPersonsModal: [() => [(_, p) => p.query], (query?: InsightVizNode) => query && query.hidePersonsModal],
showPersonsModal: [() => [(_, p) => p.query], (query?: InsightVizNode) => !query || !query.hidePersonsModal],
}),
listeners(({ actions, selectors, values }) => ({
setFiltersMerge: ({ filters }) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/scenes/insights/views/WorldMap/WorldMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@ const WorldMapSVG = React.memo(
)
)

export function WorldMap({ showPersonsModal = true }: ChartParams): JSX.Element {
export function WorldMap({ showPersonsModal = true, context }: ChartParams): JSX.Element {
const { insightProps } = useValues(insightLogic)
const { countryCodeToSeries, maxAggregatedValue } = useValues(worldMapLogic(insightProps))
const { showTooltip, hideTooltip, updateTooltipCoordinates } = useActions(worldMapLogic(insightProps))
const { chartRenderingMetadata } = insightProps
const { chartRenderingMetadata } = context

Check failure on line 208 in frontend/src/scenes/insights/views/WorldMap/WorldMap.tsx

View workflow job for this annotation

GitHub Actions / Code quality checks

Property 'chartRenderingMetadata' does not exist on type 'QueryContext | undefined'.
const renderingMetadata = chartRenderingMetadata?.[ChartDisplayType.WorldMap]

const svgRef = useWorldMapTooltip(showPersonsModal)
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/scenes/trends/Trends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { WorldMap } from 'scenes/insights/views/WorldMap'
import { BoldNumber } from 'scenes/insights/views/BoldNumber'
import { LemonButton } from '@posthog/lemon-ui'
import { trendsDataLogic } from './trendsDataLogic'
import { QueryContext } from '~/queries/types'

interface Props {
view: InsightType
context?: QueryContext
}

export function TrendInsight({ view }: Props): JSX.Element {
export function TrendInsight({ view, context }: Props): JSX.Element {
const { insightMode } = useValues(insightSceneLogic)
const { insightProps, showPersonsModal } = useValues(insightLogic)

Expand All @@ -30,10 +32,10 @@ export function TrendInsight({ view }: Props): JSX.Element {
display === ChartDisplayType.ActionsAreaGraph ||
display === ChartDisplayType.ActionsBar
) {
return <ActionsLineGraph showPersonsModal={showPersonsModal} />
return <ActionsLineGraph showPersonsModal={showPersonsModal} context={context} />
}
if (display === ChartDisplayType.BoldNumber) {
return <BoldNumber showPersonsModal={showPersonsModal} />
return <BoldNumber showPersonsModal={showPersonsModal} context={context} />
}
if (display === ChartDisplayType.ActionsTable) {
const ActionsTable = InsightsTable
Expand All @@ -47,13 +49,13 @@ export function TrendInsight({ view }: Props): JSX.Element {
)
}
if (display === ChartDisplayType.ActionsPie) {
return <ActionsPie showPersonsModal={showPersonsModal} />
return <ActionsPie showPersonsModal={showPersonsModal} context={context} />
}
if (display === ChartDisplayType.ActionsBarValue) {
return <ActionsHorizontalBar showPersonsModal={showPersonsModal} />
return <ActionsHorizontalBar showPersonsModal={showPersonsModal} context={context} />
}
if (display === ChartDisplayType.WorldMap) {
return <WorldMap showPersonsModal={showPersonsModal} />
return <WorldMap showPersonsModal={showPersonsModal} context={context} />
}
}

Expand Down
12 changes: 5 additions & 7 deletions frontend/src/scenes/web-analytics/WebAnalyticsTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,11 @@ export const WebStatsTrendTile = ({ query }: { query: InsightVizNode }): JSX.Ele
const context = useMemo((): QueryContext => {
return {
...webAnalyticsDataTableQueryContext,
insightProps: {
chartRenderingMetadata: {
[ChartDisplayType.WorldMap]: {
countryProps: (countryCode) => ({
onClick: () => onWorldMapClick(countryCode),
}),
},
chartRenderingMetadata: {
[ChartDisplayType.WorldMap]: {
countryProps: (countryCode) => ({
onClick: () => onWorldMapClick(countryCode),
}),
},
},
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/scenes/web-analytics/webAnalyticsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
},
],
},

{
layout: {
colSpan: 6,
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { QueryContext } from '~/queries/types'

import { JSONContent } from 'scenes/notebooks/Notebook/utils'
import { DashboardCompatibleScenes } from 'lib/components/SceneDashboardChoice/sceneDashboardChoiceModalLogic'
import { HTMLProps } from 'react'

export type Optional<T, K extends string | number | symbol> = Omit<T, K> & { [K in keyof T]?: T[K] }

Expand Down Expand Up @@ -2081,13 +2080,6 @@ export interface HistogramGraphDatum {
label: string
}

/** Pass custom rendering metadata to specific kinds of charts **/
export interface ChartRenderingMetadata {
[ChartDisplayType.WorldMap]?: {
countryProps?: (countryCode: string, countryData: TrendResult | undefined) => Omit<HTMLProps<SVGElement>, 'key'>
}
}

// Shared between insightLogic, dashboardItemLogic, trendsLogic, funnelLogic, pathsLogic, retentionLogic
export interface InsightLogicProps {
/** currently persisted insight */
Expand All @@ -2101,9 +2093,6 @@ export interface InsightLogicProps {
/** query when used as ad-hoc insight */
query?: InsightVizNode
setQuery?: (node: InsightVizNode) => void

/** chart-specific rendering context **/
chartRenderingMetadata?: ChartRenderingMetadata
}

export interface SetInsightOptions {
Expand Down

0 comments on commit 39a9ce1

Please sign in to comment.