-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuseNoteManagerNotes.ts
More file actions
149 lines (127 loc) · 4.62 KB
/
useNoteManagerNotes.ts
File metadata and controls
149 lines (127 loc) · 4.62 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { useCallback, useContext, useEffect, useRef, useState } from "react";
import { useLatestCallback } from "../../hooks/useLatestCallback";
import { CodeSyncContext, type ICodeSyncContext } from "../CodeSyncProvider/CodeSyncProvider";
import { type INotesContext, NotesContext } from "../NotesProvider/NotesProvider";
import type { IDecoratedNote } from "../NotesProvider/types/DecoratedNote";
export type INotesMangerNote = {
noteObject: IDecoratedNote;
metadata: {
sectionTitle: string;
subSectionTitle: string;
};
};
export const useNoteManagerNotes = () => {
const { notesReady, activeNotes, notes, handleAddNote, handleDeleteNote, handleUpdateNote } = useContext(
NotesContext,
) as INotesContext;
const { getSectionTitleAtSynctexBlock, getSubsectionTitleAtSynctexBlock, isSynctexLoaded } = useContext(
CodeSyncContext,
) as ICodeSyncContext;
const metadataCacheByKey = useRef(new Map<string, INotesMangerNote["metadata"]>());
const latestHandleAddNote = useLatestCallback(handleAddNote);
const latestDeleteNote = useLatestCallback(handleDeleteNote);
const latestUpdateNote = useLatestCallback(handleUpdateNote);
const addNote = useCallback<INotesContext["handleAddNote"]>(
(noteToAdd) => {
const { isVisible } = latestHandleAddNote.current(noteToAdd);
return { isVisible };
},
[latestHandleAddNote],
);
const updateNote = useCallback<INotesContext["handleUpdateNote"]>(
(noteToReplace, newNote) => {
metadataCacheByKey.current.delete(createNoteCacheKey(noteToReplace));
return latestUpdateNote.current(noteToReplace, newNote);
},
[latestUpdateNote],
);
const deleteNote = useCallback<INotesContext["handleDeleteNote"]>(
(noteToDelete) => {
metadataCacheByKey.current.delete(createNoteCacheKey(noteToDelete));
latestDeleteNote.current(noteToDelete);
},
[latestDeleteNote],
);
const [notesManagerNotes, setNotesManagerNotes] = useState<INotesMangerNote[]>([]);
const [sectionTitlesLoaded, setSectionTitlesLoaded] = useState(false);
useEffect(() => {
let canceled = false;
if (!notesReady || !isSynctexLoaded) {
setSectionTitlesLoaded(false);
return;
}
(async () => {
setSectionTitlesLoaded(false);
const newNotesManagerNotes: INotesMangerNote[] = [];
const promiseArray: Promise<[INotesMangerNote["metadata"], IDecoratedNote]>[] = [];
for (const maybeNewNote of notes) {
const cachedEntry = metadataCacheByKey.current.get(createNoteCacheKey(maybeNewNote));
if (cachedEntry) {
promiseArray.push(Promise.resolve([cachedEntry, maybeNewNote]));
} else {
promiseArray.push(
getSectionTitles(maybeNewNote, {
getSectionTitleAtSynctexBlock,
getSubsectionTitleAtSynctexBlock,
}).then((sectionTitle) => {
return [sectionTitle, maybeNewNote] as const;
}),
);
}
}
const asyncResults = Promise.all(promiseArray);
for (const result of await asyncResults) {
if (canceled) return;
const [sectionTitles, note] = result;
const noteManagerNote = {
noteObject: note,
metadata: {
sectionTitle: sectionTitles.sectionTitle,
subSectionTitle: sectionTitles.subSectionTitle,
},
};
metadataCacheByKey.current.set(createNoteCacheKey(note), noteManagerNote.metadata);
newNotesManagerNotes.push(noteManagerNote);
}
setSectionTitlesLoaded(true);
setNotesManagerNotes(newNotesManagerNotes);
})();
return () => {
canceled = true;
};
}, [notes, notesReady, getSectionTitleAtSynctexBlock, getSubsectionTitleAtSynctexBlock, isSynctexLoaded]);
return {
notesReady,
activeNotes,
sectionTitlesLoaded,
notes,
notesManagerNotes,
addNote,
deleteNote,
updateNote,
};
};
async function getSectionTitles(
note: IDecoratedNote,
{
getSectionTitleAtSynctexBlock,
getSubsectionTitleAtSynctexBlock,
}: Pick<ICodeSyncContext, "getSectionTitleAtSynctexBlock" | "getSubsectionTitleAtSynctexBlock">,
) {
const [sectionTitle, subSectionTitle] = await Promise.all([
getSectionTitleAtSynctexBlock(note.current.selectionStart),
getSubsectionTitleAtSynctexBlock(note.current.selectionStart),
]);
return {
sectionTitle: sectionTitle ?? "",
subSectionTitle: subSectionTitle ?? "",
};
}
const createNoteCacheKey = (note: IDecoratedNote) => {
return JSON.stringify({
key: note.key,
start: note.current.selectionStart,
end: note.current.selectionEnd,
version: note.current.version,
});
};