Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions apps/desktop/src-tauri/src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,15 @@ impl UploadProgressUpdater {
async fn send_api_update(app: &AppHandle, video_id: String, uploaded: u64, total: u64) {
let response = app
.authed_api_request("/api/desktop/video/progress", |client, url| {
client.post(url).json(&json!({
"videoId": video_id,
"uploaded": uploaded,
"total": total,
"updatedAt": chrono::Utc::now().to_rfc3339()
}))
client
.post(url)
.header("X-Cap-Desktop-Version", env!("CARGO_PKG_VERSION"))
.json(&json!({
"videoId": video_id,
"uploaded": uploaded,
"total": total,
"updatedAt": chrono::Utc::now().to_rfc3339()
}))
})
.await;

Expand Down
6 changes: 5 additions & 1 deletion apps/web/actions/video/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
import { s3Buckets, videos } from "@cap/database/schema";
import { s3Buckets, videos, videoUploads } from "@cap/database/schema";
import { buildEnv, NODE_ENV, serverEnv } from "@cap/env";
import { userIsPro } from "@cap/utils";
import { eq } from "drizzle-orm";
Expand Down Expand Up @@ -231,6 +231,10 @@ export async function createVideoAndGetUploadUrl({

await db().insert(videos).values(videoData);

await db().insert(videoUploads).values({
videoId: idToUse,
});

const fileKey = `${user.id}/${idToUse}/${
isScreenshot ? "screenshot/screen-capture.jpg" : "result.mp4"
}`;
Expand Down
33 changes: 7 additions & 26 deletions apps/web/app/(org)/dashboard/caps/Caps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ export const Caps = ({
const previousCountRef = useRef<number>(0);
const [selectedCaps, setSelectedCaps] = useState<Video.VideoId[]>([]);
const [isDraggingCap, setIsDraggingCap] = useState(false);
const {
isUploading,
setIsUploading,
setUploadingCapId,
setUploadProgress,
uploadingCapId,
setUploadingThumbnailUrl,
} = useUploadingContext();
const { uploadStatus } = useUploadingContext();

const anyCapSelected = selectedCaps.length > 0;

Expand Down Expand Up @@ -262,11 +255,13 @@ export const Caps = ({
toast.success("Cap deleted successfully");
router.refresh();
},
onError: () => {
toast.error("Failed to delete cap");
},
onError: () => toast.error("Failed to delete cap"),
});

const isUploading = uploadStatus !== undefined;
const uploadingCapId =
uploadStatus && "capId" in uploadStatus ? uploadStatus.capId : undefined;

const visibleVideos = useMemo(
() =>
isUploading && uploadingCapId
Expand All @@ -293,21 +288,7 @@ export const Caps = ({
<FontAwesomeIcon className="size-3.5" icon={faFolderPlus} />
New Folder
</Button>
<UploadCapButton
onStart={(id, thumbnailUrl) => {
setIsUploading(true);
setUploadingCapId(id);
setUploadingThumbnailUrl(thumbnailUrl);
setUploadProgress(0);
}}
size="sm"
onComplete={() => {
setIsUploading(false);
setUploadingCapId(null);
setUploadingThumbnailUrl(undefined);
setUploadProgress(0);
}}
/>
<UploadCapButton size="sm" />
</div>
{folders.length > 0 && (
<>
Expand Down
71 changes: 46 additions & 25 deletions apps/web/app/(org)/dashboard/caps/UploadingContext.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,73 @@
"use client";

import type React from "react";
import { createContext, useContext, useState } from "react";
import { createContext, useContext, useEffect, useState } from "react";

interface UploadingContextType {
isUploading: boolean;
setIsUploading: (value: boolean) => void;
uploadingCapId: string | null;
setUploadingCapId: (id: string | null) => void;
uploadingThumbnailUrl: string | undefined;
setUploadingThumbnailUrl: (url: string | undefined) => void;
uploadProgress: number;
setUploadProgress: (progress: number) => void;
uploadStatus: UploadStatus | undefined;
setUploadStatus: (state: UploadStatus | undefined) => void;
}

export type UploadStatus =
| {
status: "parsing";
}
| {
status: "creating";
}
| {
status: "converting";
capId: string;
progress: number;
}
| {
status: "uploadingThumbnail";
capId: string;
progress: number;
}
| {
status: "uploadingVideo";
capId: string;
progress: number;
thumbnailUrl: string | undefined;
};

const UploadingContext = createContext<UploadingContextType | undefined>(
undefined,
);

export function useUploadingContext() {
const context = useContext(UploadingContext);
if (!context) {
if (!context)
throw new Error(
"useUploadingContext must be used within an UploadingProvider",
);
}
return context;
}

export function UploadingProvider({ children }: { children: React.ReactNode }) {
const [isUploading, setIsUploading] = useState(false);
const [uploadingCapId, setUploadingCapId] = useState<string | null>(null);
const [uploadingThumbnailUrl, setUploadingThumbnailUrl] = useState<
string | undefined
>(undefined);
const [uploadProgress, setUploadProgress] = useState(0);
const [state, setState] = useState<UploadStatus>();

// Prevent the user closing the tab while uploading
useEffect(() => {
const handleBeforeUnload = (e: BeforeUnloadEvent) => {
if (state?.status) {
e.preventDefault();
// Chrome requires returnValue to be set
e.returnValue = "";
return "";
}
};

window.addEventListener("beforeunload", handleBeforeUnload);
return () => window.removeEventListener("beforeunload", handleBeforeUnload);
}, [state]);

return (
<UploadingContext.Provider
value={{
isUploading,
setIsUploading,
uploadingCapId,
setUploadingCapId,
uploadingThumbnailUrl,
setUploadingThumbnailUrl,
uploadProgress,
setUploadProgress,
uploadStatus: state,
setUploadStatus: setState,
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import {
Button,
Dialog,
Expand Down
Loading
Loading