Skip to content

Commit 2ef9493

Browse files
committed
feat: generalize image preview component to support both base64 and URL images.
1 parent c21717c commit 2ef9493

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

frontend/components/JsonDisplay.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ const ObjectIdDisplay: React.FC<{
201201
);
202202
};
203203

204-
const Base64ImagePreview: React.FC<{
205-
base64String: string;
204+
const ImagePreview: React.FC<{
205+
imageUrl: string;
206206
searchRegex: RegExp | null;
207-
}> = ({ base64String, searchRegex }) => {
207+
}> = ({ imageUrl, searchRegex }) => {
208208
const [isExpanded, setIsExpanded] = useState(false);
209209
const [previewPos, setPreviewPos] = useState<{ x: number; y: number } | null>(null);
210210
const iconRef = useRef<HTMLSpanElement>(null);
@@ -248,7 +248,7 @@ const Base64ImagePreview: React.FC<{
248248
title="Click to collapse back to icon"
249249
>
250250
<span className="text-emerald-700 dark:text-emerald-300">"
251-
{searchRegex ? highlightText(base64String, searchRegex) : base64String}"
251+
{searchRegex ? highlightText(imageUrl, searchRegex) : imageUrl}"
252252
</span>
253253
</span>
254254
);
@@ -281,7 +281,7 @@ const Base64ImagePreview: React.FC<{
281281
}}
282282
>
283283
<img
284-
src={base64String}
284+
src={imageUrl}
285285
alt="Preview"
286286
className="max-w-[200px] max-h-[200px] object-contain rounded bg-slate-100 dark:bg-slate-900"
287287
/>
@@ -475,8 +475,11 @@ const JsonNode: React.FC<{
475475
);
476476
}
477477

478-
if (nodeValue.startsWith("data:image/png;base64,")) {
479-
return <Base64ImagePreview base64String={nodeValue} searchRegex={searchRegex} />;
478+
const isBase64Image = nodeValue.startsWith("data:image/");
479+
const isUrlImage = /^https?:\/\/.+\.(png|jpe?g|gif|svg|webp)(\?.*)?$/i.test(nodeValue);
480+
481+
if (isBase64Image || isUrlImage) {
482+
return <ImagePreview imageUrl={nodeValue} searchRegex={searchRegex} />;
480483
}
481484

482485
return <span className="text-emerald-700 dark:text-emerald-300">"

0 commit comments

Comments
 (0)