Improve timeline scroll, media layout, avatars, and emoji coverage.
Remember room scroll position across switches, reserve image/video heights from Matrix dimensions, cache authenticated media blobs for avatars, restore jumbo emoji-only messages, and extend emoji font unicode-range for Unicode 15 glyphs.
This commit is contained in:
@@ -1,13 +1,23 @@
|
||||
import { JoinRule } from 'matrix-js-sdk';
|
||||
import { AvatarFallback, AvatarImage, color } from 'folds';
|
||||
import { Icon, Icons } from '../icons';
|
||||
import React, { ComponentProps, ReactEventHandler, ReactNode, forwardRef, useState } from 'react';
|
||||
import React, { ComponentProps, ReactEventHandler, ReactNode, forwardRef, useEffect, useState } from 'react';
|
||||
import { Blurhash } from 'react-blurhash';
|
||||
import * as css from './RoomAvatar.css';
|
||||
import { joinRuleToIconSrc } from '../../utils/room';
|
||||
import colorMXID from '../../../util/colorMXID';
|
||||
import { useAuthenticatedMediaUrl } from '../../hooks/useAuthenticatedMediaUrl';
|
||||
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
||||
import { cacheAvatar, isAvatarCached, pruneAvatarCache } from '../../utils/avatarCache';
|
||||
import {
|
||||
getCachedAuthenticatedMediaUrl,
|
||||
isAuthenticatedMediaUrl,
|
||||
} from '../../utils/authenticatedMediaCache';
|
||||
import {
|
||||
getMediaBlurHash,
|
||||
getMediaDimensions,
|
||||
rememberMediaBlurHash,
|
||||
} from '../../state/mediaDimensionCache';
|
||||
|
||||
type RoomAvatarProps = {
|
||||
roomId: string;
|
||||
@@ -17,19 +27,36 @@ type RoomAvatarProps = {
|
||||
};
|
||||
export function RoomAvatar({ roomId, src, alt, renderFallback }: RoomAvatarProps) {
|
||||
const [error, setError] = useState(false);
|
||||
const [loaded, setLoaded] = useState(() => isAvatarCached(src));
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
|
||||
const blurHash = getMediaBlurHash(src ?? '');
|
||||
const dims = getMediaDimensions(src ?? '');
|
||||
const blobCached =
|
||||
!!src &&
|
||||
(!useAuthentication ||
|
||||
!isAuthenticatedMediaUrl(src) ||
|
||||
!!getCachedAuthenticatedMediaUrl(src));
|
||||
const [loaded, setLoaded] = useState(() => isAvatarCached(src) || blobCached);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
authenticatedSrc &&
|
||||
(isAvatarCached(src) ||
|
||||
(src && (!useAuthentication || !isAuthenticatedMediaUrl(src) || getCachedAuthenticatedMediaUrl(src))))
|
||||
) {
|
||||
setLoaded(true);
|
||||
}
|
||||
}, [authenticatedSrc, src, useAuthentication]);
|
||||
|
||||
const handleLoad: ReactEventHandler<HTMLImageElement> = (evt) => {
|
||||
evt.currentTarget.setAttribute('data-image-loaded', 'true');
|
||||
setLoaded(true);
|
||||
cacheAvatar(src);
|
||||
pruneAvatarCache();
|
||||
rememberMediaBlurHash(src ?? '', evt.currentTarget);
|
||||
};
|
||||
|
||||
// No src or error - show fallback only
|
||||
if (!authenticatedSrc || error) {
|
||||
if (!src || error) {
|
||||
return (
|
||||
<AvatarFallback
|
||||
style={{ backgroundColor: colorMXID(roomId ?? ''), color: color.Surface.Container }}
|
||||
@@ -40,8 +67,7 @@ export function RoomAvatar({ roomId, src, alt, renderFallback }: RoomAvatarProps
|
||||
);
|
||||
}
|
||||
|
||||
// If already cached, show image directly without fallback flash
|
||||
if (loaded) {
|
||||
if (authenticatedSrc && loaded) {
|
||||
return (
|
||||
<AvatarImage
|
||||
className={css.RoomAvatar}
|
||||
@@ -55,12 +81,17 @@ export function RoomAvatar({ roomId, src, alt, renderFallback }: RoomAvatarProps
|
||||
);
|
||||
}
|
||||
|
||||
// Loading state - render fallback with image overlay
|
||||
const blurResolutionX = 32;
|
||||
const blurResolutionY =
|
||||
dims?.w && dims?.h && dims.w > 0
|
||||
? Math.max(1, Math.round(blurResolutionX * (dims.h / dims.w)))
|
||||
: 32;
|
||||
|
||||
return (
|
||||
<div style={{ position: 'relative', width: '100%', height: '100%' }}>
|
||||
<AvatarFallback
|
||||
style={{
|
||||
backgroundColor: colorMXID(roomId ?? ''),
|
||||
style={{
|
||||
backgroundColor: colorMXID(roomId ?? ''),
|
||||
color: color.Surface.Container,
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
@@ -69,15 +100,39 @@ export function RoomAvatar({ roomId, src, alt, renderFallback }: RoomAvatarProps
|
||||
>
|
||||
{renderFallback()}
|
||||
</AvatarFallback>
|
||||
<AvatarImage
|
||||
className={css.RoomAvatar}
|
||||
style={{ position: 'relative', zIndex: 1 }}
|
||||
src={authenticatedSrc}
|
||||
alt={alt}
|
||||
onError={() => setError(true)}
|
||||
onLoad={handleLoad}
|
||||
draggable={false}
|
||||
/>
|
||||
{blurHash && (
|
||||
<div
|
||||
style={{
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
overflow: 'hidden',
|
||||
borderRadius: 'inherit',
|
||||
filter: 'blur(6px)',
|
||||
transform: 'scale(1.08)',
|
||||
}}
|
||||
>
|
||||
<Blurhash
|
||||
hash={blurHash}
|
||||
width="100%"
|
||||
height="100%"
|
||||
resolutionX={blurResolutionX}
|
||||
resolutionY={blurResolutionY}
|
||||
punch={1.1}
|
||||
style={{ display: 'block' }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{authenticatedSrc && (
|
||||
<AvatarImage
|
||||
className={css.RoomAvatar}
|
||||
style={{ position: 'relative', zIndex: 1, opacity: loaded ? 1 : 0 }}
|
||||
src={authenticatedSrc}
|
||||
alt={alt}
|
||||
onError={() => setError(true)}
|
||||
onLoad={handleLoad}
|
||||
draggable={false}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user