/** * ContentImageCell Component * Displays image prompt, placeholder, or actual image based on status */ import React, { useState } from 'react'; import Badge from '../ui/badge/Badge'; export interface ContentImageData { id?: number; image_type?: string; image_url?: string | null; image_path?: string | null; prompt?: string | null; status: string; position?: number; created_at?: string; updated_at?: string; } interface ContentImageCellProps { image: ContentImageData | null; maxPromptLength?: number; } export default function ContentImageCell({ image, maxPromptLength = 100 }: ContentImageCellProps) { const [showFullPrompt, setShowFullPrompt] = useState(false); // Check if image_path is a valid local file path (not a URL) const isValidLocalPath = (imagePath: string): boolean => { // Reject URLs (http:// or https://) if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) { return false; } // Must contain 'ai-images' to be a valid local path return imagePath.includes('ai-images'); }; // Convert local file path to web-accessible URL const getLocalImageUrl = (imagePath: string): string => { // If path contains 'ai-images', convert to web URL if (imagePath.includes('ai-images')) { const filename = imagePath.split('ai-images/')[1] || imagePath.split('ai-images\\')[1]; if (filename) { return `/images/ai-images/${filename}`; } } // If path is already a web path, return as-is if (imagePath.startsWith('/images/')) { return imagePath; } // Otherwise, try to extract filename and use ai-images path const filename = imagePath.split('/').pop() || imagePath.split('\\').pop(); return `/images/ai-images/${filename}`; }; if (!image) { return (
-
); } const prompt = image.prompt || ''; const shouldTruncate = prompt.length > maxPromptLength; const displayPrompt = showFullPrompt || !shouldTruncate ? prompt : `${prompt.substring(0, maxPromptLength)}...`; return (
{/* Prompt Text */} {prompt && (

{displayPrompt} {shouldTruncate && ( )}

)} {/* Image Display */}
{image.status === 'pending' && (

Pending

)} {image.status === 'generated' && (
{/* Only use image_path if it's a valid local file path (not a URL) */} {image.image_path && image.image_path.trim() && isValidLocalPath(image.image_path) ? ( <> {prompt { // Show empty placeholder if local image fails to load const target = e.target as HTMLImageElement; target.style.display = 'none'; const parent = target.parentElement; if (parent) { parent.innerHTML = `

Image not available

`; } }} /> {/* Always show "View Original" button if image_url is available */} {image.image_url && ( View Original )} {/* Display database field values */}
class Images(SiteSectorBaseModel):
image_url = {image.image_url || null}
image_path = {image.image_path || null}
) : ( <>

No image available

{/* Display database field values even when no image available */}
class Images(SiteSectorBaseModel):
image_url = {image.image_url || null}
image_path = {image.image_path || null}
)}
)} {image.status === 'failed' && (

Failed

)} {/* Status Badge */}
{image.status}
); }