Skip to content

Commit

Permalink
feat(receive): dont show broken icons (p2p-org#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
beautyfree authored Mar 2, 2022
1 parent 0fd6eab commit 4e1fa2e
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/web/src/components/ui/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FunctionComponent, HTMLAttributes } from 'react';
import { useState } from 'react';

import { styled } from '@linaria/react';

Expand All @@ -14,6 +15,8 @@ const Wrapper = styled.img<{ size: string | number | undefined }>`
}
`;

const BAD_SRCS: { [src: string]: true } = {};

type Props = {
src?: string;
size?: string | number;
Expand All @@ -24,5 +27,21 @@ export const Avatar: FunctionComponent<Props & HTMLAttributes<HTMLDivElement>> =
size,
...props
}) => {
return <Wrapper src={src} size={size} {...props} />;
const [, refresh] = useState<number>(0);

const _src: string | undefined = src && !BAD_SRCS[src] ? src : undefined;

return (
<Wrapper
src={_src}
size={size}
{...props}
onError={() => {
if (src) {
BAD_SRCS[src] = true;
}
refresh((i) => i + 1);
}}
/>
);
};

0 comments on commit 4e1fa2e

Please sign in to comment.