fix: 修复 ChatPanel 无限重渲染 — useLogger 新增 bindings 参数保证引用稳定
This commit is contained in:
@@ -26,7 +26,7 @@ interface ChatPanelProps {
|
||||
|
||||
export function ChatPanel({ conversationId, onConversationCreated, projectId }: ChatPanelProps) {
|
||||
const { message } = App.useApp();
|
||||
const logger = useLogger().child({ component: "ChatPanel", page: "workbench" });
|
||||
const logger = useLogger({ component: "ChatPanel", page: "workbench" });
|
||||
const queryClient = useQueryClient();
|
||||
const [input, setInput] = useState("");
|
||||
const [editingMessageId, setEditingMessageId] = useState<null | string>(null);
|
||||
|
||||
@@ -1,14 +1,25 @@
|
||||
import { App } from "antd";
|
||||
import { useMemo } from "react";
|
||||
import { useMemo, useState } from "react";
|
||||
|
||||
import type { Logger } from "../utils/logger";
|
||||
|
||||
import { AntdMessageSink, ConsoleSink, createDefaultLogger } from "../utils/logger";
|
||||
|
||||
export function useLogger(): Logger {
|
||||
export function useLogger(bindings?: Record<string, unknown>): Logger {
|
||||
const { message } = App.useApp();
|
||||
const [stableJson, setStableJson] = useState(() => JSON.stringify(bindings ?? {}));
|
||||
const [stableBindings, setStableBindings] = useState(() => bindings);
|
||||
const currentJson = JSON.stringify(bindings ?? {});
|
||||
|
||||
if (currentJson !== stableJson) {
|
||||
setStableJson(currentJson);
|
||||
setStableBindings(bindings);
|
||||
}
|
||||
|
||||
return useMemo(() => {
|
||||
const isProduction = !!import.meta.env["PROD"];
|
||||
return createDefaultLogger([new ConsoleSink(isProduction), new AntdMessageSink(message)], isProduction);
|
||||
}, [message]);
|
||||
const base = createDefaultLogger([new ConsoleSink(isProduction), new AntdMessageSink(message)], isProduction);
|
||||
if (!stableBindings || Object.keys(stableBindings).length === 0) return base;
|
||||
return base.child(stableBindings);
|
||||
}, [message, stableBindings]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user