From 0399cbf39a32f668d7fd0f3e2be6f39f4b883415 Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 19:15:02 +0300
Subject: [PATCH 1/7] Update Caps.tsx

---
 apps/web/app/(org)/dashboard/caps/Caps.tsx | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/apps/web/app/(org)/dashboard/caps/Caps.tsx b/apps/web/app/(org)/dashboard/caps/Caps.tsx
index 6a822dc065..5036f1cc17 100644
--- a/apps/web/app/(org)/dashboard/caps/Caps.tsx
+++ b/apps/web/app/(org)/dashboard/caps/Caps.tsx
@@ -114,12 +114,18 @@ export const Caps = ({
 			const results = await Promise.allSettled(analyticsPromises);
 			const analyticsData: Record<string, number> = {};
 
+			console.log({
+				analyticsData,
+			});
+
 			results.forEach((result) => {
 				if (result.status === "fulfilled" && result.value) {
 					analyticsData[result.value.videoId] = result.value.count;
 				}
 			});
 
+			console.log(results, "results");
+
 			return analyticsData;
 		},
 		staleTime: 30000, // 30 seconds

From 4454ddb91157ff901c86a3d7a44b4b4e40459c32 Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 19:26:32 +0300
Subject: [PATCH 2/7] Update Caps.tsx

---
 apps/web/app/(org)/dashboard/caps/Caps.tsx | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/apps/web/app/(org)/dashboard/caps/Caps.tsx b/apps/web/app/(org)/dashboard/caps/Caps.tsx
index 5036f1cc17..70f633a525 100644
--- a/apps/web/app/(org)/dashboard/caps/Caps.tsx
+++ b/apps/web/app/(org)/dashboard/caps/Caps.tsx
@@ -114,22 +114,14 @@ export const Caps = ({
 			const results = await Promise.allSettled(analyticsPromises);
 			const analyticsData: Record<string, number> = {};
 
-			console.log({
-				analyticsData,
-			});
-
 			results.forEach((result) => {
 				if (result.status === "fulfilled" && result.value) {
 					analyticsData[result.value.videoId] = result.value.count;
 				}
 			});
 
-			console.log(results, "results");
-
 			return analyticsData;
 		},
-		staleTime: 30000, // 30 seconds
-		refetchOnWindowFocus: false,
 	});
 
 	const analytics = analyticsData || {};

From 17453dc00291b88c098358f562ee8f2b5c8a957d Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 19:38:04 +0300
Subject: [PATCH 3/7] always latest data

---
 apps/web/app/(org)/dashboard/caps/Caps.tsx             |  6 +++++-
 .../folder/[id]/components/FolderVideosSection.tsx     | 10 ++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/apps/web/app/(org)/dashboard/caps/Caps.tsx b/apps/web/app/(org)/dashboard/caps/Caps.tsx
