forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.ts
More file actions
35 lines (30 loc) 路 1.12 KB
/
Copy pathannotations.ts
File metadata and controls
35 lines (30 loc) 路 1.12 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
const STORAGE_KEY = 'gistpin-annotations';
export interface Annotation {
id: string;
chartId: string;
date: string;
text: string;
}
export function getAnnotations(chartId?: string): Annotation[] {
if (typeof window === 'undefined') return [];
try {
const all = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? '[]') as Annotation[];
return chartId ? all.filter((a) => a.chartId === chartId) : all;
} catch {
return [];
}
}
export function saveAnnotation(annotation: Omit<Annotation, 'id'>): Annotation {
const all = getAnnotations();
const next: Annotation = { ...annotation, id: crypto.randomUUID() };
localStorage.setItem(STORAGE_KEY, JSON.stringify([...all, next]));
return next;
}
export function updateAnnotation(id: string, patch: Partial<Pick<Annotation, 'text' | 'date'>>): void {
const all = getAnnotations().map((a) => (a.id === id ? { ...a, ...patch } : a));
localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
}
export function deleteAnnotation(id: string): void {
const all = getAnnotations().filter((a) => a.id !== id);
localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
}