Spaces:
Paused
Paused
Download frontend/src/components/PreviewFrame.tsx from 2Btainment/repospace: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/spaces/2Btainment/repospace/resolve/main/frontend/src/components/PreviewFrame.tsx
- Command line
-
hf download hf://spaces/2Btainment/repospace/frontend/src/components/PreviewFrame.tsx
-
curl -L -o PreviewFrame.tsx https://huggingface.co/spaces/2Btainment/repospace/resolve/main/frontend/src/components/PreviewFrame.tsx
2.66 kB
| 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> | |
| ); | |
| } | |