Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

perf(web): split session viewer from chat and mastery route scripts

Bingxi Zhao (Frank)c824124f6db3
diff --git a/web/components/chat/home/LazySessionViewerPanel.tsx b/web/components/chat/home/LazySessionViewerPanel.tsx
new file mode 100644
index 000000000..3a966d58f
--- /dev/null
+++ b/web/components/chat/home/LazySessionViewerPanel.tsx
@@ -0,0 +1,86 @@
+"use client";
+
+import dynamic from "next/dynamic";
+import {
+ forwardRef,
+ useCallback,
+ useImperativeHandle,
+ useRef,
+} from "react";
+import type {
+ SessionViewerPanelHandle,
+ SessionViewerPanelProps,
+} from "./SessionViewerPanel";
+
+export type { SessionViewerPanelHandle } from "./SessionViewerPanel";
+
+// Split the viewer's activity, follow-up, and consultation UI from the
+// initial chat and mastery route scripts while warming it on hydration.
+const LoadedSessionViewerPanel = dynamic(
+ () => import("./SessionViewerPanel"),
+ { ssr: false },
+);
+
+type QueuedCall = {
+ sessionId: string | null;
+ run: (panel: SessionViewerPanelHandle) => void;
+};
+
+const LazySessionViewerPanel = forwardRef<
+ SessionViewerPanelHandle,
+ SessionViewerPanelProps
+>(function LazySessionViewerPanel(props, ref) {
+ const panelRef = useRef<SessionViewerPanelHandle | null>(null);
+ const queuedCalls = useRef<QueuedCall[]>([]);
+
+ const callPanel = useCallback(
+ (run: QueuedCall["run"]) => {
+ if (panelRef.current) {
+ run(panelRef.current);
+ } else {
+ queuedCalls.current.push({ sessionId: props.sessionId, run });
+ }
+ },
+ [props.sessionId],
+ );
+
+ // Chat events and attachment clicks can arrive while the panel chunk is
+ // loading. Replay them in order once its imperative ref becomes available.
+ const setPanelRef = useCallback(
+ (panel: SessionViewerPanelHandle | null) => {
+ panelRef.current = panel;
+ if (!panel) return;
+ const calls = queuedCalls.current;
+ queuedCalls.current = [];
+ for (const call of calls) {
+ if (call.sessionId === props.sessionId) call.run(panel);
+ }
+ },
+ [props.sessionId],
+ );
+
+ useImperativeHandle(
+ ref,
+ () => ({
+ openFileTab: (attachment) =>
+ callPanel((panel) => panel.openFileTab(attachment)),
+ openWebTab: (url) => callPanel((panel) => panel.openWebTab(url)),
+ openMarkdownNoteTab: () =>
+ callPanel((panel) => panel.openMarkdownNoteTab()),
+ openQuizFollowupTab: (context) =>
+ callPanel((panel) => panel.openQuizFollowupTab(context)),
+ openSelectionTutorTab: (selection, language) =>
+ callPanel((panel) => panel.openSelectionTutorTab(selection, language)),
+ openGeogebraTab: (payload) =>
+ callPanel((panel) => panel.openGeogebraTab(payload)),
+ openSubagentTab: (callId, label, events, focus) =>
+ callPanel((panel) => panel.openSubagentTab(callId, label, events, focus)),
+ focusActivityHome: () => callPanel((panel) => panel.focusActivityHome()),
+ }),
+ [callPanel],
+ );
+
+ return <LoadedSessionViewerPanel {...props} ref={setPanelRef} />;
+});
+
+export default LazySessionViewerPanel;
diff --git a/web/components/chat/home/SessionViewerPanel.tsx b/web/components/chat/home/SessionViewerPanel.tsx
index 80c37be6d..fc0615423 100644
--- a/web/components/chat/home/SessionViewerPanel.tsx
+++ b/web/components/chat/home/SessionViewerPanel.tsx
@@ -193,7 +193,7 @@ export interface SessionViewerPanelHandle {
focusActivityHome(): void;
}
-interface SessionViewerPanelProps {
+export interface SessionViewerPanelProps {
open: boolean;
sessionId: string | null;
onClose: () => void;
diff --git a/web/components/space/learning/MasteryStudy.tsx b/web/components/space/learning/MasteryStudy.tsx
index 04a01f34c..42f127924 100644
--- a/web/components/space/learning/MasteryStudy.tsx
+++ b/web/components/space/learning/MasteryStudy.tsx
@@ -28,7 +28,7 @@ import { buildSessionActivity } from "@/components/chat/home/SessionActivityPane
import { TurnNavigator } from "@/components/chat/home/TurnNavigator";
import SessionViewerPanel, {
type SessionViewerPanelHandle,
-} from "@/components/chat/home/SessionViewerPanel";
+} from "@/components/chat/home/LazySessionViewerPanel";
import {
type MessageAttachment,
useChatStateAdapter,
diff --git a/web/features/chat/components/ChatWorkspace.tsx b/web/features/chat/components/ChatWorkspace.tsx
index 6864506b0..27d14833c 100644
--- a/web/features/chat/components/ChatWorkspace.tsx
+++ b/web/features/chat/components/ChatWorkspace.tsx
@@ -53,7 +53,7 @@ import { buildSessionActivity } from "@/components/chat/home/SessionActivityPane
import Tooltip from "@/shared/ui/Tooltip";
import SessionViewerPanel, {
type SessionViewerPanelHandle,
-} from "@/components/chat/home/SessionViewerPanel";
+} from "@/components/chat/home/LazySessionViewerPanel";
import {
QuizFollowupProvider,
useQuizFollowupController,
diff --git a/web/tests/lazy-session-viewer-panel.spec.tsx b/web/tests/lazy-session-viewer-panel.spec.tsx
new file mode 100644
index 000000000..2b79d3674
--- /dev/null
+++ b/web/tests/lazy-session-viewer-panel.spec.tsx
@@ -0,0 +1,85 @@
+import { act, cleanup, render } from "@testing-library/react";
+import { createRef } from "react";
+import { afterEach, expect, it, vi } from "vitest";
+import LazySessionViewerPanel, {
+ type SessionViewerPanelHandle,
+} from "@/components/chat/home/LazySessionViewerPanel";
+import type { SessionViewerPanelProps } from "@/components/chat/home/SessionViewerPanel";
+
+const fixture = vi.hoisted(() => ({
+ ready: false,
+ calls: [] as string[],
+}));
+
+vi.mock("next/dynamic", async () => {
+ const { forwardRef, useImperativeHandle } = await import("react");
+ const LoadedPanel = forwardRef<SessionViewerPanelHandle>(function LoadedPanel(
+ _props,
+ ref,
+ ) {
+ useImperativeHandle(ref, () => ({
+ openFileTab: () => fixture.calls.push("file"),
+ openWebTab: (url) => fixture.calls.push(url),
+ openMarkdownNoteTab: () => fixture.calls.push("markdown"),
+ openQuizFollowupTab: () => fixture.calls.push("quiz"),
+ openSelectionTutorTab: () => fixture.calls.push("selection"),
+ openGeogebraTab: () => fixture.calls.push("geogebra"),
+ openSubagentTab: () => fixture.calls.push("subagent"),
+ focusActivityHome: () => fixture.calls.push("activity"),
+ }));
+ return null;
+ });
+ return {
+ default: () =>
+ forwardRef<SessionViewerPanelHandle>(function DeferredPanel(_props, ref) {
+ return fixture.ready ? <LoadedPanel ref={ref} /> : null;
+ }),
+ };
+});
+
+afterEach(() => {
+ cleanup();
+ fixture.ready = false;
+ fixture.calls = [];
+});
+
+it("replays viewer actions in order when the chunk loads", () => {
+ const ref = createRef<SessionViewerPanelHandle>();
+ const props = {
+ open: false,
+ sessionId: "session-a",
+ activity: {} as SessionViewerPanelProps["activity"],
+ onClose: () => {},
+ onAutoOpen: () => {},
+ };
+ const view = render(<LazySessionViewerPanel {...props} ref={ref} />);
+
+ act(() => {
+ ref.current?.openWebTab("https://example.com");
+ ref.current?.focusActivityHome();
+ });
+ expect(fixture.calls).toEqual([]);
+
+ fixture.ready = true;
+ view.rerender(<LazySessionViewerPanel {...props} ref={ref} />);
+ expect(fixture.calls).toEqual(["https://example.com", "activity"]);
+});
+
+it("drops queued actions from the previous session", () => {
+ const ref = createRef<SessionViewerPanelHandle>();
+ const props = {
+ open: false,
+ sessionId: "session-a",
+ activity: {} as SessionViewerPanelProps["activity"],
+ onClose: () => {},
+ onAutoOpen: () => {},
+ };
+ const view = render(<LazySessionViewerPanel {...props} ref={ref} />);
+ act(() => ref.current?.openWebTab("stale"));
+ view.rerender(<LazySessionViewerPanel {...props} sessionId="session-b" ref={ref} />);
+ act(() => ref.current?.openWebTab("current"));
+
+ fixture.ready = true;
+ view.rerender(<LazySessionViewerPanel {...props} sessionId="session-b" ref={ref} />);
+ expect(fixture.calls).toEqual(["current"]);
+});