mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-30 00:53:14 +00:00
Merge pull request #2004 from Lanthoiba2022/AgentImageFix1
Fix #1983: Agent image fallback added
This commit is contained in:
40
frontend/src/components/AgentImage.tsx
Normal file
40
frontend/src/components/AgentImage.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import Robot from '../assets/robot.svg';
|
||||
|
||||
type AgentImageProps = {
|
||||
src?: string | null;
|
||||
alt?: string;
|
||||
className?: string;
|
||||
fallbackSrc?: string;
|
||||
};
|
||||
|
||||
export default function AgentImage({
|
||||
src,
|
||||
alt = 'agent',
|
||||
className = '',
|
||||
fallbackSrc = Robot,
|
||||
}: AgentImageProps) {
|
||||
const [currentSrc, setCurrentSrc] = useState(
|
||||
src && src.trim() !== '' ? src : fallbackSrc,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const newSrc = src && src.trim() !== '' ? src : fallbackSrc;
|
||||
if (newSrc !== currentSrc) {
|
||||
setCurrentSrc(newSrc);
|
||||
}
|
||||
}, [src, fallbackSrc]);
|
||||
|
||||
return (
|
||||
<img
|
||||
src={currentSrc}
|
||||
alt={alt}
|
||||
className={className}
|
||||
referrerPolicy="no-referrer"
|
||||
crossOrigin="anonymous"
|
||||
onError={() => {
|
||||
if (currentSrc !== fallbackSrc) setCurrentSrc(fallbackSrc);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user