diff --git a/src/app/components/room-avatar/RoomAvatar.tsx b/src/app/components/room-avatar/RoomAvatar.tsx index d586a578..51b51d86 100644 --- a/src/app/components/room-avatar/RoomAvatar.tsx +++ b/src/app/components/room-avatar/RoomAvatar.tsx @@ -1,10 +1,45 @@ import { AvatarFallback, AvatarImage, Icon, Icons, color } from 'folds'; -import React, { ComponentProps, ReactEventHandler, ReactNode, forwardRef, useState } from 'react'; +import React, { + ComponentProps, + ReactEventHandler, + ReactNode, + forwardRef, + useCallback, + useState, +} from 'react'; import classNames from 'classnames'; import * as css from './RoomAvatar.css'; import { getRoomIconSrc } from '../../utils/room'; import colorMXID from '../../../util/colorMXID'; import { JoinRule } from '../../../types/matrix/room'; +import { useDocumentVisibilityChange } from '../../hooks/useDocumentVisibilityChange'; +import { useDocumentFocusChange } from '../../hooks/useDocumentFocusChange'; + +type RoomAvatarErrorProps = { + roomId: string; + setError: (error: boolean) => void; + children: ReactNode; +}; +function RoomAvatarError({ roomId, setError, children }: RoomAvatarErrorProps) { + const handleVisibilityChange = useCallback( + (visible: boolean) => { + if (visible) setError(false); + }, + [setError] + ); + + useDocumentVisibilityChange(handleVisibilityChange); + useDocumentFocusChange(handleVisibilityChange); + + return ( + + {children} + + ); +} type RoomAvatarProps = { roomId: string; @@ -19,7 +54,7 @@ export function RoomAvatar({ roomId, src, alt, renderFallback }: RoomAvatarProps evt.currentTarget.setAttribute('data-image-loaded', 'true'); }; - if (!src || error) { + if (!src) { return ( + {renderFallback()} + + ); + } + return (