Skip to content

Commit 15367a9

Browse files
fix(studio): a media preview retries after its file or project changes (heygen-com#6451)
Co-authored-by: lorenzozanee <[email protected]>
1 parent 25e5ab7 commit 15367a9

2 files changed

Lines changed: 79 additions & 5 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// @vitest-environment happy-dom
2+
import { act } from "react";
3+
import { createRoot, type Root } from "react-dom/client";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { MediaPreview } from "./MediaPreview";
6+
7+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
8+
9+
let root: Root | null = null;
10+
let host: HTMLDivElement | null = null;
11+
12+
afterEach(() => {
13+
if (root) act(() => root?.unmount());
14+
root = null;
15+
host = null;
16+
document.body.replaceChildren();
17+
});
18+
19+
async function renderPreview(projectId: string, filePath: string): Promise<HTMLDivElement> {
20+
if (!root) {
21+
host = document.createElement("div");
22+
document.body.append(host);
23+
root = createRoot(host);
24+
}
25+
await act(async () => {
26+
root?.render(<MediaPreview projectId={projectId} filePath={filePath} />);
27+
});
28+
return host as HTMLDivElement;
29+
}
30+
31+
describe("MediaPreview", () => {
32+
it("retries a repaired file after visiting another media source", async () => {
33+
const host = await renderPreview("test", "broken.png");
34+
const failedImage = host.querySelector("img");
35+
expect(failedImage).not.toBeNull();
36+
37+
await act(async () => {
38+
failedImage?.dispatchEvent(new Event("error"));
39+
});
40+
expect(host.textContent).toContain("Couldn't load this file");
41+
42+
await renderPreview("test", "healthy.png");
43+
expect(host.querySelector("img")?.getAttribute("src")).toContain("healthy.png");
44+
45+
await renderPreview("test", "broken.png");
46+
expect(host.querySelector("img")?.getAttribute("src")).toContain("broken.png");
47+
});
48+
49+
it("resets failure state when the project changes, but preserves a current failure", async () => {
50+
const host = await renderPreview("first", "broken.png");
51+
const image = host.querySelector("img");
52+
await act(async () => {
53+
image?.dispatchEvent(new Event("error"));
54+
});
55+
expect(host.textContent).toContain("Couldn't load this file");
56+
57+
await renderPreview("second", "broken.png");
58+
expect(host.querySelector("img")?.getAttribute("src")).toContain("/second/preview/broken.png");
59+
60+
const secondImage = host.querySelector("img");
61+
await act(async () => {
62+
secondImage?.dispatchEvent(new Event("error"));
63+
});
64+
expect(host.textContent).toContain("Couldn't load this file");
65+
66+
await renderPreview("second", "broken.png");
67+
expect(host.querySelector("img")).toBeNull();
68+
});
69+
});

‎packages/studio/src/components/MediaPreview.tsx‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { buildProjectApiPath } from "../utils/projectRouting";
2-
import { useState } from "react";
2+
import { useEffect, useState } from "react";
33
import { IMAGE_EXT, VIDEO_EXT, AUDIO_EXT } from "../utils/mediaTypes";
44
import { encodeUrlPath } from "@hyperframes/parsers";
55

@@ -32,10 +32,15 @@ function MediaErrorPanel({ name, filePath }: { name: string; filePath: string })
3232
export function MediaPreview({ projectId, filePath }: { projectId: string; filePath: string }) {
3333
const serveUrl = buildProjectApiPath(projectId, `/preview/${encodeUrlPath(filePath)}`);
3434
const name = filePath.split("/").pop() ?? filePath;
35-
// Keyed by path so switching to another file clears a previous failure.
36-
const [failedPath, setFailedPath] = useState<string | null>(null);
37-
const failed = failedPath === filePath;
38-
const setFailed = () => setFailedPath(filePath);
35+
const [failure, setFailure] = useState<{ source: string; failed: boolean }>({
36+
source: serveUrl,
37+
failed: false,
38+
});
39+
useEffect(() => {
40+
setFailure({ source: serveUrl, failed: false });
41+
}, [serveUrl]);
42+
const failed = failure.source === serveUrl && failure.failed;
43+
const setFailed = () => setFailure({ source: serveUrl, failed: true });
3944

4045
if (failed) return <MediaErrorPanel name={name} filePath={filePath} />;
4146

0 commit comments

Comments
 (0)