|
| 1 | +/* |
| 2 | + * Copyright 2024 New Vector Ltd. |
| 3 | + * Copyright 2024 The Matrix.org Foundation C.I.C. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only |
| 6 | + * Please see LICENSE files in the repository root for full details. |
| 7 | + */ |
| 8 | + |
| 9 | +import React, { HTMLProps, JSX, useContext, useState } from "react"; |
| 10 | +import { IContent, M_POLL_START, MatrixEvent, MatrixEventEvent, MsgType } from "matrix-js-sdk/src/matrix"; |
| 11 | +import classNames from "classnames"; |
| 12 | + |
| 13 | +import { _t } from "../../../languageHandler"; |
| 14 | +import { MessagePreviewStore } from "../../../stores/room-list/MessagePreviewStore"; |
| 15 | +import { useAsyncMemo } from "../../../hooks/useAsyncMemo"; |
| 16 | +import MatrixClientContext from "../../../contexts/MatrixClientContext"; |
| 17 | +import { useTypedEventEmitter } from "../../../hooks/useEventEmitter.ts"; |
| 18 | + |
| 19 | +/** |
| 20 | + * The props for the {@link EventPreview} component. |
| 21 | + */ |
| 22 | +interface Props extends HTMLProps<HTMLSpanElement> { |
| 23 | + /** |
| 24 | + * The event to display the preview for |
| 25 | + */ |
| 26 | + mxEvent: MatrixEvent; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A component that displays a preview for the given event. |
| 31 | + * Wraps both `useEventPreview` & `EventPreviewTile`. |
| 32 | + */ |
| 33 | +export function EventPreview({ mxEvent, className, ...props }: Props): JSX.Element | null { |
| 34 | + const preview = useEventPreview(mxEvent); |
| 35 | + if (!preview) return null; |
| 36 | + |
| 37 | + return <EventPreviewTile {...props} preview={preview} className={className} />; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * The props for the {@link EventPreviewTile} component. |
| 42 | + */ |
| 43 | +interface EventPreviewTileProps extends HTMLProps<HTMLSpanElement> { |
| 44 | + /** |
| 45 | + * The preview to display |
| 46 | + */ |
| 47 | + preview: Preview; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * A component that displays a preview given the output from `useEventPreview`. |
| 52 | + */ |
| 53 | +export function EventPreviewTile({ |
| 54 | + preview: [preview, prefix], |
| 55 | + className, |
| 56 | + ...props |
| 57 | +}: EventPreviewTileProps): JSX.Element | null { |
| 58 | + const classes = classNames("mx_EventPreview", className); |
| 59 | + if (!prefix) |
| 60 | + return ( |
| 61 | + <span {...props} className={classes} title={preview}> |
| 62 | + {preview} |
| 63 | + </span> |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <span {...props} className={classes}> |
| 68 | + {_t( |
| 69 | + "event_preview|preview", |
| 70 | + { |
| 71 | + prefix, |
| 72 | + preview, |
| 73 | + }, |
| 74 | + { |
| 75 | + bold: (sub) => <span className="mx_EventPreview_prefix">{sub}</span>, |
| 76 | + }, |
| 77 | + )} |
| 78 | + </span> |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +type Preview = [preview: string, prefix: string | null]; |
| 83 | + |
| 84 | +/** |
| 85 | + * Hooks to generate a preview for the event. |
| 86 | + * @param mxEvent |
| 87 | + */ |
| 88 | +export function useEventPreview(mxEvent: MatrixEvent | undefined): Preview | null { |
| 89 | + const cli = useContext(MatrixClientContext); |
| 90 | + // track the content as a means to regenerate the preview upon edits & decryption |
| 91 | + const [content, setContent] = useState<IContent | undefined>(mxEvent?.getContent()); |
| 92 | + useTypedEventEmitter(mxEvent ?? undefined, MatrixEventEvent.Replaced, () => { |
| 93 | + setContent(mxEvent!.getContent()); |
| 94 | + }); |
| 95 | + const awaitDecryption = mxEvent?.shouldAttemptDecryption() || mxEvent?.isBeingDecrypted(); |
| 96 | + useTypedEventEmitter(awaitDecryption ? (mxEvent ?? undefined) : undefined, MatrixEventEvent.Decrypted, () => { |
| 97 | + setContent(mxEvent!.getContent()); |
| 98 | + }); |
| 99 | + |
| 100 | + return useAsyncMemo( |
| 101 | + async () => { |
| 102 | + if (!mxEvent || mxEvent.isRedacted() || mxEvent.isDecryptionFailure()) return null; |
| 103 | + await cli.decryptEventIfNeeded(mxEvent); |
| 104 | + return [ |
| 105 | + MessagePreviewStore.instance.generatePreviewForEvent(mxEvent), |
| 106 | + getPreviewPrefix(mxEvent.getType(), content?.msgtype as MsgType), |
| 107 | + ]; |
| 108 | + }, |
| 109 | + [mxEvent, content], |
| 110 | + null, |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Get the prefix for the preview based on the type and the message type. |
| 116 | + * @param type |
| 117 | + * @param msgType |
| 118 | + */ |
| 119 | +function getPreviewPrefix(type: string, msgType: MsgType): string | null { |
| 120 | + switch (type) { |
| 121 | + case M_POLL_START.name: |
| 122 | + return _t("event_preview|prefix|poll"); |
| 123 | + default: |
| 124 | + } |
| 125 | + |
| 126 | + switch (msgType) { |
| 127 | + case MsgType.Audio: |
| 128 | + return _t("event_preview|prefix|audio"); |
| 129 | + case MsgType.Image: |
| 130 | + return _t("event_preview|prefix|image"); |
| 131 | + case MsgType.Video: |
| 132 | + return _t("event_preview|prefix|video"); |
| 133 | + case MsgType.File: |
| 134 | + return _t("event_preview|prefix|file"); |
| 135 | + default: |
| 136 | + return null; |
| 137 | + } |
| 138 | +} |
0 commit comments