recover from room avatar error on visibility change
This commit is contained in:
parent
138561f45b
commit
ba35672cba
1 changed files with 45 additions and 2 deletions
|
|
@ -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 (
|
||||
<AvatarFallback
|
||||
style={{ backgroundColor: colorMXID(roomId ?? ''), color: color.Surface.Container }}
|
||||
className={css.RoomAvatar}
|
||||
>
|
||||
{children}
|
||||
</AvatarFallback>
|
||||
);
|
||||
}
|
||||
|
||||
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 (
|
||||
<AvatarFallback
|
||||
style={{ backgroundColor: colorMXID(roomId ?? ''), color: color.Surface.Container }}
|
||||
|
|
@ -30,6 +65,14 @@ export function RoomAvatar({ roomId, src, alt, renderFallback }: RoomAvatarProps
|
|||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<RoomAvatarError roomId={roomId} setError={setError}>
|
||||
{renderFallback()}
|
||||
</RoomAvatarError>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<AvatarImage
|
||||
className={classNames(css.RoomAvatar, css.RoomAvatarImg)}
|
||||
|
|
|
|||
Loading…
Reference in a new issue