From 578a4e0cf520c781af932f37be6fada706a3fe3c Mon Sep 17 00:00:00 2001
From: tobi <31960611+tsmethurst@users.noreply.github.com>
Date: Tue, 7 May 2024 19:48:12 +0200
Subject: [bugfix] Reset emoji fields on upload error (#2905)

---
 web/source/settings/lib/form/file.tsx | 37 +++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 17 deletions(-)

(limited to 'web/source/settings/lib/form/file.tsx')

diff --git a/web/source/settings/lib/form/file.tsx b/web/source/settings/lib/form/file.tsx
index 944d77ae1..cf9407827 100644
--- a/web/source/settings/lib/form/file.tsx
+++ b/web/source/settings/lib/form/file.tsx
@@ -27,6 +27,7 @@ import type {
 	HookOpts,
 	FileFormInputHook,
 } from "./types";
+import { Error as ErrorC } from "../../components/error";
 
 const _default = undefined;
 export default function useFileInput(
@@ -41,6 +42,15 @@ export default function useFileInput(
 	const [imageURL, setImageURL] = useState<string>();
 	const [info, setInfo] = useState<React.JSX.Element>();
 
+	function reset() {
+		if (imageURL) {
+			URL.revokeObjectURL(imageURL);
+		}
+		setImageURL(undefined);
+		setFile(undefined);
+		setInfo(undefined);
+	}
+
 	function onChange(e: React.ChangeEvent<HTMLInputElement>) {
 		const files = e.target.files;
 		if (!files) {
@@ -59,25 +69,18 @@ export default function useFileInput(
 			setImageURL(URL.createObjectURL(file));
 		}
 
-		let size = prettierBytes(file.size);
+		const sizePrettier = prettierBytes(file.size);
 		if (maxSize && file.size > maxSize) {
-			size = <span className="error-text">{size}</span>;
+			const maxSizePrettier = prettierBytes(maxSize);
+			setInfo(
+				<ErrorC
+					error={new Error(`file size ${sizePrettier} is larger than max size ${maxSizePrettier}`)}
+					reset={(reset)}
+				/>
+			);
+		} else {
+			setInfo(<>{file.name} ({sizePrettier})</>);
 		}
-
-		setInfo(
-			<>
-				{file.name} ({size})
-			</>
-		);
-	}
-
-	function reset() {
-		if (imageURL) {
-			URL.revokeObjectURL(imageURL);
-		}
-		setImageURL(undefined);
-		setFile(undefined);
-		setInfo(undefined);
 	}
 
 	const infoComponent = (
-- 
cgit v1.2.3

