import type { ChartType as ChartJSChartType, PluginChartOptions } from 'chart.js'; import { useMemo } from 'react'; import type { DeepPartial } from 'utility-types'; import type { ChartProps } from '../../core'; import { barOptionsHandler, barPluginsHandler } from './barChartOptions'; import type { ChartSpecificOptionsProps, UseChartSpecificOptionsProps } from './interfaces'; import { pieOptionsHandler, piePluginsHandler } from './pieChartOptions'; import type { ChartType } from '@buster/server-shared/metrics'; export const useChartSpecificOptions = ({ selectedChartType, ...props }: UseChartSpecificOptionsProps): { chartPlugins: DeepPartial>['plugins']; chartOptions: ChartProps['options']; } => { const chartOptions = useMemo(() => { return chartTypeOptionsHandler[selectedChartType](props); }, [props]); const chartPlugins = useMemo(() => { return chartTypePluginsHandler[selectedChartType](props); }, [props]); return { chartPlugins, chartOptions, }; }; const DEFAULT_OPTIONS: ChartProps['options'] = {}; const defaultHandler = (): ChartProps['options'] => DEFAULT_OPTIONS; const chartTypeOptionsHandler: Record< ChartType, (props: ChartSpecificOptionsProps) => ChartProps['options'] > = { ['pie']: pieOptionsHandler, ['line']: defaultHandler, ['scatter']: defaultHandler, ['bar']: barOptionsHandler, ['combo']: defaultHandler, ['metric']: defaultHandler, ['table']: defaultHandler, }; //********** PLUGINS ************ */ const DEFAULT_PLUGINS: DeepPartial>['plugins'] = {}; const defaultPluginsHandler = () => DEFAULT_PLUGINS; const chartTypePluginsHandler: Record< ChartType, (props: ChartSpecificOptionsProps) => DeepPartial>['plugins'] > = { ['pie']: piePluginsHandler, ['line']: defaultPluginsHandler, ['scatter']: defaultPluginsHandler, ['bar']: barPluginsHandler, ['combo']: defaultPluginsHandler, ['metric']: defaultPluginsHandler, ['table']: defaultPluginsHandler, };