Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

web: Add videos button in folders & more (#1161)

Ameer Al Ashhab10a7f5a67bdf
diff --git a/apps/web/actions/folders/add-videos.ts b/apps/web/actions/folders/add-videos.ts
new file mode 100644
index 000000000..2c95af584
--- /dev/null
+++ b/apps/web/actions/folders/add-videos.ts
@@ -0,0 +1,97 @@
+"use server";
+
+import { db } from "@cap/database";
+import { getCurrentUser } from "@cap/database/auth/session";
+import {
+ folders,
+ sharedVideos,
+ spaceVideos,
+ videos,
+} from "@cap/database/schema";
+import type { Folder, Space, Video } from "@cap/web-domain";
+import { and, eq, inArray } from "drizzle-orm";
+import { revalidatePath } from "next/cache";
+
+export async function addVideosToFolder(
+ folderId: Folder.FolderId,
+ videoIds: Video.VideoId[],
+ spaceId: Space.SpaceIdOrOrganisationId,
+) {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user || !user.id) {
+ throw new Error("Unauthorized");
+ }
+
+ if (!folderId || !videoIds || videoIds.length === 0) {
+ throw new Error("Missing required data");
+ }
+
+ const [folder] = await db()
+ .select({ id: folders.id, spaceId: folders.spaceId })
+ .from(folders)
+ .where(eq(folders.id, folderId));
+
+ if (!folder) {
+ throw new Error("Folder not found");
+ }
+
+ const userVideos = await db()
+ .select({ id: videos.id })
+ .from(videos)
+ .where(and(eq(videos.ownerId, user.id), inArray(videos.id, videoIds)));
+
+ const validVideoIds = userVideos.map((v) => v.id);
+
+ if (validVideoIds.length === 0) {
+ throw new Error("No valid videos found");
+ }
+
+ const isAllSpacesEntry = spaceId === user.activeOrganizationId;
+
+ //if video already exists in the space, then move it
+ if (isAllSpacesEntry) {
+ await db()
+ .update(sharedVideos)
+ .set({ folderId })
+ .where(
+ and(
+ eq(sharedVideos.organizationId, user.activeOrganizationId),
+ inArray(sharedVideos.videoId, validVideoIds),
+ ),
+ );
+ } else {
+ await db()
+ .update(spaceVideos)
+ .set({ folderId })
+ .where(
+ and(
+ eq(spaceVideos.spaceId, spaceId),
+ inArray(spaceVideos.videoId, validVideoIds),
+ ),
+ );
+ }
+
+ revalidatePath(`/dashboard/caps`);
+ revalidatePath(`/dashboard/folder/${folderId}`);
+ if (spaceId) {
+ revalidatePath(`/dashboard/spaces/${spaceId}/folder/${folderId}`);
+ }
+
+ return {
+ success: true,
+ message: `${validVideoIds.length} video${validVideoIds.length === 1 ? "" : "s"} added to folder`,
+ addedCount: validVideoIds.length,
+ };
+ } catch (error) {
+ console.error("Error adding videos to folder:", error);
+ return {
+ success: false,
+ error:
+ error instanceof Error
+ ? error.message
+ : "Failed to add videos to folder",
+ };
+ }
+}
diff --git a/apps/web/actions/folders/get-folder-videos.ts b/apps/web/actions/folders/get-folder-videos.ts
new file mode 100644
index 000000000..704f6aeb2
--- /dev/null
+++ b/apps/web/actions/folders/get-folder-videos.ts
@@ -0,0 +1,50 @@
+"use server";
+
+import { db } from "@cap/database";
+import { getCurrentUser } from "@cap/database/auth/session";
+import { sharedVideos, spaceVideos } from "@cap/database/schema";
+import type { Folder, Space, Video } from "@cap/web-domain";
+import { eq } from "drizzle-orm";
+
+export async function getFolderVideoIds(
+ folderId: Folder.FolderId,
+ spaceId: Space.SpaceIdOrOrganisationId,
+) {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user || !user.id) {
+ throw new Error("Unauthorized");
+ }
+
+ if (!folderId) {
+ throw new Error("Folder ID is required");
+ }
+
+ const isAllSpacesEntry = user.activeOrganizationId === spaceId;
+
+ const rows = isAllSpacesEntry
+ ? await db()
+ .select({ id: sharedVideos.videoId })
+ .from(sharedVideos)
+ .where(eq(sharedVideos.folderId, folderId))
+ : await db()
+ .select({ id: spaceVideos.videoId })
+ .from(spaceVideos)
+ .where(eq(spaceVideos.folderId, folderId));
+
+ return {
+ success: true,
+ data: rows.map((r) => r.id as Video.VideoId),
+ };
+ } catch (error) {
+ console.error("Error fetching folder video IDs:", error);
+ return {
+ success: false,
+ error:
+ error instanceof Error
+ ? error.message
+ : "Failed to fetch folder videos",
+ };
+ }
+}
diff --git a/apps/web/actions/folders/moveVideoToFolder.ts b/apps/web/actions/folders/moveVideoToFolder.ts
index 86a88fbbd..59d8de51d 100644
--- a/apps/web/actions/folders/moveVideoToFolder.ts
+++ b/apps/web/actions/folders/moveVideoToFolder.ts
@@ -2,11 +2,15 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
-import { folders, spaceVideos, videos } from "@cap/database/schema";
-import type { Folder, Video } from "@cap/web-domain";
+import {
+ folders,
+ sharedVideos,
+ spaceVideos,
+ videos,
+} from "@cap/database/schema";
+import type { Folder, Space, Video } from "@cap/web-domain";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
-
export async function moveVideoToFolder({
videoId,
folderId,
@@ -14,7 +18,7 @@ export async function moveVideoToFolder({
}: {
videoId: Video.VideoId;
folderId: Folder.FolderId | null;
- spaceId?: string | null;
+ spaceId?: Space.SpaceIdOrOrganisationId | null;
}) {
const user = await getCurrentUser();
if (!user || !user.activeOrganizationId)
@@ -30,6 +34,8 @@ export async function moveVideoToFolder({
const originalFolderId = currentVideo?.folderId;
+ const isAllSpacesEntry = spaceId === user.activeOrganizationId;
+
// If folderId is provided, verify it exists and belongs to the same organization
if (folderId) {
const [folder] = await db()
@@ -47,24 +53,36 @@ export async function moveVideoToFolder({
}
}
- if (spaceId) {
+ if (spaceId && !isAllSpacesEntry) {
await db()
.update(spaceVideos)
.set({
folderId: folderId === null ? null : folderId,
})
- .where(eq(spaceVideos.videoId, videoId));
+ .where(
+ and(eq(spaceVideos.videoId, videoId), eq(spaceVideos.spaceId, spaceId)),
+ );
+ } else if (spaceId && isAllSpacesEntry) {
+ await db()
+ .update(sharedVideos)
+ .set({
+ folderId: folderId === null ? null : folderId,
+ })
+ .where(
+ and(
+ eq(sharedVideos.videoId, videoId),
+ eq(sharedVideos.organizationId, user.activeOrganizationId),
+ ),
+ );
+ } else {
+ await db()
+ .update(videos)
+ .set({
+ folderId: folderId === null ? null : folderId,
+ })
+ .where(eq(videos.id, videoId));
}
- // Update the video's folderId
- await db()
- .update(videos)
- .set({
- folderId,
- updatedAt: new Date(),
- })
- .where(eq(videos.id, videoId));
-
// Always revalidate the main caps page
revalidatePath(`/dashboard/caps`);
diff --git a/apps/web/actions/folders/remove-videos.ts b/apps/web/actions/folders/remove-videos.ts
new file mode 100644
index 000000000..680a7a359
--- /dev/null
+++ b/apps/web/actions/folders/remove-videos.ts
@@ -0,0 +1,112 @@
+"use server";
+
+import { db } from "@cap/database";
+import { getCurrentUser } from "@cap/database/auth/session";
+import {
+ folders,
+ sharedVideos,
+ spaceVideos,
+ videos,
+} from "@cap/database/schema";
+import type { Folder, Space, Video } from "@cap/web-domain";
+import { and, eq, inArray, sql } from "drizzle-orm";
+import { revalidatePath } from "next/cache";
+
+export async function removeVideosFromFolder(
+ folderId: Folder.FolderId,
+ videoIds: Video.VideoId[],
+ spaceId: Space.SpaceIdOrOrganisationId,
+) {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user || !user.id) {
+ throw new Error("Unauthorized");
+ }
+
+ const isAllSpacesEntry = user.activeOrganizationId === spaceId;
+
+ if (!folderId || !videoIds || videoIds.length === 0) {
+ throw new Error("Missing required data");
+ }
+
+ // Verify folder exists and is accessible
+ const [folder] = await db()
+ .select({ id: folders.id, spaceId: folders.spaceId })
+ .from(folders)
+ .where(eq(folders.id, folderId));
+
+ if (!folder) {
+ throw new Error("Folder not found");
+ }
+
+ // Only allow updating videos the user owns
+ const userVideos = await db()
+ .select({ id: videos.id })
+ .from(videos)
+ .where(and(eq(videos.ownerId, user.id), inArray(videos.id, videoIds)));
+
+ const validVideoIds = userVideos.map((v) => v.id);
+
+ if (validVideoIds.length === 0) {
+ throw new Error("No valid videos found");
+ }
+
+ // Clear the folderId on the videos
+ await db()
+ .update(videos)
+ .set({ folderId: null, updatedAt: new Date() })
+ .where(
+ and(inArray(videos.id, validVideoIds), eq(videos.folderId, folderId)),
+ );
+
+ // Clear the folderId in the appropriate table based on context
+ if (isAllSpacesEntry || !folder.spaceId) {
+ // Organization-level folder - clear folderId in sharedVideos
+ await db()
+ .update(sharedVideos)
+ .set({ folderId: null })
+ .where(
+ and(
+ eq(sharedVideos.organizationId, user.activeOrganizationId),
+ inArray(sharedVideos.videoId, validVideoIds),
+ eq(sharedVideos.folderId, folderId),
+ ),
+ );
+ } else if (folder.spaceId) {
+ // Space-level folder - clear folderId in spaceVideos
+ await db()
+ .update(spaceVideos)
+ .set({ folderId: null })
+ .where(
+ and(
+ sql`${spaceVideos.spaceId} = ${folder.spaceId}`,
+ inArray(spaceVideos.videoId, validVideoIds),
+ eq(spaceVideos.folderId, folderId),
+ ),
+ );
+ }
+
+ // Revalidate relevant paths
+ revalidatePath(`/dashboard/caps`);
+ revalidatePath(`/dashboard/folder/${folderId}`);
+ if (folder.spaceId) {
+ revalidatePath(`/dashboard/spaces/${folder.spaceId}/folder/${folderId}`);
+ }
+
+ return {
+ success: true,
+ message: `${validVideoIds.length} video${validVideoIds.length === 1 ? "" : "s"} removed from folder`,
+ removedCount: validVideoIds.length,
+ };
+ } catch (error) {
+ console.error("Error removing videos from folder:", error);
+ return {
+ success: false,
+ error:
+ error instanceof Error
+ ? error.message
+ : "Failed to remove videos from folder",
+ };
+ }
+}
diff --git a/apps/web/actions/organizations/add-videos.ts b/apps/web/actions/organizations/add-videos.ts
index 97c24fc84..a811dcd62 100644
--- a/apps/web/actions/organizations/add-videos.ts
+++ b/apps/web/actions/organizations/add-videos.ts
@@ -87,36 +87,40 @@ export async function addVideosToOrganization(
(id) => !existingVideoIds.includes(id),
);
- if (newVideoIds.length === 0) {
- return {
- success: true,
- message: "Videos already shared with organization",
- };
+ // Update existing videos to move them to root (clear folderId)
+ if (existingVideoIds.length > 0) {
+ await db()
+ .update(sharedVideos)
+ .set({ folderId: null })
+ .where(
+ and(
+ eq(sharedVideos.organizationId, organizationId),
+ inArray(sharedVideos.videoId, existingVideoIds),
+ ),
+ );
}
- const sharedVideoEntries = newVideoIds.map((videoId) => ({
- id: nanoId(),
- videoId,
- organizationId,
- sharedByUserId: user.id,
- }));
+ // Insert new videos
+ if (newVideoIds.length > 0) {
+ const sharedVideoEntries = newVideoIds.map((videoId) => ({
+ id: nanoId(),
+ videoId,
+ organizationId,
+ sharedByUserId: user.id,
+ }));
- await db().insert(sharedVideos).values(sharedVideoEntries);
-
- // Clear folderId for videos added to organization so they appear in main view
- await db()
- .update(videos)
- .set({ folderId: null })
- .where(inArray(videos.id, newVideoIds));
+ await db().insert(sharedVideos).values(sharedVideoEntries);
+ }
revalidatePath(`/dashboard/spaces/${organizationId}`);
revalidatePath("/dashboard/caps");
+ const totalUpdated = existingVideoIds.length + newVideoIds.length;
return {
success: true,
- message: `${newVideoIds.length} video${
- newVideoIds.length === 1 ? "" : "s"
- } shared with organization`,
+ message: `${totalUpdated} video${
+ totalUpdated === 1 ? "" : "s"
+ } ${totalUpdated === 1 ? "is" : "are"} now in organization root`,
};
} catch (error) {
console.error("Error adding videos to organization:", error);
diff --git a/apps/web/actions/organizations/get-organization-videos.ts b/apps/web/actions/organizations/get-organization-videos.ts
index 41431c3af..2e584388f 100644
--- a/apps/web/actions/organizations/get-organization-videos.ts
+++ b/apps/web/actions/organizations/get-organization-videos.ts
@@ -4,7 +4,7 @@ import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { sharedVideos } from "@cap/database/schema";
import type { Organisation } from "@cap/web-domain";
-import { eq } from "drizzle-orm";
+import { and, eq, isNull } from "drizzle-orm";
export async function getOrganizationVideoIds(
organizationId: Organisation.OrganisationId,
@@ -25,7 +25,12 @@ export async function getOrganizationVideoIds(
videoId: sharedVideos.videoId,
})
.from(sharedVideos)
- .where(eq(sharedVideos.organizationId, organizationId));
+ .where(
+ and(
+ eq(sharedVideos.organizationId, organizationId),
+ isNull(sharedVideos.folderId),
+ ),
+ );
return {
success: true,
diff --git a/apps/web/actions/spaces/add-videos.ts b/apps/web/actions/spaces/add-videos.ts
index bbb565681..55cbf8d5f 100644
--- a/apps/web/actions/spaces/add-videos.ts
+++ b/apps/web/actions/spaces/add-videos.ts
@@ -3,7 +3,7 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
-import { spaces, spaceVideos, videos } from "@cap/database/schema";
+import { sharedVideos, spaceVideos, videos } from "@cap/database/schema";
import type { Space, Video } from "@cap/web-domain";
import { and, eq, inArray } from "drizzle-orm";
import { revalidatePath } from "next/cache";
@@ -23,14 +23,7 @@ export async function addVideosToSpace(
throw new Error("Missing required data");
}
- const [space] = await db()
- .select()
- .from(spaces)
- .where(eq(spaces.id, spaceId));
-
- if (!space) {
- throw new Error("Space not found");
- }
+ const isAllSpacesEntry = user.activeOrganizationId === spaceId;
const userVideos = await db()
.select({ id: videos.id })
@@ -43,40 +36,91 @@ export async function addVideosToSpace(
throw new Error("No valid videos found");
}
- const existingSpaceVideos = await db()
- .select({ videoId: spaceVideos.videoId })
- .from(spaceVideos)
- .where(
- and(
- eq(spaceVideos.spaceId, spaceId),
- inArray(spaceVideos.videoId, validVideoIds),
- ),
+ if (isAllSpacesEntry) {
+ const existingSharedVideos = await db()
+ .select({ videoId: sharedVideos.videoId })
+ .from(sharedVideos)
+ .where(
+ and(
+ eq(sharedVideos.organizationId, spaceId),
+ inArray(sharedVideos.videoId, validVideoIds),
+ ),
+ );
+
+ const existingVideoIds = existingSharedVideos.map((v) => v.videoId);
+ const newVideoIds = validVideoIds.filter(
+ (id) => !existingVideoIds.includes(id),
);
- const existingVideoIds = existingSpaceVideos.map((sv) => sv.videoId);
- const newVideoIds = validVideoIds.filter(
- (id) => !existingVideoIds.includes(id),
- );
+ if (existingVideoIds.length > 0) {
+ await db()
+ .update(sharedVideos)
+ .set({ folderId: null })
+ .where(
+ and(
+ eq(sharedVideos.organizationId, spaceId),
+ inArray(sharedVideos.videoId, existingVideoIds),
+ ),
+ );
+ }
+
+ // Insert new videos
+ if (newVideoIds.length > 0) {
+ const sharedVideoEntries = newVideoIds.map((videoId) => ({
+ id: nanoId(),
+ videoId,
+ organizationId: spaceId,
+ sharedByUserId: user.id,
+ }));
+ await db().insert(sharedVideos).values(sharedVideoEntries);
+ }
+ } else {
+ // Check which videos already exist in spaceVideos
+ const existingSpaceVideos = await db()
+ .select({ videoId: spaceVideos.videoId })
+ .from(spaceVideos)
+ .where(
+ and(
+ eq(spaceVideos.spaceId, spaceId),
+ inArray(spaceVideos.videoId, validVideoIds),
+ ),
+ );
+
+ const existingVideoIds = existingSpaceVideos.map((v) => v.videoId);
+ const newVideoIds = validVideoIds.filter(
+ (id) => !existingVideoIds.includes(id),
+ );
- if (newVideoIds.length === 0) {
- return { success: true, message: "Videos already added to space" };
+ if (existingVideoIds.length > 0) {
+ await db()
+ .update(spaceVideos)
+ .set({ folderId: null })
+ .where(
+ and(
+ eq(spaceVideos.spaceId, spaceId),
+ inArray(spaceVideos.videoId, existingVideoIds),
+ ),
+ );
+ }
+
+ if (newVideoIds.length > 0) {
+ const spaceVideoEntries = newVideoIds.map((videoId) => ({
+ id: nanoId(),
+ videoId,
+ spaceId,
+ addedById: user.id,
+ }));
+
+ await db().insert(spaceVideos).values(spaceVideoEntries);
+ }
}
- const spaceVideoEntries = newVideoIds.map((videoId) => ({
- id: nanoId(),
- videoId,
- spaceId,
- addedById: user.id,
- }));
-
- await db().insert(spaceVideos).values(spaceVideoEntries);
-
revalidatePath(`/dashboard/spaces/${spaceId}`);
revalidatePath("/dashboard/caps");
return {
success: true,
- message: `${newVideoIds.length} video${newVideoIds.length === 1 ? "" : "s"} added to space`,
+ message: `${validVideoIds.length} video${validVideoIds.length === 1 ? "" : "s"} added to ${isAllSpacesEntry ? "organization" : "space"}`,
};
} catch (error) {
console.error("Error adding videos to space:", error);
diff --git a/apps/web/actions/spaces/get-space-videos.ts b/apps/web/actions/spaces/get-space-videos.ts
index 7ebf0d61f..ddbcc42b8 100644
--- a/apps/web/actions/spaces/get-space-videos.ts
+++ b/apps/web/actions/spaces/get-space-videos.ts
@@ -2,9 +2,9 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
-import { spaceVideos } from "@cap/database/schema";
+import { sharedVideos, spaceVideos } from "@cap/database/schema";
import type { Space } from "@cap/web-domain";
-import { eq } from "drizzle-orm";
+import { and, eq, isNull } from "drizzle-orm";
export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
try {
@@ -18,12 +18,28 @@ export async function getSpaceVideoIds(spaceId: Space.SpaceIdOrOrganisationId) {
throw new Error("Space ID is required");
}
- const videoIds = await db()
- .select({
- videoId: spaceVideos.videoId,
- })
- .from(spaceVideos)
- .where(eq(spaceVideos.spaceId, spaceId));
+ const isAllSpacesEntry = user.activeOrganizationId === spaceId;
+
+ const videoIds = isAllSpacesEntry
+ ? await db()
+ .select({
+ videoId: sharedVideos.videoId,
+ })
+ .from(sharedVideos)
+ .where(
+ and(
+ eq(sharedVideos.organizationId, spaceId),
+ isNull(sharedVideos.folderId),
+ ),
+ )
+ : await db()
+ .select({
+ videoId: spaceVideos.videoId,
+ })
+ .from(spaceVideos)
+ .where(
+ and(eq(spaceVideos.spaceId, spaceId), isNull(spaceVideos.folderId)),
+ );
return {
success: true,
diff --git a/apps/web/actions/spaces/get-user-videos.ts b/apps/web/actions/spaces/get-user-videos.ts
new file mode 100644
index 000000000..201eb8647
--- /dev/null
+++ b/apps/web/actions/spaces/get-user-videos.ts
@@ -0,0 +1,140 @@
+"use server";
+
+import { db } from "@cap/database";
+import { getCurrentUser } from "@cap/database/auth/session";
+import {
+ comments,
+ folders,
+ sharedVideos,
+ spaces,
+ spaceVideos,
+ users,
+ videos,
+ videoUploads,
+} from "@cap/database/schema";
+import type { Space } from "@cap/web-domain";
+import { and, desc, eq, sql } from "drizzle-orm";
+
+export async function getUserVideos(spaceId: Space.SpaceIdOrOrganisationId) {
+ try {
+ const user = await getCurrentUser();
+
+ if (!user || !user.id) {
+ throw new Error("Unauthorized");
+ }
+
+ const userId = user.id;
+ const isAllSpacesEntry = user.activeOrganizationId === spaceId;
+
+ const selectFields = {
+ id: videos.id,
+ ownerId: videos.ownerId,
+ name: videos.name,
+ createdAt: videos.createdAt,
+ metadata: videos.metadata,
+ totalComments: sql<number>`COUNT(DISTINCT CASE WHEN ${comments.type} = 'text' THEN ${comments.id} END)`,
+ totalReactions: sql<number>`COUNT(DISTINCT CASE WHEN ${comments.type} = 'emoji' THEN ${comments.id} END)`,
+ ownerName: users.name,
+ folderName: folders.name,
+ folderColor: folders.color,
+ effectiveDate: sql<string>`
+ COALESCE(
+ JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.customCreatedAt')),
+ ${videos.createdAt}
+ )
+ `,
+ hasActiveUpload: sql`${videoUploads.videoId} IS NOT NULL`.mapWith(
+ Boolean,
+ ),
+ };
+
+ const videoData = isAllSpacesEntry
+ ? await db()
+ .select(selectFields)
+ .from(videos)
+ .leftJoin(comments, eq(videos.id, comments.videoId))
+ .leftJoin(users, eq(videos.ownerId, users.id))
+ .leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
+ .leftJoin(
+ sharedVideos,
+ and(
+ eq(videos.id, sharedVideos.videoId),
+ eq(sharedVideos.organizationId, spaceId),
+ ),
+ )
+ .leftJoin(folders, eq(sharedVideos.folderId, folders.id))
+ .leftJoin(spaces, eq(folders.spaceId, spaces.id))
+ .where(eq(videos.ownerId, userId))
+ .groupBy(
+ videos.id,
+ videos.ownerId,
+ videos.name,
+ videos.createdAt,
+ videos.metadata,
+ users.name,
+ folders.name,
+ folders.color,
+ folders.spaceId,
+ videos.folderId,
+ )
+ .orderBy(
+ desc(sql`COALESCE(
+ JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.customCreatedAt')),
+ ${videos.createdAt}
+ )`),
+ )
+ : await db()
+ .select(selectFields)
+ .from(videos)
+ .leftJoin(comments, eq(videos.id, comments.videoId))
+ .leftJoin(users, eq(videos.ownerId, users.id))
+ .leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
+ .leftJoin(
+ spaceVideos,
+ and(
+ eq(videos.id, spaceVideos.videoId),
+ eq(spaceVideos.spaceId, spaceId),
+ ),
+ )
+ .leftJoin(folders, eq(spaceVideos.folderId, folders.id))
+ .leftJoin(spaces, eq(folders.spaceId, spaces.id))
+ .where(eq(videos.ownerId, userId))
+ .groupBy(
+ videos.id,
+ videos.ownerId,
+ videos.name,
+ videos.createdAt,
+ videos.metadata,
+ users.name,
+ folders.name,
+ folders.color,
+ folders.spaceId,
+ videos.folderId,
+ )
+ .orderBy(
+ desc(sql`COALESCE(
+ JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.customCreatedAt')),
+ ${videos.createdAt}
+ )`),
+ );
+
+ const processedVideoData = videoData.map((video) => {
+ const { effectiveDate: _effectiveDate, ...videoWithoutEffectiveDate } =
+ video;
+ return {
+ ...videoWithoutEffectiveDate,
+ ownerName: video.ownerName ?? "",
+ folderName: video.folderName ?? null,
+ folderColor: video.folderColor ?? null,
+ metadata: video.metadata as
+ | { customCreatedAt?: string; [key: string]: unknown }
+ | undefined,
+ };
+ });
+
+ return { success: true, data: processedVideoData };
+ } catch (error) {
+ console.error("Error fetching user videos:", error);
+ return { success: false, error: "Failed to fetch videos" };
+ }
+}
diff --git a/apps/web/actions/spaces/remove-videos.ts b/apps/web/actions/spaces/remove-videos.ts
index dcf39fb26..fb732ad87 100644
--- a/apps/web/actions/spaces/remove-videos.ts
+++ b/apps/web/actions/spaces/remove-videos.ts
@@ -2,7 +2,7 @@
import { db } from "@cap/database";
import { getCurrentUser } from "@cap/database/auth/session";
-import { folders, spaceVideos, videos } from "@cap/database/schema";
+import { sharedVideos, spaceVideos, videos } from "@cap/database/schema";
import type { Space, Video } from "@cap/web-domain";
import { and, eq, inArray } from "drizzle-orm";
import { revalidatePath } from "next/cache";
@@ -34,32 +34,24 @@ export async function removeVideosFromSpace(
throw new Error("No valid videos found");
}
- // Remove from spaceVideos
- await db()
- .delete(spaceVideos)
- .where(
- and(
- eq(spaceVideos.spaceId, spaceId),
- inArray(spaceVideos.videoId, validVideoIds),
- ),
- );
+ const isAllSpacesEntry = user.activeOrganizationId === spaceId;
- // Set folderId to null for any removed videos that are in folders belonging to this space
- // Find all folder IDs in this space
- const folderRows = await db()
- .select({ id: folders.id })
- .from(folders)
- .where(eq(folders.spaceId, spaceId));
- const folderIds = folderRows.map((f) => f.id);
-
- if (folderIds.length > 0) {
+ if (isAllSpacesEntry) {
+ await db()
+ .delete(sharedVideos)
+ .where(
+ and(
+ eq(sharedVideos.organizationId, spaceId),
+ inArray(sharedVideos.videoId, validVideoIds),
+ ),
+ );
+ } else {
await db()
- .update(videos)
- .set({ folderId: null })
+ .delete(spaceVideos)
.where(
and(
- inArray(videos.id, validVideoIds),
- inArray(videos.folderId, folderIds),
+ eq(spaceVideos.spaceId, spaceId),
+ inArray(spaceVideos.videoId, validVideoIds),
),
);
}
@@ -68,13 +60,16 @@ export async function removeVideosFromSpace(
return {
success: true,
- message: `Removed ${validVideoIds.length} video(s) from space and folders`,
+ message: `Removed ${validVideoIds.length} video(s) from ${isAllSpacesEntry ? "organization" : "space"} and folders`,
deletedCount: validVideoIds.length,
};
- } catch (error: any) {
+ } catch (error) {
return {
success: false,
- message: error.message || "Failed to remove videos from space",
+ message:
+ error instanceof Error
+ ? error.message
+ : "Failed to remove videos from space",
};
}
}
diff --git a/apps/web/actions/videos/get-user-videos.ts b/apps/web/actions/videos/get-user-videos.ts
deleted file mode 100644
index d12d4c043..000000000
--- a/apps/web/actions/videos/get-user-videos.ts
+++ /dev/null
@@ -1,75 +0,0 @@
-"use server";
-
-import { db } from "@cap/database";
-import { getCurrentUser } from "@cap/database/auth/session";
-import { comments, users, videos, videoUploads } from "@cap/database/schema";
-import { desc, eq, sql } from "drizzle-orm";
-
-export async function getUserVideos(limit?: number) {
- try {
- const user = await getCurrentUser();
-
- if (!user || !user.id) {
- throw new Error("Unauthorized");
- }
-
- const userId = user.id;
-
- const videoData = await db()
- .select({
- id: videos.id,
- ownerId: videos.ownerId,
- name: videos.name,
- createdAt: videos.createdAt,
- metadata: videos.metadata,
- totalComments: sql<number>`COUNT(DISTINCT CASE WHEN ${comments.type} = 'text' THEN ${comments.id} END)`,
- totalReactions: sql<number>`COUNT(DISTINCT CASE WHEN ${comments.type} = 'emoji' THEN ${comments.id} END)`,
- ownerName: users.name,
- effectiveDate: sql<string>`
- COALESCE(
- JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.customCreatedAt')),
- ${videos.createdAt}
- )
- `,
- hasActiveUpload: sql`${videoUploads.videoId} IS NOT NULL`.mapWith(
- Boolean,
- ),
- })
- .from(videos)
- .leftJoin(comments, eq(videos.id, comments.videoId))
- .leftJoin(users, eq(videos.ownerId, users.id))
- .leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
- .where(eq(videos.ownerId, userId))
- .groupBy(
- videos.id,
- videos.ownerId,
- videos.name,
- videos.createdAt,
- videos.metadata,
- users.name,
- )
- .orderBy(
- desc(sql`COALESCE(
- JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, '$.customCreatedAt')),
- ${videos.createdAt}
- )`),
- )
- .limit(limit || 20);
-
- const processedVideoData = videoData.map((video) => {
- const { effectiveDate, ...videoWithoutEffectiveDate } = video;
- return {
- ...videoWithoutEffectiveDate,
- ownerName: video.ownerName ?? "",
- metadata: video.metadata as
- | { customCreatedAt?: string; [key: string]: any }
- | undefined,
- };
- });
-
- return { success: true, data: processedVideoData };
- } catch (error) {
- console.error("Error fetching user videos:", error);
- return { success: false, error: "Failed to fetch videos" };
- }
-}
diff --git a/apps/web/app/(org)/dashboard/caps/components/Folder.tsx b/apps/web/app/(org)/dashboard/caps/components/Folder.tsx
index 51b2b846d..239821e25 100644
--- a/apps/web/app/(org)/dashboard/caps/components/Folder.tsx
+++ b/apps/web/app/(org)/dashboard/caps/components/Folder.tsx
@@ -1,5 +1,5 @@
"use client";
-import type { Folder } from "@cap/web-domain";
+import type { Folder, Space } from "@cap/web-domain";
import { faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { Fit, Layout, useRive } from "@rive-app/react-canvas";
@@ -21,8 +21,8 @@ export type FolderDataType = {
id: Folder.FolderId;
color: "normal" | "blue" | "red" | "yellow";
videoCount: number;
- spaceId?: string | null;
- parentId?: string | null;
+ spaceId?: Space.SpaceIdOrOrganisationId | null;
+ parentId: Folder.FolderId | null;
};
const FolderCard = ({
diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx
index ad73d21aa..f78bb69d8 100644
--- a/apps/web/app/(org)/dashboard/caps/page.tsx
+++ b/apps/web/app/(org)/dashboard/caps/page.tsx
@@ -133,20 +133,6 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
.where(eq(organizations.id, user.activeOrganizationId))
.limit(1);
- let customDomain: string | null = null;
- let domainVerified = false;
-
- if (
- organizationData.length > 0 &&
- organizationData[0] &&
- organizationData[0].customDomain
- ) {
- customDomain = organizationData[0].customDomain;
- if (organizationData[0].domainVerified !== null) {
- domainVerified = true;
- }
- }
-
const videoData = await db()
.select({
id: videos.id,
diff --git a/apps/web/app/(org)/dashboard/folder/[id]/components/BreadcrumbItem.tsx b/apps/web/app/(org)/dashboard/folder/[id]/components/BreadcrumbItem.tsx
index 30f486272..8e41ade75 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/components/BreadcrumbItem.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/components/BreadcrumbItem.tsx
@@ -1,6 +1,6 @@
"use client";
-import type { Folder } from "@cap/web-domain";
+import type { Folder, Space } from "@cap/web-domain";
import clsx from "clsx";
import Link from "next/link";
import { useRouter } from "next/navigation";
@@ -15,6 +15,7 @@ interface BreadcrumbItemProps {
id: Folder.FolderId;
name: string;
color: "normal" | "blue" | "red" | "yellow";
+ spaceId?: Space.SpaceIdOrOrganisationId | null;
isLast: boolean;
}
@@ -23,6 +24,7 @@ export function BreadcrumbItem({
name,
color,
isLast,
+ spaceId,
}: BreadcrumbItemProps) {
const [isDragOver, setIsDragOver] = useState(false);
const [isMoving, setIsMoving] = useState(false);
@@ -59,7 +61,11 @@ export function BreadcrumbItem({
if (!capData.id) return;
setIsMoving(true);
- await moveVideoToFolder({ videoId: capData.id, folderId: id });
+ await moveVideoToFolder({
+ videoId: capData.id,
+ folderId: id,
+ spaceId: spaceId ?? null,
+ });
router.refresh();
toast.success(`"${capData.name}" moved to "${name}" folder`);
} catch (error) {
diff --git a/apps/web/app/(org)/dashboard/folder/[id]/components/ClientMyCapsLink.tsx b/apps/web/app/(org)/dashboard/folder/[id]/components/ClientMyCapsLink.tsx
index 70ed6af15..9d135f4c1 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/components/ClientMyCapsLink.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/components/ClientMyCapsLink.tsx
@@ -1,7 +1,7 @@
"use client";
import { Avatar } from "@cap/ui";
-import type { Video } from "@cap/web-domain";
+import type { Space, Video } from "@cap/web-domain";
import clsx from "clsx";
import Image from "next/image";
import Link from "next/link";
@@ -12,7 +12,11 @@ import { moveVideoToFolder } from "@/actions/folders/moveVideoToFolder";
import { useDashboardContext } from "../../../Contexts";
import { registerDropTarget } from "./ClientCapCard";
-export function ClientMyCapsLink() {
+export function ClientMyCapsLink({
+ spaceId,
+}: {
+ spaceId?: Space.SpaceIdOrOrganisationId;
+}) {
const [isDragOver, setIsDragOver] = useState(false);
const [isMovingVideo, setIsMovingVideo] = useState(false);
const linkRef = useRef<HTMLAnchorElement>(null);
@@ -90,7 +94,7 @@ export function ClientMyCapsLink() {
await moveVideoToFolder({
videoId: capData.id,
folderId: null,
- spaceId: activeSpace?.id,
+ spaceId: spaceId ?? null,
});
router.refresh();
if (activeSpace) {
@@ -109,9 +113,7 @@ export function ClientMyCapsLink() {
return (
<Link
ref={linkRef}
- href={
- activeSpace ? `/dashboard/spaces/${activeSpace.id}` : "/dashboard/caps"
- }
+ href={spaceId ? `/dashboard/spaces/${spaceId}` : "/dashboard/caps"}
className={clsx(
"text-xl whitespace-nowrap flex items-center gap-1.5 transition-colors duration-200 hover:text-gray-12",
isDragOver ? "text-blue-10" : "text-gray-9",
diff --git a/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx b/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
index ae3308928..8fcbd4afc 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
@@ -2,7 +2,6 @@
import type { Video } from "@cap/web-domain";
import { useQuery } from "@tanstack/react-query";
-import { useStore } from "@tanstack/react-store";
import { Effect, Exit } from "effect";
import { useRouter } from "next/navigation";
import { useMemo, useRef, useState } from "react";
diff --git a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
index 302be68ec..43c6cb999 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/page.tsx
@@ -26,7 +26,9 @@ const FolderPage = async ({ params }: { params: { id: Folder.FolderId } }) => {
const [childFolders, breadcrumb, videosData] = yield* Effect.all([
getChildFolders(params.id, { variant: "user" }),
getFolderBreadcrumb(params.id),
- getVideosByFolderId(params.id),
+ getVideosByFolderId(params.id, {
+ variant: "user",
+ }),
]);
return (
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx
index cf0b63387..b116ce8cd 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx
@@ -46,6 +46,7 @@ export const SharedCaps = ({
data,
count,
spaceData,
+ spaceId,
spaceMembers,
organizationMembers,
currentUserId,
@@ -57,6 +58,7 @@ export const SharedCaps = ({
count: number;
dubApiKeyEnabled: boolean;
spaceData?: SpaceData;
+ spaceId: Space.SpaceIdOrOrganisationId;
hideSharedWith?: boolean;
spaceMembers?: SpaceMemberData[];
organizationMembers?: OrganizationMemberData[];
@@ -180,7 +182,7 @@ export const SharedCaps = ({
<AddVideosDialog
open={isAddVideosDialogOpen}
onClose={() => setIsAddVideosDialogOpen(false)}
- spaceId={spaceData.id}
+ spaceId={spaceId}
spaceName={spaceData.name}
onVideosAdded={handleVideosAdded}
/>
@@ -192,6 +194,7 @@ export const SharedCaps = ({
organizationId={organizationData.id}
organizationName={organizationData.name}
onVideosAdded={handleVideosAdded}
+ spaceId={spaceId}
/>
)}
</div>
@@ -246,7 +249,7 @@ export const SharedCaps = ({
<AddVideosDialog
open={isAddVideosDialogOpen}
onClose={() => setIsAddVideosDialogOpen(false)}
- spaceId={spaceData.id}
+ spaceId={spaceId}
spaceName={spaceData.name}
onVideosAdded={handleVideosAdded}
/>
@@ -258,6 +261,7 @@ export const SharedCaps = ({
organizationId={organizationData.id}
organizationName={organizationData.name}
onVideosAdded={handleVideosAdded}
+ spaceId={spaceId}
/>
)}
<Button
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialog.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialog.tsx
index 305acfc0b..027f2083c 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialog.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialog.tsx
@@ -4,8 +4,8 @@ import type { Space } from "@cap/web-domain";
import type React from "react";
import { addVideosToSpace } from "@/actions/spaces/add-videos";
import { getSpaceVideoIds } from "@/actions/spaces/get-space-videos";
+import { getUserVideos } from "@/actions/spaces/get-user-videos";
import { removeVideosFromSpace } from "@/actions/spaces/remove-videos";
-import { getUserVideos } from "@/actions/videos/get-user-videos";
import AddVideosDialogBase from "./AddVideosDialogBase";
interface AddVideosDialogProps {
@@ -32,8 +32,8 @@ export const AddVideosDialog: React.FC<AddVideosDialogProps> = ({
onVideosAdded={onVideosAdded}
addVideos={addVideosToSpace}
removeVideos={removeVideosFromSpace}
- getVideos={getUserVideos}
- getEntityVideoIds={getSpaceVideoIds}
+ getVideos={() => getUserVideos(spaceId)}
+ getEntityVideoIds={() => getSpaceVideoIds(spaceId)}
/>
);
};
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialogBase.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialogBase.tsx
index 3ec721dab..9ab333aeb 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialogBase.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosDialogBase.tsx
@@ -16,7 +16,6 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import clsx from "clsx";
import { Search } from "lucide-react";
-import type React from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
@@ -31,8 +30,8 @@ interface AddVideosDialogBaseProp<T> {
onVideosAdded?: () => void;
addVideos: (entityId: T, videoIds: Video.VideoId[]) => Promise<any>;
removeVideos: (entityId: T, videoIds: Video.VideoId[]) => Promise<any>;
- getVideos: (limit?: number) => Promise<any>;
- getEntityVideoIds: (entityId: T) => Promise<any>;
+ getVideos: () => Promise<any>;
+ getEntityVideoIds: () => Promise<any>;
}
export interface VideoData {
@@ -43,6 +42,8 @@ export interface VideoData {
totalComments: number;
totalReactions: number;
ownerName: string;
+ folderName?: string | null;
+ folderColor?: "normal" | "blue" | "red" | "yellow" | null;
metadata?: {
customCreatedAt?: string;
};
@@ -91,9 +92,9 @@ function AddVideosDialogBase<T>({
});
const { data: entityVideoIds } = useQuery<Video.VideoId[]>({
- queryKey: ["entity-video-ids", entityId],
+ queryKey: ["entity-video-ids", entityId, entityName],
queryFn: async () => {
- const result = await getEntityVideoIds(entityId);
+ const result = await getEntityVideoIds();
if (!result.success) {
throw new Error(result.error);
}
@@ -101,7 +102,9 @@ function AddVideosDialogBase<T>({
},
enabled: open,
refetchOnWindowFocus: false,
- gcTime: 1000 * 60 * 5,
+ refetchOnMount: true,
+ staleTime: 0, // Always fetch fresh data
+ gcTime: 0, // Don't cache
});
const updateVideosMutation = useMutation({
@@ -189,6 +192,7 @@ function AddVideosDialogBase<T>({
updateVideosMutation.mutate({ toAdd, toRemove });
};
+ // Reset state when dialog closes
useEffect(() => {
if (!open) {
setSelectedVideos([]);
@@ -203,7 +207,7 @@ function AddVideosDialogBase<T>({
<DialogHeader
icon={<FontAwesomeIcon icon={faVideo} />}
description={
- "Find and add videos you have previously recorded to share with people in this " +
+ "Find and add videos you have previously recorded to share with people in " +
entityName +
"."
}
@@ -213,8 +217,9 @@ function AddVideosDialogBase<T>({
{/* Tabs for filtering */}
<div className="flex w-full h-12 border-b bg-gray-1 border-gray-4">
{filterTabs.map((tab) => (
- <div
+ <button
key={tab}
+ type="button"
className={clsx(
"flex relative flex-1 justify-center items-center w-full min-w-0 text-sm font-medium transition-colors",
videoTab === tab
@@ -222,6 +227,7 @@ function AddVideosDialogBase<T>({
: "cursor-pointer",
)}
onClick={() => setVideoTab(tab as "all" | "added" | "notAdded")}
+ disabled={videoTab === tab}
>
<p
className={clsx(
@@ -237,7 +243,7 @@ function AddVideosDialogBase<T>({
? "Added"
: "Not Added"}
</p>
- </div>
+ </button>
))}
</div>
@@ -294,7 +300,7 @@ function AddVideosDialogBase<T>({
entityVideoIds={entityVideoIds || []}
height={300}
columnCount={3}
- rowHeight={200}
+ rowHeight={230}
/>
)}
</div>
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosToOrganizationDialog.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosToOrganizationDialog.tsx
index bd5ece90c..5810e0804 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosToOrganizationDialog.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/AddVideosToOrganizationDialog.tsx
@@ -5,7 +5,7 @@ import type React from "react";
import { addVideosToOrganization } from "@/actions/organizations/add-videos";
import { getOrganizationVideoIds } from "@/actions/organizations/get-organization-videos";
import { removeVideosFromOrganization } from "@/actions/organizations/remove-videos";
-import { getUserVideos } from "@/actions/videos/get-user-videos";
+import { getUserVideos } from "@/actions/spaces/get-user-videos";
import AddVideosDialogBase from "./AddVideosDialogBase";
interface AddVideosToOrganizationDialogProps {
@@ -14,6 +14,7 @@ interface AddVideosToOrganizationDialogProps {
organizationId: Organisation.OrganisationId;
organizationName: string;
onVideosAdded?: () => void;
+ spaceId: string;
}
export const AddVideosToOrganizationDialog: React.FC<
@@ -28,8 +29,8 @@ export const AddVideosToOrganizationDialog: React.FC<
onVideosAdded={onVideosAdded}
removeVideos={removeVideosFromOrganization}
addVideos={addVideosToOrganization}
- getVideos={getUserVideos}
- getEntityVideoIds={getOrganizationVideoIds}
+ getVideos={() => getUserVideos(organizationId)}
+ getEntityVideoIds={() => getOrganizationVideoIds(organizationId)}
/>
);
};
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VideoCard.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VideoCard.tsx
index 590f1f75d..884508aca 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VideoCard.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VideoCard.tsx
@@ -1,9 +1,13 @@
+import { faHome, faRecordVinyl } from "@fortawesome/free-solid-svg-icons";
+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
+import { Fit, Layout, useRive } from "@rive-app/react-canvas";
import clsx from "clsx";
import { AnimatePresence, motion } from "framer-motion";
import { Check, Minus, Plus } from "lucide-react";
import moment from "moment";
import type React from "react";
import { memo, useState } from "react";
+import { useTheme } from "@/app/(org)/dashboard/Contexts";
import { Tooltip } from "@/components/Tooltip";
import {
type ImageLoadingStatus,
@@ -21,6 +25,7 @@ interface VideoCardProps {
const VideoCard: React.FC<VideoCardProps> = memo(
({ video, isSelected, onToggle, isAlreadyInEntity, className }) => {
+ const { theme } = useTheme();
const effectiveDate = video.metadata?.customCreatedAt
? new Date(video.metadata.customCreatedAt)
: video.createdAt;
@@ -28,11 +33,37 @@ const VideoCard: React.FC<VideoCardProps> = memo(
const [imageStatus, setImageStatus] =
useState<ImageLoadingStatus>("loading");
+ const folderColor = video.folderColor || "normal";
+ const artboard =
+ theme === "dark" && folderColor === "normal"
+ ? "folder"
+ : folderColor === "normal"
+ ? "folder-dark"
+ : `folder-${folderColor}`;
+
+ const { RiveComponent: FolderRive } = useRive({
+ src: "/rive/dashboard.riv",
+ artboard,
+ animations: "idle",
+ autoplay: true,
+ layout: new Layout({
+ fit: Fit.Contain,
+ }),
+ });
+
return (
<div
onClick={onToggle}
+ role="button"
+ tabIndex={0}
+ onKeyDown={(e) => {
+ if (e.key === "Enter" || e.key === " ") {
+ e.preventDefault();
+ onToggle();
+ }
+ }}
className={clsx(
- "flex relative flex-col p-3 w-full min-h-fit rounded-xl border transition-all duration-200 group",
+ "flex relative flex-col p-3 w-full h-full rounded-xl border transition-all duration-200 group",
className,
isAlreadyInEntity && isSelected && "border-red-500",
isAlreadyInEntity && !isSelected && "border-blue-500",
@@ -115,20 +146,51 @@ const VideoCard: React.FC<VideoCardProps> = memo(
/>
</div>
- <div className="space-y-1 min-h-fit">
- <Tooltip content={video.name}>
- <h3
- className={clsx(
- "text-sm font-medium leading-tight truncate",
- isAlreadyInEntity ? "text-gray-11" : "text-gray-12",
- )}
- >
- {video.name}
- </h3>
- </Tooltip>
- <p className="text-xs text-gray-9">
- {moment(effectiveDate).format("MMM D, YYYY")}
- </p>
+ <div className="space-y-2 min-h-fit">
+ <div className="flex flex-col gap-1">
+ <Tooltip content={video.name}>
+ <h3
+ className={clsx(
+ "text-sm font-medium leading-tight truncate text-gray-12",
+ )}
+ >
+ {video.name}
+ </h3>
+ </Tooltip>
+ <p className="text-xs text-gray-10">
+ {moment(effectiveDate).format("MMM D, YYYY")}
+ </p>
+ </div>
+ <div className="flex gap-1 items-center text-xs text-gray-10">
+ {video.folderName ? (
+ <>
+ <FolderRive
+ key={theme + folderColor}
+ color={folderColor}
+ className="size-4"
+ />
+ <p className="font-medium truncate text-gray-11">
+ {video.folderName}
+ </p>
+ </>
+ ) : isAlreadyInEntity ? (
+ <>
+ <FontAwesomeIcon
+ icon={faHome}
+ className="size-3 text-gray-10"
+ />
+ <p className="font-medium truncate text-gray-11">Root</p>
+ </>
+ ) : (
+ <>
+ <FontAwesomeIcon
+ icon={faRecordVinyl}
+ className="size-3 text-gray-10"
+ />
+ <p className="font-medium truncate text-gray-11">Caps</p>
+ </>
+ )}
+ </div>
</div>
</div>
);
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VirtualizedVideoGrid.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VirtualizedVideoGrid.tsx
index e991bb89f..e4c688a40 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VirtualizedVideoGrid.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/VirtualizedVideoGrid.tsx
@@ -83,7 +83,7 @@ const VirtualizedVideoGrid = ({
return (
<div
key={video.id}
- className="px-2 mx-auto md:mx-0 md:px-0 w-[96%]"
+ className="px-2 mx-auto md:mx-0 md:px-0 w-[96%] h-full"
>
<VideoCard
video={video}
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/AddVideosButton.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/AddVideosButton.tsx
new file mode 100644
index 000000000..415d01857
--- /dev/null
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/AddVideosButton.tsx
@@ -0,0 +1,52 @@
+"use client";
+
+import { Button } from "@cap/ui";
+import type { Folder, Space } from "@cap/web-domain";
+import { faPlus } from "@fortawesome/free-solid-svg-icons";
+import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
+import { useRouter } from "next/navigation";
+import { useState } from "react";
+import { addVideosToFolder } from "@/actions/folders/add-videos";
+import { getFolderVideoIds } from "@/actions/folders/get-folder-videos";
+import { removeVideosFromFolder } from "@/actions/folders/remove-videos";
+import { getUserVideos } from "@/actions/spaces/get-user-videos";
+import AddVideosDialogBase from "../../components/AddVideosDialogBase";
+
+export default function AddVideosButton({
+ folderId,
+ folderName,
+ spaceId,
+}: {
+ folderId: Folder.FolderId;
+ folderName: string;
+ spaceId: Space.SpaceIdOrOrganisationId;
+}) {
+ const [open, setOpen] = useState(false);
+ const router = useRouter();
+
+ return (
+ <>
+ <Button variant="dark" size="sm" onClick={() => setOpen(true)}>
+ <FontAwesomeIcon className="size-3" icon={faPlus} />
+ Add videos
+ </Button>
+ <AddVideosDialogBase
+ open={open}
+ onClose={() => setOpen(false)}
+ entityId={folderId}
+ entityName={folderName}
+ onVideosAdded={() => {
+ router.refresh();
+ }}
+ addVideos={(folderIdArg, videoIds) =>
+ addVideosToFolder(folderIdArg, videoIds, spaceId)
+ }
+ removeVideos={(folderIdArg, videoIds) =>
+ removeVideosFromFolder(folderIdArg, videoIds, spaceId)
+ }
+ getVideos={() => getUserVideos(spaceId)}
+ getEntityVideoIds={() => getFolderVideoIds(folderId, spaceId)}
+ />
+ </>
+ );
+}
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/page.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/page.tsx
index 4af71d3a4..655f78b9d 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/page.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/folder/[folderId]/page.tsx
@@ -17,9 +17,13 @@ import {
NewSubfolderButton,
} from "../../../../folder/[id]/components";
import FolderVideosSection from "../../../../folder/[id]/components/FolderVideosSection";
+import AddVideosButton from "./AddVideosButton";
const FolderPage = async (props: {
- params: Promise<{ spaceId: string; folderId: Folder.FolderId }>;
+ params: Promise<{
+ spaceId: Space.SpaceIdOrOrganisationId;
+ folderId: Folder.FolderId;
+ }>;
}) => {
const params = await props.params;
const user = await getCurrentUser();
@@ -40,21 +44,32 @@ const FolderPage = async (props: {
: { variant: "org", organizationId: spaceOrOrg.organization.id },
),
getFolderBreadcrumb(params.folderId),
- getVideosByFolderId(params.folderId),
+ getVideosByFolderId(
+ params.folderId,
+ spaceOrOrg.variant === "space"
+ ? { variant: "space", spaceId: spaceOrOrg.space.id }
+ : { variant: "org", organizationId: spaceOrOrg.organization.id },
+ ),
]);
return (
<div>
<div className="flex gap-2 items-center mb-10">
<NewSubfolderButton parentFolderId={params.folderId} />
+ <AddVideosButton
+ folderId={params.folderId}
+ spaceId={params.spaceId}
+ folderName={breadcrumb[breadcrumb.length - 1]?.name ?? "Folder"}
+ />
</div>
<div className="flex justify-between items-center mb-6 w-full">
<div className="flex overflow-x-auto items-center font-medium">
- <ClientMyCapsLink />
+ <ClientMyCapsLink spaceId={params.spaceId} />
{breadcrumb.map((folder, index) => (
<div key={folder.id} className="flex items-center">
<p className="mx-2 text-gray-10">/</p>
<BreadcrumbItem
+ spaceId={params.spaceId}
id={folder.id}
name={folder.name}
color={folder.color}
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
index 7f5d11007..750d52706 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
@@ -35,7 +35,11 @@ export type SpaceMemberData = {
};
// --- Helper functions ---
-async function fetchFolders(spaceId: Space.SpaceIdOrOrganisationId) {
+async function fetchFolders(
+ spaceId: Space.SpaceIdOrOrganisationId,
+ allSpacesEntry: boolean,
+) {
+ const table = allSpacesEntry ? sharedVideos : spaceVideos;
return db()
.select({
id: folders.id,
@@ -44,7 +48,7 @@ async function fetchFolders(spaceId: Space.SpaceIdOrOrganisationId) {
parentId: folders.parentId,
spaceId: folders.spaceId,
videoCount: sql<number>`(
- SELECT COUNT(*) FROM videos WHERE videos.folderId = folders.id
+ SELECT COUNT(*) FROM ${table} WHERE ${table}.folderId = folders.id
)`,
})
.from(folders)
@@ -110,7 +114,7 @@ export default async function SharedCapsPage(props: {
await Promise.all([
fetchSpaceMembers(space.id),
fetchOrganizationMembers(space.organizationId),
- fetchFolders(space.id),
+ fetchFolders(space.id, false),
]);
async function fetchSpaceVideos(
@@ -195,6 +199,7 @@ export default async function SharedCapsPage(props: {
data={processedVideoData}
count={totalCount}
spaceData={space}
+ spaceId={params.spaceId as Space.SpaceIdOrOrganisationId}
dubApiKeyEnabled={!!serverEnv().DUB_API_KEY}
spaceMembers={spaceMembersData}
organizationMembers={organizationMembersData}
@@ -238,7 +243,7 @@ export default async function SharedCapsPage(props: {
.where(
and(
eq(sharedVideos.organizationId, orgId),
- isNull(videos.folderId),
+ isNull(sharedVideos.folderId),
),
)
.groupBy(
@@ -280,7 +285,7 @@ export default async function SharedCapsPage(props: {
await Promise.all([
fetchOrganizationVideos(organization.id, page, limit),
fetchOrganizationMembers(organization.id),
- fetchFolders(organization.id),
+ fetchFolders(organization.id, true),
]);
const { videos: orgVideoData, totalCount } = organizationVideos;
@@ -302,6 +307,7 @@ export default async function SharedCapsPage(props: {
count={totalCount}
hideSharedWith
organizationData={organization}
+ spaceId={params.spaceId as Space.SpaceIdOrOrganisationId}
dubApiKeyEnabled={!!serverEnv().DUB_API_KEY}
organizationMembers={organizationMembersData}
currentUserId={user.id}
diff --git a/apps/web/lib/folder.ts b/apps/web/lib/folder.ts
index 89b686d5a..ed3273154 100644
--- a/apps/web/lib/folder.ts
+++ b/apps/web/lib/folder.ts
@@ -1,7 +1,5 @@
import "server-only";
-import { db } from "@cap/database";
-import { getCurrentUser } from "@cap/database/auth/session";
import {
comments,
folders,
@@ -19,7 +17,6 @@ import { CurrentUser, Folder } from "@cap/web-domain";
import { and, desc, eq } from "drizzle-orm";
import { sql } from "drizzle-orm/sql";
import { Effect } from "effect";
-import { revalidatePath } from "next/cache";
export const getFolderById = Effect.fn(function* (folderId: string) {
if (!folderId) throw new Error("Folder ID is required");
@@ -159,6 +156,10 @@ const getSharedSpacesForVideos = Effect.fn(function* (
export const getVideosByFolderId = Effect.fn(function* (
folderId: Folder.FolderId,
+ root:
+ | { variant: "user" }
+ | { variant: "space"; spaceId: Space.SpaceIdOrOrganisationId }
+ | { variant: "org"; organizationId: Organisation.OrganisationId },
) {
if (!folderId) throw new Error("Folder ID is required");
const db = yield* Database;
@@ -205,13 +206,20 @@ export const getVideosByFolderId = Effect.fn(function* (
.from(videos)
.leftJoin(comments, eq(videos.id, comments.videoId))
.leftJoin(sharedVideos, eq(videos.id, sharedVideos.videoId))
+ .leftJoin(spaceVideos, eq(videos.id, spaceVideos.videoId))
.leftJoin(
organizations,
eq(sharedVideos.organizationId, organizations.id),
)
.leftJoin(users, eq(videos.ownerId, users.id))
.leftJoin(videoUploads, eq(videos.id, videoUploads.videoId))
- .where(eq(videos.folderId, folderId))
+ .where(
+ root.variant === "space"
+ ? eq(spaceVideos.folderId, folderId)
+ : root.variant === "org"
+ ? eq(sharedVideos.folderId, folderId)
+ : eq(videos.folderId, folderId),
+ )
.groupBy(
videos.id,
videos.ownerId,
diff --git a/packages/database/migrations/meta/_journal.json b/packages/database/migrations/meta/_journal.json
index f3efe3fbd..d045a0c84 100644
--- a/packages/database/migrations/meta/_journal.json
+++ b/packages/database/migrations/meta/_journal.json
@@ -64,6 +64,13 @@
"when": 1759139970377,
"tag": "0008_condemned_gamora",
"breakpoints": true
+ },
+ {
+ "idx": 9,
+ "version": "5",
+ "when": 1759993551600,
+ "tag": "0009_sad_carmella_unuscione",
+ "breakpoints": true
}
]
}
diff --git a/packages/database/schema.ts b/packages/database/schema.ts
index bac98ae96..11d29c3e9 100644
--- a/packages/database/schema.ts
+++ b/packages/database/schema.ts
@@ -336,6 +336,7 @@ export const sharedVideos = mysqlTable(
{
id: nanoId("id").notNull().primaryKey().unique(),
videoId: nanoId("videoId").notNull().$type<Video.VideoId>(),
+ folderId: nanoIdNullable("folderId").$type<Folder.FolderId>(),
organizationId: nanoId("organizationId")
.notNull()
.$type<Organisation.OrganisationId>(),
@@ -344,6 +345,7 @@ export const sharedVideos = mysqlTable(
},
(table) => ({
videoIdIndex: index("video_id_idx").on(table.videoId),
+ folderIdIndex: index("folder_id_idx").on(table.folderId),
organizationIdIndex: index("organization_id_idx").on(table.organizationId),
sharedByUserIdIndex: index("shared_by_user_id_idx").on(
table.sharedByUserId,
@@ -352,6 +354,10 @@ export const sharedVideos = mysqlTable(
table.videoId,
table.organizationId,
),
+ videoIdFolderIdIndex: index("video_id_folder_id_idx").on(
+ table.videoId,
+ table.folderId,
+ ),
}),
);