From 899b6a3b5b2e91be2a44b5b294e5d74a41b88a71 Mon Sep 17 00:00:00 2001 From: "dayuan.jiang" Date: Sun, 11 Oct 2026 11:08:17 +0900 Subject: [PATCH] refactor(canvas): decouple the canvas from the chat engine The canvas components will be reused by the MCP server's browser shell, which has no chat and no Next.js. They now read everything they need from small contexts and the canvas store instead of the chat engine: - components/canvas/versions-context.tsx: VersionsProvider and useVersionsContext give the version cards, the strip and the compare dialog a VersionsSource (versions, onCanvasId, undoneId, isBusy, canUndo, canRedo, restore, undo, redo). The chat fills it from the versions store and the engine in components/chat/chat-versions.tsx. - stores/canvas-store.ts: isBusy and busyReason; the chat engine sets them while a turn runs, SelectionAsk and the version UI read them. - components/canvas/version-card.tsx: the visual version card and the thumbnail, out of tool-activity.tsx; the chat's card composes it and adds its "Show XML" link and code panel. - compare-dialog.tsx and version-strip.tsx move to components/canvas; the strip takes the minimum number of versions to show as a prop, the chat panel computes it from the cards it has. - components/canvas/locale-context.tsx: LocaleProvider and useLocale, fed by the [lang] layout; CanvasStage no longer uses next/navigation. The overlay (SelectionAsk) is a slot and the wait for the saved language is a prop, so the shell can leave both out. - lib/version-text.ts: describeChanges is now describeChangeSummary, so it can be imported next to the MCP core's describeChanges. --- app/[lang]/layout.tsx | 5 +- components/canvas/canvas-stage.tsx | 58 +++-- .../{chat => canvas}/compare-dialog.tsx | 18 +- components/canvas/locale-context.tsx | 29 +++ components/canvas/selection-ask.tsx | 6 +- components/canvas/version-card.tsx | 217 ++++++++++++++++ components/{chat => canvas}/version-strip.tsx | 38 +-- components/canvas/versions-context.tsx | 72 ++++++ components/chat/chat-engine.tsx | 5 + components/chat/chat-panel.tsx | 24 +- components/chat/chat-versions.tsx | 46 ++++ components/chat/tool-activity.tsx | 237 +++--------------- components/workspace/workspace.tsx | 15 +- lib/diagram-diff.ts | 5 +- lib/version-text.ts | 2 +- stores/canvas-store.ts | 11 +- stores/versions-store.ts | 26 +- tests/unit/redesign-helpers.test.ts | 10 +- 18 files changed, 527 insertions(+), 297 deletions(-) rename components/{chat => canvas}/compare-dialog.tsx (92%) create mode 100644 components/canvas/locale-context.tsx create mode 100644 components/canvas/version-card.tsx rename components/{chat => canvas}/version-strip.tsx (69%) create mode 100644 components/canvas/versions-context.tsx create mode 100644 components/chat/chat-versions.tsx diff --git a/app/[lang]/layout.tsx b/app/[lang]/layout.tsx index bc2fd112..2e09e6b9 100644 --- a/app/[lang]/layout.tsx +++ b/app/[lang]/layout.tsx @@ -3,6 +3,7 @@ import type { Metadata, Viewport } from "next" import { Instrument_Sans, JetBrains_Mono } from "next/font/google" import { notFound } from "next/navigation" import Script from "next/script" +import { LocaleProvider } from "@/components/canvas/locale-context" import { AppToaster } from "@/components/workspace/app-toaster" import { DiagramProvider } from "@/contexts/diagram-context" import { DictionaryProvider } from "@/hooks/use-dictionary" @@ -184,7 +185,9 @@ export default async function RootLayout({ className={`${instrumentSans.variable} ${jetbrainsMono.variable} antialiased`} > - {children} + + {children} + diff --git a/components/canvas/canvas-stage.tsx b/components/canvas/canvas-stage.tsx index 2cb1617e..c7f52fd1 100644 --- a/components/canvas/canvas-stage.tsx +++ b/components/canvas/canvas-stage.tsx @@ -1,8 +1,6 @@ -import { useParams } from "next/navigation" import { useEffect, useRef, useState } from "react" import { DrawioFrame } from "@/components/canvas/drawio-frame" -import { SelectionAsk } from "@/components/canvas/selection-ask" -import { useChatEngine } from "@/components/chat/chat-engine" +import { useLocale } from "@/components/canvas/locale-context" import { useDiagram } from "@/contexts/diagram-context" import { useDictionary } from "@/hooks/use-dictionary" import { DRAWIO_CSS, getDrawioSrc } from "@/lib/drawio/drawio-config" @@ -13,28 +11,38 @@ import { cn } from "@/lib/utils" import { useCanvasStore } from "@/stores/canvas-store" import { useSettingsStore } from "@/stores/settings-store" +/** Size of the canvas area, for overlays that stay inside it */ +export interface CanvasSize { + width: number + height: number +} + /** * The draw.io canvas, with draw.io's own toolbar, menus and page tabs, and - * the "ask AI" button laid over it (only when the page can drive draw.io - * directly, not with an external cross-origin draw.io). + * an overlay laid over it (the web app's "ask AI" button; only when the + * page can drive draw.io directly, not with an external cross-origin + * draw.io). */ export function CanvasStage({ className, style, hideOverlays = false, + overlay, + waitForSavedLocale = false, }: { className?: string style?: React.CSSProperties - /** Hide the "ask AI" button (e.g. while the start screen covers the canvas) */ + /** Hide the overlay (e.g. while the start screen covers the canvas) */ hideOverlays?: boolean + /** Rendered over the canvas once draw.io is ready */ + overlay?: (size: CanvasSize) => React.ReactNode + /** Web app: the page is about to switch to the language picked last + * time (useSavedLocale in the workspace); do not load draw.io in the + * URL's language meanwhile */ + waitForSavedLocale?: boolean }) { const dict = useDictionary() - const params = useParams<{ lang: string }>() - const lang = ( - i18n.locales.includes(params.lang as Locale) - ? params.lang - : i18n.defaultLocale - ) as Locale + const lang = useLocale() const { drawioRef, handleDiagramAutoSave, @@ -43,7 +51,6 @@ export function CanvasStage({ resetDrawioReady, isDrawioReady, } = useDiagram() - const engine = useChatEngine() const isDark = useSettingsStore((s) => s.isDark) const hasEditor = useCanvasStore((s) => s.hasEditor) // Canvas width when the chat panel starts sliding in or out @@ -53,14 +60,19 @@ export function CanvasStage({ // null until draw.io tells us whether we can drive it directly const [editorAccess, setEditorAccess] = useState(null) const stageRef = useRef(null) - const [size, setSize] = useState({ width: 0, height: 0 }) + const [size, setSize] = useState({ width: 0, height: 0 }) useEffect(() => { - // The page is about to switch to the language picked last time - // (useSavedLocale): load draw.io once, in that language - const saved = localStorage.getItem(STORAGE_KEYS.locale) - if (saved && saved !== lang && i18n.locales.includes(saved as Locale)) { - return + // Load draw.io once, in the language the page will have + if (waitForSavedLocale) { + const saved = localStorage.getItem(STORAGE_KEYS.locale) + if ( + saved && + saved !== lang && + i18n.locales.includes(saved as Locale) + ) { + return + } } const isElectron = !!(window as { electronAPI?: unknown }).electronAPI // The theme class is set before the page renders; the settings store @@ -159,13 +171,7 @@ export function CanvasStage({ )} - {showOverlays && ( -