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

View File

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