Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): redesign share page access, password, not found and preparing states

Richie McIlroy6b97d515de0f
diff --git a/apps/web/__tests__/unit/share-page-loading.test.ts b/apps/web/__tests__/unit/share-page-loading.test.ts
index 30dd23513..85824cd42 100644
--- a/apps/web/__tests__/unit/share-page-loading.test.ts
+++ b/apps/web/__tests__/unit/share-page-loading.test.ts
@@ -94,8 +94,8 @@ vi.mock("@/lib/video-download-permissions", () => ({
canUserDownloadVideo: async () => false,
}));
vi.mock("@/utils/flags", () => ({ isAiGenerationEnabled: async () => false }));
-vi.mock("@/app/s/[videoId]/_components/PasswordOverlay", () => ({
- PasswordOverlay: () => null,
+vi.mock("@/app/s/[videoId]/_components/PasswordGate", () => ({
+ PasswordGate: () => null,
}));
vi.mock("@/app/s/[videoId]/_components/PendingRecordingShare", () => ({
PendingRecordingShare: () => null,
@@ -105,6 +105,7 @@ vi.mock("@/app/s/[videoId]/_components/ShareHeader", () => ({
}));
vi.mock("@/app/s/[videoId]/Share", () => ({ Share: () => null }));
+import { PasswordGate } from "@/app/s/[videoId]/_components/PasswordGate";
import ShareVideoPage from "@/app/s/[videoId]/page";
const createVideo = () => ({
@@ -154,8 +155,8 @@ const renderPage = () =>
});
async function renderAuthorizedContent() {
- const page = (await renderPage()) as ReactElement<{ children: unknown[] }>;
- const content = page.props.children[1];
+ const page = (await renderPage()) as ReactElement<{ children: unknown }>;
+ const content = page.props.children;
if (!isValidElement(content) || typeof content.type !== "function") {
throw new Error("Expected authorized share content");
}
@@ -260,7 +261,7 @@ describe("share page loading", () => {
);
it.each([null, "owner/video/source/original.mp4"])(
- "keeps password-protected videos behind the password overlay with upload %s",
+ "keeps password-protected videos behind the password gate with upload %s",
async (activeUploadRawFileKey) => {
arrangeRows([
{
@@ -272,11 +273,9 @@ describe("share page loading", () => {
mocks.policy.mockReturnValue(
Effect.fail({ _tag: "VerifyVideoPasswordError" }),
);
- const page = (await renderPage()) as ReactElement<{
- children: [ReactElement<{ isOpen: boolean }>, boolean];
- }>;
- expect(page.props.children[0].props.isOpen).toBe(true);
- expect(page.props.children[1]).toBe(false);
+ const page = (await renderPage()) as ReactElement<{ videoId: string }>;
+ expect(page.type).toBe(PasswordGate);
+ expect(page.props.videoId).toBe(createVideo().id);
expect(mocks.playbackUrl).not.toHaveBeenCalled();
},
);
diff --git a/apps/web/__tests__/unit/video-edit-processing.test.ts b/apps/web/__tests__/unit/video-edit-processing.test.ts
index ccbdd6cfd..db5fc1917 100644
--- a/apps/web/__tests__/unit/video-edit-processing.test.ts
+++ b/apps/web/__tests__/unit/video-edit-processing.test.ts
@@ -108,8 +108,8 @@ vi.mock("@/lib/public-share-video", () => ({}));
vi.mock("@/lib/shareable-link-quota", () => ({}));
vi.mock("@/lib/transcribe", () => ({}));
vi.mock("@/utils/flags", () => ({}));
-vi.mock("@/app/s/[videoId]/_components/PasswordOverlay", () => ({
- PasswordOverlay: () => null,
+vi.mock("@/app/s/[videoId]/_components/PasswordGate", () => ({
+ PasswordGate: () => null,
}));
vi.mock("@/app/s/[videoId]/_components/PendingRecordingShare", () => ({}));
vi.mock("@/app/s/[videoId]/_components/ShareHeader", () => ({}));
diff --git a/apps/web/app/s/[videoId]/_components/AccessStates.tsx b/apps/web/app/s/[videoId]/_components/AccessStates.tsx
new file mode 100644
index 000000000..05eb9df3e
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/AccessStates.tsx
@@ -0,0 +1,129 @@
+"use client";
+
+import { buildEnv } from "@cap/env";
+import type { Video } from "@cap/web-domain";
+import { signOut } from "next-auth/react";
+import { useCurrentUser } from "@/app/Layout/AuthContext";
+import {
+ EnvelopeDoodle,
+ KeyDoodle,
+ LockDoodle,
+} from "@/components/paper-state/doodles";
+import {
+ PaperActions,
+ PaperState,
+ paperStateStyles as styles,
+} from "@/components/paper-state/PaperState";
+
+const accessUrls = (videoId: Video.VideoId) => {
+ const next = encodeURIComponent(`/s/${videoId}`);
+ return {
+ login: `/login?next=${next}`,
+ signup: `/signup?next=${next}`,
+ };
+};
+
+function SwitchAccountButton({ loginUrl }: { loginUrl: string }) {
+ return (
+ <button
+ type="button"
+ className={styles.cta}
+ onClick={() => signOut({ callbackUrl: loginUrl })}
+ >
+ Switch account
+ </button>
+ );
+}
+
+export function PrivateVideoState({ videoId }: { videoId: Video.VideoId }) {
+ const user = useCurrentUser();
+ const urls = accessUrls(videoId);
+
+ if (user) {
+ return (
+ <PaperState
+ homeHref={buildEnv.NEXT_PUBLIC_WEB_URL}
+ doodle={<LockDoodle />}
+ title="This video is private"
+ lede={
+ <>
+ You're signed in as <strong>{user.email}</strong>, which doesn't
+ have access. Switch to the account that was invited, or ask the
+ owner to share it with you.
+ </>
+ }
+ >
+ <PaperActions>
+ <SwitchAccountButton loginUrl={urls.login} />
+ <a
+ className={`${styles.cta} ${styles.ghost}`}
+ href={`${buildEnv.NEXT_PUBLIC_WEB_URL}/dashboard`}
+ >
+ Go to my Caps
+ </a>
+ </PaperActions>
+ </PaperState>
+ );
+ }
+
+ return (
+ <PaperState
+ homeHref={buildEnv.NEXT_PUBLIC_WEB_URL}
+ doodle={<LockDoodle />}
+ title="This video is private"
+ lede="Only people the owner invited can watch it. If that's you, sign in with the email address that was invited."
+ >
+ <PaperActions>
+ <a className={styles.cta} href={urls.login}>
+ Sign in
+ </a>
+ <a className={`${styles.cta} ${styles.ghost}`} href={urls.signup}>
+ Create an account
+ </a>
+ </PaperActions>
+ </PaperState>
+ );
+}
+
+export function SignInRequiredState({ videoId }: { videoId: Video.VideoId }) {
+ return (
+ <PaperState
+ homeHref={buildEnv.NEXT_PUBLIC_WEB_URL}
+ doodle={<KeyDoodle />}
+ title="Sign in to watch"
+ lede="The owner only shares this video with approved email addresses. Sign in with yours to start watching."
+ >
+ <PaperActions>
+ <a className={styles.cta} href={accessUrls(videoId).login}>
+ Sign in
+ </a>
+ </PaperActions>
+ </PaperState>
+ );
+}
+
+export function AccessRestrictedState({ videoId }: { videoId: Video.VideoId }) {
+ const user = useCurrentUser();
+
+ return (
+ <PaperState
+ homeHref={buildEnv.NEXT_PUBLIC_WEB_URL}
+ doodle={<EnvelopeDoodle />}
+ title="This video is restricted"
+ lede={
+ user ? (
+ <>
+ <strong>{user.email}</strong> isn't on the list of addresses the
+ owner allows. Try another account, or ask the owner for access.
+ </>
+ ) : (
+ "Your email address isn't on the list of addresses the owner allows. Try another account, or ask the owner for access."
+ )
+ }
+ >
+ <PaperActions>
+ <SwitchAccountButton loginUrl={accessUrls(videoId).login} />
+ </PaperActions>
+ </PaperState>
+ );
+}
diff --git a/apps/web/app/s/[videoId]/_components/PasswordGate.tsx b/apps/web/app/s/[videoId]/_components/PasswordGate.tsx
new file mode 100644
index 000000000..c6b6406f2
--- /dev/null
+++ b/apps/web/app/s/[videoId]/_components/PasswordGate.tsx
@@ -0,0 +1,75 @@
+"use client";
+
+import { buildEnv } from "@cap/env";
+import type { Video } from "@cap/web-domain";
+import { useMutation } from "@tanstack/react-query";
+import { useRouter } from "next/navigation";
+import { useState } from "react";
+import { verifyVideoPassword } from "@/actions/videos/password";
+import { PasswordDoodle } from "@/components/paper-state/doodles";
+import {
+ PaperState,
+ paperStateStyles as styles,
+} from "@/components/paper-state/PaperState";
+
+export function PasswordGate({ videoId }: { videoId: Video.VideoId }) {
+ const [password, setPassword] = useState("");
+ const router = useRouter();
+
+ const verifyPassword = useMutation({
+ mutationFn: () =>
+ verifyVideoPassword(videoId, password).then((v) => {
+ if (v.success) return v.value;
+ throw new Error(v.error);
+ }),
+ onSuccess: () => router.refresh(),
+ });
+
+ const failed = verifyPassword.isError;
+ const busy = verifyPassword.isPending || verifyPassword.isSuccess;
+
+ return (
+ <PaperState
+ homeHref={buildEnv.NEXT_PUBLIC_WEB_URL}
+ doodle={<PasswordDoodle />}
+ title="This video is password protected"
+ lede="Enter the password the owner gave you to start watching."
+ >
+ <form
+ className={styles.form}
+ onSubmit={(event) => {
+ event.preventDefault();
+ if (!busy && password.trim()) verifyPassword.mutate();
+ }}
+ >
+ <input
+ className={styles.input}
+ type="password"
+ aria-label="Password"
+ placeholder="Password"
+ autoComplete="current-password"
+ value={password}
+ aria-invalid={failed}
+ onChange={(event) => {
+ setPassword(event.target.value);
+ if (failed) verifyPassword.reset();
+ }}
+ // biome-ignore lint/a11y/noAutofocus: the password is the only thing to do on this page
+ autoFocus
+ />
+ <button
+ type="submit"
+ className={styles.cta}
+ disabled={busy || !password.trim()}
+ >
+ {busy ? "Checking…" : "Watch video"}
+ </button>
+ {failed && (
+ <p className={styles.error} role="alert">
+ That password didn't work. Check it and try again.
+ </p>
+ )}
+ </form>
+ </PaperState>
+ );
+}
diff --git a/apps/web/app/s/[videoId]/_components/PasswordOverlay.tsx b/apps/web/app/s/[videoId]/_components/PasswordOverlay.tsx
deleted file mode 100644
index 91606fbae..000000000
--- a/apps/web/app/s/[videoId]/_components/PasswordOverlay.tsx
+++ /dev/null
@@ -1,89 +0,0 @@
-"use client";
-
-import { Button, Dialog, DialogContent, Input, Logo } from "@cap/ui";
-import type { Video } from "@cap/web-domain";
-import { useMutation } from "@tanstack/react-query";
-import { useRouter } from "next/navigation";
-import { useState } from "react";
-import { toast } from "sonner";
-import { verifyVideoPassword } from "@/actions/videos/password";
-
-interface PasswordOverlayProps {
- isOpen: boolean;
- videoId: Video.VideoId;
-}
-
-export const PasswordOverlay: React.FC<PasswordOverlayProps> = ({
- isOpen,
- videoId,
-}) => {
- const [password, setPassword] = useState("");
- const router = useRouter();
-
- const verifyPassword = useMutation({
- mutationFn: () =>
- verifyVideoPassword(videoId, password).then((v) => {
- if (v.success) return v.value;
- throw new Error(v.error);
- }),
- onSuccess: (result) => {
- toast.success(result);
- router.refresh();
- },
- onError: (e) => {
- toast.error(e.message);
- },
- });
-
- return (
- <Dialog open={isOpen}>
- <DialogContent className="w-[95vw] max-w-sm p-4 sm:p-6 md:p-8 sm:max-w-md">
- <div className="flex flex-col items-center space-y-4 sm:space-y-6">
- <div className="flex flex-col items-center space-y-3 sm:space-y-4">
- <Logo className="w-16 sm:w-20 md:w-24 h-auto" />
- <div className="text-center space-y-2">
- <h2 className="text-lg sm:text-xl font-semibold text-gray-12">
- Protected Video
- </h2>
- <p className="text-xs sm:text-sm text-gray-10 max-w-xs sm:max-w-sm px-2 sm:px-0">
- This video is password protected. Please enter the password to
- continue watching.
- </p>
- </div>
- </div>
-
- <div className="w-full space-y-3 sm:space-y-4">
- <div className="space-y-2">
- <label
- htmlFor="password"
- className="text-sm font-medium text-gray-12"
- >
- Password
- </label>
- <Input
- id="password"
- type="password"
- value={password}
- onChange={(e) => setPassword(e.target.value)}
- placeholder="Enter password"
- className="w-full"
- autoFocus
- />
- </div>
- <Button
- type="button"
- variant="dark"
- size="lg"
- className="w-full"
- spinner={verifyPassword.isPending}
- disabled={verifyPassword.isPending || !password.trim()}
- onClick={() => verifyPassword.mutate()}
- >
- {verifyPassword.isPending ? "Verifying..." : "Access Video"}
- </Button>
- </div>
- </div>
- </DialogContent>
- </Dialog>
- );
-};
diff --git a/apps/web/app/s/[videoId]/_components/PendingRecordingShare.tsx b/apps/web/app/s/[videoId]/_components/PendingRecordingShare.tsx
index b8b61ec9f..34ce37f2d 100644
--- a/apps/web/app/s/[videoId]/_components/PendingRecordingShare.tsx
+++ b/apps/web/app/s/[videoId]/_components/PendingRecordingShare.tsx
@@ -1,8 +1,14 @@
"use client";
-import { LogoSpinner } from "@cap/ui";
+import { buildEnv } from "@cap/env";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
+import { RecordingDoodle } from "@/components/paper-state/doodles";
+import {
+ PaperActions,
+ PaperState,
+ paperStateStyles as styles,
+} from "@/components/paper-state/PaperState";
const MAX_REFRESH_ATTEMPTS = 30;
const REFRESH_INTERVAL_MS = 2000;
@@ -31,16 +37,31 @@ export function PendingRecordingShare() {
}, [router]);
return (
- <div className="flex flex-col justify-center items-center p-4 min-h-screen text-center bg-gray-2">
- <LogoSpinner className="mb-6 w-10 h-auto animate-spin" />
- <h1 className="mb-2 text-2xl font-semibold text-gray-12">
- Preparing your recording
- </h1>
- <p className="max-w-md text-gray-10">
- {hasTimedOut
- ? "This recording is taking longer than expected. Reload this page in a moment."
- : "Your recording is being made available. This page will update automatically."}
- </p>
- </div>
+ <PaperState
+ homeHref={buildEnv.NEXT_PUBLIC_WEB_URL}
+ doodle={<RecordingDoodle />}
+ title={
+ hasTimedOut
+ ? "Still preparing your recording"
+ : "Preparing your recording"
+ }
+ lede={
+ hasTimedOut
+ ? "This is taking longer than usual. Reload the page in a moment and it should be ready."
+ : "Your recording is almost ready to watch. This page will update on its own."
+ }
+ >
+ {hasTimedOut && (
+ <PaperActions>
+ <button
+ type="button"
+ className={styles.cta}
+ onClick={() => window.location.reload()}
+ >
+ Reload page
+ </button>
+ </PaperActions>
+ )}
+ </PaperState>
);
}
diff --git a/apps/web/app/s/[videoId]/_components/PrivateAccessActions.tsx b/apps/web/app/s/[videoId]/_components/PrivateAccessActions.tsx
deleted file mode 100644
index 220997b7f..000000000
--- a/apps/web/app/s/[videoId]/_components/PrivateAccessActions.tsx
+++ /dev/null
@@ -1,38 +0,0 @@
-"use client";
-
-import { Button } from "@cap/ui";
-import type { Video } from "@cap/web-domain";
-import Link from "next/link";
-import { signOut } from "next-auth/react";
-import { useCurrentUser } from "@/app/Layout/AuthContext";
-
-export function PrivateAccessActions({ videoId }: { videoId: Video.VideoId }) {
- const user = useCurrentUser();
- const next = encodeURIComponent(`/s/${videoId}`);
- const loginUrl = `/login?next=${next}`;
-
- if (user) {
- return (
- <div className="space-y-3">
- <p>
- You're signed in as {user.email}. Use the account that was invited to
- view this recording.
- </p>
- <Button
- variant="dark"
- onClick={() => signOut({ callbackUrl: loginUrl })}
- >
- Switch account
- </Button>
- </div>
- );
- }
-
- return (
- <p>
- If you have access, <Link href={loginUrl}>sign in</Link> or{" "}
- <Link href={`/signup?next=${next}`}>create an account</Link> using the
- email address that was invited.
- </p>
- );
-}
diff --git a/apps/web/app/s/[videoId]/not-found.tsx b/apps/web/app/s/[videoId]/not-found.tsx
new file mode 100644
index 000000000..bb54263bd
--- /dev/null
+++ b/apps/web/app/s/[videoId]/not-found.tsx
@@ -0,0 +1,32 @@
+import { buildEnv } from "@cap/env";
+import { SearchDoodle } from "@/components/paper-state/doodles";
+import {
+ PaperActions,
+ PaperState,
+ paperStateStyles as styles,
+} from "@/components/paper-state/PaperState";
+
+export default function VideoNotFound() {
+ const homeHref = buildEnv.NEXT_PUBLIC_WEB_URL;
+
+ return (
+ <PaperState
+ homeHref={homeHref}
+ doodle={<SearchDoodle />}
+ title="We couldn't find this video"
+ lede="It may have been deleted, or the link is missing a few characters. Check the link with whoever sent it to you."
+ footnote={
+ <>
+ Think this is a mistake? Email{" "}
+ <a href="mailto:[email protected]">[email protected]</a>
+ </>
+ }
+ >
+ <PaperActions>
+ <a className={styles.cta} href={homeHref}>
+ Go to Cap
+ </a>
+ </PaperActions>
+ </PaperState>
+ );
+}
diff --git a/apps/web/app/s/[videoId]/page.tsx b/apps/web/app/s/[videoId]/page.tsx
index e8dbf48de..88c4eddbb 100644
--- a/apps/web/app/s/[videoId]/page.tsx
+++ b/apps/web/app/s/[videoId]/page.tsx
@@ -16,7 +16,6 @@ import {
} from "@cap/database/schema";
import type { VideoMetadata } from "@cap/database/types";
import { buildEnv, serverEnv } from "@cap/env";
-import { Logo } from "@cap/ui";
import { userIsPro } from "@cap/utils";
import {
Database,
@@ -37,7 +36,6 @@ import { and, eq, type InferSelectModel, isNull, sql } from "drizzle-orm";
import { Effect, Option } from "effect";
import type { Metadata } from "next";
import { headers } from "next/headers";
-import Link from "next/link";
import { notFound } from "next/navigation";
import { after } from "next/server";
import { getVideoAnalytics } from "@/actions/videos/get-analytics";
@@ -82,9 +80,13 @@ import {
} from "@/lib/video-edits";
import { optionFromTOrFirst } from "@/utils/effect";
import { isAiGenerationEnabled } from "@/utils/flags";
-import { PasswordOverlay } from "./_components/PasswordOverlay";
+import {
+ AccessRestrictedState,
+ PrivateVideoState,
+ SignInRequiredState,
+} from "./_components/AccessStates";
+import { PasswordGate } from "./_components/PasswordGate";
import { PendingRecordingShare } from "./_components/PendingRecordingShare";
-import { PrivateAccessActions } from "./_components/PrivateAccessActions";
import { ShareHeader } from "./_components/ShareHeader";
import { Share } from "./Share";
@@ -200,33 +202,13 @@ function PolicyDeniedView({
reason?: string;
videoId: Video.VideoId;
}) {
- let title = "This video is private";
- let description: React.ReactNode = <PrivateAccessActions videoId={videoId} />;
-
if (reason === "email_restriction_login_required") {
- title = "This video requires sign-in";
- description = (
- <>
- The owner of this video has restricted access. Please{" "}
- <Link href={`/login?next=${encodeURIComponent(`/s/${videoId}`)}`}>
- sign in
- </Link>{" "}
- with an authorized email address to view.
- </>
- );
- } else if (reason === "email_restriction_denied") {
- title = "Access restricted";
- description =
- "Your email address does not meet the requirements set by the video owner.";
+ return <SignInRequiredState videoId={videoId} />;
}
-
- return (
- <div className="flex flex-col justify-center items-center p-4 min-h-screen text-center">
- <Logo className="size-32" />
- <h1 className="mb-2 text-2xl font-semibold">{title}</h1>
- <div className="text-gray-400">{description}</div>
- </div>
- );
+ if (reason === "email_restriction_denied") {
+ return <AccessRestrictedState videoId={videoId} />;
+ }
+ return <PrivateVideoState videoId={videoId} />;
}
const renderPolicyDenied = (videoId: Video.VideoId, reason?: string) =>
@@ -418,24 +400,25 @@ export default async function ShareVideoPage(props: PageProps<"/s/[videoId]">) {
Effect.catchTag("VerifyVideoPasswordError", () =>
Effect.succeed({ needsPassword: true } as const),
),
- Effect.map((data) => (
- // overflow-x-clip: the timeline view's theater strip breaks out to
- // 100vw, which overshoots by the scrollbar width on OSes with classic
- // scrollbars; clipping here keeps the page from gaining a horizontal
- // scroll without creating a new scroll container.
- // Desktop pins the page to the viewport so the comments rail can run
- // full height and the video column scrolls on its own; phones keep
- // ordinary document flow.
- <div
- key={videoId}
- className="flex overflow-x-clip flex-col min-h-screen bg-gray-2 lg:h-screen lg:min-h-0 lg:overflow-hidden"
- >
- <PasswordOverlay isOpen={data.needsPassword} videoId={videoId} />
- {!data.needsPassword && (
+ Effect.map((data) =>
+ data.needsPassword ? (
+ <PasswordGate key={videoId} videoId={videoId} />
+ ) : (
+ // overflow-x-clip: the timeline view's theater strip breaks out to
+ // 100vw, which overshoots by the scrollbar width on OSes with classic
+ // scrollbars; clipping here keeps the page from gaining a horizontal
+ // scroll without creating a new scroll container.
+ // Desktop pins the page to the viewport so the comments rail can run
+ // full height and the video column scrolls on its own; phones keep
+ // ordinary document flow.
+ <div
+ key={videoId}
+ className="flex overflow-x-clip flex-col min-h-screen bg-gray-2 lg:h-screen lg:min-h-0 lg:overflow-hidden"
+ >
<AuthorizedContent video={data.video} searchParams={searchParams} />
- )}
- </div>
- )),
+ </div>
+ ),
+ ),
Effect.catchTags(getShareVideoPageCatchers(videoId, awaitRecording)),
provideOptionalAuth,
EffectRuntime.runPromise,