"use client"; /* eslint-disable @next/next/no-img-element */ import Image from "next/image"; import type { MouseEventHandler, ReactNode } from "react"; import { useState } from "react"; import { isBrowserLocalChatImageSource, normalizeChatImageSource, } from "@/lib/chat/chat_media_url"; import { useCachedChatMediaUrl } from "@/lib/chat/use_cached_chat_media_url"; export interface ChatMediaImageProps { characterId: string; remoteMessageId?: string; remoteUrl: string; alt?: string; className?: string; nativeClassName?: string; errorClassName?: string; width?: number; height?: number; fill?: boolean; sizes?: string; showErrorFallback?: boolean; onClick?: MouseEventHandler; children?: ReactNode; } export function ChatMediaImage({ characterId, remoteMessageId, remoteUrl, alt = "", className, nativeClassName, errorClassName, width = 240, height = 240, fill = false, sizes, showErrorFallback = true, onClick, children, }: ChatMediaImageProps) { const [errorSrc, setErrorSrc] = useState(null); const { mediaUrl, isUsingCachedMedia, reportMediaError } = useCachedChatMediaUrl({ characterId, messageId: remoteMessageId, remoteUrl, kind: "image", }); const src = normalizeChatImageSource(mediaUrl || remoteUrl); const shouldUseNativeImage = isBrowserLocalChatImageSource(src); const hasError = errorSrc === src; const handleImageError = () => { if (isUsingCachedMedia) { reportMediaError(); return; } setErrorSrc(src); }; if (hasError && showErrorFallback) { return
🖼
; } return ( <> {shouldUseNativeImage ? ( {alt} ) : fill ? ( {alt} ) : ( {alt} )} {hasError ? null : children} ); }