@@ -4,12 +4,13 @@ import { useState, useEffect } from "react";
44import { useRouter } from "next/navigation" ;
55import { useSession } from "next-auth/react" ;
66import UploadSuccessAlert from "@/components/UploadSuccessAlert" ;
7+ import FileSizeErrorAlert from "@/components/FileSizeErrorAlert" ;
78import FileUploadCard from "@/components/FileUploadCard" ;
89import WelcomeCard from "@/components/WelcomeCard" ;
910import TierInfoSection from "@/components/TierInfoSection" ;
1011import Header from "@/components/Header" ;
1112import Footer from "@/components/Footer" ;
12- import { uploadFile } from "@/lib/api" ;
13+ import { uploadFile , validateFileSize , calculateExpiryTime } from "@/lib/api" ;
1314
1415export default function Home ( ) {
1516 const router = useRouter ( ) ;
@@ -21,6 +22,7 @@ export default function Home() {
2122 const [ fileId , setFileId ] = useState < string | null > ( null ) ;
2223 const [ fileSize , setFileSize ] = useState < string > ( "" ) ;
2324 const [ uploadSuccess , setUploadSuccess ] = useState < boolean > ( false ) ;
25+ const [ fileSizeError , setFileSizeError ] = useState < string | null > ( null ) ;
2426
2527 // Auto-close success alert after 3 seconds
2628 useEffect ( ( ) => {
@@ -38,12 +40,42 @@ export default function Home() {
3840 setIsSuccess ( false ) ;
3941 setUploadSuccess ( false ) ;
4042 setProgress ( 0 ) ;
41- setFileSize ( ( selectedFile . size / 1024 ) . toFixed ( 2 ) + " KB" ) ;
43+ setFileSizeError ( null ) ;
44+
45+ // Convert to appropriate unit
46+ const sizeInMB = selectedFile . size / ( 1024 * 1024 ) ;
47+ if ( sizeInMB >= 1 ) {
48+ setFileSize ( sizeInMB . toFixed ( 2 ) + " MB" ) ;
49+ } else {
50+ setFileSize ( ( selectedFile . size / 1024 ) . toFixed ( 2 ) + " KB" ) ;
51+ }
52+ } ;
53+
54+ const scrollToPricing = ( ) => {
55+ const pricingSection = document . getElementById ( 'pricing' ) ;
56+ if ( pricingSection ) {
57+ pricingSection . scrollIntoView ( {
58+ behavior : 'smooth' ,
59+ block : 'start'
60+ } ) ;
61+ }
4262 } ;
4363
4464 const handleUpload = async ( ) => {
4565 if ( ! file ) return ;
4666
67+ // Clear any previous errors
68+ setFileSizeError ( null ) ;
69+
70+ // Client-side validation first
71+ const currentTier = session ?. user ?. tier ?. toLowerCase ( ) || "anonymous" ;
72+ const validation = validateFileSize ( file , currentTier ) ;
73+
74+ if ( ! validation . isValid ) {
75+ setFileSizeError ( validation . error || "File size validation failed" ) ;
76+ return ;
77+ }
78+
4779 setIsUploading ( true ) ;
4880 setProgress ( 0 ) ;
4981 setIsSuccess ( false ) ;
@@ -53,7 +85,7 @@ export default function Home() {
5385 // 1. Request presigned URL from backend using the new API utility
5486 const { url, key, fileId, tier } = await uploadFile (
5587 file ,
56- session ?. user ?. tier ?. toLowerCase ( ) || "anonymous" ,
88+ currentTier ,
5789 ""
5890 ) ;
5991
@@ -87,7 +119,7 @@ export default function Home() {
87119 size : file . size ,
88120 tier : tier || "anonymous" ,
89121 hasPassword : false ,
90- expiresAt : new Date ( Date . now ( ) + 24 * 60 * 60 * 1000 ) . toISOString ( ) ,
122+ expiresAt : calculateExpiryTime ( tier || "anonymous" ) ,
91123 downloadCount : 0
92124 } ;
93125
@@ -117,9 +149,17 @@ export default function Home() {
117149 xhr . setRequestHeader ( "Content-Type" , file . type ) ;
118150 xhr . send ( file ) ;
119151 } ) ;
120- } catch ( err ) {
152+ } catch ( err : any ) {
121153 console . error ( "Upload failed" , err ) ;
122154 setIsUploading ( false ) ;
155+
156+ // Handle file size errors specifically
157+ if ( err . error === "FILE_TOO_LARGE" || err . message ?. includes ( "File too large" ) ) {
158+ setFileSizeError ( err . message || "File is too large for your current tier" ) ;
159+ } else {
160+ // Handle other errors
161+ setFileSizeError ( err . message || "Upload failed. Please try again." ) ;
162+ }
123163 } finally {
124164 setIsUploading ( false ) ;
125165 }
@@ -144,6 +184,7 @@ export default function Home() {
144184 isUploading = { isUploading }
145185 progress = { progress }
146186 uploadSuccess = { uploadSuccess }
187+ tier = { session ?. user ?. tier ?. toLowerCase ( ) || "anonymous" }
147188 />
148189 </ div >
149190
@@ -163,6 +204,16 @@ export default function Home() {
163204 { isSuccess && (
164205 < UploadSuccessAlert onClose = { ( ) => setIsSuccess ( false ) } />
165206 ) }
207+
208+ { /* File Size Error Alert */ }
209+ { fileSizeError && (
210+ < FileSizeErrorAlert
211+ error = { fileSizeError }
212+ onClose = { ( ) => setFileSizeError ( null ) }
213+ onUpgrade = { scrollToPricing }
214+ />
215+ ) }
216+
166217 < Footer />
167218 </ div >
168219 ) ;
0 commit comments