Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

3.0 KB
Raw Blame
import { ChartOptions } from 'billboard.js'
import React, { useMemo } from 'react'
import { useTranslation } from 'react-i18next'

import { Annotation } from '~/lib/models/Project'
import type { ChartDataPointClick } from '~/pages/Project/View/utils/chartPoint'

import { MainChart } from '../../View/components/MainChart'
import {
  getAnnotationGridLines,
  getSettings,
} from '../../View/ViewProject.helpers'

interface TrafficChartProps {
  chartData: any
  timeBucket: string
  activeChartMetrics: Record<string, boolean>
  applyRegions: boolean
  timeFormat: string
  rotateXAxis: boolean
  chartType: string
  customEventsChartData?: Record<string, string[]>
  dataChartCompare?: any
  onZoom?: (domain: [Date, Date] | null) => void
  enableZoom?: boolean
  dataNames: Record<string, string>
  className?: string
  annotations?: Annotation[]
  period?: string
  timezone?: string
  onDataPointClick?: ChartDataPointClick
}

export const TrafficChart = ({
  chartData,
  timeBucket,
  activeChartMetrics,
  applyRegions,
  timeFormat,
  rotateXAxis,
  chartType,
  customEventsChartData,
  dataChartCompare,
  onZoom,
  enableZoom,
  dataNames,
  className,
  annotations,
  period,
  timezone,
  onDataPointClick,
}: TrafficChartProps) => {
  const { t } = useTranslation()

  const dataPointClickLabel = useMemo(
    () => (onDataPointClick ? t('project.exploreSessions') : undefined),
    [onDataPointClick, t],
  )

  const annotationLines = useMemo(
    () => getAnnotationGridLines(annotations),
    [annotations],
  )

  const options: ChartOptions = useMemo(() => {
    return getSettings(
      chartData,
      timeBucket,
      activeChartMetrics,
      applyRegions,
      timeFormat,
      rotateXAxis,
      chartType,
      customEventsChartData,
      dataChartCompare,
      onZoom,
      enableZoom,
      undefined,
      period,
      timezone,
      onDataPointClick,
      dataPointClickLabel,
    )
  }, [
    chartData,
    timeBucket,
    activeChartMetrics,
    applyRegions,
    timeFormat,
    rotateXAxis,
    chartType,
    customEventsChartData,
    dataChartCompare,
    onZoom,
    enableZoom,
    period,
    timezone,
    onDataPointClick,
    dataPointClickLabel,
  ])

  const deps = useMemo(
    () => [
      chartData,
      timeBucket,
      activeChartMetrics,
      applyRegions,
      timeFormat,
      rotateXAxis,
      chartType,
      customEventsChartData,
      dataChartCompare,
      onZoom,
      enableZoom,
      period,
      timezone,
      onDataPointClick,
      dataPointClickLabel,
    ],
    [
      chartData,
      timeBucket,
      activeChartMetrics,
      applyRegions,
      timeFormat,
      rotateXAxis,
      chartType,
      customEventsChartData,
      dataChartCompare,
      onZoom,
      enableZoom,
      period,
      timezone,
      onDataPointClick,
      dataPointClickLabel,
    ],
  )

  return (
    <MainChart
      chartId='traffic-chart'
      options={options}
      dataNames={dataNames}
      className={className}
      deps={deps}
      xGridLines={annotationLines}
    />
  )
}