index 70f633a525..54dbb3fec2 100644
--- a/apps/web/app/(org)/dashboard/caps/Caps.tsx
+++ b/apps/web/app/(org)/dashboard/caps/Caps.tsx
@@ -81,8 +81,10 @@ export const Caps = ({
 
 	const anyCapSelected = selectedCaps.length > 0;
 
+	const videoIds = data.map((video) => video.id).sort();
+
 	const { data: analyticsData } = useQuery({
-		queryKey: ["analytics", data.map((video) => video.id)],
+		queryKey: ["analytics", videoIds],
 		queryFn: async () => {
 			if (!dubApiKeyEnabled || data.length === 0) {
 				return {};
@@ -122,6 +124,8 @@ export const Caps = ({
 
 			return analyticsData;
 		},
+		refetchOnWindowFocus: false,
+		refetchOnMount: true,
 	});
 
 	const analytics = analyticsData || {};
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 e517156546..e99d46a97f 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
@@ -17,7 +17,13 @@ import { SharedCapCard } from "../../../spaces/[spaceId]/components/SharedCapCar
 import { ClientCapCard } from "./index";
 
 interface FolderVideosSectionProps {
-	initialVideos: VideoData;
+	initialVideos: VideoData & {
+		sharedOrganizations: {
+			id: string;
+			name: string;
+			iconUrl: string;
+		}[];
+	};
 	dubApiKeyEnabled: boolean;
 	cardType?: "shared" | "default";
 	userId?: string;
@@ -145,8 +151,8 @@ export default function FolderVideosSection({
 			});
 			return analyticsData;
 		},
-		staleTime: 30000, // 30 seconds
 		refetchOnWindowFocus: false,
+		refetchOnMount: true,
 	});
 
 	const analytics = analyticsData || {};

From cc7b827d944c0910ba73ab40359b75622ba20169 Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 19:45:05 +0300
Subject: [PATCH 4/7] add missing public value for folder videos

---
 .../[id]/components/FolderVideosSection.tsx   |  8 +-----
 apps/web/lib/folder.ts                        | 26 ++++++++++++++-----
 2 files changed, 21 insertions(+), 13 deletions(-)

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 e99d46a97f..d8948bf1a0 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
@@ -17,13 +17,7 @@ import { SharedCapCard } from "../../../spaces/[spaceId]/components/SharedCapCar
 import { ClientCapCard } from "./index";
 
 interface FolderVideosSectionProps {
-	initialVideos: VideoData & {
-		sharedOrganizations: {
-			id: string;
-			name: string;
-			iconUrl: string;
-		}[];
-	};
+	initialVideos: VideoData;
 	dubApiKeyEnabled: boolean;
 	cardType?: "shared" | "default";
 	userId?: string;
diff --git a/apps/web/lib/folder.ts b/apps/web/lib/folder.ts
index 5cb98ae16c..47c53f451f 100644
--- a/apps/web/lib/folder.ts
+++ b/apps/web/lib/folder.ts
@@ -12,6 +12,7 @@ import {
 	users,
 	videos,
 } from "@cap/database/schema";
+import type { Video } from "@cap/web-domain";
 import { Folder } from "@cap/web-domain";
 import { and, desc, eq } from "drizzle-orm";
 import { sql } from "drizzle-orm/sql";
@@ -112,7 +113,10 @@ async function getSharedSpacesForVideos(videoIds: string[]) {
 
 	// Add space-level sharing
 	spaceSharing.forEach((space) => {
-		(sharedSpacesMap[space.videoId] ??= []).push({
+		if (!sharedSpacesMap[space.videoId]) {
+			sharedSpacesMap[space.videoId] = [];
+		}
+		sharedSpacesMap[space.videoId].push({
 			id: space.id,
 			name: space.name,
 			organizationId: space.organizationId,
@@ -123,7 +127,10 @@ async function getSharedSpacesForVideos(videoIds: string[]) {
 
 	// Add organization-level sharing
 	orgSharing.forEach((org) => {
-		(sharedSpacesMap[org.videoId] ??= []).push({
+		if (!sharedSpacesMap[org.videoId]) {
+			sharedSpacesMap[org.videoId] = [];
+		}
+		sharedSpacesMap[org.videoId].push({
 			id: org.id,
 			name: org.name,
 			organizationId: org.organizationId,
@@ -144,6 +151,7 @@ export async function getVideosByFolderId(folderId: string) {
 			ownerId: videos.ownerId,
 			name: videos.name,
 			createdAt: videos.createdAt,
+			public: videos.public,
 			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)`,
@@ -180,6 +188,7 @@ export async function getVideosByFolderId(folderId: string) {
 			videos.ownerId,
 			videos.name,
 			videos.createdAt,
+			videos.public,
 			videos.metadata,
 			users.name,
 		)
@@ -196,10 +205,14 @@ export async function getVideosByFolderId(folderId: string) {
 
 	// Process the video data to match the expected format
 	const processedVideoData = videoData.map((video) => {
-		const { effectiveDate, ...videoWithoutEffectiveDate } = video;
-
 		return {
-			...videoWithoutEffectiveDate,
+			id: video.id as Video.VideoId, // Cast to Video.VideoId branded type
+			ownerId: video.ownerId,
+			name: video.name,
+			createdAt: video.createdAt,
+			public: video.public,
+			totalComments: video.totalComments,
+			totalReactions: video.totalReactions,
 			sharedOrganizations: Array.isArray(video.sharedOrganizations)
 				? video.sharedOrganizations.filter(
 						(organization) => organization.id !== null,
@@ -212,10 +225,11 @@ export async function getVideosByFolderId(folderId: string) {
 			metadata: video.metadata as
 				| {
 						customCreatedAt?: string;
-						[key: string]: any;
+						[key: string]: unknown;
 				  }
 				| undefined,
 			hasPassword: video.hasPassword === 1,
+			foldersData: [], // Empty array since videos in a folder don't need folder data
 		};
 	});
 

From fa3f56715cdf49131d1893aa771f9f663ad3faae Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 20:19:16 +0300
Subject: [PATCH 5/7] fix cap cards for folder

---
 .../caps/components/CapCard/CapCard.tsx       |  3 ++
 .../[id]/components/FolderVideosSection.tsx   | 49 ++++++-------------
 apps/web/components/VideoThumbnail.tsx        |  2 +-
 3 files changed, 20 insertions(+), 34 deletions(-)

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 35137a9e07..8137c6fb93 100644
--- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
+++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
@@ -61,6 +61,7 @@ export interface CapCardProps extends PropsWithChildren {
 		hasPassword?: boolean;
 	};
 	analytics: number;
+	isLoadingAnalytics?: boolean;
 	onDelete?: () => Promise<any>;
 	userId?: string;
 	sharedCapCard?: boolean;
@@ -81,6 +82,7 @@ export const CapCard = ({
 	children,
 	onDelete,
 	userId,
+	isLoadingAnalytics,
 	sharedCapCard = false,
 	hideSharedStatus = false,
 	customDomain,
@@ -461,6 +463,7 @@ export const CapCard = ({
 					<CapCardAnalytics
 						capId={cap.id}
 						displayCount={analytics}
+						isLoadingAnalytics={isLoadingAnalytics}
 						totalComments={cap.totalComments}
 						totalReactions={cap.totalReactions}
 					/>
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 d8948bf1a0..171d24639a 100644
--- a/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
+++ b/apps/web/app/(org)/dashboard/folder/[id]/components/FolderVideosSection.tsx
@@ -10,28 +10,25 @@ import { useDashboardContext } from "@/app/(org)/dashboard/Contexts";
 import { useEffectMutation } from "@/lib/EffectRuntime";
 import { Rpc } from "@/lib/Rpcs";
 import type { VideoData } from "../../../caps/Caps";
+import { CapCard } from "../../../caps/components/CapCard/CapCard";
 import { SelectedCapsBar } from "../../../caps/components/SelectedCapsBar";
 import { UploadPlaceholderCard } from "../../../caps/components/UploadPlaceholderCard";
 import { useUploadingContext } from "../../../caps/UploadingContext";
-import { SharedCapCard } from "../../../spaces/[spaceId]/components/SharedCapCard";
-import { ClientCapCard } from "./index";
 
 interface FolderVideosSectionProps {
 	initialVideos: VideoData;
 	dubApiKeyEnabled: boolean;
 	cardType?: "shared" | "default";
-	userId?: string;
 }
 
 export default function FolderVideosSection({
 	initialVideos,
 	dubApiKeyEnabled,
 	cardType = "default",
-	userId,
 }: FolderVideosSectionProps) {
 	const router = useRouter();
 	const { isUploading } = useUploadingContext();
-	const { activeOrganization } = useDashboardContext();
+	const { activeOrganization, user } = useDashboardContext();
 
 	const [selectedCaps, setSelectedCaps] = useState<Video.VideoId[]>([]);
 	const previousCountRef = useRef<number>(0);
@@ -167,34 +164,20 @@ export default function FolderVideosSection({
 						{isUploading && (
 							<UploadPlaceholderCard key={"upload-placeholder"} />
 						)}
-
-						{cardType === "shared"
-							? initialVideos.map((video) => (
-									<SharedCapCard
-										key={video.id}
-										cap={video}
-										hideSharedStatus
-										analytics={analytics[video.id] || 0}
-										organizationName={
-											activeOrganization?.organization.name || ""
-										}
-										userId={userId}
-									/>
-								))
-							: initialVideos.map((video) => (
-									<ClientCapCard
-										key={video.id}
-										videoId={video.id}
-										cap={video}
-										analytics={analytics[video.id] || 0}
-										isLoadingAnalytics={isLoadingAnalytics}
-										isSelected={selectedCaps.includes(video.id)}
-										anyCapSelected={selectedCaps.length > 0}
-										isDeleting={deleteCaps.isPending}
-										onSelectToggle={() => handleCapSelection(video.id)}
-										onDelete={() => deleteCaps.mutateAsync(selectedCaps)}
-									/>
-								))}
+						{initialVideos.map((video) => (
+							<CapCard
+								key={video.id}
+								cap={video}
+								analytics={analytics[video.id] || 0}
+								userId={user?.id}
+								isLoadingAnalytics={isLoadingAnalytics}
+								isSelected={selectedCaps.includes(video.id)}
+								anyCapSelected={selectedCaps.length > 0}
+								isDeleting={deleteCaps.isPending}
+								onSelectToggle={() => handleCapSelection(video.id)}
+								onDelete={() => deleteCaps.mutateAsync(selectedCaps)}
+							/>
+						))}
 					</>
 				)}
 			</div>
diff --git a/apps/web/components/VideoThumbnail.tsx b/apps/web/components/VideoThumbnail.tsx
index 4394a36d27..bbe47b6b12 100644
--- a/apps/web/components/VideoThumbnail.tsx
+++ b/apps/web/components/VideoThumbnail.tsx
@@ -13,7 +13,7 @@ interface VideoThumbnailProps {
 	imageClass?: string;
 	objectFit?: string;
 	containerClass?: string;
-	videoDuration?: string;
+	videoDuration?: string | number;
 }
 
 const formatDuration = (duration: string) => {

From f7df7ce9cfd5dec8d0a2bf69f1c277b9b1e1654e Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 20:25:13 +0300
Subject: [PATCH 6/7] adjustments

---
 apps/web/app/(org)/dashboard/caps/Caps.tsx                     | 3 ++-
 .../app/(org)/dashboard/caps/components/CapCard/CapCard.tsx    | 2 +-
 .../dashboard/caps/components/CapCard/CapCardAnalytics.tsx     | 1 +
 apps/web/components/VideoThumbnail.tsx                         | 2 +-
 4 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/apps/web/app/(org)/dashboard/caps/Caps.tsx b/apps/web/app/(org)/dashboard/caps/Caps.tsx
index 54dbb3fec2..0d835246b1 100644
--- a/apps/web/app/(org)/dashboard/caps/Caps.tsx
+++ b/apps/web/app/(org)/dashboard/caps/Caps.tsx
@@ -83,7 +83,7 @@ export const Caps = ({
 
 	const videoIds = data.map((video) => video.id).sort();
 
-	const { data: analyticsData } = useQuery({
+	const { data: analyticsData, isLoading: isLoadingAnalytics } = useQuery({
 		queryKey: ["analytics", videoIds],
 		queryFn: async () => {
 			if (!dubApiKeyEnabled || data.length === 0) {
@@ -333,6 +333,7 @@ export const Caps = ({
 								}}
 								userId={user?.id}
 								customDomain={customDomain}
+								isLoadingAnalytics={isLoadingAnalytics}
 								domainVerified={domainVerified}
 								isSelected={selectedCaps.includes(cap.id)}
 								anyCapSelected={anyCapSelected}
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 8137c6fb93..c17fd4a852 100644
--- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
+++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCard.tsx
@@ -61,7 +61,7 @@ export interface CapCardProps extends PropsWithChildren {
 		hasPassword?: boolean;
 	};
 	analytics: number;
-	isLoadingAnalytics?: boolean;
+	isLoadingAnalytics: boolean;
 	onDelete?: () => Promise<any>;
 	userId?: string;
 	sharedCapCard?: boolean;
diff --git a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardAnalytics.tsx b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardAnalytics.tsx
index 5db260a914..8c47c7b6f8 100644
--- a/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardAnalytics.tsx
+++ b/apps/web/app/(org)/dashboard/caps/components/CapCard/CapCardAnalytics.tsx
@@ -15,6 +15,7 @@ interface CapCardAnalyticsProps {
 	capId: string;
 	displayCount: number;
 	totalComments: number;
+	isLoadingAnalytics: boolean;
 	totalReactions: number;
 }
 
diff --git a/apps/web/components/VideoThumbnail.tsx b/apps/web/components/VideoThumbnail.tsx
index bbe47b6b12..15aaf3c7f3 100644
--- a/apps/web/components/VideoThumbnail.tsx
+++ b/apps/web/components/VideoThumbnail.tsx
@@ -110,7 +110,7 @@ export const VideoThumbnail: React.FC<VideoThumbnailProps> = memo(
 				</div>
 				{videoDuration && Number(videoDuration) > 0 && (
 					<p className="text-white leading-0 px-2 left-3 rounded-full backdrop-blur-sm absolute z-10 bottom-3 bg-black/50 text-[11px]">
-						{formatDuration(videoDuration)}
+						{formatDuration(videoDuration.toString())}
 					</p>
 				)}
 				{imageUrl.data && (

From ec79648de8a0e05237b38353858101e25a0e869b Mon Sep 17 00:00:00 2001
From: ameer2468 <33054370+ameer2468@users.noreply.github.com>
Date: Thu, 21 Aug 2025 20:41:26 +0300
Subject: [PATCH 7/7] loading

---
 apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx   | 3 ++-
 .../dashboard/spaces/[spaceId]/components/SharedCapCard.tsx    | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx
index b5afbb2686..abf99a3e4c 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/SharedCaps.tsx
@@ -91,7 +91,7 @@ export const SharedCaps = ({
 
 	const organizationMemberCount = organizationMembers?.length || 0;
 
-	const { data: analyticsData } = useQuery({
+	const { data: analyticsData, isLoading: isLoadingAnalytics } = useQuery({
 		queryKey: ["analytics", data.map((video) => video.id)],
 		queryFn: async () => {
 			if (!dubApiKeyEnabled || data.length === 0) {
@@ -289,6 +289,7 @@ export const SharedCaps = ({
 							key={cap.id}
 							cap={cap}
 							hideSharedStatus
+							isLoadingAnalytics={isLoadingAnalytics}
 							analytics={analytics[cap.id] || 0}
 							organizationName={activeOrganization?.organization.name || ""}
 							spaceName={spaceData?.name || ""}
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/SharedCapCard.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/SharedCapCard.tsx
index 653dfb7b0b..d308bc5592 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/SharedCapCard.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/components/SharedCapCard.tsx
@@ -16,6 +16,7 @@ interface SharedCapCardProps {
 		metadata?: VideoMetadata;
 	};
 	analytics: number;
+	isLoadingAnalytics: boolean;
 	organizationName: string;
 	userId?: string;
 	hideSharedStatus?: boolean;
@@ -44,6 +45,7 @@ export const SharedCapCard: React.FC<SharedCapCardProps> = ({
 		<div onDragStart={onDragStart} onDragEnd={onDragEnd}>
 			<CapCard
 				hideSharedStatus={hideSharedStatus}
+				isLoadingAnalytics={isLoadingAnalytics}
 				cap={cap}
 				analytics={displayCount}
 				sharedCapCard
