Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: show restore controls only for recoverable legacy edits
Richie McIlroy1e7848359eb4
diff --git a/apps/web/__tests__/unit/video-edit-processing.test.ts b/apps/web/__tests__/unit/video-edit-processing.test.tsindex 8488656f1..ccbdd6cfd 100644--- a/apps/web/__tests__/unit/video-edit-processing.test.ts+++ b/apps/web/__tests__/unit/video-edit-processing.test.ts@@ -1,6 +1,6 @@import { Context, Effect } from "effect";import { isValidElement } from "react";-import { beforeEach, describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";type EditUpload = {phase: "processing" | "generating_thumbnail" | "complete" | "error";@@ -10,7 +10,10 @@ type EditUpload = {};const database = vi.hoisted(() => {- const state: { upload: EditUpload | null } = { upload: null };+ const state: {+ upload: EditUpload | null;+ metadata: Record<string, unknown> | null;+ } = { upload: null, metadata: null };let selection: Record<string, unknown> | undefined;const where = vi.fn(async () => {if (selection && "editSpec" in selection) return [];@@ -24,6 +27,7 @@ const database = vi.hoisted(() => {width: 1920,height: 1080,source: { type: "desktopMP4" },+ metadata: state.metadata,isScreenshot: false,password: null,organizationTombstoneAt: null,@@ -122,7 +126,10 @@ describe("viewing a recording during an edit", () => {beforeEach(() => {vi.clearAllMocks();database.state.upload = null;+ database.state.metadata = null;+ vi.stubEnv("CAP_LEGACY_EDIT_RECOVERY", "enabled");});+ afterEach(() => vi.unstubAllEnvs());it.each([["processing", 15],@@ -161,6 +168,7 @@ describe("viewing a recording during an edit", () => {);expect(isValidElement(element)).toBe(true);expect(element.type).toBe(EditRecovery);+ expect(element.props.canRestore).toBe(true);expect(database.state.upload).toBe(upload);expect(database.remove).not.toHaveBeenCalled();@@ -168,4 +176,28 @@ describe("viewing a recording during an edit", () => {expect(database.update).not.toHaveBeenCalled();},);+ it.each(["pending", "dispatching", "accepted", "rollout-disabled"])(+ "hides restoration while the operation is %s",+ async (dispatch) => {+ database.state.upload = {+ phase: "processing",+ processingProgress: 15,+ updatedAt: new Date(),+ rawFileKey: "owner123/video123/source/original.mp4",+ };+ if (dispatch === "rollout-disabled") {+ vi.stubEnv("CAP_LEGACY_EDIT_RECOVERY", "");+ } else {+ database.state.metadata = { editProcessing: { dispatch } };+ }+ const { default: EditVideoPage } = await import(+ "@/app/s/[videoId]/edit/page"+ );+ const element = await EditVideoPage({+ params: Promise.resolve({ videoId: "video123" }),+ });+ expect(element.props.canRestore).toBe(false);+ expect(database.remove).not.toHaveBeenCalled();+ },+ );});diff --git a/apps/web/__tests__/unit/video-edit-recovery-view.test.ts b/apps/web/__tests__/unit/video-edit-recovery-view.test.tsnew file mode 100644index 000000000..562c414a2--- /dev/null+++ b/apps/web/__tests__/unit/video-edit-recovery-view.test.ts@@ -0,0 +1,30 @@+import { Video } from "@cap/web-domain";+import { createElement } from "react";+import { renderToStaticMarkup } from "react-dom/server";+import { describe, expect, it, vi } from "vitest";++vi.mock("next/navigation", () => ({ useRouter: () => ({}) }));+vi.mock("@/actions/videos/save-edits", () => ({+ restoreVideoToOriginal: vi.fn(),+}));++import { EditRecovery } from "@/app/s/[videoId]/edit/edit-recovery";++const videoId = Video.VideoId.make("video");+describe("edit recovery controls", () => {+ it("offers only the recording link while restoration is unavailable", () => {+ const html = renderToStaticMarkup(+ createElement(EditRecovery, { videoId, canRestore: false }),+ );+ expect(html).not.toContain("<button");+ expect(html).not.toContain("Restore original");+ expect(html).toContain('href="/s/video"');+ });+ it("offers restoration for an eligible legacy edit", () => {+ const html = renderToStaticMarkup(+ createElement(EditRecovery, { videoId, canRestore: true }),+ );+ expect(html).toContain("<button");+ expect(html).toContain("Restore original");+ });+});diff --git a/apps/web/app/s/[videoId]/edit/edit-recovery.tsx b/apps/web/app/s/[videoId]/edit/edit-recovery.tsxindex 6aefa9d9c..83fe6df88 100644--- a/apps/web/app/s/[videoId]/edit/edit-recovery.tsx+++ b/apps/web/app/s/[videoId]/edit/edit-recovery.tsx@@ -5,7 +5,13 @@ import { useRouter } from "next/navigation";import { useState, useTransition } from "react";import { restoreVideoToOriginal } from "@/actions/videos/save-edits";-export function EditRecovery({ videoId }: { videoId: Video.VideoId }) {+export function EditRecovery({+ videoId,+ canRestore,+}: {+ videoId: Video.VideoId;+ canRestore: boolean;+}) {const router = useRouter();const [pending, startTransition] = useTransition();const [error, setError] = useState<string>();@@ -16,37 +22,46 @@ export function EditRecovery({ videoId }: { videoId: Video.VideoId }) {Your edit is still processing</h1><p className="text-sm text-gray-11">- If this edit could not finish, you can restore the original recording.- Your original source is preserved.+ {canRestore+ ? "If this edit could not finish, you can restore the original recording. Your original source is preserved."+ : "You can view the recording while your edit finishes. If processing does not finish, contact support for help."}</p>{error ? (<p role="alert" className="text-sm text-red-11">{error}</p>) : null}- <button- type="button"- disabled={pending}- className="rounded-lg bg-blue-9 px-4 py-2 text-sm font-medium text-white disabled:opacity-50"- onClick={() => {- setError(undefined);- startTransition(async () => {- try {- await restoreVideoToOriginal(videoId);- router.push(`/s/${videoId}`);- router.refresh();- } catch (cause) {- setError(- cause instanceof Error- ? cause.message- : "The original could not be restored. Please try again.",- );- }- });- }}+ {canRestore ? (+ <button+ type="button"+ disabled={pending}+ className="rounded-lg bg-blue-9 px-4 py-2 text-sm font-medium text-white disabled:opacity-50"+ onClick={() => {+ setError(undefined);+ startTransition(async () => {+ try {+ await restoreVideoToOriginal(videoId);+ router.push(`/s/${videoId}`);+ router.refresh();+ } catch (cause) {+ setError(+ cause instanceof Error+ ? cause.message+ : "The original could not be restored. Please try again.",+ );+ }+ });+ }}+ >+ {pending ? "Preparing recovery..." : "Restore original"}+ </button>+ ) : null}+ <a+ href={`/s/${videoId}`}+ className="block text-sm text-blue-11 underline">- {pending ? "Preparing recovery..." : "Restore original"}- </button>+ View recording+ </a></div></main>);diff --git a/apps/web/app/s/[videoId]/edit/page.tsx b/apps/web/app/s/[videoId]/edit/page.tsxindex fe64781e2..9a9c6ad3d 100644--- a/apps/web/app/s/[videoId]/edit/page.tsx+++ b/apps/web/app/s/[videoId]/edit/page.tsx@@ -36,6 +36,7 @@ export default async function EditVideoPage(props: {width: videos.width,height: videos.height,source: videos.source,+ metadata: videos.metadata,isScreenshot: videos.isScreenshot,transcriptionStatus: videos.transcriptionStatus,uploadPhase: videoUploads.phase,@@ -68,7 +69,15 @@ export default async function EditVideoPage(props: {rawFileKey: video.rawFileKey,})) {- return <EditRecovery videoId={videoId} />;+ return (+ <EditRecovery+ videoId={videoId}+ canRestore={+ !video.metadata?.editProcessing &&+ process.env.CAP_LEGACY_EDIT_RECOVERY === "enabled"+ }+ />+ );}if (video.uploadPhase &&