aboutsummaryrefslogtreecommitdiff
path: root/apps/web/stores/highlights.ts
blob: de04e10590d9b888df44294b39d291028281d5c9 (plain) (blame)
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
import { create } from "zustand"

interface GraphHighlightsState {
	documentIds: string[]
	lastUpdated: number
	setDocumentIds: (ids: string[]) => void
	clear: () => void
}

export const useGraphHighlightsStore = create<GraphHighlightsState>()(
	(set, get) => ({
		documentIds: [],
		lastUpdated: 0,
		setDocumentIds: (ids) => {
			const next = Array.from(new Set(ids))
			const prev = get().documentIds
			if (
				prev.length === next.length &&
				prev.every((id) => next.includes(id))
			) {
				return
			}
			set({ documentIds: next, lastUpdated: Date.now() })
		},
		clear: () => set({ documentIds: [], lastUpdated: Date.now() }),
	}),
)

export function useGraphHighlights() {
	const documentIds = useGraphHighlightsStore((s) => s.documentIds)
	const lastUpdated = useGraphHighlightsStore((s) => s.lastUpdated)
	const setDocumentIds = useGraphHighlightsStore((s) => s.setDocumentIds)
	const clear = useGraphHighlightsStore((s) => s.clear)
	return { documentIds, lastUpdated, setDocumentIds, clear }
}