Files
opencode-manager/frontend/src/components/ui/inline-image.tsx
T
Chris Scott be93975d90 feat(markdown): render inline images in messages and markdown files
Route markdown image nodes through a shared MarkdownImage component that
loads local paths through the raw file API, and allow data:image/* sources
through the sanitizer and URL transform so inline images survive rendering.

Render image attachments as thumbnails in the message thread, and accept
image/webp uploads.
2026-10-13 00:29:00 +00:00

45 lines
1.4 KiB
TypeScript

import { useState } from 'react'
import { cn } from '@/lib/utils'
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'
/**
* Renders an image as a bounded thumbnail button that opens a full-size preview dialog.
* Falls back to a muted alt-text span (or nothing) when the image fails to load.
*/
export function InlineImage({ src, alt, className }: { src: string; alt?: string; className?: string }) {
const [open, setOpen] = useState(false)
const [failed, setFailed] = useState(false)
if (failed) {
return alt ? <span className="text-xs text-muted-foreground">{alt}</span> : null
}
return (
<>
<button
type="button"
aria-label={alt ? `Expand image: ${alt}` : 'Expand image'}
onClick={() => setOpen(true)}
className="inline-flex"
>
<img
src={src}
alt={alt ?? ''}
loading="lazy"
onError={() => setFailed(true)}
className={cn(
'max-w-full max-h-80 rounded-lg border border-border object-contain cursor-zoom-in',
className,
)}
/>
</button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="w-auto max-w-none p-2">
<DialogTitle className="sr-only">{alt ?? 'Image'}</DialogTitle>
<img src={src} alt={alt ?? ''} className="max-w-[95vw] max-h-[90vh] object-contain" />
</DialogContent>
</Dialog>
</>
)
}