Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(web): match dashboard pagination counts to folder scope

Aditya Garud9ea54726e3cc
diff --git a/apps/web/__tests__/unit/dashboard-video-count.test.ts b/apps/web/__tests__/unit/dashboard-video-count.test.ts
new file mode 100644
index 000000000..991854bc8
--- /dev/null
+++ b/apps/web/__tests__/unit/dashboard-video-count.test.ts
@@ -0,0 +1,112 @@
+import type { Database, ImageUploads, Spaces } from "@cap/web-backend";
+import { Effect, Layer } from "effect";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import CapsPage from "@/app/(org)/dashboard/caps/page";
+import SharedCapsPage from "@/app/(org)/dashboard/spaces/[spaceId]/page";
+
+const mocks = vi.hoisted(() => ({ query: vi.fn() }));
+
+vi.mock("@cap/database", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const client = drizzle(mocks.query);
+ return { db: () => client };
+});
+vi.mock("@cap/database/auth/session", () => ({
+ getCurrentUser: async () => ({ id: "user-1", activeOrganizationId: "org-1" }),
+}));
+vi.mock("@cap/env", () => ({ serverEnv: () => ({}) }));
+vi.mock("@cap/web-backend", async () => {
+ const { drizzle } = await import("drizzle-orm/mysql-proxy");
+ const { Effect, Layer } = await import("effect");
+ const client = drizzle(mocks.query);
+ class Database extends Effect.Service<Database>()("Database", {
+ succeed: {
+ use: <T>(callback: (db: typeof client) => Promise<T>) =>
+ Effect.promise(() => callback(client)),
+ },
+ }) {}
+ class ImageUploads extends Effect.Service<ImageUploads>()("ImageUploads", {
+ succeed: { resolveImageUrl: (value: string) => Effect.succeed(value) },
+ }) {}
+ class Spaces extends Effect.Service<Spaces>()("Spaces", {
+ succeed: {
+ getSpaceOrOrg: () =>
+ Effect.succeed({
+ variant: "organization",
+ organization: { id: "org-1" },
+ }),
+ },
+ }) {}
+ return {
+ Database,
+ ImageUploads,
+ Spaces,
+ makeCurrentUserLayer: () => Layer.empty,
+ };
+});
+vi.mock("@/lib/server", async () => {
+ const { Database, ImageUploads, Spaces } = await import("@cap/web-backend");
+ return {
+ runPromise: <A, E>(
+ effect: Effect.Effect<A, E, Database | ImageUploads | Spaces>,
+ ) =>
+ Effect.runPromise(
+ Effect.provide(
+ effect,
+ Layer.mergeAll(
+ Database.Default,
+ ImageUploads.Default,
+ Spaces.Default,
+ ),
+ ),
+ ),
+ };
+});
+vi.mock("@/app/(org)/dashboard/caps/Caps", () => ({ Caps: () => null }));
+vi.mock("@/app/(org)/dashboard/spaces/[spaceId]/SharedCaps", () => ({
+ SharedCaps: () => null,
+}));
+
+beforeEach(() => {
+ mocks.query.mockResolvedValue({ rows: [] });
+});
+
+describe("dashboard pagination counts", () => {
+ it.each([
+ {
+ name: "personal Caps",
+ folderTable: "videos",
+ load: () =>
+ CapsPage({
+ params: Promise.resolve({}),
+ searchParams: Promise.resolve({}),
+ }),
+ },
+ {
+ name: "organization Caps",
+ folderTable: "shared_videos",
+ load: () =>
+ SharedCapsPage({
+ params: Promise.resolve({ spaceId: "org-1" }),
+ searchParams: Promise.resolve({}),
+ }),
+ },
+ ])(
+ "counts the same root videos it lists for $name",
+ async ({ load, folderTable }) => {
+ await load();
+ const queries = mocks.query.mock.calls.map(([query]) => String(query));
+ const countQuery = queries.find((query) =>
+ query.startsWith("select count(*)"),
+ );
+ const listQuery = queries.find((query) => query.includes(" group by "));
+ const folderFilter = `\`${folderTable}\`.\`folderId\` is null`;
+
+ expect(listQuery).toContain(folderFilter);
+ expect(countQuery).toContain(folderFilter);
+ if (folderTable === "shared_videos") {
+ expect(countQuery).not.toContain("`videos`.`folderId`");
+ }
+ },
+ );
+});
diff --git a/apps/web/app/(org)/dashboard/caps/page.tsx b/apps/web/app/(org)/dashboard/caps/page.tsx
index 6200ac48b..1b636ecf6 100644
--- a/apps/web/app/(org)/dashboard/caps/page.tsx
+++ b/apps/web/app/(org)/dashboard/caps/page.tsx
@@ -146,6 +146,7 @@ export default async function CapsPage(props: PageProps<"/dashboard/caps">) {
and(
eq(videos.ownerId, userId),
eq(organizations.id, user.activeOrganizationId),
+ isNull(videos.folderId),
isNull(organizations.tombstoneAt),
),
);
diff --git a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
index f267ce433..9d8b91a17 100644
--- a/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
+++ b/apps/web/app/(org)/dashboard/spaces/[spaceId]/page.tsx
@@ -506,7 +506,7 @@ export default async function SharedCapsPage(props: {
.where(
and(
eq(sharedVideos.organizationId, orgId),
- isNull(videos.folderId),
+ isNull(sharedVideos.folderId),
),
),
]);