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!

1.2 KB
Raw Blame
import { Chart, ChartOptions, GridLineOptions } from 'billboard.js'
import React, { useEffect, useMemo } from 'react'

import BillboardChart from '~/ui/BillboardChart'

import { useViewProjectContext } from '../ViewProject'

import { useChartManager } from './ChartManager'

interface MainChartProps {
  chartId: string
  options: ChartOptions
  dataNames?: Record<string, string>
  className?: string
  deps?: any[]
  xGridLines?: GridLineOptions[]
}

export const MainChart = ({
  chartId,
  options,
  dataNames,
  className,
  deps,
  xGridLines,
}: MainChartProps) => {
  const { registerChart, unregisterChart } = useChartManager()
  const { timezone } = useViewProjectContext()

  const mergedDeps = useMemo(
    () => deps || [options, dataNames],
    [deps, options, dataNames],
  )

  const handleChartReady = (chart: Chart | null) => {
    registerChart(chartId, chart)
  }

  useEffect(() => {
    return () => {
      unregisterChart(chartId)
    }
  }, [chartId, unregisterChart])

  return (
    <BillboardChart
      options={options}
      dataNames={dataNames}
      className={className}
      onReady={handleChartReady}
      deps={mergedDeps}
      xGridLines={xGridLines}
      relativeTimeTimezone={timezone}
    />
  )
}