Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Refactor upload progress UI for video components

Richie McIlroy4a48888da277
diff --git a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
index c9e807075..cd802e3b5 100644
--- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
+++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
@@ -8,6 +8,7 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
} from "@cap/ui";
+import { calculateStrokeDashoffset, getProgressCircleConfig } from "@cap/utils";
import type { ImageUpload, Video } from "@cap/web-domain";
import { HttpClient } from "@effect/platform";
import {
@@ -34,15 +35,14 @@ import { type PropsWithChildren, useState } from "react";
import { toast } from "sonner";
import { ConfirmationDialog } from "@/app/(org)/dashboard/_components/ConfirmationDialog";
import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
-import ProgressCircle, {
- useUploadProgress,
-} from "@/app/s/[videoId]/_components/ProgressCircle";
+import { useUploadProgress } from "@/app/s/[videoId]/_components/ProgressCircle";
import {
type ImageLoadingStatus,
VideoThumbnail,
} from "@/components/VideoThumbnail";
import { useEffectMutation, useRpcClient } from "@/lib/EffectRuntime";
import { usePublicEnv } from "@/utils/public-env";
+
import { PasswordDialog } from "../PasswordDialog";
import { SettingsDialog } from "../SettingsDialog";
import { SharingDialog } from "../SharingDialog";
@@ -50,6 +50,21 @@ import { CapCardAnalytics } from "./CapCardAnalytics";
import { CapCardButton } from "./CapCardButton";
import { CapCardContent } from "./CapCardContent";
+const { circumference } = getProgressCircleConfig();
+
+function getProgressStatusText(
+ status: "uploading" | "processing" | "generating_thumbnail",
+) {
+ switch (status) {
+ case "processing":
+ return "Processing";
+ case "generating_thumbnail":
+ return "Finishing up";
+ default:
+ return "Uploading";
+ }
+}
+
export interface CapCardProps extends PropsWithChildren {
cap: {
id: Video.VideoId;
@@ -565,53 +580,64 @@ export const CapCard = ({
}}
href={`/s/${cap.id}`}
>
- {imageStatus !== "success" &&
- uploadProgress &&
- uploadProgress?.status !== "fetching" ? (
- <div className="relative inset-0 z-20 w-full h-full">
- <div className="overflow-hidden relative mx-auto w-full h-full bg-black rounded-t-xl border-b border-gray-3 aspect-video z-5">
- <div className="flex absolute inset-0 justify-center items-center rounded-t-xl">
- {uploadProgress.status === "failed" ||
- uploadProgress.status === "error" ? (
- <div className="flex flex-col items-center">
- <div className="flex justify-center items-center mb-2 w-8 h-8 bg-red-500 rounded-full">
- <FontAwesomeIcon
- icon={faVideo}
- className="text-white size-3"
- />
- </div>
- <p className="text-[13px] text-center text-white">
- {uploadProgress.status === "error"
- ? "Processing failed"
- : "Upload failed"}
- </p>
- </div>
- ) : (
- <div className="relative size-20 md:size-16">
- <ProgressCircle
- progressTextClassName="md:!text-[11px]"
- subTextClassName="!mt-0 md:!text-[7px] !text-[10px] mb-1"
- className="md:scale-[1.5] scale-[1.2]"
- progress={uploadProgress.progress}
- status={
- uploadProgress.status === "processing"
- ? "processing"
- : uploadProgress.status === "generating_thumbnail"
- ? "generating_thumbnail"
- : "uploading"
- }
- message={
- uploadProgress.status === "processing"
- ? uploadProgress.message
- : undefined
- }
+ {uploadProgress && uploadProgress?.status !== "fetching" && (
+ <>
+ <div className="absolute inset-0 z-20 transition-all duration-300 bg-black/60 rounded-t-xl" />
+ <div className="flex absolute bottom-3 left-3 gap-2 items-center z-30">
+ {uploadProgress.status === "failed" ||
+ uploadProgress.status === "error" ? (
+ <span className="text-sm font-semibold text-red-400">
+ {uploadProgress.status === "error"
+ ? "Processing failed"
+ : "Upload failed"}
+ </span>
+ ) : (
+ <>
+ <span className="text-sm font-semibold text-white">
+ {getProgressStatusText(
+ uploadProgress.status === "processing"
+ ? "processing"
+ : uploadProgress.status === "generating_thumbnail"
+ ? "generating_thumbnail"
+ : "uploading",
+ )}
+ </span>
+ <svg
+ className="w-4 h-4 transform -rotate-90"
+ viewBox="0 0 20 20"
+ >
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ className="text-white/30"
+ />
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ strokeLinecap="round"
+ className="text-white transition-all duration-200 ease-out"
+ style={{
+ strokeDasharray: `${circumference} ${circumference}`,
+ strokeDashoffset: `${calculateStrokeDashoffset(
+ uploadProgress.progress,
+ circumference,
+ )}`,
+ }}
/>
- </div>
- )}
- </div>
+ </svg>
+ </>
+ )}
</div>
- </div>
- ) : null}
+ </>
+ )}
<VideoThumbnail
videoDuration={cap.duration}
@@ -623,10 +649,7 @@ export const CapCard = ({
: "group-hover:opacity-30",
"transition-opacity duration-200",
)}
- containerClass={clsx(
- imageStatus !== "success" && uploadProgress ? "hidden" : "",
- "absolute inset-0",
- )}
+ containerClass="absolute inset-0"
videoId={cap.id}
alt={`${cap.name} Thumbnail`}
imageStatus={imageStatus}
diff --git a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
index 78d51b21f..3700d6c78 100644
--- a/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/CapVideoPlayer.tsx
@@ -1,6 +1,7 @@
"use client";
import { LogoSpinner } from "@cap/ui";
+import { calculateStrokeDashoffset, getProgressCircleConfig } from "@cap/utils";
import type { Video } from "@cap/web-domain";
import { faPlay } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
@@ -10,7 +11,8 @@ import { AnimatePresence, motion } from "framer-motion";
import { AlertTriangleIcon } from "lucide-react";
import { useCallback, useEffect, useRef, useState } from "react";
import CommentStamp from "./CommentStamp";
-import ProgressCircle, { useUploadProgress } from "./ProgressCircle";
+import { useUploadProgress } from "./ProgressCircle";
+
import {
MediaPlayer,
MediaPlayerCaptions,
@@ -31,6 +33,21 @@ import {
MediaPlayerVolumeIndicator,
} from "./video/media-player";
+const { circumference } = getProgressCircleConfig();
+
+function getProgressStatusText(
+ status: "uploading" | "processing" | "generating_thumbnail",
+) {
+ switch (status) {
+ case "processing":
+ return "Processing";
+ case "generating_thumbnail":
+ return "Finishing up";
+ default:
+ return "Uploading";
+ }
+}
+
interface Props {
videoSrc: string;
videoId: Video.VideoId;
@@ -102,10 +119,14 @@ export function CapVideoPlayer({
videoId,
hasActiveUpload || false,
);
- // if the video comes back from S3, just ignore the upload progress.
const uploadProgress = videoLoaded ? null : uploadProgressRaw;
const isUploading = uploadProgress?.status === "uploading";
+ const isProcessing = uploadProgress?.status === "processing";
+ const isGeneratingThumbnail =
+ uploadProgress?.status === "generating_thumbnail";
const isUploadProgressPending = uploadProgress?.status === "fetching";
+ const hasActiveProgress =
+ isUploading || isProcessing || isGeneratingThumbnail;
const resolvedSrc = useQuery<{ url: string; supportsCrossOrigin: boolean }>({
queryKey: ["resolvedSrc", videoSrc],
@@ -488,6 +509,8 @@ export function CapVideoPlayer({
);
const isUploadFailed = uploadProgress?.status === "failed";
+ const isUploadError = uploadProgress?.status === "error";
+ const hasFailedOrError = isUploadFailed || isUploadError;
const prevUploadProgress = useRef<typeof uploadProgress>(uploadProgress);
useEffect(() => {
@@ -513,12 +536,13 @@ export function CapVideoPlayer({
)}
autoHide
>
- {isUploadFailed && (
+ {hasFailedOrError && (
<div className="flex absolute inset-0 flex-col px-3 gap-3 z-[20] justify-center items-center bg-black transition-opacity duration-300">
<AlertTriangleIcon className="text-red-500 size-12" />
<p className="text-gray-11 text-sm leading-relaxed text-center text-balance w-full max-w-[340px] mx-auto">
- Upload failed. Please try re-uploading from the Cap desktop app via
- Settings {">"} Previous Recordings.
+ {isUploadError
+ ? "Processing failed. Please try re-uploading from the Cap desktop app via Settings > Previous Recordings."
+ : "Upload stalled. Please try re-uploading from the Cap desktop app via Settings > Previous Recordings."}
</p>
</div>
)}
@@ -568,28 +592,68 @@ export function CapVideoPlayer({
</MediaPlayerVideo>
)}
<AnimatePresence>
- {!videoLoaded && isUploading && !isUploadFailed && (
- <motion.div
- initial={{ opacity: 0, y: 10 }}
- animate={{ opacity: 1, y: 0 }}
- exit={{ opacity: 0, y: 10 }}
- transition={{ duration: 0.2 }}
- className="flex absolute inset-0 z-10 justify-center items-center m-auto size-[130px] md:size-32"
- >
- <ProgressCircle
- progress={
- uploadProgress?.status === "uploading"
- ? uploadProgress.progress
- : 0
- }
+ {!videoLoaded && hasActiveProgress && !hasFailedOrError && (
+ <>
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.2 }}
+ className="absolute inset-0 z-10 transition-all duration-300 bg-black/60 rounded-xl"
/>
- </motion.div>
+ <motion.div
+ initial={{ opacity: 0, y: 10 }}
+ animate={{ opacity: 1, y: 0 }}
+ exit={{ opacity: 0, y: 10 }}
+ transition={{ duration: 0.2 }}
+ className="flex absolute bottom-3 left-3 gap-2 items-center z-20"
+ >
+ <span className="text-sm font-semibold text-white">
+ {getProgressStatusText(
+ isProcessing
+ ? "processing"
+ : isGeneratingThumbnail
+ ? "generating_thumbnail"
+ : "uploading",
+ )}
+ {uploadProgress?.progress != null &&
+ uploadProgress.progress > 0 &&
+ ` ${Math.round(uploadProgress.progress)}%`}
+ </span>
+ <svg className="w-4 h-4 transform -rotate-90" viewBox="0 0 20 20">
+ <title>Progress</title>
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ className="text-white/30"
+ />
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ strokeLinecap="round"
+ className="text-white transition-all duration-200 ease-out"
+ style={{
+ strokeDasharray: `${circumference} ${circumference}`,
+ strokeDashoffset: `${calculateStrokeDashoffset(uploadProgress?.progress ?? 0, circumference)}`,
+ }}
+ />
+ </svg>
+ </motion.div>
+ </>
)}
{showPlayButton &&
videoLoaded &&
!hasPlayedOnce &&
- !isUploading &&
- !isUploadFailed && (
+ !hasActiveProgress &&
+ !hasFailedOrError && (
<motion.div
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
@@ -661,7 +725,7 @@ export function CapVideoPlayer({
<MediaPlayerControls
className="flex-col items-start gap-2.5"
mainControlsVisible={(arg: boolean) => setMainControlsVisible(arg)}
- isUploadingOrFailed={isUploading || isUploadFailed}
+ isUploadingOrFailed={hasActiveProgress || hasFailedOrError}
>
<MediaPlayerControlsOverlay className="rounded-b-xl" />
<MediaPlayerSeek
diff --git a/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx b/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
index 55f76c489..426d3fb51 100644
--- a/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
+++ b/apps/web/app/s/[videoId]/_components/HLSVideoPlayer.tsx
@@ -1,6 +1,7 @@
"use client";
import { LogoSpinner } from "@cap/ui";
+import { calculateStrokeDashoffset, getProgressCircleConfig } from "@cap/utils";
import type { Video } from "@cap/web-domain";
import { faPlay } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
@@ -9,7 +10,23 @@ import { AnimatePresence, motion } from "framer-motion";
import Hls from "hls.js";
import { AlertTriangleIcon } from "lucide-react";
import { useEffect, useRef, useState } from "react";
-import ProgressCircle, { useUploadProgress } from "./ProgressCircle";
+import { useUploadProgress } from "./ProgressCircle";
+
+const { circumference } = getProgressCircleConfig();
+
+function getProgressStatusText(
+ status: "uploading" | "processing" | "generating_thumbnail",
+) {
+ switch (status) {
+ case "processing":
+ return "Processing";
+ case "generating_thumbnail":
+ return "Finishing up";
+ default:
+ return "Uploading";
+ }
+}
+
import {
MediaPlayer,
MediaPlayerCaptions,
@@ -270,10 +287,16 @@ export function HLSVideoPlayer({
videoId,
hasActiveUpload || false,
);
- // if the video comes back from S3, just ignore the upload progress.
const uploadProgress = videoLoaded ? null : uploadProgressRaw;
const isUploading = uploadProgress?.status === "uploading";
+ const isProcessing = uploadProgress?.status === "processing";
+ const isGeneratingThumbnail =
+ uploadProgress?.status === "generating_thumbnail";
const isUploadFailed = uploadProgress?.status === "failed";
+ const isUploadError = uploadProgress?.status === "error";
+ const hasFailedOrError = isUploadFailed || isUploadError;
+ const hasActiveProgress =
+ isUploading || isProcessing || isGeneratingThumbnail;
return (
<MediaPlayer
@@ -287,19 +310,20 @@ export function HLSVideoPlayer({
)}
autoHide
>
- {isUploadFailed && (
+ {hasFailedOrError && (
<div className="flex absolute inset-0 flex-col px-3 gap-3 z-[20] justify-center items-center bg-black transition-opacity duration-300">
<AlertTriangleIcon className="text-red-500 size-12" />
<p className="text-gray-11 text-sm leading-relaxed text-center text-balance w-full max-w-[340px] mx-auto">
- Upload failed. Please try re-uploading from the Cap desktop app via
- Settings {">"} Previous Recordings.
+ {isUploadError
+ ? "Processing failed. Please try re-uploading from the Cap desktop app via Settings > Previous Recordings."
+ : "Upload stalled. Please try re-uploading from the Cap desktop app via Settings > Previous Recordings."}
</p>
</div>
)}
<div
className={clsx(
"flex absolute inset-0 z-10 justify-center items-center bg-black transition-opacity duration-300",
- isUploading || videoLoaded || !isUploadFailed
+ hasActiveProgress || videoLoaded || !hasFailedOrError
? "opacity-0 pointer-events-none"
: "opacity-100",
)}
@@ -309,40 +333,83 @@ export function HLSVideoPlayer({
</div>
</div>
<AnimatePresence>
- {!videoLoaded && isUploading && (
- <motion.div
- initial={{ opacity: 0, y: 10 }}
- animate={{ opacity: 1, y: 0 }}
- exit={{ opacity: 0, y: 10 }}
- transition={{ duration: 0.2 }}
- className="flex absolute inset-0 z-10 justify-center items-center m-auto size-[130px] md:size-32"
- >
- <ProgressCircle
- progress={
- uploadProgress?.status === "uploading"
- ? uploadProgress.progress
- : 0
- }
- />
- </motion.div>
- )}
- {showPlayButton && videoLoaded && !hasPlayedOnce && !isUploading && (
- <motion.div
- whileHover={{ scale: 1.1 }}
- whileTap={{ scale: 0.9 }}
- initial={{ opacity: 0, y: 10 }}
- animate={{ opacity: 1, y: 0 }}
- exit={{ opacity: 0, y: 10 }}
- transition={{ duration: 0.2 }}
- onClick={() => videoRef.current?.play()}
- className="flex absolute inset-0 z-10 justify-center items-center m-auto bg-blue-500 rounded-full transition-colors transform cursor-pointer hover:bg-blue-600 size-12 xs:size-20 md:size-32"
- >
- <FontAwesomeIcon
- icon={faPlay}
- className="text-white size-4 xs:size-8 md:size-12"
+ {!videoLoaded && hasActiveProgress && (
+ <>
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ exit={{ opacity: 0 }}
+ transition={{ duration: 0.2 }}
+ className="absolute inset-0 z-10 transition-all duration-300 bg-black/60 rounded-xl"
/>
- </motion.div>
+ <motion.div
+ initial={{ opacity: 0, y: 10 }}
+ animate={{ opacity: 1, y: 0 }}
+ exit={{ opacity: 0, y: 10 }}
+ transition={{ duration: 0.2 }}
+ className="flex absolute bottom-3 left-3 gap-2 items-center z-20"
+ >
+ <span className="text-sm font-semibold text-white">
+ {getProgressStatusText(
+ isProcessing
+ ? "processing"
+ : isGeneratingThumbnail
+ ? "generating_thumbnail"
+ : "uploading",
+ )}
+ {uploadProgress?.progress != null &&
+ uploadProgress.progress > 0 &&
+ ` ${Math.round(uploadProgress.progress)}%`}
+ </span>
+ <svg className="w-4 h-4 transform -rotate-90" viewBox="0 0 20 20">
+ <title>Progress</title>
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ className="text-white/30"
+ />
+ <circle
+ cx="10"
+ cy="10"
+ r="8"
+ stroke="currentColor"
+ strokeWidth="3"
+ fill="none"
+ strokeLinecap="round"
+ className="text-white transition-all duration-200 ease-out"
+ style={{
+ strokeDasharray: `${circumference} ${circumference}`,
+ strokeDashoffset: `${calculateStrokeDashoffset(uploadProgress?.progress ?? 0, circumference)}`,
+ }}
+ />
+ </svg>
+ </motion.div>
+ </>
)}
+ {showPlayButton &&
+ videoLoaded &&
+ !hasPlayedOnce &&
+ !hasActiveProgress && (
+ <motion.div
+ whileHover={{ scale: 1.1 }}
+ whileTap={{ scale: 0.9 }}
+ initial={{ opacity: 0, y: 10 }}
+ animate={{ opacity: 1, y: 0 }}
+ exit={{ opacity: 0, y: 10 }}
+ transition={{ duration: 0.2 }}
+ onClick={() => videoRef.current?.play()}
+ className="flex absolute inset-0 z-10 justify-center items-center m-auto bg-blue-500 rounded-full transition-colors transform cursor-pointer hover:bg-blue-600 size-12 xs:size-20 md:size-32"
+ >
+ <FontAwesomeIcon
+ icon={faPlay}
+ className="text-white size-4 xs:size-8 md:size-12"
+ />
+ </motion.div>
+ )}
</AnimatePresence>
<MediaPlayerVideo
src={undefined} // HLS source is handled by HLS.js
@@ -382,7 +449,7 @@ export function HLSVideoPlayer({
<MediaPlayerVolumeIndicator />
<MediaPlayerControls
className="flex-col items-start gap-2.5"
- isUploadingOrFailed={isUploading || isUploadFailed}
+ isUploadingOrFailed={hasActiveProgress || hasFailedOrError}
>
<MediaPlayerControlsOverlay />
<MediaPlayerSeek />
diff --git a/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx b/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx
index 1b66691fe..d0270c623 100644
--- a/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx
+++ b/apps/web/app/s/[videoId]/_components/ProgressCircle.tsx
@@ -120,14 +120,12 @@ export function useUploadProgress(
const ProgressCircle = ({
progress,
status = "uploading",
- message,
className,
progressTextClassName,
subTextClassName,
}: {
progress: number;
status?: "uploading" | "processing" | "generating_thumbnail";
- message?: string | null;
className?: string;
progressTextClassName?: string;
subTextClassName?: string;
@@ -137,11 +135,11 @@ const ProgressCircle = ({
const getStatusText = () => {
switch (status) {
case "processing":
- return message || "Processing...";
+ return "Processing";
case "generating_thumbnail":
- return "Generating thumbnail...";
+ return "Finishing up";
default:
- return "Uploading...";
+ return "Uploading";
}
};