Skip to content

Commit 47a05d7

Browse files
authored
Merge pull request #18 from Priveetee/dev
fix: simplify mobile artifact handoff after backend fix
2 parents 69346df + 3bd51cc commit 47a05d7

2 files changed

Lines changed: 4 additions & 80 deletions

File tree

apps/web/src/components/download-sheet.tsx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { useEffect, useMemo, useState } from "react";
1+
import { useMemo, useState } from "react";
22
import { useArtifactDownloadOnDone } from "../hooks/use-artifact-download-on-done";
33
import { useDownloaderJob } from "../hooks/use-downloader-job";
44
import { useOverlayLock } from "../hooks/use-overlay-lock";
55
import { useSmoothDismiss } from "../hooks/use-smooth-dismiss";
6-
import { cancelPreparedIosArtifactWindow, prepareIosArtifactWindow } from "../lib/api-downloader";
76
import type { VideoStream } from "../types/stream";
87
import {
98
buildDownloaderCreatePayload,
@@ -23,8 +22,7 @@ export function DownloadSheet({ stream, onClose, onDone }: Props) {
2322
useOverlayLock(true);
2423
const { isClosing, dismiss } = useSmoothDismiss({ onClose });
2524
const downloader = useDownloaderJob();
26-
const { isDone, jobId, isQueued, isRunning, isFailed, errorText, openArtifact, reset, start } =
27-
downloader;
25+
const { isDone, jobId, isQueued, isRunning, errorText, openArtifact, reset, start } = downloader;
2826
const isBusy = isQueued || isRunning;
2927
const [artifactError, setArtifactError] = useState<string | null>(null);
3028
const options = useMemo(() => buildDownloadOptions(stream), [stream]);
@@ -46,17 +44,6 @@ export function DownloadSheet({ stream, onClose, onDone }: Props) {
4644
});
4745
const showWorkingState = isBusy || completion.isCompleting;
4846

49-
useEffect(() => {
50-
if (!isFailed) return;
51-
cancelPreparedIosArtifactWindow();
52-
}, [isFailed]);
53-
54-
useEffect(() => {
55-
return () => {
56-
cancelPreparedIosArtifactWindow();
57-
};
58-
}, []);
59-
6047
function selectMode(next: DownloadMode) {
6148
setMode(next);
6249
const modeOptions = options.filter((option) => option.mode === next);
@@ -66,7 +53,6 @@ export function DownloadSheet({ stream, onClose, onDone }: Props) {
6653
function startDownload() {
6754
if (!selected) return;
6855
setArtifactError(null);
69-
prepareIosArtifactWindow();
7056
start(buildDownloaderCreatePayload(stream.id, selected));
7157
}
7258

apps/web/src/lib/api-downloader.ts

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import type {
55
} from "../types/downloader";
66
import { ApiError } from "./api";
77
import { API_BASE as BASE } from "./env";
8-
import { isIosDevice, isMobileDownloadDevice } from "./ios-device";
9-
10-
let preparedIosWindow: Window | null = null;
8+
import { isMobileDownloadDevice } from "./ios-device";
119

1210
type ErrorBody = {
1311
error?: string;
@@ -52,41 +50,6 @@ export async function fetchDownloaderJob(jobId: string): Promise<DownloaderJobRe
5250
return body as DownloaderJobResponse;
5351
}
5452

55-
export function prepareIosArtifactWindow() {
56-
if (!isIosDevice()) return;
57-
if (preparedIosWindow && !preparedIosWindow.closed) return;
58-
const opened = window.open("about:blank", "_blank");
59-
if (!opened) return;
60-
opened.document.title = "Preparing download";
61-
preparedIosWindow = opened;
62-
}
63-
64-
export function cancelPreparedIosArtifactWindow() {
65-
if (!preparedIosWindow || preparedIosWindow.closed) {
66-
preparedIosWindow = null;
67-
return;
68-
}
69-
preparedIosWindow.close();
70-
preparedIosWindow = null;
71-
}
72-
73-
function consumePreparedIosArtifactWindow(): Window | null {
74-
const target = preparedIosWindow;
75-
preparedIosWindow = null;
76-
if (!target || target.closed) return null;
77-
return target;
78-
}
79-
80-
function clickDownloadAnchor(doc: Document, href: string) {
81-
const a = doc.createElement("a");
82-
a.href = href;
83-
a.download = "";
84-
a.rel = "noopener";
85-
doc.body.appendChild(a);
86-
a.click();
87-
doc.body.removeChild(a);
88-
}
89-
9053
function extensionFromType(contentType: string | null): string {
9154
const value = contentType ?? "";
9255
if (value.includes("video/mp4")) return "mp4";
@@ -106,33 +69,8 @@ function filenameFromHeader(contentDisposition: string | null): string | null {
10669

10770
export async function downloadDownloaderArtifact(jobId: string): Promise<void> {
10871
const endpoint = `${BASE}/downloader/jobs/${encodeURIComponent(jobId)}/artifact`;
109-
if (isIosDevice()) {
110-
const target = consumePreparedIosArtifactWindow();
111-
if (target) {
112-
try {
113-
const doc = target.document;
114-
doc.open();
115-
doc.write(
116-
"<!doctype html><html><head><title>Preparing download</title></head><body></body></html>",
117-
);
118-
doc.close();
119-
clickDownloadAnchor(doc, endpoint);
120-
return;
121-
} catch {
122-
target.location.assign(endpoint);
123-
return;
124-
}
125-
}
126-
try {
127-
clickDownloadAnchor(document, endpoint);
128-
return;
129-
} catch {
130-
window.location.assign(endpoint);
131-
return;
132-
}
133-
}
13472
if (isMobileDownloadDevice()) {
135-
clickDownloadAnchor(document, endpoint);
73+
window.location.assign(endpoint);
13674
return;
13775
}
13876
const res = await fetch(endpoint);

0 commit comments

Comments
 (0)