File size: 2,660 Bytes
eec101d
3c07134
 
 
 
 
eec101d
6ba0cb8
 
3c07134
 
eec101d
 
 
 
 
 
 
 
 
 
 
 
 
6ba0cb8
3c07134
eec101d
 
 
 
 
 
 
 
3c07134
eec101d
3c07134
eec101d
3c07134
 
eec101d
 
 
 
 
3c07134
eec101d
3c07134
 
eec101d
6ba0cb8
eec101d
 
6ba0cb8
eec101d
 
 
 
 
 
3c07134
eec101d
 
 
 
 
 
 
 
 
 
6ba0cb8
 
 
 
eec101d
 
 
 
 
3c07134
eec101d
3c07134
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { useMemo, useState } from "react";
import { t } from "../i18n";

interface Props {
  previewUrl: string | null | undefined;
  status: string;
  sessionId?: string;
  error?: string | null;
  logExcerpt?: string | null;
}

/** Resolve iframe src — always absolute same-origin /s/{id}/ with trailing slash. */
export function resolvePreviewSrc(
  previewUrl: string | null | undefined,
  sessionId?: string
): string | null {
  if (previewUrl && previewUrl.startsWith("/")) {
    const path = previewUrl.endsWith("/") ? previewUrl : `${previewUrl}/`;
    return path;
  }
  if (sessionId) return `/s/${sessionId}/`;
  return null;
}

export function PreviewFrame({ previewUrl, status, sessionId, error, logExcerpt }: Props) {
  const m = t();
  const [reloadKey, setReloadKey] = useState(0);
  const src = useMemo(
    () => resolvePreviewSrc(previewUrl, sessionId),
    [previewUrl, sessionId]
  );
  const running = status === "running" && !!src;

  if (running && src) {
    return (
      <div className="preview-stage live" data-testid="preview-stage">
        <iframe
          key={`${src}-${reloadKey}`}
          className="preview-frame"
          title="Repository live preview"
          src={src}
          data-preview-src={src}
          sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"
          referrerPolicy="no-referrer-when-downgrade"
          allow="clipboard-read; clipboard-write"
        />
      </div>
    );
  }

  const detail = (error || logExcerpt || "").trim();
  const title =
    status === "failed"
      ? detail || m.failedHint
      : status === "terminated"
        ? m.expired
        : status === "building" || status === "pending"
          ? m.waitingPreview
          : m.waitingPreview;

  return (
    <div className="preview-stage" data-testid="preview-stage">
      <div className="preview-placeholder">
        {(status === "building" || status === "pending") && <div className="spinner" aria-hidden />}
        <h3>
          {status === "failed"
            ? "Preview unavailable"
            : status === "terminated"
              ? "Session ended"
              : "Live preview SPACE"}
        </h3>
        <p>{status === "failed" && detail ? m.failedHint : title}</p>
        {status === "failed" && detail ? (
          <pre className="preview-error" data-testid="preview-error">{detail}</pre>
        ) : null}
        {status === "running" && !src ? (
          <button type="button" className="secondary" onClick={() => setReloadKey((k) => k + 1)}>
            Retry iframe
          </button>
        ) : null}
      </div>
    </div>
  );
}