From 4bd31f29a8b7405ba26c42772f2a83f4032e3433 Mon Sep 17 00:00:00 2001 From: swve Date: Fri, 13 Jun 2025 21:11:23 +0200 Subject: [PATCH] feat: increase upload limit to 8mb, improve errors indicator --- .../EditCourseGeneral/ThumbnailUpdate.tsx | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx b/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx index cf907a9d..705aec1b 100644 --- a/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx +++ b/apps/web/components/Dashboard/Pages/Course/EditCourseGeneral/ThumbnailUpdate.tsx @@ -5,22 +5,24 @@ import { updateCourseThumbnail } from '@services/courses/courses' import { getCourseThumbnailMediaDirectory } from '@services/media/media' import { ArrowBigUpDash, UploadCloud, Image as ImageIcon } from 'lucide-react' import { useLHSession } from '@components/Contexts/LHSessionContext' -import React, { useState, useEffect } from 'react' +import React, { useState, useEffect, useRef } from 'react' import { mutate } from 'swr' import UnsplashImagePicker from './UnsplashImagePicker' -const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB +const MAX_FILE_SIZE = 8_000_000; // 8MB const VALID_MIME_TYPES = ['image/jpeg', 'image/jpg', 'image/png'] as const; type ValidMimeType = typeof VALID_MIME_TYPES[number]; function ThumbnailUpdate() { + const fileInputRef = useRef(null); const course = useCourse() as any const session = useLHSession() as any; const org = useOrg() as any const [localThumbnail, setLocalThumbnail] = useState<{ file: File; url: string } | null>(null) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState('') + const [showError, setShowError] = useState(false) const [showUnsplashPicker, setShowUnsplashPicker] = useState(false) const withUnpublishedActivities = course ? course.withUnpublishedActivities : false @@ -35,21 +37,31 @@ function ThumbnailUpdate() { const validateFile = (file: File): boolean => { if (!VALID_MIME_TYPES.includes(file.type as ValidMimeType)) { - setError('Please upload only PNG or JPG/JPEG images'); + setError(`Invalid file type: ${file.type}. Please upload only PNG or JPG/JPEG images`); + setShowError(true); return false; } if (file.size > MAX_FILE_SIZE) { - setError('File size should be less than 5MB'); + setError(`File size (${(file.size / 1024 / 1024).toFixed(2)}MB) exceeds the 8MB limit`); + setShowError(true); return false; } + setShowError(false); return true; } const handleFileChange = async (event: React.ChangeEvent) => { + setError(''); + setShowError(false); const file = event.target.files?.[0]; - if (!file) return; + + if (!file) { + setError('Please select a file'); + setShowError(true); + return; + } if (!validateFile(file)) { event.target.value = ''; @@ -100,25 +112,27 @@ function ThumbnailUpdate() { if (res.success === false) { setError(res.HTTPmessage); + setShowError(true); } else { setError(''); + setShowError(false); } } catch (err) { setError('Failed to update thumbnail'); + setShowError(true); } finally { setIsLoading(false); } } return ( -
+
+ {showError && error && ( +
+
{error}
+
+ )}
- {error && ( -
-
{error}
-
- )} -
{localThumbnail ? (