Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Boken Image & Sticker #1455

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/app/organisms/room/RoomTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1008,13 +1008,10 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
const content = mEvent.getContent<IImageContent>();
const imgInfo = content?.info;
const mxcUrl = content.file?.url ?? content.url;
if (!imgInfo || typeof imgInfo.mimetype !== 'string' || typeof mxcUrl !== 'string') {
if (mxcUrl) {
return fileRenderer(mEventId, mEvent);
}
if (typeof mxcUrl !== 'string') {
return null;
}
const height = scaleYDimension(imgInfo.w || 400, 400, imgInfo.h || 400);
const height = scaleYDimension(imgInfo?.w || 400, 400, imgInfo?.h || 400);

return (
<Attachment>
Expand All @@ -1026,7 +1023,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
<ImageContent
body={content.body || 'Image'}
info={imgInfo}
mimeType={imgInfo.mimetype}
mimeType={imgInfo?.mimetype}
url={mxcUrl}
encInfo={content.file}
autoPlay={mediaAutoLoad}
Expand Down Expand Up @@ -1283,6 +1280,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
>
<EncryptedContent mEvent={mEvent}>
{() => {
if (mEvent.isRedacted()) return <MessageDeletedContent />;
if (mEvent.getType() === MessageEvent.Sticker)
return <StickerContent mEvent={mEvent} autoPlay={mediaAutoLoad} />;
if (mEvent.getType() === MessageEvent.RoomMessage)
Expand Down
7 changes: 4 additions & 3 deletions src/app/organisms/room/message/EncryptedContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ type EncryptedContentProps = {
};

export function EncryptedContent({ mEvent, children }: EncryptedContentProps) {
const [, setDecrypted] = useState(mEvent.isBeingDecrypted());
const [, toggleDecrypted] = useState(!mEvent.isBeingDecrypted());

useEffect(() => {
const handleDecrypted: MatrixEventHandlerMap[MatrixEventEvent.Decrypted] = () =>
setDecrypted(true);
const handleDecrypted: MatrixEventHandlerMap[MatrixEventEvent.Decrypted] = () => {
toggleDecrypted((s) => !s);
};
mEvent.on(MatrixEventEvent.Decrypted, handleDecrypted);
return () => {
mEvent.removeListener(MatrixEventEvent.Decrypted, handleDecrypted);
Expand Down
11 changes: 6 additions & 5 deletions src/app/organisms/room/message/ImageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,28 @@ import { Image } from '../../../components/media';
import * as css from './styles.css';
import { bytesToSize } from '../../../utils/common';
import { ImageViewer } from '../../../components/image-viewer';
import { FALLBACK_MIMETYPE } from '../../../utils/mimeTypes';

export type ImageContentProps = {
body: string;
mimeType: string;
mimeType?: string;
url: string;
info: IImageInfo;
info?: IImageInfo;
encInfo?: EncryptedAttachmentInfo;
autoPlay?: boolean;
};
export const ImageContent = as<'div', ImageContentProps>(
({ className, body, mimeType, url, info, encInfo, autoPlay, ...props }, ref) => {
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 [error, setError] = useState(false);
const [viewer, setViewer] = useState(false);

const [srcState, loadSrc] = useAsyncCallback(
useCallback(
() => getFileSrcUrl(mx.mxcUrlToHttp(url) ?? '', mimeType, encInfo),
() => getFileSrcUrl(mx.mxcUrlToHttp(url) ?? '', mimeType || FALLBACK_MIMETYPE, encInfo),
[mx, url, mimeType, encInfo]
)
);
Expand Down Expand Up @@ -161,7 +162,7 @@ export const ImageContent = as<'div', ImageContentProps>(
</TooltipProvider>
</Box>
)}
{!load && typeof info.size === 'number' && (
{!load && typeof info?.size === 'number' && (
<Box className={css.AbsoluteFooter} justifyContent="End" alignContent="Center" gap="200">
<Badge variant="Secondary" fill="Soft">
<Text size="L400">{bytesToSize(info.size)}</Text>
Expand Down
65 changes: 36 additions & 29 deletions src/app/organisms/room/message/StickerContent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React from 'react';
import { as, toRem } from 'folds';
import { MatrixEvent } from 'matrix-js-sdk';
import { AttachmentBox, MessageBrokenContent } from '../../../components/message';
import {
AttachmentBox,
MessageBrokenContent,
MessageDeletedContent,
} from '../../../components/message';
import { ImageContent } from './ImageContent';
import { scaleYDimension } from '../../../utils/common';
import { IImageContent } from '../../../../types/matrix/common';
Expand All @@ -10,32 +14,35 @@ type StickerContentProps = {
mEvent: MatrixEvent;
autoPlay: boolean;
};
export const StickerContent = as<'div', StickerContentProps>(({ mEvent, autoPlay, ...props }, ref) => {
const content = mEvent.getContent<IImageContent>();
const imgInfo = content?.info;
const mxcUrl = content.file?.url ?? content.url;
if (!imgInfo || typeof imgInfo.mimetype !== 'string' || typeof mxcUrl !== 'string') {
return <MessageBrokenContent />;
}
const height = scaleYDimension(imgInfo.w || 152, 152, imgInfo.h || 152);
export const StickerContent = as<'div', StickerContentProps>(
({ mEvent, autoPlay, ...props }, ref) => {
if (mEvent.isRedacted()) return <MessageDeletedContent />;
const content = mEvent.getContent<IImageContent>();
const imgInfo = content?.info;
const mxcUrl = content.file?.url ?? content.url;
if (typeof mxcUrl !== 'string') {
return <MessageBrokenContent />;
}
const height = scaleYDimension(imgInfo?.w || 152, 152, imgInfo?.h || 152);

return (
<AttachmentBox
style={{
height: toRem(height < 48 ? 48 : height),
width: toRem(152),
}}
{...props}
ref={ref}
>
<ImageContent
autoPlay={autoPlay}
body={content.body || 'Image'}
info={imgInfo}
mimeType={imgInfo.mimetype}
url={mxcUrl}
encInfo={content.file}
/>
</AttachmentBox>
);
});
return (
<AttachmentBox
style={{
height: toRem(height < 48 ? 48 : height),
width: toRem(152),
}}
{...props}
ref={ref}
>
<ImageContent
autoPlay={autoPlay}
body={content.body || 'Image'}
info={imgInfo}
mimeType={imgInfo?.mimetype}
url={mxcUrl}
encInfo={content.file}
/>
</AttachmentBox>
);
}
);