/
hooks.ts
52 lines (49 loc) · 1.3 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { useMemo } from 'react'
import { bindAnnotations, computeAnnotation } from './compute'
import {
AnnotationDimensionsGetter,
AnnotationMatcher,
AnnotationPositionGetter,
BoundAnnotation,
} from './types'
/**
* Bind annotations to a dataset.
*/
export const useAnnotations = <Datum>({
data,
annotations,
getPosition,
getDimensions,
}: {
data: Datum[]
annotations: AnnotationMatcher<Datum>[]
getPosition: AnnotationPositionGetter<Datum>
getDimensions: AnnotationDimensionsGetter<Datum>
}) =>
useMemo(
() =>
bindAnnotations<Datum>({
data,
annotations,
getPosition,
getDimensions,
}),
[data, annotations, getPosition, getDimensions]
)
export const useComputedAnnotations = <Datum>({
annotations,
}: {
annotations: BoundAnnotation<Datum>[]
}) =>
useMemo(
() =>
annotations.map(annotation => ({
...annotation,
computed: computeAnnotation<Datum>({
...annotation,
}),
})),
[annotations]
)
export const useComputedAnnotation = <Datum>(annotation: BoundAnnotation<Datum>) =>
useMemo(() => computeAnnotation<Datum>(annotation), [annotation])