import { Modal } from "../Modal"; import { useEffect, useRef, useState } from "preact/hooks"; import { useQuery } from "react-query"; import axios from "axios"; import { fetchUserInfo, uploadImage } from "../../api"; const useImageDrop = (enabled: boolean, onFiles: (files: File[]) => void, busy = false) => { const [dragging, setDragging] = useState(false); const dragDepth = useRef(0); const acceptsDrag = (event: DragEvent) => enabled && event.dataTransfer?.types.includes("Files"); return { dragging, handlers: { onDragEnter: (event: DragEvent) => { if (!acceptsDrag(event)) return; event.preventDefault(); event.stopPropagation(); dragDepth.current += 1; if (!busy) setDragging(true); }, onDragOver: (event: DragEvent) => { if (!acceptsDrag(event)) return; event.preventDefault(); event.stopPropagation(); event.dataTransfer.dropEffect = busy ? "none" : "copy"; }, onDragLeave: () => { dragDepth.current = Math.max(0, dragDepth.current - 1); if (dragDepth.current === 0) setDragging(false); }, onDrop: (event: DragEvent) => { if (!acceptsDrag(event)) return; event.preventDefault(); event.stopPropagation(); dragDepth.current = 0; setDragging(false); if (!busy) onFiles(Array.from(event.dataTransfer.files)); }, }, }; }; export const ImagePicker = ({ alt, url, setImage, ...props }) => { const [modalOpen, setModalOpen] = useState(false); const [droppedFiles, setDroppedFiles] = useState(null); const { data: userInfo } = useQuery(fetchUserInfo()); const uploadsEnabled = userInfo?.image_uploads === true; const { dragging, handlers } = useImageDrop(uploadsEnabled, (files) => { setDroppedFiles(files); setModalOpen(true); }); return ( <> { setDroppedFiles(null); setModalOpen(true); }} role={"button"} > {alt} {modalOpen && ( )} ); }; const ImagePickerModal = ({ setModalOpen, setImage, url, uploadsEnabled, droppedFiles }) => { const [value, setValue] = useState(url || ""); const [uploading, setUploading] = useState(false); const [error, setError] = useState(""); const [blocked, setBlocked] = useState(false); const fileInput = useRef(null); const uploadInProgress = useRef(false); const upload = async (files: File[]) => { setBlocked(false); if (!uploadsEnabled || uploadInProgress.current) return; setError(""); if (files.length !== 1) { setError("Please upload only one image."); return; } const file = files[0]; if (file.size > 8 * 1024 * 1024) { setError("Images must be no larger than 8 MiB."); return; } uploadInProgress.current = true; setUploading(true); try { setValue(await uploadImage(file)); } catch (error) { if (axios.isAxiosError(error) && error.response?.status === 451) { setBlocked(true); } else { setError( axios.isAxiosError(error) && typeof error.response?.data?.error === "string" ? error.response.data.error : "Could not upload image. Please try again.", ); } } finally { uploadInProgress.current = false; setUploading(false); } }; const { dragging, handlers } = useImageDrop( uploadsEnabled, (files) => void upload(files), uploading, ); useEffect(() => { if (droppedFiles) void upload(droppedFiles); }, [droppedFiles]); return ( { setImage(value); setModalOpen(false); }} onSubmitText={"Save"} onSubmitDisabled={uploading} >

Please note: if you attach an image directly from Discord, it will not be visible in the dashboard, but will be visible on reminders. Other image-sharing sites such as Imgur don't have this issue.

{ setValue(ev.currentTarget.value); }} > {uploadsEnabled && (

or…

{ const file = event.currentTarget.files?.[0]; event.currentTarget.value = ""; if (file) void upload([file]); }} />

Or drag and drop an image here.

Static PNG, JPEG, GIF or WebP, up to 8 MiB. All uploads are converted to PNGs.

{blocked && ( <>

This image was flagged as potentially NSFW. Unfortunately, due to the UK's Online Safety Act, we cannot handle explicit content. Please defer to using Imgur or another image storage platform who have the resources to deal with idiotic laws.

The image you attempted to upload has not been stored.

)} {uploading &&

Uploading image…

}
)} {error && ( )}
); };