@@ -53,6 +53,9 @@ export const UploadCapButton = ({
53
53
} ,
54
54
onError : ( error ) => {
55
55
console . error ( "Upload failed:" , error ) ;
56
+ toast . error (
57
+ "Failed to process video file. This format may not be supported for upload." ,
58
+ ) ;
56
59
if ( inputRef . current ) inputRef . current . value = "" ;
57
60
} ,
58
61
} ) ;
@@ -61,12 +64,11 @@ export const UploadCapButton = ({
61
64
const file = e . target . files ?. [ 0 ] ;
62
65
if ( ! file || ! user ) return ;
63
66
64
- // TODO: Enable this
65
- // uploadCapMutation.mutate(file);
67
+ uploadCapMutation . mutate ( file ) ;
66
68
67
- const ok = await legacyUploadCap ( file , folderId , setUploadStatus ) ;
68
- if ( ok ) router . refresh ( ) ;
69
- if ( inputRef . current ) inputRef . current . value = "" ;
69
+ // const ok = await legacyUploadCap(file, folderId, setUploadStatus);
70
+ // if (ok) router.refresh();
71
+ // if (inputRef.current) inputRef.current.value = "";
70
72
} ;
71
73
72
74
const isUploading = ! ! uploadStatus || uploadCapMutation . isPending ;
@@ -436,6 +438,8 @@ const createUploadEffect = (
436
438
} ) ;
437
439
}
438
440
441
+ const progressTracker = createProgressTracker ( ) ;
442
+
439
443
const progressStream = Stream . fromQueue ( progressQueue ) . pipe (
440
444
Stream . tap ( ( { loaded, total } ) =>
441
445
Effect . sync ( ( ) => {
@@ -447,7 +451,7 @@ const createUploadEffect = (
447
451
progress : percent ,
448
452
thumbnailUrl,
449
453
} ) ;
450
- sendProgressUpdate ( uploadId , loaded , total ) ;
454
+ progressTracker . scheduleProgressUpdate ( uploadId , loaded , total ) ;
451
455
} else {
452
456
const thumbnailProgress = 90 + percent * 0.1 ;
453
457
setUploadStatus ( {
@@ -480,14 +484,21 @@ const createUploadEffect = (
480
484
481
485
xhr . onload = ( ) => {
482
486
if ( xhr . status >= 200 && xhr . status < 300 ) {
487
+ progressTracker . cleanup ( ) ;
488
+ if ( uploadType === "video" ) {
489
+ const total = progressTracker . getTotal ( ) || 1 ;
490
+ sendProgressUpdate ( uploadId , total , total ) ;
491
+ }
483
492
Effect . runSync ( Queue . shutdown ( progressQueue ) ) ;
484
493
resolve ( ) ;
485
494
} else {
495
+ progressTracker . cleanup ( ) ;
486
496
Effect . runSync ( Queue . shutdown ( progressQueue ) ) ;
487
497
reject ( new Error ( `Upload failed with status ${ xhr . status } ` ) ) ;
488
498
}
489
499
} ;
490
500
xhr . onerror = ( ) => {
501
+ progressTracker . cleanup ( ) ;
491
502
Effect . runSync ( Queue . shutdown ( progressQueue ) ) ;
492
503
reject ( new Error ( "Upload failed" ) ) ;
493
504
} ;
@@ -499,6 +510,60 @@ const createUploadEffect = (
499
510
yield * Effect . race ( progressStream , uploadEffect ) ;
500
511
} ) ;
501
512
513
+ const createProgressTracker = ( ) => {
514
+ const uploadState = {
515
+ videoId : "" ,
516
+ uploaded : 0 ,
517
+ total : 0 ,
518
+ pendingTask : undefined as ReturnType < typeof setTimeout > | undefined ,
519
+ lastUpdateTime : Date . now ( ) ,
520
+ } ;
521
+
522
+ const scheduleProgressUpdate = (
523
+ videoId : string ,
524
+ uploaded : number ,
525
+ total : number ,
526
+ ) => {
527
+ uploadState . videoId = videoId ;
528
+ uploadState . uploaded = uploaded ;
529
+ uploadState . total = total ;
530
+ uploadState . lastUpdateTime = Date . now ( ) ;
531
+
532
+ if ( uploadState . pendingTask ) {
533
+ clearTimeout ( uploadState . pendingTask ) ;
534
+ uploadState . pendingTask = undefined ;
535
+ }
536
+
537
+ const shouldSendImmediately = uploaded >= total ;
538
+
539
+ if ( shouldSendImmediately ) {
540
+ return ;
541
+ } else {
542
+ uploadState . pendingTask = setTimeout ( ( ) => {
543
+ if ( uploadState . videoId ) {
544
+ sendProgressUpdate (
545
+ uploadState . videoId ,
546
+ uploadState . uploaded ,
547
+ uploadState . total ,
548
+ ) ;
549
+ }
550
+ uploadState . pendingTask = undefined ;
551
+ } , 2000 ) ;
552
+ }
553
+ } ;
554
+
555
+ const cleanup = ( ) => {
556
+ if ( uploadState . pendingTask ) {
557
+ clearTimeout ( uploadState . pendingTask ) ;
558
+ uploadState . pendingTask = undefined ;
559
+ }
560
+ } ;
561
+
562
+ const getTotal = ( ) => uploadState . total ;
563
+
564
+ return { scheduleProgressUpdate, cleanup, getTotal } ;
565
+ } ;
566
+
502
567
const sendProgressUpdate = async (
503
568
videoId : string ,
504
569
uploaded : number ,
0 commit comments