import { useMemo } from 'react' import { bindAnnotations, computeAnnotation } from './compute' import { AnnotationDimensionsGetter, AnnotationMatcher, AnnotationPositionGetter, BoundAnnotation, } from './types' /** * Bind annotations to a dataset. */ export const useAnnotations = ({ data, annotations, getPosition, getDimensions, }: { data: readonly Datum[] annotations: readonly AnnotationMatcher[] getPosition: AnnotationPositionGetter getDimensions: AnnotationDimensionsGetter }) => useMemo( () => bindAnnotations({ data, annotations, getPosition, getDimensions, }), [data, annotations, getPosition, getDimensions] ) export const useComputedAnnotations = ({ annotations, }: { annotations: readonly BoundAnnotation[] }) => useMemo( () => annotations.map(annotation => ({ ...annotation, computed: computeAnnotation({ ...annotation, }), })), [annotations] ) export const useComputedAnnotation = (annotation: BoundAnnotation) => useMemo(() => computeAnnotation(annotation), [annotation])