Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 45x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 179x 13x 13x 13x 13x 13x 13x 13x 13x 179x 13x 13x 13x 13x 13x 13x 2x 11x 13x 179x 94x 94x 94x 94x 94x 94x 179x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 4x 4x 4x 179x 179x 48x 4x 4x 3x 1x 1x 93x 2x 2x 148x | import { useState, useRef, useCallback } from "react";
import ReactQuill from "react-quill";
import "react-quill/dist/quill.bubble.css";
import { Button } from "@/ahuora-design-system/ui/button";
import { Input } from "@/ahuora-design-system/ui/input";
import { toast } from "sonner";
import { useCurrentObject } from "@/hooks/flowsheetObjects";
import { Bold, Italic, Underline, List, ListOrdered } from "lucide-react";
import Note from "./Note";
import {
useCoreNoteCreateMutation,
useCoreNoteListQuery,
useCoreNotePartialUpdateMutation,
} from "@/api/apiStore.gen";
type QuillFormatValue = boolean | string | number | null;
// Main Notes component
const Notes = ({ simulationObjectId }: { simulationObjectId?: number }) => {
const [createNote] = useCoreNoteCreateMutation();
const [editNote] = useCoreNotePartialUpdateMutation();
const currentObj = useCurrentObject();
const simId = simulationObjectId ?? currentObj?.id ?? undefined;
const { data: notes } = useCoreNoteListQuery({
simulationObject: simId,
});
const [title, setTitle] = useState("");
const [content, setContent] = useState("");
const [showNewNote, setShowNewNote] = useState(false);
const [editingNoteId, setEditingNoteId] = useState<number | null>(null);
const initialFormatState = {
bold: false,
italic: false,
underline: false,
list: {
bullet: false,
ordered: false,
},
};
const [activeFormats, setActiveFormats] = useState(initialFormatState);
// Ref for the Quill editor instance
const quillRef = useRef<ReactQuill>(null);
// React-Quill modules config: toolbar false as custom
const modules = {
toolbar: false,
};
const formatText = useCallback((format: string, value?: QuillFormatValue) => {
const editor = quillRef.current?.getEditor();
Iif (!editor) return;
const range = editor.getSelection();
Iif (!range) return;
Iif (!editor.hasFocus()) {
editor.focus();
}
const currentFormats = editor.getFormat(range);
// Toggle if no explicit value provided
const nextValue = value !== undefined ? value : !currentFormats[format];
editor.format(format, nextValue);
}, []);
const handleFormat = useCallback(
(format: string, value?: QuillFormatValue) => {
const editor = quillRef.current?.getEditor();
Iif (!editor) {
return;
}
if (!editor.hasFocus()) {
editor.focus();
}
setActiveFormats((prev) => {
// Case 1: Complex formats with a value (like list: 'bullet')
if (value) {
return {
...prev,
list: {
ordered: value === "ordered" ? !prev.list.ordered : false,
bullet: value === "bullet" ? !prev.list.bullet : false,
},
};
}
// Case 2: Simple boolean formats (bold, italic, etc.)
return {
...prev,
[format]: !prev[format],
};
});
formatText(format, value);
},
[formatText]
);
// Update active formats based on current selection in the editor
const handleSelectionChange = useCallback(() => {
const editor = quillRef.current?.getEditor();
Iif (!editor) return;
Iif (!editor.hasFocus()) return;
const rawFormat = editor?.getFormat() ?? {};
const newState = {
bold: !!rawFormat?.bold,
italic: !!rawFormat?.italic,
underline: !!rawFormat?.underline,
list: {
bullet: rawFormat?.list === "bullet",
ordered: rawFormat?.list === "ordered",
},
};
setActiveFormats(newState);
}, []);
// Function to save a new note
const saveNoteHandler = useCallback(async () => {
Iif (!title.trim()) {
toast.error("Please enter a title for the note");
return;
}
Iif (!content.trim()) {
toast.error("Please enter some content for the note");
return;
}
Iif (content.trim().length > 2000) {
toast.error("Note content is too long");
return;
}
Iif (simId == null) {
toast.error("No simulation object selected");
return;
}
setShowNewNote(false);
let promise;
// PATCH existing note
if (editingNoteId) {
promise = editNote({
id: editingNoteId,
patchedNote: {
title: title.trim(),
content,
simulationObject: simId,
},
});
toast.promise(promise, {
loading: "Updating note...",
success: "Note updated",
error: "Failed to update note",
});
}
// CREATE new note
else {
promise = createNote({
note: {
title: title.trim(),
content,
simulationObject: simId,
},
});
toast.promise(promise, {
loading: "Saving note...",
success: "New note saved",
error: "Failed to save note",
});
}
// Clear the form and editor content
setTitle("");
setContent("");
setEditingNoteId(null);
}, [title, content, simId, editingNoteId, editNote, createNote]);
// Function to cancel note creation/editing
const handleCancel = useCallback(() => {
setShowNewNote(false);
setTitle("");
setContent("");
setEditingNoteId(null);
}, []);
// div for the Notes component
return (
<div>
<style>
{`
.ql-editor {
font-family: 'Inter', sans-serif;
color: hsl(var(--foreground));
min-height: 30px;
overflow-wrap: anywhere;
width: 100%;
background-color: hsl(var(--background));
}
.ql-editor.ql-blank::before {
color: hsl(var(--text-faint));
font-family: 'Inter', sans-serif;
font-size: 0.9em;
}
.ql-editor ul, .ql-editor ol {
list-style-position: inside;
color: hsl(var(--foreground));
}
.prose ul, .prose ol {
list-style-position: inside;
color: hsl(var(--foreground));
}
.prose ul {
list-style-type: disc;
}
.prose ol {
list-style-type: decimal;
}
`}
</style>
{showNewNote ? (
<div className="rounded-lg p-4">
<Input
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Title"
className="mb-4"
aria-label="note-title-box"
/>
<div className="flex space-x-2 mb-2">
<Button
aria-label="note-bold"
// variant="ghost"
variant={activeFormats.bold ? "secondary" : "ghost"}
onClick={() => handleFormat("bold")}
>
<Bold className="h-4 w-4" />
</Button>
<Button
aria-label="note-italic"
// variant="ghost"
variant={activeFormats.italic ? "secondary" : "ghost"}
onClick={() => handleFormat("italic")}
>
<Italic className="h-4 w-4" />
</Button>
<Button
aria-label="note-underline"
// variant="ghost"
variant={activeFormats.underline ? "secondary" : "ghost"}
onClick={() => handleFormat("underline")}
>
<Underline className="h-4 w-4" />
</Button>
<Button
aria-label="note-list"
// variant="ghost"
variant={activeFormats.list.bullet ? "secondary" : "ghost"}
onClick={() => handleFormat("list", "bullet")}
>
<List className="h-4 w-4" />
</Button>
<Button
aria-label="note-listordered"
// variant="ghost"
variant={activeFormats.list.ordered ? "secondary" : "ghost"}
onClick={() => handleFormat("list", "ordered")}
>
<ListOrdered className="h-4 w-4" />
</Button>
</div>
<div
className="min-h-[30px] border-2 border-gray-600 rounded-md p-0.1 w-full"
aria-label="note-text-box"
>
<ReactQuill
ref={quillRef}
theme="bubble"
value={content}
onChange={(e) => {
setContent(e);
}}
onChangeSelection={handleSelectionChange}
modules={modules}
placeholder="Type a note..."
/>
</div>
<div className="flex justify-end mt-4 space-x-2">
<Button variant="outline" onClick={handleCancel}>
Cancel
</Button>
<Button onClick={saveNoteHandler}>Save</Button>
</div>
</div>
) : (
<Button
onClick={() => {
setShowNewNote(true);
setActiveFormats(initialFormatState);
}}
className="w-full"
aria-label="New Note"
>
New note
</Button>
)}
{notes?.map((note) => (
<Note
key={note.id}
data={note}
showEditor={setShowNewNote}
setNoteTitle={setTitle}
setNoteContent={setContent}
setEditingNoteId={setEditingNoteId}
/>
))}
</div>
);
};
export default Notes;
|