From 4855aa56e8c2a5014e18f74297803f7e6e703ef9 Mon Sep 17 00:00:00 2001 From: Chad Burt Date: Fri, 17 Jan 2025 10:50:15 -0800 Subject: [PATCH] Error handling for about page image uploads --- packages/api/src/plugins/fileUploadPlugin.ts | 4 +- .../client/src/admin/AboutPageSettings.tsx | 4 +- packages/client/src/editor/EditorMenuBar.tsx | 16 ++- packages/client/src/editor/config.ts | 122 ++++++++++-------- 4 files changed, 86 insertions(+), 60 deletions(-) diff --git a/packages/api/src/plugins/fileUploadPlugin.ts b/packages/api/src/plugins/fileUploadPlugin.ts index 1e7c5654..23bf41f1 100644 --- a/packages/api/src/plugins/fileUploadPlugin.ts +++ b/packages/api/src/plugins/fileUploadPlugin.ts @@ -121,7 +121,9 @@ const FileUploadPlugin = makeExtendSchemaPlugin((build) => { const isCloudflareImagesSupported = cloudflareImagesSupported.includes(contentType) && fileSizeBytes < bytes("10mb"); - if (isCloudflareImagesSupported) { + if (usage === "about_page" && fileSizeBytes >= bytes("10mb")) { + throw new Error("File size must be less than 10MB"); + } else if (isCloudflareImagesSupported) { const img = await getDirectCreatorUploadUrl(); const { rows } = await context.adminPool.query( diff --git a/packages/client/src/admin/AboutPageSettings.tsx b/packages/client/src/admin/AboutPageSettings.tsx index a19a2474..1e8c2479 100644 --- a/packages/client/src/admin/AboutPageSettings.tsx +++ b/packages/client/src/admin/AboutPageSettings.tsx @@ -21,6 +21,7 @@ import "prosemirror-image-plugin/dist/styles/common.css"; import "./prosemirror-image.css"; import { useApolloClient } from "@apollo/client"; import { startImageUpload } from "prosemirror-image-plugin"; +import { useGlobalErrorHandler } from "../components/GlobalErrorHandler"; export default function AboutPageSettings({ projectId, @@ -34,9 +35,10 @@ export default function AboutPageSettings({ }); const client = useApolloClient(); + const onError = useGlobalErrorHandler(); const { schema, plugins, imageSettings } = useMemo(() => { - return createAboutPageEditorConfig(client, projectId); + return createAboutPageEditorConfig(client, projectId, onError); }, [client, projectId]); const [mutate, mutationState] = useUpdateAboutPageContentsMutation(); diff --git a/packages/client/src/editor/EditorMenuBar.tsx b/packages/client/src/editor/EditorMenuBar.tsx index 1a86a744..2f31381c 100644 --- a/packages/client/src/editor/EditorMenuBar.tsx +++ b/packages/client/src/editor/EditorMenuBar.tsx @@ -960,6 +960,7 @@ function ImageModal({ }) { const { t } = useTranslation(); const [saving, setSaving] = useState(false); + const [error, setError] = useState(null); return (

@@ -990,6 +991,14 @@ function ImageModal({ type="file" name="image" accept="image/*" + onChange={(e) => { + const file = e.target.files?.[0]; + if (file && file.size >= 10 * 1024 * 1024) { + setError("File must be less than 10MB"); + } else { + setError(null); + } + }} /> + {error &&

{error}

}