From f4858d36844389c9c0fdf5ece8a2b9778e63a2d0 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 30 Mar 2026 19:40:38 +0300 Subject: [PATCH] refactor(llm): simplify the saving process --- .../widgets/type_widgets/llm_chat/LlmChat.tsx | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx index 667566d9c4..f22e8016cc 100644 --- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx +++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx @@ -1,21 +1,22 @@ -import { useCallback, useEffect, useState } from "preact/hooks"; +import "./LlmChat.css"; + +import { useCallback, useEffect, useRef } from "preact/hooks"; import { t } from "../../../services/i18n.js"; -import NoItems from "../../react/NoItems.js"; import { useEditorSpacedUpdate } from "../../react/hooks.js"; +import NoItems from "../../react/NoItems.js"; import { TypeWidgetProps } from "../type_widget.js"; import ChatInputBar from "./ChatInputBar.js"; import ChatMessage from "./ChatMessage.js"; import type { LlmChatContent } from "./llm_chat_types.js"; import { useLlmChat } from "./useLlmChat.js"; -import "./LlmChat.css"; export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) { - const [shouldSave, setShouldSave] = useState(false); + const spacedUpdateRef = useRef<{ scheduleUpdate: () => void }>(null); const chat = useLlmChat( // onMessagesChange - trigger save - () => setShouldSave(true), + () => spacedUpdateRef.current?.scheduleUpdate(), { defaultEnableNoteTools: false, supportsExtendedThinking: true, chatNoteId: note?.noteId } ); @@ -46,17 +47,10 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) { } } }); - - // Trigger save after state updates when shouldSave is set - useEffect(() => { - if (shouldSave) { - setShouldSave(false); - spacedUpdate.scheduleUpdate(); - } - }, [shouldSave, spacedUpdate]); + spacedUpdateRef.current = spacedUpdate; const triggerSave = useCallback(() => { - setShouldSave(true); + spacedUpdateRef.current?.scheduleUpdate(); }, []); return (