|  | 
| 1 | 1 | import { | 
| 2 |  | -  type ChartOptions as ChartNativeOptions, | 
|  | 2 | +  type ChartOptions as TimeChartNativeOptions, | 
|  | 3 | +  type YieldCurveChartOptions as YieldCurveChartNativeOptions, | 
|  | 4 | +  type PriceChartOptions as PriceChartNativeOptions, | 
| 3 | 5 |   type DeepPartial, | 
| 4 | 6 |   type IChartApi, | 
| 5 | 7 |   type MouseEventHandler, | 
| 6 | 8 |   type Time, | 
|  | 9 | +  type IYieldCurveChartApi, | 
|  | 10 | +  // createOptionsChart, | 
|  | 11 | +  // createYieldCurveChart | 
| 7 | 12 | } from "lightweight-charts"; | 
|  | 13 | +import type { IChartApiBase } from "lightweight-charts"; | 
| 8 | 14 | import type { JSX, ReactNode } from "react"; | 
| 9 | 15 | 
 | 
| 10 | 16 | /** | 
| 11 |  | - * Chart component props. | 
|  | 17 | + * Chart component properties that can be used to create a chart. | 
| 12 | 18 |  */ | 
| 13 |  | -export type ChartProps = { | 
| 14 |  | -  children?: ReactNode; | 
| 15 |  | -  containerProps?: JSX.IntrinsicElements["div"]; | 
| 16 |  | -  onClick?: MouseEventHandler<Time>; | 
| 17 |  | -  onCrosshairMove?: MouseEventHandler<Time>; | 
| 18 |  | -  options?: DeepPartial<ChartNativeOptions>; | 
| 19 |  | -  onDblClick?: MouseEventHandler<Time>; | 
| 20 |  | -}; | 
|  | 19 | +export type ChartApiType = IYieldCurveChartApi | IChartApiBase<number> | IChartApi; | 
| 21 | 20 | 
 | 
| 22 | 21 | /** | 
| 23 | 22 |  * Chart API reference type that can be used to access the chart plugin API. | 
| 24 | 23 |  */ | 
| 25 |  | -export type ChartApiRef = { | 
| 26 |  | -  _chart: IChartApi | null; | 
| 27 |  | -  api: () => IChartApi | null; | 
| 28 |  | -  init: () => IChartApi | null; | 
|  | 24 | +export type ChartApiRef<T extends ChartApiType> = { | 
|  | 25 | +  _chart: T | null; | 
|  | 26 | +  api: () => T | null; | 
|  | 27 | +  init: () => T | null; | 
| 29 | 28 |   clear: () => void; | 
| 30 | 29 | }; | 
| 31 | 30 | 
 | 
| 32 | 31 | /** | 
| 33 | 32 |  * Chart context that provides access to the chart API and readiness state. | 
| 34 | 33 |  */ | 
| 35 |  | -export interface IChartContext { | 
| 36 |  | -  chartApiRef: ChartApiRef | null; | 
|  | 34 | +export interface IChartContext<T extends ChartApiType> { | 
|  | 35 | +  chartApiRef: ChartApiRef<T> | null; | 
| 37 | 36 |   isReady: boolean; | 
| 38 | 37 | } | 
| 39 | 38 | 
 | 
|  | 39 | +type ChartCommonProps = { | 
|  | 40 | +  children?: ReactNode; | 
|  | 41 | +  containerProps?: JSX.IntrinsicElements["div"]; | 
|  | 42 | +}; | 
|  | 43 | + | 
|  | 44 | +type TimeChartOwnProps = { | 
|  | 45 | +  onClick?: MouseEventHandler<Time>; | 
|  | 46 | +  onCrosshairMove?: MouseEventHandler<Time>; | 
|  | 47 | +  onDblClick?: MouseEventHandler<Time>; | 
|  | 48 | +  options?: DeepPartial<TimeChartNativeOptions>; | 
|  | 49 | +}; | 
|  | 50 | + | 
|  | 51 | +type YieldCurveChartProps = { | 
|  | 52 | +  onClick?: MouseEventHandler<number>; | 
|  | 53 | +  onCrosshairMove?: MouseEventHandler<number>; | 
|  | 54 | +  onDblClick?: MouseEventHandler<number>; | 
|  | 55 | +  options?: DeepPartial<YieldCurveChartNativeOptions>; | 
|  | 56 | +}; | 
|  | 57 | + | 
|  | 58 | +type OptionsChartOwnProps = { | 
|  | 59 | +  onClick?: MouseEventHandler<number>; | 
|  | 60 | +  onCrosshairMove?: MouseEventHandler<number>; | 
|  | 61 | +  onDblClick?: MouseEventHandler<number>; | 
|  | 62 | +  options?: DeepPartial<PriceChartNativeOptions>; | 
|  | 63 | +}; | 
|  | 64 | + | 
| 40 | 65 | /** | 
| 41 | 66 |  * Chart component properties that can be used to create a chart. | 
| 42 | 67 |  */ | 
|  | 68 | +export type ChartOwnProps = | 
|  | 69 | +  | TimeChartOwnProps | 
|  | 70 | +  | YieldCurveChartProps | 
|  | 71 | +  | OptionsChartOwnProps; | 
|  | 72 | + | 
|  | 73 | +/** | 
|  | 74 | + * Chart component props. | 
|  | 75 | + */ | 
|  | 76 | +export type ChartProps = ChartCommonProps & TimeChartOwnProps; | 
|  | 77 | + | 
|  | 78 | +/** | 
|  | 79 | + * Yield chart component properties. | 
|  | 80 | + */ | 
|  | 81 | +export type YieldCurveChartOwnProps = ChartCommonProps & YieldCurveChartProps; | 
|  | 82 | + | 
|  | 83 | +/** | 
|  | 84 | + * Options chart component properties. | 
|  | 85 | + */ | 
|  | 86 | +export type OptionsChartProps = ChartCommonProps & OptionsChartOwnProps; | 
|  | 87 | + | 
|  | 88 | +/** | 
|  | 89 | + * Chart component properties. | 
|  | 90 | + */ | 
| 43 | 91 | export type ChartComponentProps = { | 
| 44 | 92 |   container: HTMLElement; | 
| 45 | 93 | } & Omit<ChartProps, "containerProps">; | 
| 46 | 94 | 
 | 
| 47 | 95 | /** | 
| 48 | 96 |  * Options for the useChart hook. | 
| 49 | 97 |  */ | 
| 50 |  | -export type UseChartOptions = { | 
|  | 98 | +export type UseChartOptions<T extends ChartOwnProps> = { | 
| 51 | 99 |   container: HTMLElement; | 
| 52 |  | -} & Omit<ChartProps, "children" | "containerProps">; | 
|  | 100 | +  createChartHandler: ( | 
|  | 101 | +    container: string | HTMLElement, | 
|  | 102 | +    options?: DeepPartial<T["options"]> | 
|  | 103 | +  ) => IChartApi; | 
|  | 104 | +} & T; | 
|  | 105 | + | 
|  | 106 | +// so the chart api for different charts is also diferent - different type | 
|  | 107 | +// can i handle it in 1 hook? | 
|  | 108 | +// need to explore here more | 
0 commit comments