| 157 | } |
| 158 | |
| 159 | function ChartUsersView({ chartData, report, date }: ChartUsersViewProps) { |
| 160 | const trpc = useTRPC(); |
| 161 | const [selectedSerieId, setSelectedSerieId] = useState<string | null>( |
| 162 | report.series[0]?.id || null, |
| 163 | ); |
| 164 | const [selectedBreakdownId, setSelectedBreakdownId] = useState<string | null>( |
| 165 | null, |
| 166 | ); |
| 167 | |
| 168 | const selectedReportSerie = useMemo( |
| 169 | () => report.series.find((s) => s.id === selectedSerieId), |
| 170 | [report.series, selectedSerieId], |
| 171 | ); |
| 172 | |
| 173 | // Get all chart series that match the selected report serie |
| 174 | const matchingChartSeries = useMemo(() => { |
| 175 | if (!selectedSerieId || !chartData) return []; |
| 176 | return chartData.series.filter((s) => s.event.id === selectedSerieId); |
| 177 | }, [chartData?.series, selectedSerieId]); |
| 178 | |
| 179 | const selectedBreakdown = useMemo(() => { |
| 180 | if (!selectedBreakdownId) return null; |
| 181 | return matchingChartSeries.find((s) => s.id === selectedBreakdownId); |
| 182 | }, [matchingChartSeries, selectedBreakdownId]); |
| 183 | |
| 184 | // Reset breakdown selection when serie changes |
| 185 | const handleSerieChange = (value: string) => { |
| 186 | setSelectedSerieId(value); |
| 187 | setSelectedBreakdownId(null); |
| 188 | }; |
| 189 | |
| 190 | const profilesQuery = useQuery( |
| 191 | trpc.chart.getProfiles.queryOptions( |
| 192 | { |
| 193 | projectId: report.projectId, |
| 194 | date: date, |
| 195 | series: |
| 196 | selectedReportSerie && selectedReportSerie.type === 'event' |
| 197 | ? [ |
| 198 | { |
| 199 | ...selectedReportSerie, |
| 200 | filters: [ |
| 201 | ...(report.globalFilters ?? []), |
| 202 | ...selectedReportSerie.filters, |
| 203 | ], |
| 204 | }, |
| 205 | ] |
| 206 | : [], |
| 207 | breakdowns: selectedBreakdown?.event.breakdowns, |
| 208 | interval: report.interval, |
| 209 | }, |
| 210 | { |
| 211 | enabled: !!selectedReportSerie && selectedReportSerie.type === 'event', |
| 212 | }, |
| 213 | ), |
| 214 | ); |
| 215 | |
| 216 | const profiles = profilesQuery.data ?? []; |