Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): leave a deferred focus to the shown type widget, not a hidden chat

Elian Doran6f655393c5dd
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.spec.tsx
new file mode 100644
index 0000000000..c0fcdd821b
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.spec.tsx
@@ -0,0 +1,50 @@
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import type Component from "../../../components/component";
+import { ParentComponent } from "../../react/react_utils";
+import { useFocusInputOnDetail } from "./LlmChat";
+
+describe("useFocusInputOnDetail", () => {
+ const container = document.createElement("div");
+ const focusInput = vi.fn();
+ const handlers = new Map<string, (data: unknown) => void>();
+ const parent = {
+ registerHandler: (name: string, callback: (data: unknown) => void) => handlers.set(name, callback),
+ removeHandler: () => {}
+ } as unknown as Component;
+
+ function Harness({ isVisible }: { isVisible?: boolean }) {
+ useFocusInputOnDetail(focusInput, "ntx-1", isVisible);
+ return null;
+ }
+ function renderHarness(isVisible?: boolean) {
+ act(() => render(
+ <ParentComponent.Provider value={parent}>
+ <Harness isVisible={isVisible} />
+ </ParentComponent.Provider>,
+ container
+ ));
+ }
+ const focusOnDetail = (ntxId: string) => handlers.get("focusOnDetail")?.({ ntxId });
+
+ afterEach(() => {
+ render(null, container);
+ focusInput.mockReset();
+ });
+
+ it("focuses the input of the shown chat in the requested context only", () => {
+ renderHarness(true);
+ expect(handlers.has("focusOnDetail")).toBe(true);
+ focusOnDetail("ntx-2");
+ expect(focusInput).not.toHaveBeenCalled();
+ focusOnDetail("ntx-1");
+ expect(focusInput).toHaveBeenCalledOnce();
+
+ // A chat kept mounted but hidden after its note's type changed leaves the focus to the shown widget.
+ renderHarness(false);
+ focusOnDetail("ntx-1");
+ expect(focusInput).toHaveBeenCalledOnce();
+ });
+});
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 5e79f4acbc..aac377c3bf 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
@@ -15,7 +15,7 @@ import ChatReadOnlyNotice from "./ChatReadOnlyNotice.js";
import type { LlmChatContent } from "./llm_chat_types.js";
import { useLlmChat } from "./useLlmChat.js";
-export default function LlmChat({ note, noteContext }: TypeWidgetProps) {
+export default function LlmChat({ note, noteContext, isVisible }: TypeWidgetProps) {
const spacedUpdateRef = useRef<{ scheduleUpdate: () => void }>(null);
// A `#readOnly` chat is immutable: the reply bar is replaced by a notice and every
@@ -46,9 +46,7 @@ export default function LlmChat({ note, noteContext }: TypeWidgetProps) {
useChatMessageJumps(chat.scrollContainerRef);
// Switching to the tab or creating the note focuses the reply input, like other types' editors.
- useTriliumEvent("focusOnDetail", ({ ntxId }) => {
- if (ntxId === noteContext?.ntxId) chat.focusInput();
- });
+ useFocusInputOnDetail(chat.focusInput, noteContext?.ntxId, isVisible);
const spacedUpdate = useEditorSpacedUpdate({
note,
@@ -99,3 +97,15 @@ export default function LlmChat({ note, noteContext }: TypeWidgetProps) {
</div>
);
}
+
+/**
+ * Focuses the reply input on a `focusOnDetail` for this chat's context. A chat kept mounted but
+ * hidden after its note's type changed (`isVisible` is `false`) ignores it.
+ */
+export function useFocusInputOnDetail(
+ focusInput: () => void, ntxId: string | null | undefined, isVisible: boolean | undefined
+) {
+ useTriliumEvent("focusOnDetail", (data) => {
+ if (data.ntxId === ntxId && isVisible !== false) focusInput();
+ });
+}