@hyperframes/studio 0.9.32 → 0.9.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{codec-Bz1jsLs8.js → codec-OdIlx7W3.js} +1 -1
- package/dist/assets/{hyperframes-player-cdH5Sz_t.js → hyperframes-player-XtPxZL3k.js} +1 -1
- package/dist/assets/{index-dsfjBidg.css → index-BJTCbv8z.css} +1 -1
- package/dist/assets/{index-D1l7izdf.js → index-DTU1D-zj.js} +166 -166
- package/dist/assets/{index-DFRbcd5J.js → index-DWZWPFkU.js} +1 -1
- package/dist/assets/{pausedVideoDecoder-CgV8YB6S.js → pausedVideoDecoder-NVr8gRm5.js} +1 -1
- package/dist/index.html +2 -2
- package/dist/index.js +1927 -1212
- package/dist/styles.css +1 -1
- package/package.json +8 -8
- package/src/App.tsx +1 -0
- package/src/components/StudioHeader.tsx +4 -0
- package/src/components/share/QuickShare.test.tsx +622 -0
- package/src/components/share/QuickShare.tsx +104 -0
- package/src/components/share/QuickShareControls.tsx +265 -0
- package/src/components/share/ShareAudience.tsx +120 -0
- package/src/components/share/localShareClient.ts +171 -0
- package/src/components/share/localShareTelemetry.test.ts +22 -0
- package/src/components/share/localShareTelemetry.ts +13 -0
- package/src/components/share/useQuickShare.ts +215 -0
- package/src/components/sidebar/CompositionsTab.drag.test.tsx +9 -2
- package/src/components/sidebar/CompositionsTab.hoverPreview.test.tsx +30 -10
- package/src/components/sidebar/CompositionsTab.thumbnails.test.tsx +4 -2
- package/src/components/sidebar/CompositionsTab.tsx +28 -3
- package/src/components/sidebar/compositionsTabTestUtils.tsx +16 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { LocalShareInteraction } from "@hyperframes/studio-server";
|
|
2
|
+
import { browserTelemetryAllowed } from "../../telemetry/policy";
|
|
3
|
+
import { studioApiFetch } from "../../utils/studioApiFetch";
|
|
4
|
+
|
|
5
|
+
export function shareInteraction(token: string, event: LocalShareInteraction): void {
|
|
6
|
+
if (!browserTelemetryAllowed()) return;
|
|
7
|
+
void studioApiFetch("/api/share/events", {
|
|
8
|
+
method: "POST",
|
|
9
|
+
signal: AbortSignal.timeout(5000),
|
|
10
|
+
headers: { "Content-Type": "application/json", "X-Hyperframes-Share-CSRF": token },
|
|
11
|
+
body: JSON.stringify(event),
|
|
12
|
+
}).catch(() => {});
|
|
13
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import type {
|
|
3
|
+
LocalShareAction,
|
|
4
|
+
LocalShareAudience,
|
|
5
|
+
LocalShareJob,
|
|
6
|
+
LocalShareSnapshot,
|
|
7
|
+
} from "@hyperframes/studio-server";
|
|
8
|
+
import { useMountEffect } from "../../hooks/useMountEffect";
|
|
9
|
+
import { LocalShareError, shareAction, shareJob, shareSnapshot } from "./localShareClient";
|
|
10
|
+
import { shareInteraction } from "./localShareTelemetry";
|
|
11
|
+
const STEP_LABELS: Record<string, string> = {
|
|
12
|
+
preparing: "Preparing your video…",
|
|
13
|
+
signing_in: "Finish signing in in your browser…",
|
|
14
|
+
uploading: "Uploading your video…",
|
|
15
|
+
processing: "Processing your video…",
|
|
16
|
+
checking: "Checking your video…",
|
|
17
|
+
};
|
|
18
|
+
const failure = (error: unknown) =>
|
|
19
|
+
error instanceof LocalShareError
|
|
20
|
+
? error.message
|
|
21
|
+
: "Sharing couldn’t finish. Try again to check its status.";
|
|
22
|
+
|
|
23
|
+
export function useQuickShare(token: string, beforeShare?: () => Promise<void>) {
|
|
24
|
+
const [snapshot, setSnapshot] = useState<LocalShareSnapshot | null>(null);
|
|
25
|
+
const [audience, setAudience] = useState<LocalShareAudience | "">("");
|
|
26
|
+
const [spaceId, setSpaceId] = useState("");
|
|
27
|
+
const [busy, setBusyState] = useState(false);
|
|
28
|
+
const working = useRef(false);
|
|
29
|
+
const reconnectOnReturn = useRef(false);
|
|
30
|
+
const [error, setError] = useState<string | null>(null);
|
|
31
|
+
const [errorCode, setErrorCode] = useState<string | null>(null);
|
|
32
|
+
const { copied, setCopied, acknowledgeCopy } = useCopyAcknowledgement();
|
|
33
|
+
const [notice, setNotice] = useState("");
|
|
34
|
+
const opened = useRef(false);
|
|
35
|
+
const lifetime = useRef<AbortController | null>(null);
|
|
36
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
37
|
+
const [progress, setProgress] = useState("");
|
|
38
|
+
|
|
39
|
+
function setWorking(value: boolean) {
|
|
40
|
+
working.current = value;
|
|
41
|
+
setBusyState(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function poll(job: LocalShareJob, signal: AbortSignal) {
|
|
45
|
+
if (signal.aborted) return;
|
|
46
|
+
if (job.state === "running") {
|
|
47
|
+
setWorking(true);
|
|
48
|
+
setProgress(STEP_LABELS[job.step] ?? "Preparing share…");
|
|
49
|
+
timer.current = setTimeout(() => {
|
|
50
|
+
void shareJob(token, job.id, signal)
|
|
51
|
+
.then((next) => poll(next, signal))
|
|
52
|
+
.catch((cause) => {
|
|
53
|
+
reportFailure(cause, signal);
|
|
54
|
+
});
|
|
55
|
+
}, 1000);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
setWorking(false);
|
|
59
|
+
setProgress("");
|
|
60
|
+
const result = job.result;
|
|
61
|
+
if (result) setSnapshot((prior) => (prior ? { ...prior, share: result, job } : prior));
|
|
62
|
+
await refresh(signal, job.state === "failed");
|
|
63
|
+
if (!signal.aborted) setNotice(completionNotice(job));
|
|
64
|
+
}
|
|
65
|
+
async function refresh(signal: AbortSignal, preserveSelection = false) {
|
|
66
|
+
setError(null);
|
|
67
|
+
setErrorCode(null);
|
|
68
|
+
setWorking(true);
|
|
69
|
+
if (timer.current) clearTimeout(timer.current);
|
|
70
|
+
try {
|
|
71
|
+
const next = await shareSnapshot(token, signal);
|
|
72
|
+
if (signal.aborted) return;
|
|
73
|
+
recordOpen(next);
|
|
74
|
+
reconnectOnReturn.current = false;
|
|
75
|
+
setSnapshot(next);
|
|
76
|
+
if (!preserveSelection) resetSelection(next);
|
|
77
|
+
if (next.job?.error) {
|
|
78
|
+
setError(next.job.error.message);
|
|
79
|
+
setErrorCode(next.job.error.code);
|
|
80
|
+
}
|
|
81
|
+
if (next.job?.state === "running") await poll(next.job, signal);
|
|
82
|
+
else setWorking(false);
|
|
83
|
+
return true;
|
|
84
|
+
} catch (cause) {
|
|
85
|
+
reportFailure(cause, signal);
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
useMountEffect(() => {
|
|
90
|
+
const controller = new AbortController();
|
|
91
|
+
lifetime.current = controller;
|
|
92
|
+
const resume = () => {
|
|
93
|
+
if (document.visibilityState !== "hidden" && reconnectOnReturn.current && !working.current)
|
|
94
|
+
void refresh(controller.signal);
|
|
95
|
+
};
|
|
96
|
+
window.addEventListener("focus", resume);
|
|
97
|
+
document.addEventListener("visibilitychange", resume);
|
|
98
|
+
void refresh(controller.signal);
|
|
99
|
+
return () => {
|
|
100
|
+
window.removeEventListener("focus", resume);
|
|
101
|
+
document.removeEventListener("visibilitychange", resume);
|
|
102
|
+
controller.abort();
|
|
103
|
+
if (timer.current) clearTimeout(timer.current);
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
async function run(action: LocalShareAction) {
|
|
107
|
+
const signal = lifetime.current?.signal;
|
|
108
|
+
if (!signal || signal.aborted || working.current) return;
|
|
109
|
+
setWorking(true);
|
|
110
|
+
setError(null);
|
|
111
|
+
setErrorCode(null);
|
|
112
|
+
setCopied(false);
|
|
113
|
+
setNotice("");
|
|
114
|
+
setProgress("Preparing share…");
|
|
115
|
+
try {
|
|
116
|
+
if (["share", "update"].includes(action.action)) await beforeShare?.();
|
|
117
|
+
if (!signal.aborted) await poll(await shareAction(token, action, signal), signal);
|
|
118
|
+
} catch (cause) {
|
|
119
|
+
await recoverActionFailure(cause, signal);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function recoverActionFailure(cause: unknown, signal: AbortSignal) {
|
|
123
|
+
if (
|
|
124
|
+
cause instanceof LocalShareError &&
|
|
125
|
+
[
|
|
126
|
+
"login_changed",
|
|
127
|
+
"login_expired",
|
|
128
|
+
"unauthenticated",
|
|
129
|
+
"refresh_failed",
|
|
130
|
+
"not_configured",
|
|
131
|
+
].includes(cause.code)
|
|
132
|
+
) {
|
|
133
|
+
reportFailure(cause, signal);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
setProgress("");
|
|
137
|
+
const recovered = await refresh(signal, true);
|
|
138
|
+
if (recovered && !signal.aborted) {
|
|
139
|
+
setError(failure(cause));
|
|
140
|
+
setErrorCode(cause instanceof LocalShareError ? cause.code : null);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function recordOpen(value: LocalShareSnapshot | null) {
|
|
144
|
+
if (opened.current) return;
|
|
145
|
+
opened.current = true;
|
|
146
|
+
const share = value?.share;
|
|
147
|
+
const link = share?.url ? (share.changed ? "changed" : "live") : "none";
|
|
148
|
+
shareInteraction(token, { event: "share_opened", link });
|
|
149
|
+
}
|
|
150
|
+
function resetSelection(next: LocalShareSnapshot) {
|
|
151
|
+
setAudience(next.share.audience ?? "");
|
|
152
|
+
setSpaceId(next.share.spaceId ?? next.spaces.find((space) => space.bound)?.id ?? "");
|
|
153
|
+
}
|
|
154
|
+
function reportFailure(cause: unknown, signal: AbortSignal) {
|
|
155
|
+
if (signal.aborted) return;
|
|
156
|
+
recordOpen(null);
|
|
157
|
+
reconnectOnReturn.current = true;
|
|
158
|
+
setSnapshot(null);
|
|
159
|
+
setCopied(false);
|
|
160
|
+
setError(failure(cause));
|
|
161
|
+
setErrorCode(cause instanceof LocalShareError ? cause.code : null);
|
|
162
|
+
if (cause instanceof LocalShareError && cause.job?.state === "running") {
|
|
163
|
+
void poll(cause.job, signal);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
setWorking(false);
|
|
167
|
+
setProgress("");
|
|
168
|
+
}
|
|
169
|
+
async function copy(url: string) {
|
|
170
|
+
try {
|
|
171
|
+
await navigator.clipboard.writeText(url);
|
|
172
|
+
if (lifetime.current?.signal.aborted) return;
|
|
173
|
+
acknowledgeCopy();
|
|
174
|
+
shareInteraction(token, { event: "share_link_copied", via: "copy" });
|
|
175
|
+
} catch {
|
|
176
|
+
setError("Couldn’t copy. Open the shared page and copy its address.");
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
snapshot,
|
|
181
|
+
audience,
|
|
182
|
+
setAudience,
|
|
183
|
+
spaceId,
|
|
184
|
+
busy,
|
|
185
|
+
error,
|
|
186
|
+
errorCode,
|
|
187
|
+
copied,
|
|
188
|
+
notice,
|
|
189
|
+
progress,
|
|
190
|
+
run,
|
|
191
|
+
copy,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
export type QuickShareModel = ReturnType<typeof useQuickShare> & {
|
|
195
|
+
includeSource: boolean;
|
|
196
|
+
setIncludeSource: (value: boolean) => void;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function completionNotice(job: LocalShareJob) {
|
|
200
|
+
if (job.state !== "done") return "";
|
|
201
|
+
if (job.result?.upToDate) return "Already up to date.";
|
|
202
|
+
return job.result?.updated ? "Shared version updated." : "";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function useCopyAcknowledgement() {
|
|
206
|
+
const [copied, setCopied] = useState(false);
|
|
207
|
+
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
|
|
208
|
+
useMountEffect(() => () => clearTimeout(timer.current));
|
|
209
|
+
function acknowledgeCopy() {
|
|
210
|
+
clearTimeout(timer.current);
|
|
211
|
+
setCopied(true);
|
|
212
|
+
timer.current = setTimeout(() => setCopied(false), 2000);
|
|
213
|
+
}
|
|
214
|
+
return { copied, setCopied, acknowledgeCopy };
|
|
215
|
+
}
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
import { act } from "react";
|
|
4
4
|
import { describe, expect, it, vi } from "vitest";
|
|
5
5
|
import { TIMELINE_COMPOSITION_MIME } from "../../utils/timelineCompositionDrop";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
mountCompositionsTab,
|
|
8
|
+
settleFrameToken,
|
|
9
|
+
stubFrameTokenFetch,
|
|
10
|
+
} from "./compositionsTabTestUtils";
|
|
7
11
|
|
|
8
12
|
function mount(onSelect = vi.fn(), onAddToTimeline = vi.fn()) {
|
|
9
13
|
const host = mountCompositionsTab({ onSelect, onAddToTimeline });
|
|
@@ -13,15 +17,17 @@ function mount(onSelect = vi.fn(), onAddToTimeline = vi.fn()) {
|
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
describe("composition card drag", () => {
|
|
16
|
-
it("mounts one live preview only after sustained hover and removes it on leave", () => {
|
|
20
|
+
it("mounts one live preview only after sustained hover and removes it on leave", async () => {
|
|
17
21
|
vi.useFakeTimers();
|
|
18
22
|
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
23
|
+
const tokenFetch = stubFrameTokenFetch();
|
|
19
24
|
try {
|
|
20
25
|
const { host, card } = mount();
|
|
21
26
|
act(() => {
|
|
22
27
|
card.dispatchEvent(new Event("pointerover", { bubbles: true }));
|
|
23
28
|
vi.advanceTimersByTime(300);
|
|
24
29
|
});
|
|
30
|
+
await settleFrameToken();
|
|
25
31
|
expect(host.querySelectorAll("iframe")).toHaveLength(1);
|
|
26
32
|
|
|
27
33
|
act(() => {
|
|
@@ -30,6 +36,7 @@ describe("composition card drag", () => {
|
|
|
30
36
|
expect(host.querySelector("iframe")).toBeNull();
|
|
31
37
|
expect(vi.getTimerCount()).toBe(0);
|
|
32
38
|
} finally {
|
|
39
|
+
tokenFetch.mockRestore();
|
|
33
40
|
consoleError.mockRestore();
|
|
34
41
|
vi.useRealTimers();
|
|
35
42
|
}
|
|
@@ -2,11 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
import { act } from "react";
|
|
4
4
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
FRAME_TOKEN,
|
|
7
|
+
mountCompositionsTab,
|
|
8
|
+
settleFrameToken,
|
|
9
|
+
stubFrameTokenFetch,
|
|
10
|
+
} from "./compositionsTabTestUtils";
|
|
6
11
|
|
|
7
12
|
type PostedControl = { source: string; type: string; action: string; muted?: boolean };
|
|
8
13
|
|
|
9
|
-
function hoverPreview() {
|
|
14
|
+
async function hoverPreview() {
|
|
10
15
|
const host = mountCompositionsTab();
|
|
11
16
|
const card = host.querySelector<HTMLElement>('[draggable="true"]');
|
|
12
17
|
if (!card) throw new Error("composition card did not render");
|
|
@@ -14,6 +19,7 @@ function hoverPreview() {
|
|
|
14
19
|
card.dispatchEvent(new Event("pointerover", { bubbles: true }));
|
|
15
20
|
vi.advanceTimersByTime(300);
|
|
16
21
|
});
|
|
22
|
+
await settleFrameToken();
|
|
17
23
|
const iframe = host.querySelector("iframe");
|
|
18
24
|
if (!iframe) throw new Error("hover preview did not mount");
|
|
19
25
|
// Stand-in for the frame's window. The document getter throws, as reading into
|
|
@@ -48,23 +54,37 @@ let consoleError: ReturnType<typeof vi.spyOn>;
|
|
|
48
54
|
beforeEach(() => {
|
|
49
55
|
vi.useFakeTimers();
|
|
50
56
|
consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
57
|
+
stubFrameTokenFetch();
|
|
51
58
|
});
|
|
52
59
|
|
|
53
60
|
afterEach(() => {
|
|
61
|
+
vi.restoreAllMocks();
|
|
54
62
|
consoleError.mockRestore();
|
|
55
63
|
vi.useRealTimers();
|
|
56
64
|
});
|
|
57
65
|
|
|
58
66
|
describe("composition card hover preview", () => {
|
|
59
|
-
it("frames the composition in an opaque origin", () => {
|
|
60
|
-
const { iframe } = hoverPreview();
|
|
67
|
+
it("frames the composition in an opaque origin, at the project's token path", async () => {
|
|
68
|
+
const { iframe } = await hoverPreview();
|
|
69
|
+
expect(iframe.getAttribute("src")).toBe(
|
|
70
|
+
`/api/projects/demo/preview/~${FRAME_TOKEN}/comp/compositions/headline.html`,
|
|
71
|
+
);
|
|
61
72
|
expect(iframe.getAttribute("sandbox")).toBe("allow-scripts");
|
|
62
73
|
expect(iframe.sandbox.contains("allow-scripts")).toBe(true);
|
|
63
74
|
expect(iframe.sandbox.contains("allow-same-origin")).toBe(false);
|
|
64
75
|
});
|
|
65
76
|
|
|
66
|
-
it("
|
|
67
|
-
|
|
77
|
+
it("falls back to the plain path when the token is unavailable", async () => {
|
|
78
|
+
vi.restoreAllMocks();
|
|
79
|
+
stubFrameTokenFetch(null);
|
|
80
|
+
const { iframe } = await hoverPreview();
|
|
81
|
+
expect(iframe.getAttribute("src")).toBe(
|
|
82
|
+
"/api/projects/demo/preview/comp/compositions/headline.html",
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("plays muted over the runtime bridge once the runtime is listening", async () => {
|
|
87
|
+
const { frameWindow } = await hoverPreview();
|
|
68
88
|
expect(frameWindow.postMessage).not.toHaveBeenCalled();
|
|
69
89
|
|
|
70
90
|
fromFrame(frameWindow, { type: "ready" });
|
|
@@ -79,8 +99,8 @@ describe("composition card hover preview", () => {
|
|
|
79
99
|
}
|
|
80
100
|
});
|
|
81
101
|
|
|
82
|
-
it("sizes the stage from the runtime's timeline message", () => {
|
|
83
|
-
const { iframe, frameWindow } = hoverPreview();
|
|
102
|
+
it("sizes the stage from the runtime's timeline message", async () => {
|
|
103
|
+
const { iframe, frameWindow } = await hoverPreview();
|
|
84
104
|
expect([iframe.style.width, iframe.style.height]).toEqual(["1920px", "1080px"]);
|
|
85
105
|
|
|
86
106
|
fromFrame(frameWindow, { type: "timeline", compositionWidth: 1080, compositionHeight: 1920 });
|
|
@@ -88,8 +108,8 @@ describe("composition card hover preview", () => {
|
|
|
88
108
|
expect([iframe.style.width, iframe.style.height]).toEqual(["1080px", "1920px"]);
|
|
89
109
|
});
|
|
90
110
|
|
|
91
|
-
it("ignores runtime messages that come from another window", () => {
|
|
92
|
-
const { iframe, frameWindow } = hoverPreview();
|
|
111
|
+
it("ignores runtime messages that come from another window", async () => {
|
|
112
|
+
const { iframe, frameWindow } = await hoverPreview();
|
|
93
113
|
|
|
94
114
|
fromFrame({ postMessage: vi.fn() }, { type: "ready" });
|
|
95
115
|
fromFrame(window, { type: "timeline", compositionWidth: 640, compositionHeight: 640 });
|
|
@@ -8,7 +8,7 @@ import { TIMELINE_VIEWPORT_BUDGETS } from "../../player/lib/timelineViewportBudg
|
|
|
8
8
|
import { CompositionThumbnail } from "../../player/components/CompositionThumbnail";
|
|
9
9
|
import { usePlayerStore } from "../../player/store/playerStore";
|
|
10
10
|
import { renderPosterForNextOpen } from "../nle/PreviewPoster";
|
|
11
|
-
import { mountCompositionsTab } from "./compositionsTabTestUtils";
|
|
11
|
+
import { mountCompositionsTab, settleFrameToken } from "./compositionsTabTestUtils";
|
|
12
12
|
|
|
13
13
|
class DecodingImage {
|
|
14
14
|
onload: (() => void) | null = null;
|
|
@@ -247,7 +247,7 @@ describe("composition card thumbnails", () => {
|
|
|
247
247
|
expect(URL.revokeObjectURL).not.toHaveBeenCalledWith("blob:frame-2");
|
|
248
248
|
});
|
|
249
249
|
|
|
250
|
-
it("tell a hover preview how large it shows, so its videos are not copied at source size", () => {
|
|
250
|
+
it("tell a hover preview how large it shows, so its videos are not copied at source size", async () => {
|
|
251
251
|
vi.useFakeTimers();
|
|
252
252
|
try {
|
|
253
253
|
const host = mount();
|
|
@@ -258,6 +258,8 @@ describe("composition card thumbnails", () => {
|
|
|
258
258
|
);
|
|
259
259
|
});
|
|
260
260
|
act(() => vi.advanceTimersByTime(300));
|
|
261
|
+
renders.at(-1)!.answer(404); // the hover preview's frame-token request
|
|
262
|
+
await settleFrameToken();
|
|
261
263
|
const frame = host.querySelector("iframe")!;
|
|
262
264
|
const postMessage = vi.fn();
|
|
263
265
|
Object.defineProperty(frame, "contentWindow", { value: { postMessage } });
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { buildProjectApiPath } from "../../utils/projectRouting";
|
|
2
|
+
import { studioApiFetch } from "../../utils/studioApiFetch";
|
|
2
3
|
import { memo, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
4
|
import { useThumbnailLease } from "../../hooks/useThumbnailLease";
|
|
4
5
|
import {
|
|
@@ -61,6 +62,30 @@ function compositionPreviewUrl(projectId: string, comp: string): string {
|
|
|
61
62
|
return buildProjectApiPath(projectId, `/preview/comp/${encodePreviewPath(comp)}`);
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
function framePreviewUrl(projectId: string, comp: string, frameToken: string | null): string {
|
|
66
|
+
const tokenPrefix = frameToken ? `/~${frameToken}` : "";
|
|
67
|
+
return buildProjectApiPath(projectId, `/preview${tokenPrefix}/comp/${encodePreviewPath(comp)}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function useFreshFrameToken(projectId: string, wanted: boolean): string | null | undefined {
|
|
71
|
+
const [known, setKnown] = useState<{ token: string | null } | undefined>(undefined);
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
if (!wanted) return setKnown(undefined);
|
|
74
|
+
let live = true;
|
|
75
|
+
void studioApiFetch(buildProjectApiPath(projectId, "/frame-token"))
|
|
76
|
+
.then((res) => (res.ok ? res.json() : null))
|
|
77
|
+
.then((body: { token?: unknown } | null) =>
|
|
78
|
+
typeof body?.token === "string" ? body.token : null,
|
|
79
|
+
)
|
|
80
|
+
.catch(() => null)
|
|
81
|
+
.then((token) => live && setKnown({ token }));
|
|
82
|
+
return () => {
|
|
83
|
+
live = false;
|
|
84
|
+
};
|
|
85
|
+
}, [projectId, wanted]);
|
|
86
|
+
return known?.token;
|
|
87
|
+
}
|
|
88
|
+
|
|
64
89
|
export function compositionCardThumbnailUrl(
|
|
65
90
|
projectId: string,
|
|
66
91
|
comp: string,
|
|
@@ -173,7 +198,7 @@ function CompCard({
|
|
|
173
198
|
setLivePreviewLoaded(false);
|
|
174
199
|
};
|
|
175
200
|
const name = comp.replace(/^compositions\//, "").replace(/\.html$/, "");
|
|
176
|
-
const
|
|
201
|
+
const frameToken = useFreshFrameToken(projectId, hovered);
|
|
177
202
|
const thumbnailUrl = compositionCardThumbnailUrl(projectId, comp, contentRevision);
|
|
178
203
|
const [thumbnailBox, nearViewport] = useNearViewport<HTMLDivElement>();
|
|
179
204
|
const timelineSessionEpoch = usePlayerStore((state) => state.timelineSessionEpoch);
|
|
@@ -287,10 +312,10 @@ function CompCard({
|
|
|
287
312
|
}`}
|
|
288
313
|
/>
|
|
289
314
|
)}
|
|
290
|
-
{hovered && (
|
|
315
|
+
{hovered && frameToken !== undefined && (
|
|
291
316
|
<iframe
|
|
292
317
|
ref={iframeRef}
|
|
293
|
-
src={
|
|
318
|
+
src={framePreviewUrl(projectId, comp, frameToken)}
|
|
294
319
|
sandbox={OPAQUE_ORIGIN_SCRIPTS_ONLY_SANDBOX}
|
|
295
320
|
className="absolute border-none pointer-events-none"
|
|
296
321
|
style={{
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { act, type ComponentProps } from "react";
|
|
2
2
|
import { createRoot, type Root } from "react-dom/client";
|
|
3
|
-
import { afterEach, vi } from "vitest";
|
|
3
|
+
import { afterEach, expect, vi } from "vitest";
|
|
4
4
|
import { thumbnailScheduler } from "../../player/lib/thumbnailScheduler";
|
|
5
5
|
import { usePlayerStore } from "../../player/store/playerStore";
|
|
6
6
|
import { CompositionsTab } from "./CompositionsTab";
|
|
@@ -37,3 +37,18 @@ export function mountCompositionsTab(props: Partial<ComponentProps<typeof Compos
|
|
|
37
37
|
});
|
|
38
38
|
return host;
|
|
39
39
|
}
|
|
40
|
+
|
|
41
|
+
export const FRAME_TOKEN = "T".repeat(43);
|
|
42
|
+
|
|
43
|
+
export function stubFrameTokenFetch(token: string | null = FRAME_TOKEN) {
|
|
44
|
+
return vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => {
|
|
45
|
+
expect(String(input)).toBe("/api/projects/demo/frame-token");
|
|
46
|
+
return { ok: token !== null, json: async () => ({ token }) } as Response;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function settleFrameToken() {
|
|
51
|
+
await act(async () => {
|
|
52
|
+
for (let i = 0; i < 10; i += 1) await Promise.resolve();
|
|
53
|
+
});
|
|
54
|
+
}
|