docket-image: handle broken images

This commit is contained in:
Hunter Miller 2021-10-25 13:12:21 -05:00
parent 2ccfcd9d7f
commit f72da65014
2 changed files with 14 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { Docket } from '@urbit/api/docket';
import cn from 'classnames';
import { useTileColor } from '../tiles/useTileColor';
@ -19,13 +19,20 @@ const sizeMap: Record<DocketImageSizes, string> = {
export function DocketImage({ color, image, className = '', size = 'full' }: DocketImageProps) {
const { tileColor } = useTileColor(color);
const [imageError, setImageError] = useState(false);
return (
<div
className={cn('flex-none relative bg-gray-200 overflow-hidden', sizeMap[size], className)}
style={{ backgroundColor: tileColor }}
>
{image && (
<img className="absolute top-0 left-0 h-full w-full object-cover" src={image} alt="" />
{image && !imageError && (
<img
className="absolute top-0 left-0 h-full w-full object-cover"
src={image}
alt=""
onError={() => setImageError(true)}
/>
)}
</div>
);

View File

@ -199,6 +199,8 @@ const ClampedText = styled(Text)`
type AppTileProps = Treaty & BoxProps;
export function AppTile({ color, image, ...props }: AppTileProps) {
const [imageError, setImageError] = useState(false);
return (
<Box
position="relative"
@ -210,7 +212,7 @@ export function AppTile({ color, image, ...props }: AppTileProps) {
bg={color || 'washedGray'}
{...props}
>
{image && (
{image && !imageError && (
<Image
src={image}
position="absolute"
@ -218,6 +220,7 @@ export function AppTile({ color, image, ...props }: AppTileProps) {
left="0"
width="100%"
height="100%"
onError={() => setImageError(true)}
/>
)}
</Box>