import { Modal } from "../Modal"; import { useRef, useState } from "preact/hooks"; import { useQuery } from "react-query"; import axios from "axios"; import { fetchUserInfo, uploadImage } from "../../api"; export const ImagePicker = ({ alt, url, setImage, ...props }) => { const [modalOpen, setModalOpen] = useState(false); return ( <> { setModalOpen(true); }} role={"button"} > {alt} {modalOpen && ( )} ); }; const ImagePickerModal = ({ setModalOpen, setImage, url }) => { const [value, setValue] = useState(url || ""); const [uploading, setUploading] = useState(false); const [error, setError] = useState(""); const [blocked, setBlocked] = useState(false); const fileInput = useRef(null); const { data: userInfo } = useQuery(fetchUserInfo()); const upload = async (file: File) => { setError(""); if (file.size > 8 * 1024 * 1024) { setError("Images must be no larger than 8 MiB."); return; } 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 { setUploading(false); } }; if (blocked) { return ( setBlocked(false)} title="Image upload blocked" onSubmit={() => setBlocked(false)} onSubmitText="OK" >

This image was flagged as probably NSFW (not safe for work) and cannot be uploaded. Please choose a different image that does not contain sexual or explicit content.

); } 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); }} > {userInfo?.image_uploads && (
{ const file = event.currentTarget.files?.[0]; event.currentTarget.value = ""; if (file) void upload(file); }} />

PNG, JPEG, GIF or WebP, up to 8 MiB. Saved as a static PNG.

{uploading &&

Checking and uploading image…

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