:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import './autocapture-preview-image.scss'
import posthog from 'posthog-js'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { ElementType } from '~/types'
interface AutocapturedImage {
src: string | undefined
width: string | undefined
height: string | undefined
}
function isBlobImage(img: AutocapturedImage): boolean {
return img.src?.startsWith('blob:') || false
}
function ensureNoTrailingSlash(origin: string): string {
if (origin.endsWith('/')) {
return origin.slice(0, -1)
}
return origin
}
function correctRelativeSrcImages(
img: AutocapturedImage | null,
properties?: Record<string, any>
): AutocapturedImage | null {
if (!img) {
return null
}
const isRelativePath = img.src?.startsWith('/') && !img.src?.startsWith('//')
const propertiesHasURL = !!properties?.['$current_url']
if (isRelativePath && propertiesHasURL) {
try {
const origin = new URL(properties['$current_url'])?.origin
return {
...img,
src: ensureNoTrailingSlash(origin) + img.src,
}
} catch (e) {
posthog.captureException(e, { imageSource: img.src, properties: properties || {} })
// don't show this image... something is unexpected about the URL
return null
}
}
return img
}
export function hasAutocaptureImage(elements: ElementType[] | undefined): boolean {
return !!elements?.find((el) => el.tag_name === 'img')?.attributes?.attr__src
}
export function autocaptureToImage(elements: ElementType[] | undefined): null | AutocapturedImage {
const find = elements?.find((el) => el.tag_name === 'img')
const image = {
src: find?.attributes?.attr__src,
width: find?.attributes?.attr__width,
height: find?.attributes?.attr__height,
}
return image.src ? image : null
}
function AutocaptureImage({ img }: { img: AutocapturedImage }): JSX.Element | null {
if (img && !isBlobImage(img)) {
return (
<div className="flex bg-primary items-center justify-center relative border-2">
{/* Transparent grid background */}
<div className="AutocaptureImage__grid-background absolute h-full w-full" />
<img
className="relative z-10 max-h-100 object-contain"
src={img.src}
alt="Autocapture image src"
height={img.height || 'auto'}
width={img.width || 'auto'}
/>
</div>
)
}
return null
}
export function AutocaptureImageTab({
elements,
properties,
}: {
elements: ElementType[]
properties?: Record<string, any>
}): JSX.Element | null {
const img = correctRelativeSrcImages(autocaptureToImage(elements), properties)
if (img) {
return (
<div className="flex bg-primary items-center justify-center relative border-2 w-full">
<AutocaptureImage img={img} />
</div>
)
}
return null
}
export function AutocapturePreviewImage({
elements,
properties,
imgPreviewHeight = '40',
}: {
elements: ElementType[]
properties?: Record<string, any>
imgPreviewHeight?: string
}): JSX.Element | null {
const img = correctRelativeSrcImages(autocaptureToImage(elements), properties)
if (img) {
return (
<Tooltip title={<AutocaptureImage img={img} />}>
<img
className="max-h-10"
src={img.src}
alt="Autocapture image src"
height={imgPreviewHeight}
width="auto"
/>
</Tooltip>
)
}
return null
}