@hyperframes/studio 0.9.28-alpha.1 → 0.9.29

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.
@@ -1,189 +0,0 @@
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] = useState(false);
33
- const opened = useRef(false);
34
- const lifetime = useRef<AbortController | null>(null);
35
- const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
36
- const [progress, setProgress] = useState("");
37
-
38
- function setWorking(value: boolean) {
39
- working.current = value;
40
- setBusyState(value);
41
- }
42
-
43
- async function poll(job: LocalShareJob, signal: AbortSignal) {
44
- if (signal.aborted) return;
45
- if (job.state === "running") {
46
- setWorking(true);
47
- setProgress(STEP_LABELS[job.step] ?? "Preparing share…");
48
- timer.current = setTimeout(() => {
49
- void shareJob(token, job.id, signal)
50
- .then((next) => poll(next, signal))
51
- .catch((cause) => {
52
- reportFailure(cause, signal);
53
- });
54
- }, 1000);
55
- return;
56
- }
57
- setWorking(false);
58
- setProgress("");
59
- const result = job.result;
60
- if (result) setSnapshot((prior) => (prior ? { ...prior, share: result, job } : prior));
61
- await refresh(signal, job.state === "failed");
62
- }
63
- async function refresh(signal: AbortSignal, preserveSelection = false) {
64
- setError(null);
65
- setErrorCode(null);
66
- setWorking(true);
67
- if (timer.current) clearTimeout(timer.current);
68
- try {
69
- const next = await shareSnapshot(token, signal);
70
- if (signal.aborted) return;
71
- recordOpen(next);
72
- reconnectOnReturn.current = false;
73
- setSnapshot(next);
74
- if (!preserveSelection) resetSelection(next);
75
- if (next.job?.error) {
76
- setError(next.job.error.message);
77
- setErrorCode(next.job.error.code);
78
- }
79
- if (next.job?.state === "running") await poll(next.job, signal);
80
- else setWorking(false);
81
- return true;
82
- } catch (cause) {
83
- reportFailure(cause, signal);
84
- return false;
85
- }
86
- }
87
- useMountEffect(() => {
88
- const controller = new AbortController();
89
- lifetime.current = controller;
90
- const resume = () => {
91
- if (document.visibilityState !== "hidden" && reconnectOnReturn.current && !working.current)
92
- void refresh(controller.signal);
93
- };
94
- window.addEventListener("focus", resume);
95
- document.addEventListener("visibilitychange", resume);
96
- void refresh(controller.signal);
97
- return () => {
98
- window.removeEventListener("focus", resume);
99
- document.removeEventListener("visibilitychange", resume);
100
- controller.abort();
101
- if (timer.current) clearTimeout(timer.current);
102
- };
103
- });
104
- async function run(action: LocalShareAction) {
105
- const signal = lifetime.current?.signal;
106
- if (!signal || signal.aborted || working.current) return;
107
- setWorking(true);
108
- setError(null);
109
- setErrorCode(null);
110
- setCopied(false);
111
- setProgress("Preparing share…");
112
- try {
113
- if (["share", "update"].includes(action.action)) await beforeShare?.();
114
- if (!signal.aborted) await poll(await shareAction(token, action, signal), signal);
115
- } catch (cause) {
116
- await recoverActionFailure(cause, signal);
117
- }
118
- }
119
- async function recoverActionFailure(cause: unknown, signal: AbortSignal) {
120
- if (
121
- cause instanceof LocalShareError &&
122
- [
123
- "login_changed",
124
- "login_expired",
125
- "unauthenticated",
126
- "refresh_failed",
127
- "not_configured",
128
- ].includes(cause.code)
129
- ) {
130
- reportFailure(cause, signal);
131
- return;
132
- }
133
- setProgress("");
134
- const recovered = await refresh(signal, true);
135
- if (recovered && !signal.aborted) {
136
- setError(failure(cause));
137
- setErrorCode(cause instanceof LocalShareError ? cause.code : null);
138
- }
139
- }
140
- function recordOpen(value: LocalShareSnapshot | null) {
141
- if (opened.current) return;
142
- opened.current = true;
143
- const share = value?.share;
144
- const link = share?.url ? (share.changed ? "changed" : "live") : "none";
145
- shareInteraction(token, { event: "share_opened", link });
146
- }
147
- function resetSelection(next: LocalShareSnapshot) {
148
- setAudience(next.share.audience ?? "");
149
- setSpaceId(next.share.spaceId ?? next.spaces.find((space) => space.bound)?.id ?? "");
150
- }
151
- function reportFailure(cause: unknown, signal: AbortSignal) {
152
- if (signal.aborted) return;
153
- recordOpen(null);
154
- reconnectOnReturn.current = true;
155
- setSnapshot(null);
156
- setCopied(false);
157
- setError(failure(cause));
158
- setErrorCode(cause instanceof LocalShareError ? cause.code : null);
159
- if (cause instanceof LocalShareError && cause.job?.state === "running") {
160
- void poll(cause.job, signal);
161
- return;
162
- }
163
- setWorking(false);
164
- setProgress("");
165
- }
166
- async function copy(url: string) {
167
- try {
168
- await navigator.clipboard.writeText(url);
169
- setCopied(true);
170
- shareInteraction(token, { event: "share_link_copied", via: "copy" });
171
- } catch {
172
- setError("Couldn’t copy. Open the shared page and copy its address.");
173
- }
174
- }
175
- return {
176
- snapshot,
177
- audience,
178
- setAudience,
179
- spaceId,
180
- busy,
181
- error,
182
- errorCode,
183
- copied,
184
- progress,
185
- run,
186
- copy,
187
- };
188
- }
189
- export type QuickShareModel = ReturnType<typeof useQuickShare>;