Fixed bug on chatMessage

This commit is contained in:
Lucas Oliveira 2023-07-11 20:33:46 -03:00
commit d15c5636a2

View file

@ -150,8 +150,8 @@ export default function ChatMessage({
</div> </div>
</div> </div>
) : ( ) : (
<div className="flex flex-col"> <div>
{template && ( {template ? (
<> <>
<button <button
className="form-modal-initial-prompt-btn" className="form-modal-initial-prompt-btn"
@ -167,40 +167,41 @@ export default function ChatMessage({
/> />
</button> </button>
<span className="prose inline-block break-words text-primary dark:prose-invert"> <span className="prose inline-block break-words text-primary dark:prose-invert">
{promptOpen && {promptOpen
template?.split("\n")?.map((line, index) => { ? template?.split("\n")?.map((line, index) => {
const regex = /{([^}]+)}/g; const regex = /{([^}]+)}/g;
let match; let match;
let parts = []; let parts = [];
let lastIndex = 0; let lastIndex = 0;
while ((match = regex.exec(line)) !== null) { while ((match = regex.exec(line)) !== null) {
// Push text up to the match // Push text up to the match
if (match.index !== lastIndex) { if (match.index !== lastIndex) {
parts.push(line.substring(lastIndex, match.index)); parts.push(line.substring(lastIndex, match.index));
} }
// Push div with matched text // Push div with matched text
if (chat.message[match[1]]) { if (chat.message[match[1]]) {
parts.push( parts.push(
<span className="chat-message-highlight"> <span className="chat-message-highlight">
{chat.message[match[1]]} {chat.message[match[1]]}
</span> </span>
); );
} }
// Update last index // Update last index
lastIndex = regex.lastIndex; lastIndex = regex.lastIndex;
} }
// Push text after the last match // Push text after the last match
if (lastIndex !== line.length) { if (lastIndex !== line.length) {
parts.push(line.substring(lastIndex)); parts.push(line.substring(lastIndex));
} }
return <p>{parts}</p>; return <p>{parts}</p>;
})} })
: chat.message[chat.chatKey]}
</span> </span>
</> </>
) : (
<span>{chat.message[chat.chatKey]}</span>
)} )}
<br />
<span>{chat.message[chat.chatKey]}</span>
</div> </div>
)} )}
</div> </div>