-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNoteManager.tsx
More file actions
210 lines (186 loc) · 7.32 KB
/
NoteManager.tsx
File metadata and controls
210 lines (186 loc) · 7.32 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { memo, useCallback, useContext, useEffect, useRef } from "react";
import "./NoteManager.css";
import type { ISynctexBlockId } from "@fluffylabs/links-metadata";
import { Alert, Button, cn } from "@fluffylabs/shared-ui";
import { twMerge } from "tailwind-merge";
import { useLatestCallback } from "../../hooks/useLatestCallback";
import { type ILocationContext, LocationContext } from "../LocationProvider/LocationProvider";
import type { IDecoratedNote } from "../NotesProvider/types/DecoratedNote";
import type { IStorageNote } from "../NotesProvider/types/StorageNote";
import { areSelectionsEqual } from "../NotesProvider/utils/areSelectionsEqual";
import { type ISelectionContext, SelectionContext } from "../SelectionProvider/SelectionProvider";
import { InactiveNoteSkeleton } from "./components/InactiveNoteSkeleton";
import { NewNote } from "./components/NewNote";
import { NotesList } from "./components/NotesList";
import { useFilteredNoteAlert } from "./hooks/useFilteredNoteAlert";
import { useNoteManagerNotes } from "./useNoteManagerNotes";
const DEFAULT_AUTHOR = "";
export function NoteManager({ className }: { className?: string }) {
return (
<div className={twMerge("notes-wrapper gap-4", className)}>
<Notes />
</div>
);
}
const MemoizedNotesList = memo(NotesList);
function Notes() {
const { locationParams, setLocationParams } = useContext(LocationContext) as ILocationContext;
const {
notesManagerNotes: notes,
activeNotes,
addNote,
sectionTitlesLoaded,
notesReady,
deleteNote,
updateNote,
} = useNoteManagerNotes();
const { selectedBlocks, pageNumber, handleClearSelection } = useContext(SelectionContext) as ISelectionContext;
const keepShowingNewNote = useRef<{ selectionEnd: ISynctexBlockId; selectionStart: ISynctexBlockId }>(undefined);
const latestHandleClearSelection = useLatestCallback(handleClearSelection);
const { noteAlertVisibilityState, triggerFilteredNoteAlert, closeNoteAlert } = useFilteredNoteAlert();
const memoizedHandleDeleteNote = useCallback(
(note: IDecoratedNote) => {
deleteNote(note);
latestHandleClearSelection.current();
},
[latestHandleClearSelection, deleteNote],
);
const memoizedHandleUpdateNote = useCallback(
(note: IDecoratedNote, newNote: IStorageNote) => {
// NOTE(optimistic): intentional mutation for immediate UI feedback; be aware this bypasses immutability
note.original.content = newNote.content;
note.original.labels = newNote.labels;
const { isVisible } = updateNote(note, newNote);
if (!isVisible) {
handleClearSelection();
triggerFilteredNoteAlert("visibleForUpdated");
}
},
[updateNote, handleClearSelection, triggerFilteredNoteAlert],
);
const handleNewNoteCancel = useCallback(() => {
handleClearSelection();
}, [handleClearSelection]);
const handleAddNoteClick = useCallback(
({ noteContent, labels }: { noteContent: string; labels: string[] }) => {
if (
selectedBlocks.length === 0 ||
pageNumber === null ||
!locationParams.selectionStart ||
!locationParams.selectionEnd
) {
throw new Error("Attempted saving a note without selection.");
}
const newNote: IStorageNote = {
noteVersion: 3,
content: noteContent,
date: Date.now(),
author: DEFAULT_AUTHOR,
selectionStart: locationParams.selectionStart,
selectionEnd: locationParams.selectionEnd,
version: locationParams.version,
labels,
};
const { isVisible } = addNote(newNote);
if (!isVisible) {
triggerFilteredNoteAlert("visibleForCreated");
handleClearSelection();
}
keepShowingNewNote.current = {
selectionStart: locationParams.selectionStart,
selectionEnd: locationParams.selectionEnd,
};
},
[pageNumber, selectedBlocks, locationParams, addNote, handleClearSelection, triggerFilteredNoteAlert],
);
const locationRef = useRef({ locationParams, setLocationParams });
locationRef.current = { locationParams, setLocationParams };
const memoizedHandleSelectNote = useCallback(
(note: IDecoratedNote, { type = "currentVersion" }: { type: "currentVersion" | "originalVersion" | "close" }) => {
let selectionStart: ISynctexBlockId | undefined = note.current.selectionStart;
let selectionEnd: ISynctexBlockId | undefined = note.current.selectionEnd;
let version: string | undefined = locationRef.current.locationParams.version;
if (type === "originalVersion") {
selectionStart = note.original.selectionStart;
selectionEnd = note.original.selectionEnd;
version = note.original.version;
}
if (type === "close") {
selectionStart = undefined;
selectionEnd = undefined;
}
locationRef.current.setLocationParams({
selectionStart,
selectionEnd,
version: version,
});
},
[],
);
const isActiveNotes = notes.some((note) => activeNotes.has(note.noteObject));
const readyAndLoaded = notesReady && sectionTitlesLoaded;
useEffect(() => {
if (readyAndLoaded) {
keepShowingNewNote.current = undefined;
}
}, [readyAndLoaded]);
return (
<>
{noteAlertVisibilityState !== "hidden" && (
<Alert intent="warning">
<Alert.Title>
{noteAlertVisibilityState === "visibleForUpdated"
? "Note hidden after update"
: "Note hidden by label filter"}
</Alert.Title>
<div className="flex gap-4">
<Alert.Text>
{noteAlertVisibilityState === "visibleForUpdated"
? "Updated note doesn't match active labels."
: "Created note doesn't match active labels."}
</Alert.Text>
<Button variant="secondary" intent="warning" size="sm" className="self-end" onClick={closeNoteAlert}>
Close
</Button>
</div>
</Alert>
)}
<div className={cn("note-manager flex flex-col gap-2.5", !readyAndLoaded && "opacity-30 pointer-events-none")}>
{locationParams.selectionEnd &&
locationParams.selectionStart &&
pageNumber !== null &&
selectedBlocks.length > 0 &&
!isActiveNotes &&
(readyAndLoaded || areSelectionsEqual(locationParams, keepShowingNewNote.current)) && (
<NewNote
selectionStart={locationParams.selectionStart}
selectionEnd={locationParams.selectionEnd}
version={locationParams.version}
onCancel={handleNewNoteCancel}
onSave={handleAddNoteClick}
/>
)}
{!readyAndLoaded && notes.length === 0 && (
<>
<InactiveNoteSkeleton />
<InactiveNoteSkeleton />
<InactiveNoteSkeleton />
<InactiveNoteSkeleton />
</>
)}
{readyAndLoaded && notes.length === 0 && (
<div className="no-notes text-sidebar-foreground">No notes available</div>
)}
{notes.length > 0 && (
<MemoizedNotesList
activeNotes={activeNotes}
notes={notes}
onEditNote={memoizedHandleUpdateNote}
onDeleteNote={memoizedHandleDeleteNote}
onSelectNote={memoizedHandleSelectNote}
/>
)}
</div>
</>
);
}