Fixed display prompt showing when there is no template

This commit is contained in:
Lucas Oliveira 2023-07-10 19:45:12 -03:00
commit 07e6b137c4

View file

@ -138,51 +138,56 @@ export default function ChatMessage({
</div> </div>
) : ( ) : (
<div> <div>
<button {template && (
className="form-modal-initial-prompt-btn" <>
onClick={() => { <button
setPromptOpen((old) => !old); className="form-modal-initial-prompt-btn"
}} onClick={() => {
> setPromptOpen((old) => !old);
Display Prompt }}
<ChevronDown >
className={ Display Prompt
"h-3 w-3 transition-all " + (promptOpen ? "rotate-180" : "") <ChevronDown
} className={
/> "h-3 w-3 transition-all " + (promptOpen ? "rotate-180" : "")
</button> }
<span className="prose inline-block break-words text-primary dark:prose-invert"> />
{promptOpen </button>
? template?.split("\n")?.map((line, index) => { <span className="prose inline-block break-words text-primary dark:prose-invert">
const regex = /{([^}]+)}/g; {promptOpen
let match; ? template?.split("\n")?.map((line, index) => {
let parts = []; const regex = /{([^}]+)}/g;
let lastIndex = 0; let match;
while ((match = regex.exec(line)) !== null) { let parts = [];
// Push text up to the match let lastIndex = 0;
if (match.index !== lastIndex) { while ((match = regex.exec(line)) !== null) {
parts.push(line.substring(lastIndex, match.index)); // Push text up to the match
} if (match.index !== lastIndex) {
// Push div with matched text parts.push(line.substring(lastIndex, match.index));
if (chat.message[match[1]]) { }
parts.push( // Push div with matched text
<span className="my-1 rounded-md bg-indigo-100"> if (chat.message[match[1]]) {
{chat.message[match[1]]} parts.push(
</span> <span className="my-1 rounded-md bg-indigo-100">
); {chat.message[match[1]]}
} </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]} : chat.message[chat.chatKey]}
</span> </span>
</>
)}
{chat.message[chat.chatKey]}
</div> </div>
)} )}
</div> </div>