From 420a7dab4e00e995601a137abacf41c86c34cfc7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 11 Oct 2024 00:05:15 +0300 Subject: [PATCH] web/timeline: cache sanitized html --- web/src/ui/timeline/content/MessageBody.tsx | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/web/src/ui/timeline/content/MessageBody.tsx b/web/src/ui/timeline/content/MessageBody.tsx index e8cd6dc..9b4db78 100644 --- a/web/src/ui/timeline/content/MessageBody.tsx +++ b/web/src/ui/timeline/content/MessageBody.tsx @@ -13,7 +13,7 @@ // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -import { use } from "react" +import { use, useMemo } from "react" import sanitizeHtml from "sanitize-html" import { getMediaURL } from "../../../api/media.ts" import { ContentURI } from "../../../api/types" @@ -67,24 +67,26 @@ const MessageBody = ({ event }: EventContentProps) => { if (event.type === "m.sticker") { content.msgtype = "m.image" } + const __html = useMemo(() => { + if (content.format === "org.matrix.custom.html") { + return sanitizeHtml(content.formatted_body!, sanitizeHtmlParams) + } + return undefined + }, [content.format, content.formatted_body]) switch (content.msgtype) { case "m.text": case "m.emote": case "m.notice": - if (content.format === "org.matrix.custom.html") { - return
+ if (__html) { + return
} return content.body case "m.image": { const openLightbox = use(LightboxContext) const style = calculateMediaSize(content.info?.w, content.info?.h) let caption = null - if (content.format === "org.matrix.custom.html") { - caption =
+ if (__html) { + caption =
} else if (content.body && content.filename && content.body !== content.filename) { caption = content.body }