Table: Add some error-case handling to ImageCell (#110461)

This commit is contained in:
Paul Marbach 2025-10-07 11:13:51 -04:00 committed by GitHub
parent 7d1c6b6bd2
commit 6f70cf5e00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 1 deletions

View File

@ -1,17 +1,23 @@
import { css } from '@emotion/css';
import { useState } from 'react';
import { TableCellDisplayMode } from '../../types';
import { MaybeWrapWithLink } from '../components/MaybeWrapWithLink';
import { ImageCellProps, TableCellStyles } from '../types';
export const ImageCell = ({ cellOptions, field, value, rowIdx }: ImageCellProps) => {
const [error, setError] = useState(false);
const { text } = field.display!(value);
const { alt, title } =
cellOptions.type === TableCellDisplayMode.Image ? cellOptions : { alt: undefined, title: undefined };
if (!text) {
return null;
}
return (
<MaybeWrapWithLink field={field} rowIdx={rowIdx}>
<img alt={alt} src={text} title={title} />
{error ? text : <img alt={alt} src={text} title={title} onError={() => setError(true)} />}
</MaybeWrapWithLink>
);
};