forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAnnotations.ts
More file actions
42 lines (35 loc) 路 1.01 KB
/
Copy pathuseAnnotations.ts
File metadata and controls
42 lines (35 loc) 路 1.01 KB
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
'use client';
import { useCallback, useEffect, useState } from 'react';
import {
deleteAnnotation,
getAnnotations,
saveAnnotation,
updateAnnotation,
type Annotation,
} from '@/lib/annotations';
export function useAnnotations(chartId: string) {
const [annotations, setAnnotations] = useState<Annotation[]>([]);
useEffect(() => {
setAnnotations(getAnnotations(chartId));
}, [chartId]);
const add = useCallback(
(date: string, text: string) => {
const next = saveAnnotation({ chartId, date, text });
setAnnotations((prev) => [...prev, next]);
return next;
},
[chartId],
);
const edit = useCallback(
(id: string, patch: Partial<Pick<Annotation, 'text' | 'date'>>) => {
updateAnnotation(id, patch);
setAnnotations(getAnnotations(chartId));
},
[chartId],
);
const remove = useCallback((id: string) => {
deleteAnnotation(id);
setAnnotations((prev) => prev.filter((a) => a.id !== id));
}, []);
return { annotations, add, edit, remove };
}