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:
@@ -28,6 +28,7 @@ import {
|
||||
} from '../../../types/matrix/common';
|
||||
import { FALLBACK_MIMETYPE, getBlobSafeMimeType } from '../../utils/mimeTypes';
|
||||
import { parseGeoUri, scaleYDimension } from '../../utils/common';
|
||||
import { resolveAttachmentBoxSize } from '../../state/mediaDimensionCache';
|
||||
import { Attachment, AttachmentBox, AttachmentContent, AttachmentHeader } from './attachment';
|
||||
import { FileHeader, FileDownloadButton } from './FileHeader';
|
||||
|
||||
@@ -199,6 +200,13 @@ type MImageProps = {
|
||||
renderImageContent: (props: RenderImageContentProps) => ReactNode;
|
||||
outlined?: boolean;
|
||||
};
|
||||
|
||||
const resolveMediaBoxSize = (
|
||||
mxcUrl: string,
|
||||
w?: number,
|
||||
h?: number
|
||||
): { width: number; height: number } => resolveAttachmentBoxSize(mxcUrl, w, h);
|
||||
|
||||
export function MImage({ content, renderImageContent, outlined }: MImageProps) {
|
||||
const imgInfo = content?.info;
|
||||
const mxcUrl = content.file?.url ?? content.url;
|
||||
@@ -206,9 +214,14 @@ export function MImage({ content, renderImageContent, outlined }: MImageProps) {
|
||||
return <BrokenContent />;
|
||||
}
|
||||
|
||||
const { width, height } = resolveMediaBoxSize(mxcUrl, imgInfo?.w, imgInfo?.h);
|
||||
|
||||
return (
|
||||
<Attachment outlined={outlined} transparent>
|
||||
<AttachmentBox>
|
||||
<AttachmentBox
|
||||
style={{ width: toRem(width), height: toRem(height) }}
|
||||
data-paarrot-media-height={height}
|
||||
>
|
||||
{renderImageContent({
|
||||
body: content.body || 'Image',
|
||||
info: imgInfo,
|
||||
@@ -251,6 +264,11 @@ export function MVideo({ content, renderAsFile, renderVideoContent, outlined }:
|
||||
}
|
||||
|
||||
const filename = content.filename ?? content.body ?? 'Video';
|
||||
const { width, height } = resolveMediaBoxSize(
|
||||
mxcUrl,
|
||||
videoInfo.w ?? videoInfo.thumbnail_info?.w,
|
||||
videoInfo.h ?? videoInfo.thumbnail_info?.h
|
||||
);
|
||||
|
||||
return (
|
||||
<Attachment outlined={outlined} transparent>
|
||||
@@ -268,7 +286,10 @@ export function MVideo({ content, renderAsFile, renderVideoContent, outlined }:
|
||||
}
|
||||
/>
|
||||
</AttachmentHeader>
|
||||
<AttachmentBox>
|
||||
<AttachmentBox
|
||||
style={{ width: toRem(width), height: toRem(height) }}
|
||||
data-paarrot-media-height={height}
|
||||
>
|
||||
{renderVideoContent({
|
||||
body: content.body || 'Video',
|
||||
info: videoInfo,
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { ReactNode, useCallback, useEffect, useState } from 'react';
|
||||
import { Badge, Box, Button, Chip, Modal, Overlay, OverlayBackdrop, OverlayCenter, Spinner, Text, Tooltip, TooltipProvider, as } from 'folds';
|
||||
import { Icon, Icons } from '../../icons';
|
||||
import classNames from 'classnames';
|
||||
import { BlurhashCanvas } from 'react-blurhash';
|
||||
import { Blurhash } from 'react-blurhash';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { EncryptedAttachmentInfo } from 'browser-encrypt-attachment';
|
||||
import { IImageInfo, MATRIX_BLUR_HASH_PROPERTY_NAME } from '../../../../types/matrix/common';
|
||||
@@ -17,6 +17,7 @@ import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
|
||||
import { ModalWide } from '../../../styles/Modal.css';
|
||||
import { validBlurHash } from '../../../utils/blurHash';
|
||||
import { getCurrentAccessToken } from '../../../utils/auth';
|
||||
import { setMediaDimensions, getMediaBlurHash, getMediaDimensions, rememberMediaBlurHash } from '../../../state/mediaDimensionCache';
|
||||
|
||||
/**
|
||||
* Fetches media with authentication headers and returns a blob URL.
|
||||
@@ -68,7 +69,7 @@ type RenderImageProps = {
|
||||
alt: string;
|
||||
title: string;
|
||||
src: string;
|
||||
onLoad: () => void;
|
||||
onLoad: (event?: React.SyntheticEvent<HTMLImageElement>) => void;
|
||||
onError: () => void;
|
||||
onClick: () => void;
|
||||
tabIndex: number;
|
||||
@@ -105,12 +106,23 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
) => {
|
||||
const mx = useMatrixClient();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const blurHash = validBlurHash(info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]);
|
||||
const eventBlurHash = validBlurHash(info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]);
|
||||
const cachedBlurHash = getMediaBlurHash(url);
|
||||
const blurHash = eventBlurHash ?? cachedBlurHash;
|
||||
const cachedDims = getMediaDimensions(url);
|
||||
const blurResolutionX = 32;
|
||||
const ratioW = info?.w || cachedDims?.w;
|
||||
const ratioH = info?.h || cachedDims?.h;
|
||||
const blurResolutionY =
|
||||
ratioW && ratioH && ratioW > 0
|
||||
? Math.max(1, Math.round(blurResolutionX * (ratioH / ratioW)))
|
||||
: 32;
|
||||
|
||||
const [load, setLoad] = useState(false);
|
||||
const [error, setError] = useState(false);
|
||||
const [viewer, setViewer] = useState(false);
|
||||
const [blurred, setBlurred] = useState(markedAsSpoiler ?? false);
|
||||
const [showPlaceholder, setShowPlaceholder] = useState(true);
|
||||
|
||||
const [srcState, loadSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
@@ -139,6 +151,7 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
const handleError = () => {
|
||||
setLoad(false);
|
||||
setError(true);
|
||||
setShowPlaceholder(true);
|
||||
};
|
||||
|
||||
const handleRetry = () => {
|
||||
@@ -150,8 +163,34 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
if (autoPlay) loadSrc();
|
||||
}, [autoPlay, loadSrc]);
|
||||
|
||||
// Keep blurhash under the fade, then remove it once the image is fully opaque.
|
||||
useEffect(() => {
|
||||
if (!load) {
|
||||
setShowPlaceholder(true);
|
||||
return undefined;
|
||||
}
|
||||
const timer = window.setTimeout(() => setShowPlaceholder(false), 540);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [load]);
|
||||
|
||||
return (
|
||||
<Box className={classNames(css.RelativeBase, className)} {...props} ref={ref}>
|
||||
{showPlaceholder &&
|
||||
(typeof blurHash === 'string' ? (
|
||||
<div className={css.BlurhashPlaceholder}>
|
||||
<Blurhash
|
||||
hash={blurHash}
|
||||
width="100%"
|
||||
height="100%"
|
||||
resolutionX={blurResolutionX}
|
||||
resolutionY={blurResolutionY}
|
||||
punch={1.2}
|
||||
style={{ display: 'block' }}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className={css.MediaSkeleton} />
|
||||
))}
|
||||
{srcState.status === AsyncStatus.Success && (
|
||||
<Overlay open={viewer} backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
@@ -177,15 +216,6 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
</OverlayCenter>
|
||||
</Overlay>
|
||||
)}
|
||||
{typeof blurHash === 'string' && !load && (
|
||||
<BlurhashCanvas
|
||||
style={{ width: '100%', height: '100%' }}
|
||||
width={32}
|
||||
height={32}
|
||||
hash={blurHash}
|
||||
punch={1}
|
||||
/>
|
||||
)}
|
||||
{!autoPlay && !markedAsSpoiler && srcState.status === AsyncStatus.Idle && (
|
||||
<Box className={css.AbsoluteContainer} alignItems="Center" justifyContent="Center">
|
||||
<Button
|
||||
@@ -201,12 +231,26 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
</Box>
|
||||
)}
|
||||
{srcState.status === AsyncStatus.Success && (
|
||||
<Box className={classNames(css.AbsoluteContainer, blurred && css.Blur)}>
|
||||
<Box
|
||||
className={classNames(
|
||||
css.AbsoluteContainer,
|
||||
css.MediaFadeIn,
|
||||
load && css.MediaFadeInLoaded,
|
||||
blurred && css.Blur
|
||||
)}
|
||||
>
|
||||
{renderImage({
|
||||
alt: body,
|
||||
title: body,
|
||||
src: srcState.data,
|
||||
onLoad: handleLoad,
|
||||
onLoad: (e?: React.SyntheticEvent<HTMLImageElement>) => {
|
||||
const img = e?.currentTarget;
|
||||
if (img?.naturalWidth && img?.naturalHeight) {
|
||||
setMediaDimensions(url, img.naturalWidth, img.naturalHeight);
|
||||
}
|
||||
if (img) rememberMediaBlurHash(url, img);
|
||||
handleLoad();
|
||||
},
|
||||
onError: handleError,
|
||||
onClick: () => setViewer(true),
|
||||
tabIndex: 0,
|
||||
@@ -248,7 +292,8 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||
)}
|
||||
{(srcState.status === AsyncStatus.Loading || srcState.status === AsyncStatus.Success) &&
|
||||
!load &&
|
||||
!blurred && (
|
||||
!blurred &&
|
||||
typeof blurHash !== 'string' && (
|
||||
<Box className={css.AbsoluteContainer} alignItems="Center" justifyContent="Center">
|
||||
<Spinner variant="Secondary" />
|
||||
</Box>
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { ReactNode, useCallback, useEffect, useState } from 'react';
|
||||
import { Badge, Box, Button, Chip, Modal, Overlay, OverlayBackdrop, OverlayCenter, Spinner, Text, Tooltip, TooltipProvider, as } from 'folds';
|
||||
import { Icon, Icons } from '../../icons';
|
||||
import classNames from 'classnames';
|
||||
import { BlurhashCanvas } from 'react-blurhash';
|
||||
import { Blurhash } from 'react-blurhash';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { EncryptedAttachmentInfo } from 'browser-encrypt-attachment';
|
||||
import {
|
||||
@@ -25,6 +25,7 @@ import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
|
||||
import { validBlurHash } from '../../../utils/blurHash';
|
||||
import { ModalWide } from '../../../styles/Modal.css';
|
||||
import { stopPropagation } from '../../../utils/keyboard';
|
||||
import { setMediaDimensions, getMediaBlurHash, getMediaDimensions, rememberMediaBlurHash } from '../../../state/mediaDimensionCache';
|
||||
|
||||
type RenderViewerProps = {
|
||||
src: string;
|
||||
@@ -34,7 +35,7 @@ type RenderViewerProps = {
|
||||
type RenderVideoProps = {
|
||||
title: string;
|
||||
src: string;
|
||||
onLoadedMetadata: () => void;
|
||||
onLoadedMetadata: (event?: React.SyntheticEvent<HTMLVideoElement>) => void;
|
||||
onError: () => void;
|
||||
autoPlay: boolean;
|
||||
controls: boolean;
|
||||
@@ -74,12 +75,23 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
) => {
|
||||
const mx = useMatrixClient();
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const blurHash = validBlurHash(info.thumbnail_info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]);
|
||||
const eventBlurHash = validBlurHash(info.thumbnail_info?.[MATRIX_BLUR_HASH_PROPERTY_NAME]);
|
||||
const cachedBlurHash = getMediaBlurHash(url);
|
||||
const blurHash = eventBlurHash ?? cachedBlurHash;
|
||||
const cachedDims = getMediaDimensions(url);
|
||||
const blurResolutionX = 32;
|
||||
const blurW = info.w ?? info.thumbnail_info?.w ?? cachedDims?.w;
|
||||
const blurH = info.h ?? info.thumbnail_info?.h ?? cachedDims?.h;
|
||||
const blurResolutionY =
|
||||
blurW && blurH && blurW > 0
|
||||
? Math.max(1, Math.round(blurResolutionX * (blurH / blurW)))
|
||||
: 32;
|
||||
|
||||
const [load, setLoad] = useState(false);
|
||||
const [error, setError] = useState(false);
|
||||
const [viewer, setViewer] = useState(false);
|
||||
const [blurred, setBlurred] = useState(markedAsSpoiler ?? false);
|
||||
const [showPlaceholder, setShowPlaceholder] = useState(true);
|
||||
|
||||
const [srcState, loadSrc] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
@@ -95,12 +107,17 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
}, [mx, url, useAuthentication, mimeType, encInfo])
|
||||
);
|
||||
|
||||
const handleLoad = () => {
|
||||
const handleLoad = (video?: HTMLVideoElement) => {
|
||||
if (video?.videoWidth && video?.videoHeight) {
|
||||
setMediaDimensions(url, video.videoWidth, video.videoHeight);
|
||||
}
|
||||
if (video) rememberMediaBlurHash(url, video);
|
||||
setLoad(true);
|
||||
};
|
||||
const handleError = () => {
|
||||
setLoad(false);
|
||||
setError(true);
|
||||
setShowPlaceholder(true);
|
||||
};
|
||||
|
||||
const handleRetry = () => {
|
||||
@@ -112,8 +129,33 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
if (autoPlay) loadSrc();
|
||||
}, [autoPlay, loadSrc]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!load) {
|
||||
setShowPlaceholder(true);
|
||||
return undefined;
|
||||
}
|
||||
const timer = window.setTimeout(() => setShowPlaceholder(false), 540);
|
||||
return () => window.clearTimeout(timer);
|
||||
}, [load]);
|
||||
|
||||
return (
|
||||
<Box className={classNames(css.RelativeBase, className)} {...props} ref={ref}>
|
||||
{showPlaceholder &&
|
||||
(typeof blurHash === 'string' ? (
|
||||
<div className={css.BlurhashPlaceholder}>
|
||||
<Blurhash
|
||||
hash={blurHash}
|
||||
width="100%"
|
||||
height="100%"
|
||||
resolutionX={blurResolutionX}
|
||||
resolutionY={blurResolutionY}
|
||||
punch={1.2}
|
||||
style={{ display: 'block' }}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<div className={css.MediaSkeleton} />
|
||||
))}
|
||||
{renderViewer && srcState.status === AsyncStatus.Success && (
|
||||
<Overlay open={viewer} backdrop={<OverlayBackdrop />}>
|
||||
<OverlayCenter>
|
||||
@@ -136,15 +178,6 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
</OverlayCenter>
|
||||
</Overlay>
|
||||
)}
|
||||
{typeof blurHash === 'string' && !load && (
|
||||
<BlurhashCanvas
|
||||
style={{ width: '100%', height: '100%' }}
|
||||
width={32}
|
||||
height={32}
|
||||
hash={blurHash}
|
||||
punch={1}
|
||||
/>
|
||||
)}
|
||||
{renderThumbnail && !load && (
|
||||
<Box
|
||||
className={classNames(css.AbsoluteContainer, blurred && css.Blur)}
|
||||
@@ -169,11 +202,20 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
</Box>
|
||||
)}
|
||||
{srcState.status === AsyncStatus.Success && (
|
||||
<Box className={classNames(css.AbsoluteContainer, blurred && css.Blur)}>
|
||||
<Box
|
||||
className={classNames(
|
||||
css.AbsoluteContainer,
|
||||
css.MediaFadeIn,
|
||||
load && css.MediaFadeInLoaded,
|
||||
blurred && css.Blur
|
||||
)}
|
||||
>
|
||||
{renderVideo({
|
||||
title: body,
|
||||
src: srcState.data,
|
||||
onLoadedMetadata: handleLoad,
|
||||
onLoadedMetadata: (e?: React.SyntheticEvent<HTMLVideoElement>) => {
|
||||
handleLoad(e?.currentTarget);
|
||||
},
|
||||
onError: handleError,
|
||||
autoPlay: true,
|
||||
controls: true,
|
||||
@@ -213,7 +255,8 @@ export const VideoContent = as<'div', VideoContentProps>(
|
||||
)}
|
||||
{(srcState.status === AsyncStatus.Loading || srcState.status === AsyncStatus.Success) &&
|
||||
!load &&
|
||||
!blurred && (
|
||||
!blurred &&
|
||||
typeof blurHash !== 'string' && (
|
||||
<Box className={css.AbsoluteContainer} alignItems="Center" justifyContent="Center">
|
||||
<Spinner variant="Secondary" />
|
||||
</Box>
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { style } from '@vanilla-extract/css';
|
||||
import { DefaultReset, config } from 'folds';
|
||||
import { keyframes, style } from '@vanilla-extract/css';
|
||||
import { DefaultReset, color, config } from 'folds';
|
||||
|
||||
export const RelativeBase = style([
|
||||
DefaultReset,
|
||||
{
|
||||
position: 'relative',
|
||||
display: 'flex',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
maxWidth: '100%',
|
||||
maxHeight: '100%',
|
||||
borderRadius: '7px',
|
||||
@@ -16,9 +18,13 @@ export const RelativeBase = style([
|
||||
export const AbsoluteContainer = style([
|
||||
DefaultReset,
|
||||
{
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
width: '100%',
|
||||
height: '100%',
|
||||
maxWidth: '100%',
|
||||
maxHeight: '100%',
|
||||
borderRadius: 'inherit',
|
||||
@@ -34,6 +40,7 @@ export const AbsoluteFooter = style([
|
||||
bottom: config.space.S100,
|
||||
left: config.space.S100,
|
||||
right: config.space.S100,
|
||||
zIndex: 1,
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -43,3 +50,67 @@ export const Blur = style([
|
||||
filter: 'blur(44px)',
|
||||
},
|
||||
]);
|
||||
|
||||
const shimmer = keyframes({
|
||||
'0%': {
|
||||
backgroundPosition: '200% 0',
|
||||
},
|
||||
'100%': {
|
||||
backgroundPosition: '-200% 0',
|
||||
},
|
||||
});
|
||||
|
||||
/** Soft fallback when no blurhash is available. */
|
||||
export const MediaSkeleton = style([
|
||||
DefaultReset,
|
||||
{
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
borderRadius: 'inherit',
|
||||
backgroundImage: `linear-gradient(
|
||||
90deg,
|
||||
${color.SurfaceVariant.Container} 0%,
|
||||
${color.SurfaceVariant.ContainerHover} 40%,
|
||||
${color.SurfaceVariant.Container} 80%
|
||||
)`,
|
||||
backgroundSize: '200% 100%',
|
||||
animation: `${shimmer} 1.4s ease-in-out infinite`,
|
||||
},
|
||||
]);
|
||||
|
||||
const blurhashFadeIn = keyframes({
|
||||
from: {
|
||||
opacity: 0,
|
||||
},
|
||||
to: {
|
||||
opacity: 1,
|
||||
},
|
||||
});
|
||||
|
||||
/** Soft blurhash layer that fills the reserved media box. */
|
||||
export const BlurhashPlaceholder = style([
|
||||
DefaultReset,
|
||||
{
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
overflow: 'hidden',
|
||||
borderRadius: 'inherit',
|
||||
zIndex: 0,
|
||||
// Soften the decode so it reads as color blobs; slight scale hides blur edges.
|
||||
filter: 'blur(14px)',
|
||||
transform: 'scale(1.06)',
|
||||
opacity: 0,
|
||||
animation: `${blurhashFadeIn} 560ms cubic-bezier(0.22, 1, 0.36, 1) forwards`,
|
||||
},
|
||||
]);
|
||||
|
||||
/** Media starts invisible and fades in once loaded over the placeholder. */
|
||||
export const MediaFadeIn = style({
|
||||
zIndex: 1,
|
||||
opacity: 0,
|
||||
transition: 'opacity 520ms cubic-bezier(0.22, 1, 0.36, 1)',
|
||||
});
|
||||
|
||||
export const MediaFadeInLoaded = style({
|
||||
opacity: 1,
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { createVar, keyframes, style, styleVariants } from '@vanilla-extract/css';
|
||||
import { createVar, globalStyle, keyframes, style, styleVariants } from '@vanilla-extract/css';
|
||||
import * as htmlCss from '../../../styles/CustomHtml.css';
|
||||
import { recipe, RecipeVariants } from '@vanilla-extract/recipes';
|
||||
import { DefaultReset, color, config, toRem } from 'folds';
|
||||
|
||||
@@ -117,6 +118,7 @@ export const MessageBase = recipe({
|
||||
collapse: {
|
||||
true: {
|
||||
marginTop: 0,
|
||||
paddingTop: config.space.S0,
|
||||
},
|
||||
},
|
||||
autoCollapse: {
|
||||
@@ -209,6 +211,8 @@ export const UsernameBold = style({
|
||||
fontWeight: 550,
|
||||
});
|
||||
|
||||
const jumboEmojiSize = toRem(70);
|
||||
|
||||
export const MessageTextBody = recipe({
|
||||
base: {
|
||||
wordBreak: 'break-word',
|
||||
@@ -226,8 +230,9 @@ export const MessageTextBody = recipe({
|
||||
},
|
||||
jumboEmoji: {
|
||||
true: {
|
||||
fontSize: '1.504em',
|
||||
lineHeight: '1.4962em',
|
||||
lineHeight: 1,
|
||||
overflow: 'visible',
|
||||
overflowY: 'visible',
|
||||
},
|
||||
},
|
||||
emote: {
|
||||
@@ -240,3 +245,29 @@ export const MessageTextBody = recipe({
|
||||
});
|
||||
|
||||
export type MessageTextBodyVariants = RecipeVariants<typeof MessageTextBody>;
|
||||
|
||||
const jumboEmojiClass = MessageTextBody.classNames.variants.jumboEmoji.true;
|
||||
|
||||
globalStyle(`${jumboEmojiClass} .${htmlCss.EmoticonBase}`, {
|
||||
height: jumboEmojiSize,
|
||||
padding: 0,
|
||||
overflow: 'visible',
|
||||
verticalAlign: 'middle',
|
||||
});
|
||||
|
||||
globalStyle(`${jumboEmojiClass} .${htmlCss.Emoticon.classNames.base}`, {
|
||||
fontSize: jumboEmojiSize,
|
||||
height: jumboEmojiSize,
|
||||
minWidth: jumboEmojiSize,
|
||||
lineHeight: 1,
|
||||
position: 'static',
|
||||
top: 0,
|
||||
overflow: 'visible',
|
||||
});
|
||||
|
||||
globalStyle(`${jumboEmojiClass} .${htmlCss.EmoticonImg}`, {
|
||||
height: jumboEmojiSize,
|
||||
width: jumboEmojiSize,
|
||||
maxHeight: 'none',
|
||||
objectFit: 'contain',
|
||||
});
|
||||
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
import { AvatarFallback, AvatarImage, color } from 'folds';
|
||||
import React, { ReactEventHandler, ReactNode, useEffect, useRef, useState } from 'react';
|
||||
import classNames from 'classnames';
|
||||
import { Blurhash } from 'react-blurhash';
|
||||
import * as css from './UserAvatar.css';
|
||||
import colorMXID from '../../../util/colorMXID';
|
||||
import { useAuthenticatedMediaUrl } from '../../hooks/useAuthenticatedMediaUrl';
|
||||
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
||||
import { cacheAvatar, isAvatarCached, pruneAvatarCache } from '../../utils/avatarCache';
|
||||
|
||||
// Check if image is already in browser cache
|
||||
function isImageInBrowserCache(url: string): boolean {
|
||||
const img = new Image();
|
||||
img.src = url;
|
||||
return img.complete && img.naturalWidth > 0;
|
||||
}
|
||||
import {
|
||||
getCachedAuthenticatedMediaUrl,
|
||||
isAuthenticatedMediaUrl,
|
||||
} from '../../utils/authenticatedMediaCache';
|
||||
import {
|
||||
getMediaBlurHash,
|
||||
getMediaDimensions,
|
||||
rememberMediaBlurHash,
|
||||
} from '../../state/mediaDimensionCache';
|
||||
|
||||
type UserAvatarProps = {
|
||||
className?: string;
|
||||
@@ -25,20 +28,28 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User
|
||||
const [error, setError] = useState(false);
|
||||
const useAuthentication = useMediaAuthentication();
|
||||
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
|
||||
|
||||
// Check both our cache and browser's native cache
|
||||
const [loaded, setLoaded] = useState(() => {
|
||||
if (isAvatarCached(src)) return true;
|
||||
if (authenticatedSrc && isImageInBrowserCache(authenticatedSrc)) {
|
||||
cacheAvatar(src);
|
||||
return true;
|
||||
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);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
}, [authenticatedSrc, src, useAuthentication]);
|
||||
|
||||
const imgRef = useRef<HTMLImageElement>(null);
|
||||
|
||||
// Also check on mount if image is already complete (browser cached)
|
||||
useEffect(() => {
|
||||
if (!loaded && imgRef.current?.complete && imgRef.current?.naturalWidth > 0) {
|
||||
setLoaded(true);
|
||||
@@ -51,10 +62,10 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User
|
||||
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(userId), color: color.Surface.Container }}
|
||||
@@ -65,8 +76,7 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User
|
||||
);
|
||||
}
|
||||
|
||||
// If already cached, show image directly without fallback flash
|
||||
if (loaded) {
|
||||
if (authenticatedSrc && loaded) {
|
||||
return (
|
||||
<AvatarImage
|
||||
className={classNames(css.UserAvatar, className)}
|
||||
@@ -80,13 +90,17 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User
|
||||
);
|
||||
}
|
||||
|
||||
// Loading state - render image with hidden fallback behind it
|
||||
// Image loads invisibly, then we show it once complete
|
||||
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(userId),
|
||||
style={{
|
||||
backgroundColor: colorMXID(userId),
|
||||
color: color.Surface.Container,
|
||||
position: 'absolute',
|
||||
inset: 0,
|
||||
@@ -95,16 +109,40 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User
|
||||
>
|
||||
{renderFallback()}
|
||||
</AvatarFallback>
|
||||
<AvatarImage
|
||||
ref={imgRef}
|
||||
className={classNames(css.UserAvatar, className)}
|
||||
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
|
||||
ref={imgRef}
|
||||
className={classNames(css.UserAvatar, className)}
|
||||
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