|
| 1 | +import React, { ReactNode, useCallback } from 'react' |
| 2 | + |
| 3 | +import { |
| 4 | + DimensionCellWithBar, |
| 5 | + DimensionCellWithBarProps, |
| 6 | + IndexBreakdown |
| 7 | +} from '../reports/index-breakdown' |
| 8 | +import { |
| 9 | + BREAKDOWN_REPORTS, |
| 10 | + BreakdownReportKey |
| 11 | +} from '../reports/reports-config' |
| 12 | +import { chooseBreakdownMetricsByContext } from '../breakdowns' |
| 13 | +import { |
| 14 | + hasConversionGoalFilter, |
| 15 | + isRealTimeDashboard |
| 16 | +} from '../../util/filters' |
| 17 | +import { useDashboardStateContext } from '../../dashboard-state-context' |
| 18 | +import { QueryApiResponse, QueryResultRow } from '../../api' |
| 19 | +import { NonTimeDimension } from '../../stats-query' |
| 20 | +import { FilterInfo } from '../../components/drilldown-link' |
| 21 | + |
| 22 | +const BAR_COLOR = 'bg-red-50 group-hover/row:bg-red-100' |
| 23 | + |
| 24 | +type ConversionsProps = { |
| 25 | + afterFetchData?: (data: QueryApiResponse) => void |
| 26 | + onGoalFilterClick?: (goalName: string) => void |
| 27 | +} |
| 28 | + |
| 29 | +export default function Conversions({ |
| 30 | + afterFetchData, |
| 31 | + onGoalFilterClick |
| 32 | +}: ConversionsProps): ReactNode { |
| 33 | + const { dashboardState } = useDashboardStateContext() |
| 34 | + const reportConfig = BREAKDOWN_REPORTS[BreakdownReportKey.goals] |
| 35 | + |
| 36 | + const baseMetrics = chooseBreakdownMetricsByContext( |
| 37 | + reportConfig.metricsByContext, |
| 38 | + { |
| 39 | + isRealtime: isRealTimeDashboard(dashboardState), |
| 40 | + isDetailed: false, |
| 41 | + hasConversionGoalFilter: hasConversionGoalFilter(dashboardState), |
| 42 | + isRevenueAvailable: false |
| 43 | + } |
| 44 | + ) |
| 45 | + |
| 46 | + /*global BUILD_EXTRA*/ |
| 47 | + const metrics = BUILD_EXTRA |
| 48 | + ? [...baseMetrics, 'total_revenue' as const, 'average_revenue' as const] |
| 49 | + : baseMetrics |
| 50 | + |
| 51 | + const DimensionElement = useCallback( |
| 52 | + (props: DimensionCellWithBarProps) => { |
| 53 | + const goalName = props.row.dimensions[0] |
| 54 | + return ( |
| 55 | + <DimensionCellWithBar |
| 56 | + {...props} |
| 57 | + barClassName={BAR_COLOR} |
| 58 | + text={goalName} |
| 59 | + getFilterInfo={getGoalsFilterInfo} |
| 60 | + onClick={() => onGoalFilterClick && onGoalFilterClick(goalName)} |
| 61 | + /> |
| 62 | + ) |
| 63 | + }, |
| 64 | + [onGoalFilterClick] |
| 65 | + ) |
| 66 | + |
| 67 | + return ( |
| 68 | + <IndexBreakdown |
| 69 | + metrics={metrics} |
| 70 | + dimensions={reportConfig.dimensions} |
| 71 | + dimensionLabel={reportConfig.dimensionLabel} |
| 72 | + alwaysOnFilters={reportConfig.alwaysOnFilters} |
| 73 | + DimensionElement={DimensionElement} |
| 74 | + onDataReady={afterFetchData} |
| 75 | + /> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +function getGoalsFilterInfo( |
| 80 | + _dimension: NonTimeDimension, |
| 81 | + row: QueryResultRow |
| 82 | +): FilterInfo { |
| 83 | + const goalName = row.dimensions[0] |
| 84 | + return { |
| 85 | + prefix: 'goal', |
| 86 | + filter: ['is', 'goal', [goalName]] |
| 87 | + } |
| 88 | +} |
0 commit comments