Skip to content

Commit 338ef7b

Browse files
committed
fix: update model message in a more stable manner in the Electron template
1 parent 2fb0e29 commit 338ef7b

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

templates/electron-typescript-react/src/App/components/MessageMarkdown/MessageMarkdown.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1+
import {useMemo} from "react";
12
import classNames from "classnames";
23
import {MarkdownContent} from "../MarkdownContent/MarkdownContent.js";
34

45
import "./MessageMarkdown.css";
56

67

78
export function MessageMarkdown({children, activeDot = false, className}: MessageMarkdownProps) {
9+
const renderContent = useMemo(() => {
10+
if (children == null)
11+
return "";
12+
13+
if (!activeDot)
14+
return children;
15+
16+
const lines = children.split("\n");
17+
const lastLine = lines.at(-1);
18+
19+
// to frequent line jumps and instability while the content is being generated,
20+
// wait with rendering the last line until its content is properly formed and is ready to be appended
21+
if (lastLine != null && ["-", "+", "*", "1.", "1", "--"].includes(lastLine.trim()))
22+
return lines.slice(0, -1).join("\n");
23+
else if (lastLine != null && lastLine.trim().length === 1 && (
24+
lastLine.endsWith(" *") || lastLine.endsWith(" _") || lastLine.endsWith(" ~")
25+
))
26+
return [
27+
...lines.slice(0, -1),
28+
lastLine.slice(0, -" _".length)
29+
].join("\n");
30+
31+
return children;
32+
}, [children, activeDot]);
33+
834
return <MarkdownContent className={classNames("appMessageMarkdown", activeDot && "active", className)}>
9-
{children ?? ""}
35+
{renderContent}
1036
</MarkdownContent>;
1137
}
1238

0 commit comments

Comments
 (0)