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 | 33x 33x | import { useState, useRef, useCallback, useEffect } from "react";
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 { X, Bold, Italic, Underline, List, ListOrdered } from "lucide-react";
import Quill from "quill";
import {
api,
NoteRead,
useCoreNoteCreateMutation,
useCoreNoteListQuery,
useCoreNoteDestroyMutation,
} from "@/api/apiStore.gen";
import { useAppDispatch } from "@/store/hooks";
type QuillFormatValue = boolean | string | number | null;
// Note component to display individual notes
const Note = ({ data }: { data: NoteRead }) => {
const unitop = useCurrentObject();
const dispatch = useAppDispatch();
const [deleteNote] = useCoreNoteDestroyMutation();
// Format the saved date for display
const displayDate = new Date(data.savedDate || "").toLocaleDateString(
"en-GB",
);
// State to hold the converted content
const [convertedContent, setConvertedContent] = useState("");
// Function to handle note deletion with optimistic update
const deleteNoteHandler = () => {
// Optimistically update the cache before the mutation is confirmed
dispatch(
api.util.updateQueryData(
"coreNoteList",
{ unitop: unitop?.simulationObjectId },
(draftNotes) => {
const index = draftNotes.findIndex((note) => note.id === data.id);
Iif (index !== -1) {
draftNotes.splice(index, 1);
}
},
),
);
// Call the delete mutation
const promise = deleteNote({ id: data.id });
toast.promise(promise, {
success: "Note deleted",
error: "Failed to delete note",
});
};
// Function to convert data-list="bullet" to <ul> tags
const convertBulletLists = (htmlContent: string) => {
const tempDiv = document.createElement("div");
tempDiv.innerHTML = htmlContent;
const bulletItems = tempDiv.querySelectorAll('li[data-list="bullet"]');
bulletItems.forEach((item) => {
const parent = item.parentElement;
Iif (parent && parent.tagName === "OL") {
const ul = document.createElement("ul");
while (parent.firstChild) {
ul.appendChild(parent.firstChild);
}
parent.replaceWith(ul);
}
});
return tempDiv.innerHTML;
};
// Convert the content whenever it changes
useEffect(() => {
const converted = convertBulletLists(data.content);
setConvertedContent(converted);
}, [data.content]);
return (
<div className="relative bg-[#2a2a2a] p-4 my-4 rounded-lg shadow-lg">
{/* Delete button */}
<Button
size="icon"
className="h-5 w-5 absolute top-2 right-2"
variant="ghost"
onClick={deleteNoteHandler}
>
<X size={16} />
</Button>
{/* Note title */}
<h3 className="font-bold text-lg mb-2">{data.title}</h3>
{/* Note content */}
<div className="prose prose-invert">
<div dangerouslySetInnerHTML={{ __html: convertedContent }} />
</div>
{/* Saved date */}
<p className="text-gray-500 text-xs text-right mt-2">{displayDate}</p>
</div>
);
};
// Main Notes component
const Notes = () => {
// API hooks for mutations and queries
const [createNote] = useCoreNoteCreateMutation();
const currentObj = useCurrentObject();
const { data: notes } = useCoreNoteListQuery({
simulationObject: currentObj?.id,
});
const dispatch = useAppDispatch();
// Local state for title, content, and UI state
const [title, setTitle] = useState("");
const [content, setContent] = useState("");
const [showNewNote, setShowNewNote] = useState(false);
// Ref for the Quill editor instance
const quillRef = useRef<Quill | null>(null);
// Callback ref to initialize Quill editor when the DOM element is available
const initializeQuill = useCallback((el: HTMLDivElement | null) => {
if (el && !quillRef.current) {
// Initialize Quill editor with the bubble theme
quillRef.current = new Quill(el, {
theme: "bubble",
placeholder: "Type a note...",
modules: {
toolbar: false, // Disable default toolbar as we're using a custom one
},
});
// Update content state on text change
quillRef.current.on("text-change", () => {
setContent(quillRef.current?.root.innerHTML || "");
});
// Focus the editor when it is initialized
quillRef.current.focus();
I} else if (!el) {
// If the element is unmounted, reset the Quill instance
quillRef.current = null;
}
}, []);
// Function to format text using custom toolbar
const formatText = (format: string, value: QuillFormatValue = true) => {
Iif (quillRef.current) {
const range = quillRef.current.getSelection();
Iif (range) {
quillRef.current.format(format, value);
}
}
};
// Function to save a new note
const saveNoteHandler = 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;
}
setShowNewNote(false);
// Call the create mutation
const promise = createNote({
note: {
title: title.trim(),
content: content,
simulationObject: currentObj?.id,
},
});
toast.promise(promise, {
loading: "Saving note...",
success: "New note saved",
error: "Failed to save note",
});
// Clear the form and editor content
setTitle("");
setContent("");
quillRef.current?.setText("");
};
return (
<div>
<style>
{`
.ql-editor {
font-family: 'Inter', sans-serif;
color: white;
}
.ql-editor.ql-blank::before {
color: white;
font-family: 'Inter', sans-serif;
}
.ql-editor ul, .ql-editor ol {
list-style-position: inside;
color: white;
}
.ql-editor ul {
list-style-type: disc;
}
.ql-editor ol {
list-style-type: decimal;
}
.prose ul, .prose ol {
list-style-position: inside;
color: white;
}
.prose ul {
list-style-type: disc;
}
.prose ol {
list-style-type: decimal;
}
`}
</style>
{showNewNote ? (
<div className="rounded-lg p-4">
{/* Input for the note title */}
<Input
value={title}
onChange={(e) => setTitle(e.target.value)}
placeholder="Title"
className="mb-4"
/>
{/* Custom toolbar */}
<div className="flex space-x-2 mb-2">
<Button
variant="ghost"
size="icon"
onClick={() => formatText("bold")}
>
<Bold className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => formatText("italic")}
>
<Italic className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => formatText("underline")}
>
<Underline className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => formatText("list", "bullet")}
>
<List className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
onClick={() => formatText("list", "ordered")}
>
<ListOrdered className="h-4 w-4" />
</Button>
</div>
{/* Quill editor container */}
<div className="min-h-[30px] border-2 border-gray-600 rounded-md p-2">
<div ref={initializeQuill} /> {/* Attach the callback ref here */}
</div>
{/* Action buttons */}
<div className="flex justify-end mt-4 space-x-2">
<Button variant="outline" onClick={() => setShowNewNote(false)}>
Cancel
</Button>
<Button onClick={saveNoteHandler}>Save</Button>
</div>
</div>
) : (
// Button to show the new note form
<Button onClick={() => setShowNewNote(true)} className="w-full">
New note
</Button>
)}
{/* Display the list of notes */}
{notes?.map((note) => (
<Note key={note.id} data={note} />
))}
</div>
);
};
export default Notes;
|