2024-01-30 14:19:25 +00:00
|
|
|
'use client'
|
|
|
|
|
2024-02-12 14:30:55 +00:00
|
|
|
import { SandpackCodeViewer, SandpackFiles, SandpackProvider } from '@codesandbox/sandpack-react'
|
2024-01-30 14:19:25 +00:00
|
|
|
import { useTheme } from 'next-themes'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
|
|
|
export default function ExampleCodeBlock({
|
|
|
|
articleId,
|
|
|
|
files = {},
|
|
|
|
activeFile,
|
|
|
|
}: {
|
|
|
|
articleId: string
|
|
|
|
activeFile: string
|
|
|
|
files: SandpackFiles
|
|
|
|
}) {
|
|
|
|
const [isClientSide, setIsClientSide] = useState(false)
|
|
|
|
const { theme } = useTheme()
|
|
|
|
useEffect(() => setIsClientSide(true), [])
|
2024-07-12 13:07:07 +00:00
|
|
|
|
|
|
|
// This is to avoid hydration mismatch between the server and the client because of the useTheme.
|
|
|
|
if (!isClientSide) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:19:25 +00:00
|
|
|
const SERVER =
|
2024-07-10 09:07:16 +00:00
|
|
|
process.env.NODE_ENV === 'development'
|
2024-07-12 10:36:31 +00:00
|
|
|
? `http://${location.hostname}:5420`
|
2024-07-10 09:07:16 +00:00
|
|
|
: location.host.includes('staging') || location.host.includes('canary')
|
|
|
|
? `https://examples-canary.tldraw.com`
|
|
|
|
: 'https://examples.tldraw.com'
|
2024-01-30 14:19:25 +00:00
|
|
|
|
|
|
|
return (
|
2024-02-05 14:42:55 +00:00
|
|
|
<div className="code-example">
|
2024-07-12 13:07:07 +00:00
|
|
|
<iframe
|
|
|
|
src={`${SERVER}/${articleId}/full`}
|
|
|
|
allow={`clipboard-read; clipboard-write self ${SERVER}`}
|
|
|
|
/>
|
2024-01-30 14:19:25 +00:00
|
|
|
<SandpackProvider
|
2024-02-05 14:42:55 +00:00
|
|
|
className="sandpack"
|
2024-01-30 14:19:25 +00:00
|
|
|
key={`sandpack-${theme}-${activeFile}`}
|
|
|
|
template="react-ts"
|
|
|
|
options={{ activeFile }}
|
|
|
|
customSetup={{
|
|
|
|
dependencies: {
|
|
|
|
'@tldraw/assets': 'latest',
|
2024-02-29 16:06:19 +00:00
|
|
|
tldraw: 'latest',
|
2024-01-30 14:19:25 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
files={{
|
|
|
|
...files,
|
|
|
|
}}
|
|
|
|
theme={theme === 'dark' ? 'dark' : 'light'}
|
|
|
|
>
|
2024-02-12 14:30:55 +00:00
|
|
|
<SandpackCodeViewer />
|
2024-01-30 14:19:25 +00:00
|
|
|
</SandpackProvider>
|
2024-02-05 14:42:55 +00:00
|
|
|
</div>
|
2024-01-30 14:19:25 +00:00
|
|
|
)
|
|
|
|
}
|