Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
import { memo, useMemo, useState } from 'react'
import { useTranslation } from '@/i18n/react-i18next-compat'
import { CodeIcon, EyeIcon, TriangleAlert } from 'lucide-react'
import { cn } from '@/lib/utils'
import { CodeBlock } from '@/components/ai-elements/code-block'
import { countUnresolvedAssetRefs } from '@/lib/htmlAssets'
import { buildSrcDoc } from '@/lib/htmlSandbox'
import type { BundledLanguage } from 'shiki'
interface HtmlArtifactProps {
code: string
className?: string
// While streaming, Preview is disabled and the toggle stays on Code.
isStreaming?: boolean
// Off by default so model HTML has no network access; on relaxes CSP to https:.
allowNetwork?: boolean
// Off (SVG static mode) forbids scripts via CSP and drops iframe allow-scripts.
allowScripts?: boolean
language?: string
}
type View = 'code' | 'preview'
// Strict CSP governs what model HTML can reach out to; the iframe is already
// sandboxed to an opaque origin (no allow-same-origin).
function HtmlArtifactComponent({
code,
className,
isStreaming,
allowNetwork = false,
allowScripts = true,
language = 'html',
}: HtmlArtifactProps) {
const { t } = useTranslation()
const [view, setView] = useState<View>('preview')
// Relative refs (`./logo.png`, `style.css`) cannot resolve inside the
// opaque-origin sandbox; surface the count instead of a silently broken page.
const unresolvedAssets = useMemo(
() => (view === 'preview' ? countUnresolvedAssetRefs(code) : 0),
[view, code]
)
const srcDoc = useMemo(
() =>
view === 'preview' ? buildSrcDoc(code, allowNetwork, allowScripts) : '',
[view, code, allowNetwork, allowScripts]
)
const previewDisabled = isStreaming ?? false
const activeView: View = previewDisabled ? 'code' : view
const tabClass = (active: boolean) =>
cn(
'flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium transition-colors',
active
? 'text-foreground border-b-2 border-primary'
: 'text-muted-foreground hover:text-foreground border-b-2 border-transparent'
)
return (
<div
className={cn(
'my-4 overflow-hidden rounded-xl border border-border bg-background',
className
)}
data-testid="html-artifact"
>
<div
className="flex items-center gap-1 border-b border-border px-2"
role="tablist"
>
<button
type="button"
role="tab"
aria-selected={activeView === 'code'}
className={tabClass(activeView === 'code')}
onClick={() => setView('code')}
>
<CodeIcon size={14} />
{t('htmlArtifact.code')}
</button>
<button
type="button"
role="tab"
aria-selected={activeView === 'preview'}
disabled={previewDisabled}
title={
previewDisabled ? t('htmlArtifact.previewStreaming') : undefined
}
className={cn(
tabClass(activeView === 'preview'),
previewDisabled && 'cursor-not-allowed opacity-50'
)}
onClick={() => setView('preview')}
>
<EyeIcon size={14} />
{t('htmlArtifact.preview')}
</button>
</div>
{activeView === 'preview' ? (
<>
{unresolvedAssets > 0 && (
<div
role="note"
data-testid="html-artifact-unresolved"
className="flex items-start gap-2 border-b border-border bg-amber-500/10 px-3 py-2 text-xs text-amber-600 dark:text-amber-400"
>
<TriangleAlert size={14} className="mt-0.5 shrink-0" />
<span>{t('htmlArtifact.unresolvedAssets', { count: unresolvedAssets })}</span>
</div>
)}
<iframe
title={t('htmlArtifact.preview')}
data-testid="html-artifact-iframe"
className="h-[600px] max-h-[80vh] min-h-64 w-full resize-y overflow-auto border-0 bg-white"
sandbox={allowScripts ? 'allow-scripts' : ''}
referrerPolicy="no-referrer"
srcDoc={srcDoc}
/>
</>
) : (
<CodeBlock code={code} language={language as BundledLanguage} />
)}
</div>
)
}
export const HtmlArtifact = memo(HtmlArtifactComponent)