Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): resolve where a share page viewer's dashboard back link goes

Richie McIlroy03715c232545
diff --git a/apps/web/__tests__/unit/share-dashboard-destination.test.ts b/apps/web/__tests__/unit/share-dashboard-destination.test.ts
new file mode 100644
index 000000000..b46853cc3
--- /dev/null
+++ b/apps/web/__tests__/unit/share-dashboard-destination.test.ts
@@ -0,0 +1,125 @@
+import { describe, expect, it, vi } from "vitest";
+
+vi.mock("@cap/database", () => ({ db: vi.fn() }));
+vi.mock("@cap/database/schema", () => ({
+ folders: {},
+ organizationMembers: {},
+ organizations: {},
+ sharedVideos: {},
+ spaceMembers: {},
+ spaces: {},
+ spaceVideos: {},
+ videos: {},
+}));
+
+import {
+ pickShareDashboardDestination,
+ type ShareDashboardAccess,
+} from "@/lib/share-dashboard-destination";
+
+const access = (
+ overrides: Partial<ShareDashboardAccess>,
+): ShareDashboardAccess => ({
+ isOwner: false,
+ activeOrganizationId: "org-active",
+ ownerFolder: null,
+ memberSpaces: [],
+ memberOrganizations: [],
+ ...overrides,
+});
+
+describe("pickShareDashboardDestination", () => {
+ it("sends the owner back to My Caps", () => {
+ expect(pickShareDashboardDestination(access({ isOwner: true }))).toEqual({
+ kind: "caps",
+ href: "/dashboard/caps",
+ label: "My Caps",
+ });
+ });
+
+ it("sends the owner back to the folder the Cap lives in", () => {
+ expect(
+ pickShareDashboardDestination(
+ access({
+ isOwner: true,
+ ownerFolder: { id: "folder-1", name: "Launch" },
+ }),
+ ),
+ ).toEqual({
+ kind: "folder",
+ href: "/dashboard/folder/folder-1",
+ label: "Launch",
+ });
+ });
+
+ it("ignores spaces for the owner", () => {
+ expect(
+ pickShareDashboardDestination(
+ access({
+ isOwner: true,
+ memberSpaces: [
+ { id: "space-1", name: "Design", organizationId: "org-active" },
+ ],
+ }),
+ )?.kind,
+ ).toBe("caps");
+ });
+
+ it("sends a space member to the space the Cap was shared into", () => {
+ expect(
+ pickShareDashboardDestination(
+ access({
+ memberSpaces: [
+ { id: "space-1", name: "Design", organizationId: "org-active" },
+ ],
+ }),
+ ),
+ ).toEqual({
+ kind: "space",
+ href: "/dashboard/spaces/space-1",
+ label: "Design",
+ });
+ });
+
+ it("sends an organization member to the organization's shared view", () => {
+ expect(
+ pickShareDashboardDestination(
+ access({
+ memberOrganizations: [{ id: "org-active", name: "Acme" }],
+ }),
+ ),
+ ).toEqual({
+ kind: "organization",
+ href: "/dashboard/spaces/org-active",
+ label: "Acme",
+ });
+ });
+
+ it("prefers destinations in the viewer's active organization", () => {
+ expect(
+ pickShareDashboardDestination(
+ access({
+ memberSpaces: [
+ { id: "space-other", name: "Other", organizationId: "org-other" },
+ { id: "space-active", name: "Mine", organizationId: "org-active" },
+ ],
+ }),
+ )?.href,
+ ).toBe("/dashboard/spaces/space-active");
+
+ expect(
+ pickShareDashboardDestination(
+ access({
+ memberSpaces: [
+ { id: "space-other", name: "Other", organizationId: "org-other" },
+ ],
+ memberOrganizations: [{ id: "org-active", name: "Acme" }],
+ }),
+ )?.href,
+ ).toBe("/dashboard/spaces/org-active");
+ });
+
+ it("offers nothing to a signed-in viewer the Cap was never shared with", () => {
+ expect(pickShareDashboardDestination(access({}))).toBeNull();
+ });
+});
diff --git a/apps/web/lib/share-dashboard-destination.ts b/apps/web/lib/share-dashboard-destination.ts
new file mode 100644
index 000000000..25ed59c24
--- /dev/null
+++ b/apps/web/lib/share-dashboard-destination.ts
@@ -0,0 +1,160 @@
+import { db } from "@cap/database";
+import {
+ folders,
+ organizationMembers,
+ organizations,
+ sharedVideos,
+ spaceMembers,
+ spaces,
+ spaceVideos,
+ videos,
+} from "@cap/database/schema";
+import type { User, Video } from "@cap/web-domain";
+import { and, eq, isNull } from "drizzle-orm";
+
+export type ShareDashboardDestination = {
+ kind: "caps" | "folder" | "space" | "organization";
+ href: string;
+ label: string;
+};
+
+export type ShareDashboardAccess = {
+ isOwner: boolean;
+ activeOrganizationId: string | null;
+ ownerFolder: { id: string; name: string } | null;
+ memberSpaces: { id: string; name: string; organizationId: string }[];
+ memberOrganizations: { id: string; name: string }[];
+};
+
+const byActiveOrganizationFirst =
+ (activeOrganizationId: string | null) =>
+ <T>(items: T[], organizationOf: (item: T) => string): T[] =>
+ activeOrganizationId === null
+ ? items
+ : [...items].sort(
+ (a, b) =>
+ Number(organizationOf(b) === activeOrganizationId) -
+ Number(organizationOf(a) === activeOrganizationId),
+ );
+
+export function pickShareDashboardDestination(
+ access: ShareDashboardAccess,
+): ShareDashboardDestination | null {
+ if (access.isOwner) {
+ if (access.ownerFolder) {
+ return {
+ kind: "folder",
+ href: `/dashboard/folder/${access.ownerFolder.id}`,
+ label: access.ownerFolder.name,
+ };
+ }
+ return { kind: "caps", href: "/dashboard/caps", label: "My Caps" };
+ }
+
+ const activeFirst = byActiveOrganizationFirst(access.activeOrganizationId);
+ const [space] = activeFirst(
+ access.memberSpaces,
+ (space) => space.organizationId,
+ );
+ const [organization] = activeFirst(
+ access.memberOrganizations,
+ (organization) => organization.id,
+ );
+
+ const spaceInActiveOrg =
+ space && space.organizationId === access.activeOrganizationId;
+ const organizationIsActive =
+ organization && organization.id === access.activeOrganizationId;
+
+ if (space && (spaceInActiveOrg || !organizationIsActive)) {
+ return {
+ kind: "space",
+ href: `/dashboard/spaces/${space.id}`,
+ label: space.name,
+ };
+ }
+
+ if (organization) {
+ return {
+ kind: "organization",
+ href: `/dashboard/spaces/${organization.id}`,
+ label: organization.name,
+ };
+ }
+
+ return null;
+}
+
+export async function getShareDashboardDestination({
+ viewer,
+ videoId,
+ ownerId,
+}: {
+ viewer: { id: User.UserId; activeOrganizationId: string | null } | null;
+ videoId: Video.VideoId;
+ ownerId: User.UserId;
+}): Promise<ShareDashboardDestination | null> {
+ if (!viewer) return null;
+
+ const isOwner = viewer.id === ownerId;
+
+ const [ownerFolderRows, memberSpaces, memberOrganizations] =
+ await Promise.all([
+ isOwner
+ ? db()
+ .select({ id: folders.id, name: folders.name })
+ .from(videos)
+ .innerJoin(folders, eq(folders.id, videos.folderId))
+ .where(and(eq(videos.id, videoId), isNull(folders.spaceId)))
+ .limit(1)
+ : Promise.resolve([]),
+ isOwner
+ ? Promise.resolve([])
+ : db()
+ .select({
+ id: spaces.id,
+ name: spaces.name,
+ organizationId: spaces.organizationId,
+ })
+ .from(spaceVideos)
+ .innerJoin(spaces, eq(spaceVideos.spaceId, spaces.id))
+ .innerJoin(
+ spaceMembers,
+ and(
+ eq(spaceMembers.spaceId, spaces.id),
+ eq(spaceMembers.userId, viewer.id),
+ ),
+ )
+ .where(eq(spaceVideos.videoId, videoId)),
+ isOwner
+ ? Promise.resolve([])
+ : db()
+ .select({ id: organizations.id, name: organizations.name })
+ .from(sharedVideos)
+ .innerJoin(
+ organizations,
+ eq(sharedVideos.organizationId, organizations.id),
+ )
+ .innerJoin(
+ organizationMembers,
+ and(
+ eq(organizationMembers.organizationId, organizations.id),
+ eq(organizationMembers.userId, viewer.id),
+ ),
+ )
+ .where(
+ and(
+ eq(sharedVideos.videoId, videoId),
+ isNull(organizations.tombstoneAt),
+ ),
+ ),
+ ]);
+
+ return pickShareDashboardDestination({
+ isOwner,
+ activeOrganizationId: viewer.activeOrganizationId,
+ ownerFolder: ownerFolderRows[0] ?? null,
+ memberSpaces,
+ memberOrganizations,
+ });
+}