-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNotesProvider.tsx
More file actions
260 lines (225 loc) · 9.5 KB
/
NotesProvider.tsx
File metadata and controls
260 lines (225 loc) · 9.5 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { type ReactNode, createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
import { type ILocationContext, LocationContext } from "../LocationProvider/LocationProvider";
import { LABEL_IMPORTED } from "./consts/labels";
import { NEW_REMOTE_SOURCE_ID } from "./consts/remoteSources";
import { useDecoratedNotes } from "./hooks/useDecoratedNotes";
import { type ILabelTreeNode, getFilteredNotes, useLabels } from "./hooks/useLabels";
import { useRemoteNotes } from "./hooks/useRemoteNotes";
import { type IDecoratedNote, NoteSource } from "./types/DecoratedNote";
import type { IRemoteSource } from "./types/RemoteSource";
import type { INotesEnvelope, IStorageNote } from "./types/StorageNote";
import { areSelectionsEqual } from "./utils/areSelectionsEqual";
import { downloadNotesAsJson, importNotesFromJson } from "./utils/notesImportExport";
import * as notes from "./utils/notesLocalStorage";
import * as remote from "./utils/remoteSources";
const HISTORY_STEPS_LIMIT = 10;
export const NotesContext = createContext<INotesContext | null>(null);
export interface INotesContext {
notesPinned: boolean;
setNotesPinned: (v: boolean) => void;
notesReady: boolean;
notes: IDecoratedNote[];
activeNotes: Set<IDecoratedNote>;
labels: ILabelTreeNode[];
canUndo: boolean;
canRedo: boolean;
remoteSources: IRemoteSource[];
handleSetRemoteSources(r: IRemoteSource, remove?: true): void;
handleAddNote(note: IStorageNote): { isVisible: boolean };
handleUpdateNote(noteToReplace: IDecoratedNote, newNote: IStorageNote): { isVisible: boolean };
handleDeleteNote(note: IDecoratedNote): void;
handleUndo(): void;
handleRedo(): void;
handleImport(jsonStr: string, label: string): void;
handleExport(): void;
handleDeleteNotes(): void;
handleToggleLabel(label: ILabelTreeNode): void;
}
interface INotesProviderProps {
children: ReactNode;
}
const emptyActiveNotes = new Set<IDecoratedNote>();
export function NotesProvider({ children }: INotesProviderProps) {
const [remoteSources, setRemoteSources] = useState<IRemoteSource[]>([]);
const [notesPinned, setNotesPinned] = useState<boolean>(false);
const [localNotes, setLocalNotes] = useState<INotesEnvelope>({ version: 3, notes: [] });
const [localNotesDecorated, setLocalNotesDecorated] = useState<IDecoratedNote[]>([]);
const [localNotesReady, setLocalNotesReady] = useState<boolean>(false);
const [history, setHistory] = useState<INotesEnvelope[]>([]);
const [redoHistory, setRedoHistory] = useState<INotesEnvelope[]>([]);
const { locationParams } = useContext(LocationContext) as ILocationContext;
const currentVersion = locationParams.version;
const canUndo = history.length > 0;
const canRedo = redoHistory.length > 0;
// load local notes
useEffect(() => {
setLocalNotes(notes.loadFromLocalStorage());
}, []);
// load remote sources
useEffect(() => {
setRemoteSources(remote.loadFromLocalStorage());
}, []);
const decorateNotes = useDecoratedNotes();
const { remoteNotesDecorated, remoteNotesReady } = useRemoteNotes(remoteSources, decorateNotes, currentVersion);
// Set in-state local notes, but also update history and local storage.
const updateLocalNotes = useCallback((currentNotes: INotesEnvelope, newNotes: INotesEnvelope) => {
setHistory((history) => [...history, currentNotes].slice(-1 * HISTORY_STEPS_LIMIT));
setLocalNotes(newNotes);
notes.saveToLocalStorage(newNotes);
}, []);
// Decorate all local notes.
useEffect(() => {
let isCancelled = false;
setLocalNotesReady(false);
decorateNotes(localNotes.notes, NoteSource.Local, currentVersion).then((notes) => {
if (!isCancelled) {
setLocalNotesDecorated(notes);
setLocalNotesReady(true);
}
});
return () => {
isCancelled = true;
};
}, [localNotes.notes, currentVersion, decorateNotes]);
// Local and remote notes merged together.
const allNotes = useMemo(
() => [...localNotesDecorated, ...remoteNotesDecorated],
[localNotesDecorated, remoteNotesDecorated],
);
const allNotesReady = useMemo(() => localNotesReady && remoteNotesReady, [localNotesReady, remoteNotesReady]);
const { filteredNotes, labels, toggleLabel: handleToggleLabel, isVisibleByActiveLabelsLatest } = useLabels(allNotes);
const handleSetRemoteSources = useCallback((newSource: IRemoteSource, remove?: true) => {
setRemoteSources((prevRemoteSources) => {
let newRemoteSources = prevRemoteSources;
if (newSource.id === NEW_REMOTE_SOURCE_ID) {
const newId = 1 + Math.max(NEW_REMOTE_SOURCE_ID, ...prevRemoteSources.map((x) => x.id));
const newSourceWithSafeId = { ...newSource, id: newId };
newRemoteSources = [...prevRemoteSources, newSourceWithSafeId];
} else {
newRemoteSources = prevRemoteSources
.map((x) => (x.id === newSource.id ? newSource : x))
.filter((x) => (remove === true ? x.id !== newSource.id : true));
}
remote.saveToLocalStorage(newRemoteSources);
return newRemoteSources;
});
}, []);
const activeNotes = useMemo(() => {
if (!locationParams || !locationParams.selectionStart || !locationParams.selectionEnd) return emptyActiveNotes;
const { selectionStart, selectionEnd } = locationParams;
const activeNotesArray = filteredNotes.filter((note) =>
areSelectionsEqual(note.current, { selectionStart, selectionEnd }),
);
return new Set(activeNotesArray);
}, [filteredNotes, locationParams]);
const context: INotesContext = {
notesPinned,
setNotesPinned,
notesReady: allNotesReady,
notes: filteredNotes,
activeNotes,
remoteSources,
labels,
canUndo,
canRedo,
handleSetRemoteSources,
handleToggleLabel,
handleAddNote: useCallback(
(note) => {
const isVisible = isVisibleByActiveLabelsLatest.current(note);
updateLocalNotes(localNotes, {
...localNotes,
notes: [note, ...localNotes.notes],
});
return { isVisible };
},
[localNotes, updateLocalNotes, isVisibleByActiveLabelsLatest],
),
handleUpdateNote: useCallback(
(noteToReplace, newNote) => {
if (noteToReplace.source === NoteSource.Remote) {
console.warn("Refusing to edit remote note.", noteToReplace);
return { isVisible: true };
}
const isVisible = isVisibleByActiveLabelsLatest.current(newNote);
const updateIdx = localNotesDecorated.indexOf(noteToReplace);
const newNotes = localNotes.notes.map((note, idx) => (updateIdx === idx ? newNote : note));
updateLocalNotes(localNotes, {
...localNotes,
notes: newNotes,
});
return { isVisible };
},
[localNotes, localNotesDecorated, updateLocalNotes, isVisibleByActiveLabelsLatest],
),
handleDeleteNote: useCallback(
(noteToDelete) => {
if (noteToDelete.source === NoteSource.Remote) {
console.warn("Refusing to remove remote note.", noteToDelete);
return;
}
const noteToDeleteIdx = localNotesDecorated.indexOf(noteToDelete);
if (noteToDeleteIdx !== -1) {
const updatedNotes = localNotes.notes.slice();
updatedNotes.splice(noteToDeleteIdx, 1);
updateLocalNotes(localNotes, {
...localNotes,
notes: updatedNotes,
});
}
},
[localNotes, localNotesDecorated, updateLocalNotes],
),
handleUndo: useCallback(() => {
const currentNotes = localNotes;
const previousNotes = history.pop();
if (!previousNotes) {
return;
}
setRedoHistory((redoHistory) => [...redoHistory, currentNotes]);
setLocalNotes(previousNotes);
notes.saveToLocalStorage(previousNotes);
setHistory([...history]);
}, [history, localNotes]),
handleRedo: useCallback(() => {
const currentNotes = localNotes;
const previousNotes = redoHistory.pop();
if (!previousNotes) {
return;
}
updateLocalNotes(currentNotes, previousNotes);
setRedoHistory([...redoHistory]);
}, [redoHistory, localNotes, updateLocalNotes]),
handleImport: useCallback(
(jsonStr: string, label: string) => {
let newNotes = [];
try {
newNotes = importNotesFromJson(jsonStr, { mustHaveLabel: `${LABEL_IMPORTED}${label}` }).notes;
} catch (e) {
alert("Unable to read given notes file. See console for error.");
console.error(e);
return;
}
// merge notes together
updateLocalNotes(localNotes, {
...localNotes,
notes: [...localNotes.notes, ...newNotes],
});
},
[localNotes, updateLocalNotes],
),
handleExport: useCallback(() => {
const fileName = `graypaper-notes-${new Date().toISOString()}.json`;
downloadNotesAsJson(localNotes, fileName);
}, [localNotes]),
handleDeleteNotes: useCallback(() => {
const activeLabels = labels.filter((label) => label.isActive).map((label) => label.prefixedLabel);
const fileName = `removed-graypaper-notes-${new Date().toISOString()}.json`;
const deletedNotes = getFilteredNotes(localNotes.notes, activeLabels);
downloadNotesAsJson({ version: 3, notes: deletedNotes }, fileName);
const updatedNotes = getFilteredNotes(localNotes.notes, activeLabels, { includesLabel: false });
updateLocalNotes(localNotes, { ...localNotes, notes: updatedNotes });
}, [localNotes, labels, updateLocalNotes]),
};
return <NotesContext.Provider value={context}>{children}</NotesContext.Provider>;
}