Fix Boken Image & Sticker (#1455)
* fix image without info rendered as broken * fix enc msg appear as decrypting after deletion
This commit is contained in:
parent
c980fddfa1
commit
b92b281050
4 changed files with 50 additions and 43 deletions
|
@ -1034,13 +1034,10 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
|
||||||
const content = mEvent.getContent<IImageContent>();
|
const content = mEvent.getContent<IImageContent>();
|
||||||
const imgInfo = content?.info;
|
const imgInfo = content?.info;
|
||||||
const mxcUrl = content.file?.url ?? content.url;
|
const mxcUrl = content.file?.url ?? content.url;
|
||||||
if (!imgInfo || typeof imgInfo.mimetype !== 'string' || typeof mxcUrl !== 'string') {
|
if (typeof mxcUrl !== 'string') {
|
||||||
if (mxcUrl) {
|
|
||||||
return fileRenderer(mEventId, mEvent);
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const height = scaleYDimension(imgInfo.w || 400, 400, imgInfo.h || 400);
|
const height = scaleYDimension(imgInfo?.w || 400, 400, imgInfo?.h || 400);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Attachment>
|
<Attachment>
|
||||||
|
@ -1052,7 +1049,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
|
||||||
<ImageContent
|
<ImageContent
|
||||||
body={content.body || 'Image'}
|
body={content.body || 'Image'}
|
||||||
info={imgInfo}
|
info={imgInfo}
|
||||||
mimeType={imgInfo.mimetype}
|
mimeType={imgInfo?.mimetype}
|
||||||
url={mxcUrl}
|
url={mxcUrl}
|
||||||
encInfo={content.file}
|
encInfo={content.file}
|
||||||
autoPlay={mediaAutoLoad}
|
autoPlay={mediaAutoLoad}
|
||||||
|
@ -1309,6 +1306,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
|
||||||
>
|
>
|
||||||
<EncryptedContent mEvent={mEvent}>
|
<EncryptedContent mEvent={mEvent}>
|
||||||
{() => {
|
{() => {
|
||||||
|
if (mEvent.isRedacted()) return <MessageDeletedContent />;
|
||||||
if (mEvent.getType() === MessageEvent.Sticker)
|
if (mEvent.getType() === MessageEvent.Sticker)
|
||||||
return <StickerContent mEvent={mEvent} autoPlay={mediaAutoLoad} />;
|
return <StickerContent mEvent={mEvent} autoPlay={mediaAutoLoad} />;
|
||||||
if (mEvent.getType() === MessageEvent.RoomMessage)
|
if (mEvent.getType() === MessageEvent.RoomMessage)
|
||||||
|
|
|
@ -7,11 +7,12 @@ type EncryptedContentProps = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export function EncryptedContent({ mEvent, children }: EncryptedContentProps) {
|
export function EncryptedContent({ mEvent, children }: EncryptedContentProps) {
|
||||||
const [, setDecrypted] = useState(mEvent.isBeingDecrypted());
|
const [, toggleDecrypted] = useState(!mEvent.isBeingDecrypted());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleDecrypted: MatrixEventHandlerMap[MatrixEventEvent.Decrypted] = () =>
|
const handleDecrypted: MatrixEventHandlerMap[MatrixEventEvent.Decrypted] = () => {
|
||||||
setDecrypted(true);
|
toggleDecrypted((s) => !s);
|
||||||
|
};
|
||||||
mEvent.on(MatrixEventEvent.Decrypted, handleDecrypted);
|
mEvent.on(MatrixEventEvent.Decrypted, handleDecrypted);
|
||||||
return () => {
|
return () => {
|
||||||
mEvent.removeListener(MatrixEventEvent.Decrypted, handleDecrypted);
|
mEvent.removeListener(MatrixEventEvent.Decrypted, handleDecrypted);
|
||||||
|
|
|
@ -27,19 +27,20 @@ import { Image } from '../../../components/media';
|
||||||
import * as css from './styles.css';
|
import * as css from './styles.css';
|
||||||
import { bytesToSize } from '../../../utils/common';
|
import { bytesToSize } from '../../../utils/common';
|
||||||
import { ImageViewer } from '../../../components/image-viewer';
|
import { ImageViewer } from '../../../components/image-viewer';
|
||||||
|
import { FALLBACK_MIMETYPE } from '../../../utils/mimeTypes';
|
||||||
|
|
||||||
export type ImageContentProps = {
|
export type ImageContentProps = {
|
||||||
body: string;
|
body: string;
|
||||||
mimeType: string;
|
mimeType?: string;
|
||||||
url: string;
|
url: string;
|
||||||
info: IImageInfo;
|
info?: IImageInfo;
|
||||||
encInfo?: EncryptedAttachmentInfo;
|
encInfo?: EncryptedAttachmentInfo;
|
||||||
autoPlay?: boolean;
|
autoPlay?: boolean;
|
||||||
};
|
};
|
||||||
export const ImageContent = as<'div', ImageContentProps>(
|
export const ImageContent = as<'div', ImageContentProps>(
|
||||||
({ className, body, mimeType, url, info, encInfo, autoPlay, ...props }, ref) => {
|
({ className, body, mimeType, url, info, encInfo, autoPlay, ...props }, ref) => {
|
||||||
const mx = useMatrixClient();
|
const mx = useMatrixClient();
|
||||||
const blurHash = info[MATRIX_BLUR_HASH_PROPERTY_NAME];
|
const blurHash = info?.[MATRIX_BLUR_HASH_PROPERTY_NAME];
|
||||||
|
|
||||||
const [load, setLoad] = useState(false);
|
const [load, setLoad] = useState(false);
|
||||||
const [error, setError] = useState(false);
|
const [error, setError] = useState(false);
|
||||||
|
@ -47,7 +48,7 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||||
|
|
||||||
const [srcState, loadSrc] = useAsyncCallback(
|
const [srcState, loadSrc] = useAsyncCallback(
|
||||||
useCallback(
|
useCallback(
|
||||||
() => getFileSrcUrl(mx.mxcUrlToHttp(url) ?? '', mimeType, encInfo),
|
() => getFileSrcUrl(mx.mxcUrlToHttp(url) ?? '', mimeType || FALLBACK_MIMETYPE, encInfo),
|
||||||
[mx, url, mimeType, encInfo]
|
[mx, url, mimeType, encInfo]
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -161,7 +162,7 @@ export const ImageContent = as<'div', ImageContentProps>(
|
||||||
</TooltipProvider>
|
</TooltipProvider>
|
||||||
</Box>
|
</Box>
|
||||||
)}
|
)}
|
||||||
{!load && typeof info.size === 'number' && (
|
{!load && typeof info?.size === 'number' && (
|
||||||
<Box className={css.AbsoluteFooter} justifyContent="End" alignContent="Center" gap="200">
|
<Box className={css.AbsoluteFooter} justifyContent="End" alignContent="Center" gap="200">
|
||||||
<Badge variant="Secondary" fill="Soft">
|
<Badge variant="Secondary" fill="Soft">
|
||||||
<Text size="L400">{bytesToSize(info.size)}</Text>
|
<Text size="L400">{bytesToSize(info.size)}</Text>
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { as, toRem } from 'folds';
|
import { as, toRem } from 'folds';
|
||||||
import { MatrixEvent } from 'matrix-js-sdk';
|
import { MatrixEvent } from 'matrix-js-sdk';
|
||||||
import { AttachmentBox, MessageBrokenContent } from '../../../components/message';
|
import {
|
||||||
|
AttachmentBox,
|
||||||
|
MessageBrokenContent,
|
||||||
|
MessageDeletedContent,
|
||||||
|
} from '../../../components/message';
|
||||||
import { ImageContent } from './ImageContent';
|
import { ImageContent } from './ImageContent';
|
||||||
import { scaleYDimension } from '../../../utils/common';
|
import { scaleYDimension } from '../../../utils/common';
|
||||||
import { IImageContent } from '../../../../types/matrix/common';
|
import { IImageContent } from '../../../../types/matrix/common';
|
||||||
|
@ -10,32 +14,35 @@ type StickerContentProps = {
|
||||||
mEvent: MatrixEvent;
|
mEvent: MatrixEvent;
|
||||||
autoPlay: boolean;
|
autoPlay: boolean;
|
||||||
};
|
};
|
||||||
export const StickerContent = as<'div', StickerContentProps>(({ mEvent, autoPlay, ...props }, ref) => {
|
export const StickerContent = as<'div', StickerContentProps>(
|
||||||
const content = mEvent.getContent<IImageContent>();
|
({ mEvent, autoPlay, ...props }, ref) => {
|
||||||
const imgInfo = content?.info;
|
if (mEvent.isRedacted()) return <MessageDeletedContent />;
|
||||||
const mxcUrl = content.file?.url ?? content.url;
|
const content = mEvent.getContent<IImageContent>();
|
||||||
if (!imgInfo || typeof imgInfo.mimetype !== 'string' || typeof mxcUrl !== 'string') {
|
const imgInfo = content?.info;
|
||||||
return <MessageBrokenContent />;
|
const mxcUrl = content.file?.url ?? content.url;
|
||||||
}
|
if (typeof mxcUrl !== 'string') {
|
||||||
const height = scaleYDimension(imgInfo.w || 152, 152, imgInfo.h || 152);
|
return <MessageBrokenContent />;
|
||||||
|
}
|
||||||
|
const height = scaleYDimension(imgInfo?.w || 152, 152, imgInfo?.h || 152);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AttachmentBox
|
<AttachmentBox
|
||||||
style={{
|
style={{
|
||||||
height: toRem(height < 48 ? 48 : height),
|
height: toRem(height < 48 ? 48 : height),
|
||||||
width: toRem(152),
|
width: toRem(152),
|
||||||
}}
|
}}
|
||||||
{...props}
|
{...props}
|
||||||
ref={ref}
|
ref={ref}
|
||||||
>
|
>
|
||||||
<ImageContent
|
<ImageContent
|
||||||
autoPlay={autoPlay}
|
autoPlay={autoPlay}
|
||||||
body={content.body || 'Image'}
|
body={content.body || 'Image'}
|
||||||
info={imgInfo}
|
info={imgInfo}
|
||||||
mimeType={imgInfo.mimetype}
|
mimeType={imgInfo?.mimetype}
|
||||||
url={mxcUrl}
|
url={mxcUrl}
|
||||||
encInfo={content.file}
|
encInfo={content.file}
|
||||||
/>
|
/>
|
||||||
</AttachmentBox>
|
</AttachmentBox>
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in a new issue