Skip to content

Commit

Permalink
fix: reset of error on thumbnails-image (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
malmen237 authored Nov 1, 2024
1 parent f57791a commit df7c9aa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
28 changes: 12 additions & 16 deletions src/components/image/ImageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {
PropsWithChildren,
SyntheticEvent,
useContext,
useEffect,
useRef,
Expand All @@ -12,41 +13,34 @@ import { IconExclamationCircle } from '@tabler/icons-react';
import { Loader } from '../loader/Loader';
import { GlobalContext } from '../../contexts/GlobalContext';
import { Type } from '../../interfaces/Source';

interface ImageComponentProps extends PropsWithChildren {
src?: string;
alt?: string;
type?: Type;
isStatusGone?: boolean;
}

const ImageComponent: React.FC<ImageComponentProps> = (props) => {
const { src, alt = 'Image', children, type, isStatusGone } = props;
const { src, alt = 'Image', children, type } = props;
const { imageRefetchKey } = useContext(GlobalContext);
const [imgSrc, setImgSrc] = useState<string>();
const [loaded, setLoaded] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<SyntheticEvent<HTMLImageElement, Event>>();
const timeout = useRef<ReturnType<typeof setTimeout>>();

const refetchImage = () => {
setImgSrc(`${src}?${imageRefetchKey}`);
setError(undefined);
setLoading(true);
clearTimeout(timeout.current);
timeout.current = setTimeout(() => setLoading(false), 500);
};

useEffect(() => {
refetchImage();
}, [imageRefetchKey]);

useEffect(() => {
if (isStatusGone) {
setLoading(false);
setLoaded(true);
}
}, [isStatusGone]);

useEffect(() => {
setError(undefined);
setImgSrc(`${src}?${imageRefetchKey}`);
}, [src]);

Expand All @@ -55,12 +49,11 @@ const ImageComponent: React.FC<ImageComponentProps> = (props) => {
clearTimeout(timeout.current);
};
}, []);

return (
<>
{(!type || type === 'ingest_source') && src && (
<div className="relative z-10 aspect-video min-w-full overflow-hidden border rounded-lg bg-zinc-700">
{((!imgSrc || isStatusGone) && (
{((!imgSrc || error) && (
<IconExclamationCircle className="text-error fill-white w-full h-full" />
)) || (
<>
Expand All @@ -71,10 +64,14 @@ const ImageComponent: React.FC<ImageComponentProps> = (props) => {
}`}
src={imgSrc!}
onLoad={() => {
isStatusGone ? '' : setLoaded(false);
setError(undefined);
setLoaded(false);
}}
onLoadingComplete={() => {
isStatusGone ? '' : setLoaded(true);
setLoaded(true);
}}
onError={(err) => {
setError(err);
}}
placeholder="empty"
width={0}
Expand Down Expand Up @@ -109,5 +106,4 @@ const ImageComponent: React.FC<ImageComponentProps> = (props) => {
</>
);
};

export default ImageComponent;
5 changes: 1 addition & 4 deletions src/components/inventory/editView/EditView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export default function EditView({
<EditViewContext source={source} updateSource={updateSource}>
<div className="flex flex-row mb-10">
<div className="relative w-[34rem]">
<ImageComponent
src={getSourceThumbnail(source)}
isStatusGone={source.status === 'gone'}
/>
<ImageComponent src={getSourceThumbnail(source)} />
</div>
<GeneralSettings locked={locked} />
</div>
Expand Down
7 changes: 1 addition & 6 deletions src/components/sourceCard/SourceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ export default function SourceCard({
disabled={locked}
/>
</div>
{source && (
<ImageComponent
src={getSourceThumbnail(source)}
isStatusGone={source.status === 'gone'}
/>
)}
{source && <ImageComponent src={getSourceThumbnail(source)} />}
{!source && sourceRef && <ImageComponent type={sourceRef.type} />}
{(source || sourceRef) && (
<h2
Expand Down
1 change: 0 additions & 1 deletion src/components/sourceListItem/SourceListItemThumbnail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const SourceListItemThumbnail = (props: SourceThumbnailProps) => {
{/* TODO perhaps add alts to translations */}
<ImageComponent
src={getSourceThumbnail(source)}
isStatusGone={source.status === 'gone'}
alt="Source List Thumbnail"
>
<div className="absolute top-4 left-4">{getIcon(source)}</div>
Expand Down

0 comments on commit df7c9aa

Please sign in to comment.