Skip to content

Commit 1d7b744

Browse files
improvements
1 parent bac0aaf commit 1d7b744

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

apps/web/app/(org)/dashboard/caps/components/UploadCapButton.tsx

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ export const UploadCapButton = ({
5353
},
5454
onError: (error) => {
5555
console.error("Upload failed:", error);
56+
toast.error(
57+
"Failed to process video file. This format may not be supported for upload.",
58+
);
5659
if (inputRef.current) inputRef.current.value = "";
5760
},
5861
});
@@ -61,12 +64,11 @@ export const UploadCapButton = ({
6164
const file = e.target.files?.[0];
6265
if (!file || !user) return;
6366

64-
// TODO: Enable this
65-
// uploadCapMutation.mutate(file);
67+
uploadCapMutation.mutate(file);
6668

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 = "";
7072
};
7173

7274
const isUploading = !!uploadStatus || uploadCapMutation.isPending;
@@ -436,6 +438,8 @@ const createUploadEffect = (
436438
});
437439
}
438440

441+
const progressTracker = createProgressTracker();
442+
439443
const progressStream = Stream.fromQueue(progressQueue).pipe(
440444
Stream.tap(({ loaded, total }) =>
441445
Effect.sync(() => {
@@ -447,7 +451,7 @@ const createUploadEffect = (
447451
progress: percent,
448452
thumbnailUrl,
449453
});
450-
sendProgressUpdate(uploadId, loaded, total);
454+
progressTracker.scheduleProgressUpdate(uploadId, loaded, total);
451455
} else {
452456
const thumbnailProgress = 90 + percent * 0.1;
453457
setUploadStatus({
@@ -480,14 +484,21 @@ const createUploadEffect = (
480484

481485
xhr.onload = () => {
482486
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+
}
483492
Effect.runSync(Queue.shutdown(progressQueue));
484493
resolve();
485494
} else {
495+
progressTracker.cleanup();
486496
Effect.runSync(Queue.shutdown(progressQueue));
487497
reject(new Error(`Upload failed with status ${xhr.status}`));
488498
}
489499
};
490500
xhr.onerror = () => {
501+
progressTracker.cleanup();
491502
Effect.runSync(Queue.shutdown(progressQueue));
492503
reject(new Error("Upload failed"));
493504
};
@@ -499,6 +510,60 @@ const createUploadEffect = (
499510
yield* Effect.race(progressStream, uploadEffect);
500511
});
501512

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+
502567
const sendProgressUpdate = async (
503568
videoId: string,
504569
uploaded: number,

0 commit comments

Comments
 (0)