All files / src/pages/flowsheet-page/flowsheet/PropertiesSidebar/Notes Notes.tsx

1.88% Statements 1/53
0% Branches 0/14
0% Functions 0/12
1.88% Lines 1/53

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                                    37x                                                                                                                                                                                                                                                                                                                                                                                                                                
import { useState, useRef } 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);
 
  // Ref for the Quill editor instance
  const quillRef = useRef<ReactQuill>(null);
 
  // React-Quill modules config: toolbar false as custom
  const modules = {
    toolbar: false,
  };
 
  // Function to format text using custom toolbar
  const formatText = (format: string, value: QuillFormatValue = true) => {
    const editor = quillRef.current?.getEditor();
    Iif (editor) {
      const range = editor.getSelection();
      Iif (range) {
        editor.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;
    }
 
    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);
  };
 
  // Function to cancel note creation/editing
  function handleCancel() {
    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"
          />
 
          <div className="flex space-x-2 mb-2">
            <Button variant="ghost" onClick={() => formatText("bold")}>
              <Bold className="h-4 w-4" />
            </Button>
            <Button variant="ghost" onClick={() => formatText("italic")}>
              <Italic className="h-4 w-4" />
            </Button>
            <Button variant="ghost" onClick={() => formatText("underline")}>
              <Underline className="h-4 w-4" />
            </Button>
            <Button
              variant="ghost"
              onClick={() => formatText("list", "bullet")}
            >
              <List className="h-4 w-4" />
            </Button>
            <Button
              variant="ghost"
              onClick={() => formatText("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 ">
            <ReactQuill
              ref={quillRef}
              theme="bubble"
              value={content}
              // onChange={setContent}
              onChange={setContent}
              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)} className="w-full">
          New note
        </Button>
      )}
 
      {notes?.map((note) => (
        <Note
          key={note.id}
          data={note}
          showEditor={setShowNewNote}
          setNoteTitle={setTitle}
          setNoteContent={setContent}
          setEditingNoteId={setEditingNoteId}
        />
      ))}
    </div>
  );
};
export default Notes;