Skip to content

Commit 2af2f07

Browse files
committed
Refactor chatPanel component initialization and event registration
1 parent 21bb9d6 commit 2af2f07

1 file changed

Lines changed: 27 additions & 22 deletions

File tree

src/views/pages/ChatPanel.tsx

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const chatPanel = observer(() => {
3030
const scrollViewport = useRef<HTMLDivElement>(null);
3131
const { height } = useViewportSize();
3232
const { ref: inputAreatRef, height: inputAreaHeight } = useElementSize();
33+
const isFirstRender = useRef(true);
3334

3435
const chatPanelWidth = chatContainerRect.width;
3536

@@ -90,30 +91,34 @@ const chatPanel = observer(() => {
9091
"regCommandList",
9192
(message: { result: Item[] }) => {
9293
input.fetchCommandMenus(message.result);
94+
if (!isFirstRender.current) {
95+
return;
96+
}
97+
isFirstRender.current = false;
98+
messageUtil.registerHandler("reloadMessage", (message: any) => {
99+
chat.reloadMessage(message);
100+
});
101+
messageUtil.registerHandler("loadHistoryMessages", (message: any) => {
102+
chat.reloadMessage({
103+
entries: message.entries,
104+
pageIndex: 0,
105+
reset: message.entries.length === 0,
106+
});
107+
});
108+
// The history records need to be obtained after setting the key,
109+
// as the display information in the history record requires adjustment
110+
// based on whether the key is present.
111+
messageUtil.registerHandler("getUserAccessKey", (message: any) => {
112+
chat.setKey(message.accessKey);
113+
chat.fetchHistoryMessages();
114+
});
115+
// The history records need to be obtained after setting the key,
116+
input.fetchContextMenus().then();
117+
input.fetchModelMenus().then();
118+
getFeatureToggles();
119+
getSettings();
93120
}
94121
);
95-
messageUtil.registerHandler("reloadMessage", (message: any) => {
96-
chat.reloadMessage(message);
97-
});
98-
messageUtil.registerHandler("loadHistoryMessages", (message: any) => {
99-
chat.reloadMessage({
100-
entries: message.entries,
101-
pageIndex: 0,
102-
reset: message.entries.length === 0,
103-
});
104-
});
105-
// The history records need to be obtained after setting the key,
106-
// as the display information in the history record requires adjustment
107-
// based on whether the key is present.
108-
messageUtil.registerHandler("getUserAccessKey", (message: any) => {
109-
chat.setKey(message.accessKey);
110-
chat.fetchHistoryMessages();
111-
});
112-
// The history records need to be obtained after setting the key,
113-
input.fetchContextMenus().then();
114-
input.fetchModelMenus().then();
115-
getFeatureToggles();
116-
getSettings();
117122
messageUtil.registerHandler(
118123
"receiveMessagePartial",
119124
(message: { text: string }) => {

0 commit comments

Comments
 (0)