From a8aad30fc8f4d6bc75347cf7898f91946d1a9aab Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Wed, 15 Mar 2023 14:36:17 -0400 Subject: [PATCH] chore: memoized Messages component, will require custom equality check --- api/server/routes/handlers.js | 21 +++++++++++++++------ client/src/components/Messages/index.jsx | 2 +- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/api/server/routes/handlers.js b/api/server/routes/handlers.js index 7c6d6ce315..e3bd72583f 100644 --- a/api/server/routes/handlers.js +++ b/api/server/routes/handlers.js @@ -1,6 +1,7 @@ -const { titleConvo, citeText, detectCode } = require('../../app/'); const _ = require('lodash'); const sanitizeHtml = require('sanitize-html'); +const { titleConvo, citeText, detectCode } = require('../../app/'); +const htmlTagRegex = /(<\/?\s*[a-zA-Z]*\s*(?:\s+[a-zA-Z]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*(?:\/?)>|<\s*[a-zA-Z]+\s*(?:\s+[a-zA-Z]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*(?:\/?>|<\/?>))/g; const handleError = (res, message) => { res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`); @@ -42,8 +43,13 @@ const createOnProgress = () => { if (tokens.match(/^\n/)) { tokens = tokens.replace(/^\n/, ''); } - // if (tokens.includes('```')) { - // tokens = sanitizeHtml(tokens); + + // const htmlTags = tokens.match(htmlTagRegex); + // if (tokens.includes('```') && htmlTags && htmlTags.length > 0) { + // htmlTags.forEach((tag) => { + // const sanitizedTag = sanitizeHtml(tag); + // tokens = tokens.replaceAll(tag, sanitizedTag); + // }); // } if (bing) { @@ -65,9 +71,12 @@ const createOnProgress = () => { const handleText = async (input) => { let text = input; text = await detectCode(text); - // if (text.includes('```')) { - // text = sanitizeHtml(text); - // text = text.replaceAll(') =>', ') =>'); + // const htmlTags = text.match(htmlTagRegex); + // if (text.includes('```') && htmlTags && htmlTags.length > 0) { + // htmlTags.forEach((tag) => { + // const sanitizedTag = sanitizeHtml(tag); + // text = text.replaceAll(tag, sanitizedTag); + // }); // } return text; diff --git a/client/src/components/Messages/index.jsx b/client/src/components/Messages/index.jsx index d9b5b2a80b..611a0231d3 100644 --- a/client/src/components/Messages/index.jsx +++ b/client/src/components/Messages/index.jsx @@ -95,4 +95,4 @@ const Messages = ({ messages, messageTree }) => { ); }; -export default Messages; +export default React.memo(Messages);