-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integrated Excalidraw for drawing Files #10680
base: develop
Are you sure you want to change the base?
Changes from all commits
1837fbb
a1b6f2c
aa5327a
a84bd76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,159 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Excalidraw } from "@excalidraw/excalidraw"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { type ExcalidrawElement } from "@excalidraw/excalidraw/types/element/types"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { useMutation } from "@tanstack/react-query"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { t } from "i18next"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { navigate } from "raviger"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { useEffect, useState } from "react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { toast } from "sonner"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { debounce } from "@/lib/utils"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import CareIcon from "@/CAREUI/icons/CareIcon"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Button } from "@/components/ui/button"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { Input } from "@/components/ui/input"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import Loading from "@/components/Common/Loading"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { CreateFileResponse } from "@/components/Patient/models"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import routes from "@/Utils/request/api"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import mutate from "@/Utils/request/mutate"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type Props = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
associatingId: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
fileType: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export default function ExcalidrawEditor({ associatingId, fileType }: Props) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [elements, setElements] = useState<readonly ExcalidrawElement[] | null>( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
[], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [name, setName] = useState(""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [id, setId] = useState(""); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const [isDirty, setIsDirty] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setIsDirty(!!elements?.length); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, [elements?.length]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { mutateAsync: markUploadComplete, error: markUploadCompleteError } = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
useMutation({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mutationFn: mutate(routes.markUploadCompleted, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
pathParams: { id: id || "" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { mutateAsync: createUpload } = useMutation({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mutationFn: mutate(routes.createUpload), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onSuccess: (response: CreateFileResponse) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setId(response.id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+39
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance mutation hooks with better error handling and retry logic. The mutation hooks could benefit from:
const { mutateAsync: markUploadComplete, error: markUploadCompleteError } =
useMutation({
mutationFn: mutate(routes.markUploadCompleted, {
pathParams: { id: id || "" },
}),
+ retry: 3,
+ retryDelay: 1000,
});
- const { mutateAsync: createUpload } = useMutation({
+ const { mutateAsync: createUpload, error: createUploadError, isLoading: isUploading } = useMutation({
mutationFn: mutate(routes.createUpload),
onSuccess: (response: CreateFileResponse) => {
setId(response.id);
},
+ onError: (error) => {
+ toast.error(t("error_in_createUpload"));
+ },
+ retry: 3,
+ retryDelay: 1000,
}); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const handleSave = async () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!name.trim()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toast.error(t("please_enter_a_name_for_the_drawing")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const obj = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: "excalidraw", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
version: "2", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
source: window.location.origin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elements: elements, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
appState: {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
files: {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const file = new File([JSON.stringify(obj)], `${name}.excalidraw`, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: "application/vnd.excalidraw", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let signedUrl = ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let response: CreateFileResponse | null = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
response = await createUpload({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
original_name: `${name}.excalidraw`, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name: name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
file_type: fileType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
file_category: "unspecified", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
associating_id: associatingId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mime_type: "text/plain", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
signedUrl = response.signed_url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const formData = new FormData(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
formData.append("file", file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const upload = await fetch(signedUrl, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
method: "PUT", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
body: file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!upload.ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toast.error(t("error_uploading_file")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
await markUploadComplete({ id: response?.id }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (markUploadCompleteError) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toast.error(t("file_error__mark_complete_failed")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toast.success(t("file_success__upload_complete")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
navigate(`drawings/${response!.id}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
toast.error(t("error_in_createUpload")); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+53
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix file upload implementation and improve error handling. Several issues need attention:
- const formData = new FormData();
- formData.append("file", file);
-
const upload = await fetch(signedUrl, {
method: "PUT",
body: file,
+ headers: {
+ 'Content-Type': 'application/vnd.excalidraw',
+ },
});
if (!upload.ok) {
- toast.error(t("error_uploading_file"));
+ toast.error(t("error_uploading_file_with_status", { status: upload.status }));
return;
} Also consider adding upload progress tracking: const upload = await fetch(signedUrl, {
method: "PUT",
body: file,
headers: {
'Content-Type': 'application/vnd.excalidraw',
},
onUploadProgress: (progressEvent) => {
const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
// Update progress state
},
}); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (elements === null) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return <Loading />; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className="flex flex-col h-[calc(100vh-4rem)]"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className="flex flex-row justify-between items-center p-2"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className="flex flex-row items-center"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className="rounded-full bg-primary-100 px-5 py-4"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<CareIcon icon="l-pen" className="text-lg text-primary-500" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className="m-4"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type="text" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
value={name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
placeholder={t("enter_the_file_name")} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onChange={(e) => setName(e.target.value)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{isDirty && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Button className="ml-auto" onClick={handleSave}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{t("save")} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</Button> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<div className="flex-grow h-[calc(100vh-10rem)] -m-2"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
<Excalidraw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
UIOptions={{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
canvasActions: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
saveAsImage: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
loadScene: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
initialData={{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
appState: { theme: "light" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onChange={debounce((elements) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
setElements(elements); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, 100)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bodhish marking this for hold since backend would not allow editing a file due to audit reasons.
@vigneshhari suggests making a plug and writing the elements to the DB itself instead of a file upload.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Buckets have versions what stops us from editing 🤔