-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNote.tsx
More file actions
195 lines (171 loc) · 5.65 KB
/
Note.tsx
File metadata and controls
195 lines (171 loc) · 5.65 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
import { Button, cn } from "@fluffylabs/shared-ui";
import { type ChangeEvent, useCallback, useEffect, useMemo, useState } from "react";
import { validateMath } from "../../../utils/validateMath";
import type { INotesContext } from "../../NotesProvider/NotesProvider";
import { type IDecoratedNote, NoteSource } from "../../NotesProvider/types/DecoratedNote";
import type { IStorageNote } from "../../NotesProvider/types/StorageNote";
import { NoteLayout } from "./NoteLayout";
import { NoteLink } from "./NoteLink";
import { TinyIconButton } from "./SiimpleComponents";
export type NotesItem = {
location: string; // serialized InDocSelection
content: string;
};
type NoteProps = {
note: IDecoratedNote;
active: boolean;
onEditNote: INotesContext["handleUpdateNote"];
onDeleteNote: INotesContext["handleDeleteNote"];
onSelectNote: (note: IDecoratedNote) => void;
};
export function Note({ note, active = false, onEditNote, onDeleteNote, onSelectNote }: NoteProps) {
const [isEditing, setIsEditing] = useState(false);
const [noteDirty, setNoteDirty] = useState<IStorageNote>({
...note.original,
});
const [noteContentError, setNoteContentError] = useState("");
const isEditable = note.source !== NoteSource.Remote;
const handleSaveClick = useCallback(() => {
const mathValidationError = validateMath(noteDirty.content);
setNoteContentError("");
if (mathValidationError) {
setNoteContentError(mathValidationError);
return;
}
onEditNote(note, noteDirty);
setIsEditing(false);
}, [note, noteDirty, onEditNote]);
const handleEditClick = useCallback(() => {
if (!isEditable) {
return;
}
setIsEditing(true);
setNoteDirty({ ...note.original });
setNoteContentError("");
}, [note, isEditable]);
const handleNoteLabelsChange = useCallback((labels: string[]) => {
setNoteDirty((prevNoteDirty) => ({ ...prevNoteDirty, labels }));
}, []);
const handleNoteContentChange = useCallback((ev: ChangeEvent<HTMLTextAreaElement>) => {
setNoteDirty((prev) => ({ ...prev, content: ev.currentTarget.value }));
}, []);
const handleDeleteClick = useCallback(() => {
onDeleteNote(note);
}, [note, onDeleteNote]);
const handleCancelClick = useCallback(() => {
setNoteContentError("");
setIsEditing(false);
}, []);
const handleWholeNoteClick = (e: React.MouseEvent<HTMLDivElement>) => {
const target = e.target;
if (target instanceof Element && (target.closest("button") || target.closest("a"))) {
e.preventDefault();
return;
}
if (active) {
return;
}
onSelectNote(note);
};
const handleNoteEnter = (e: React.KeyboardEvent<HTMLDivElement>) => {
if (e.target instanceof Element && (e.target.closest("input") || e.target.closest("textarea"))) {
return;
}
const isActivation = e.key === "Enter" || e.key === " " || e.code === "Space";
if (!isActivation) return;
e.preventDefault();
if (active) {
return;
}
onSelectNote(note);
};
useEffect(() => {
if (!active) {
setIsEditing(false);
}
}, [active]);
const noteLayoutContext = useMemo(
() => ({
note,
isEditable,
handleEditClick,
handleSaveClick,
handleCancelClick,
onEditNote,
isEditing,
noteDirty,
handleNoteContentChange,
handleNoteLabelsChange,
}),
[
note,
isEditable,
handleEditClick,
handleSaveClick,
handleCancelClick,
onEditNote,
isEditing,
noteDirty,
handleNoteContentChange,
handleNoteLabelsChange,
],
);
return (
<NoteLayout.Root value={noteLayoutContext}>
<div
data-testid="notes-manager-card"
className={cn(
"note rounded-xl p-4 flex flex-col gap-2",
active && "bg-[var(--active-note-bg)] shadow-[0px_4px_0px_1px_var(--active-note-shadow-bg)] mb-1",
!active && "bg-[var(--inactive-note-bg)] cursor-pointer",
)}
role={!active ? "button" : undefined}
tabIndex={!active ? 0 : undefined}
aria-label={!active ? "Activate label" : ""}
onClick={handleWholeNoteClick}
onKeyDown={handleNoteEnter}
>
{!active && (
<>
<NoteLink note={note} active={false} />
<NoteLayout.Text />
</>
)}
{active && !isEditing && (
<>
<NoteLayout.SelectedText />
<NoteLayout.Text />
<NoteLayout.Labels />
{isEditable && (
<div className="flex flex-1 justify-end">
<TinyIconButton data-testid={"edit-button"} onClick={handleEditClick} aria-label="Edit note" icon="✏️" />
</div>
)}
</>
)}
{active && isEditing && (
<>
<>
<NoteLayout.SelectedText />
<NoteLayout.TextArea className={noteContentError ? "error" : ""} />
{noteContentError ? <div className="validation-message">{noteContentError}</div> : null}
<NoteLayout.Labels />
<div className="actions gap-2">
<Button variant="ghost" intent="destructive" size="sm" onClick={handleDeleteClick}>
Delete
</Button>
<div className="fill" />
<Button variant="tertiary" data-testid={"cancel-button"} onClick={handleCancelClick} size="sm">
Cancel
</Button>
<Button data-testid={"save-button"} onClick={handleSaveClick} size="sm">
Save
</Button>
</div>
</>
</>
)}
</div>
</NoteLayout.Root>
);
}