Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/components/CircuitPreviewCdnCodeBlockExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { useLatestCircuitPreviewCdnUrl } from "../hooks/use-latest-circuit-previ
export default () => {
const url = useLatestCircuitPreviewCdnUrl()

if (!url) return null
if (!url) {
return (
<div className="skeleton-container">
<div style={{ textAlign: "center", padding: "20px" }}>
Loading circuit preview CDN URL...
</div>
</div>
)
}

return (
<CodeBlock language="html">{`<!DOCTYPE html>
Expand Down
10 changes: 9 additions & 1 deletion src/components/TscircuitCdnCodeBlockExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { useLatestTscircuitCdnUrl } from "../hooks/use-latest-tscircuit-cdn-url"
export default () => {
const url = useLatestTscircuitCdnUrl()

if (!url) return null
if (!url) {
return (
<div className="skeleton-container">
<div style={{ textAlign: "center", padding: "20px" }}>
Loading CDN URL...
</div>
</div>
)
}

return (
<CodeBlock language="html">{`<!DOCTYPE html>
Expand Down
10 changes: 9 additions & 1 deletion src/components/TscircuitCdnIframeExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { useLatestTscircuitCdnUrl } from "../hooks/use-latest-tscircuit-cdn-url"
export default () => {
const url = useLatestTscircuitCdnUrl()

if (!url) return null
if (!url) {
return (
<div className="skeleton-container">
<div style={{ textAlign: "center", padding: "20px" }}>
Loading CDN URL...
</div>
</div>
)
}

return (
<iframe
Expand Down
35 changes: 32 additions & 3 deletions src/components/TscircuitIframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface TscircuitIframeProps {
export const TscircuitIframe = (runFrameProps: TscircuitIframeProps) => {
const iframeRef = useRef<HTMLIFrameElement>(null)
const [isLoading, setIsLoading] = useState(true)
const [hasError, setHasError] = useState(false)

let additionalProps = {}

Expand Down Expand Up @@ -41,20 +42,38 @@ export const TscircuitIframe = (runFrameProps: TscircuitIframeProps) => {
}
}

// Set a timeout fallback to hide loading after 10 seconds if message never comes
const timeoutId = setTimeout(() => {
setIsLoading(false)
}, 10000)

window.addEventListener("message", handleMessage)
return () => window.removeEventListener("message", handleMessage)
return () => {
window.removeEventListener("message", handleMessage)
clearTimeout(timeoutId)
}
}, [])

return (
<div>
{isLoading && (
{isLoading && !hasError && (
<div className="skeleton-container">
<div>
<div className="skeleton-header" />
<div className="skeleton-body" />
</div>
</div>
)}
{hasError && (
<div className="skeleton-container">
<div style={{ textAlign: "center", padding: "20px" }}>
<p>Unable to load the interactive circuit preview.</p>
<p style={{ fontSize: "14px", opacity: 0.7 }}>
The circuit can still be viewed in the code editor above.
</p>
</div>
</div>
)}
<iframe
ref={iframeRef}
src="https://runframe.tscircuit.com/iframe.html"
Expand All @@ -69,11 +88,21 @@ export const TscircuitIframe = (runFrameProps: TscircuitIframeProps) => {
padding: 0,
margin: 0,
boxSizing: "border-box",
display: isLoading ? "none" : "block",
display: isLoading || hasError ? "none" : "block",
}}
onLoad={() => {
// The iframe is loaded, but we'll only hide the skeleton
// when we receive the "runframe_ready_to_receive" message
// Set a fallback timeout in case the message never comes
setTimeout(() => {
if (isLoading) {
setIsLoading(false)
}
}, 5000)
}}
onError={() => {
setHasError(true)
setIsLoading(false)
}}
/>
</div>
Expand Down
23 changes: 21 additions & 2 deletions src/hooks/use-latest-circuit-preview-cdn-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,34 @@ export const useLatestCircuitPreviewCdnUrl = () => {
const [url, setUrl] = useState<string | null>(null)

useEffect(() => {
// Set a timeout to provide a fallback URL if API call fails
const timeoutId = setTimeout(() => {
if (!url) {
// Fallback to a known working version
setUrl(
"https://unpkg.com/@tscircuit/circuit-preview@latest/dist/index.global.js",
)
}
}, 5000)

fetch("https://registry.npmjs.org/@tscircuit/circuit-preview/latest")
.then((res) => res.json())
.then((data: { version: string }) => {
clearTimeout(timeoutId)
setUrl(
`https://unpkg.com/@tscircuit/circuit-preview@${data.version}/dist/index.global.js`,
)
})
.catch(() => {})
}, [])
.catch(() => {
clearTimeout(timeoutId)
// Fallback to latest version on error
setUrl(
"https://unpkg.com/@tscircuit/circuit-preview@latest/dist/index.global.js",
)
})

return () => clearTimeout(timeoutId)
}, [url])

return url
}
23 changes: 21 additions & 2 deletions src/hooks/use-latest-tscircuit-cdn-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,34 @@ export const useLatestTscircuitCdnUrl = () => {
const [url, setUrl] = useState<string | null>(null)

useEffect(() => {
// Set a timeout to provide a fallback URL if API call fails
const timeoutId = setTimeout(() => {
if (!url) {
// Fallback to a known working version
setUrl(
"https://cdnjs.cloudflare.com/ajax/libs/tscircuit/1.0.0/browser.min.js",
)
}
}, 5000)

fetch("https://api.cdnjs.com/libraries/tscircuit?fields=version")
.then((res) => res.json())
.then((data) => {
clearTimeout(timeoutId)
setUrl(
`https://cdnjs.cloudflare.com/ajax/libs/tscircuit/${data.version}/browser.min.js`,
)
})
.catch(() => {})
}, [])
.catch(() => {
clearTimeout(timeoutId)
// Fallback to a known working version on error
setUrl(
"https://cdnjs.cloudflare.com/ajax/libs/tscircuit/1.0.0/browser.min.js",
)
})

return () => clearTimeout(timeoutId)
}, [url])

return url
}