import { useCallback, useEffect, useLayoutEffect, useState, type ReactNode } from "react"; import { ArrowLeft, LoaderCircle } from "lucide-react"; import { type WebRemoteControlMobileNavigationIntent } from "@zcode/shared"; import { Button } from "@/components/ui/button.js"; import { WebRemoteControlThemeMenu } from "@/WebRemoteControlThemeMenu.js"; import { useZCodeIntl } from "@/i18n/IntlProvider.js"; import { WebRemoteControlTaskViewReporter } from "@/web-remote-control/index.js"; import type { WebRemoteControlTerminalTransportState } from "@/root/types.js"; import type { WebRemoteControlWorkspaceSwitcherApi } from "@/root/types.js"; import { useWebRemoteControlTerminalTransportState } from "@/webRemoteControlTerminalTransportState.js"; type WebRemoteControlMobilePage = "home" | "chat"; function hasMobileHistoryPage( state: unknown, page: WebRemoteControlMobilePage, ): state is { zcodeMobilePage: WebRemoteControlMobilePage } { return ( typeof state === "object" && state !== null && "zcodeMobilePage" in state && state.zcodeMobilePage === page ); } function recordMobileChatHistory() { if (typeof window === "undefined") { return; } const state: unknown = window.history.state; if (hasMobileHistoryPage(state, "chat")) { return; } if (hasMobileHistoryPage(state, "home")) { window.history.replaceState({ ...state, zcodeMobilePage: "chat" }, ""); } else { window.history.pushState({ zcodeMobilePage: "chat" }, ""); } } function resolveInitialWebRemoteControlMobilePage( initialNavigationIntent?: WebRemoteControlMobileNavigationIntent, ): WebRemoteControlMobilePage { if (initialNavigationIntent === "chat") { return "chat"; } // Bugfix: 手机端在聊天页刷新时,React 内存状态会丢失,但浏览器会保留当前 history.state。 // 这里复用点击进入聊天页时写入的 history state,避免刷新后只恢复 task 选中态却回到任务首页。 if (typeof window !== "undefined" && hasMobileHistoryPage(window.history.state, "chat")) { return "chat"; } return "home"; } export function WebRemoteControlMobileShell({ children, isMobileViewport = true, activeTaskId, activeWorkspaceIdentity, activeWorkspacePath, taskViewEnabled = true, chatContent, chatOverlay, initialNavigationIntent, isSidePaneOpen = false, onCloseSidePane, renderTaskHome, renderChatHeader, sidePaneContent, switcher, webRemoteControlTerminalTransportState, }: { children?: ReactNode; isMobileViewport?: boolean; switcher: WebRemoteControlWorkspaceSwitcherApi; activeWorkspacePath: string; activeWorkspaceIdentity?: string; activeTaskId: string | null; taskViewEnabled?: boolean; initialNavigationIntent?: WebRemoteControlMobileNavigationIntent; isSidePaneOpen?: boolean; onCloseSidePane?: () => void; renderTaskHome: (options: { navigateToChat(): void; setSwitching(switching: boolean): void; }) => ReactNode; renderChatHeader: (options: { onBackHome: () => void }) => ReactNode; chatContent: ReactNode; chatOverlay?: ReactNode; sidePaneContent?: ReactNode; webRemoteControlTerminalTransportState?: WebRemoteControlTerminalTransportState; }) { const { intl } = useZCodeIntl(); const [currentPage, setCurrentPage] = useState(() => resolveInitialWebRemoteControlMobilePage(initialNavigationIntent), ); const [switchingWorkspace, setSwitchingWorkspace] = useState(false); const terminalTransportState = useWebRemoteControlTerminalTransportState( webRemoteControlTerminalTransportState, ); const shouldInertChatSurface = Boolean(sidePaneContent) && isSidePaneOpen; const navigateToChat = useCallback(() => { setCurrentPage("chat"); }, []); const navigateHome = useCallback(() => { // 聊天可能是历史首项,浏览器无法后退。箭头直接回列表,并同步历史供刷新恢复。 setCurrentPage("home"); const state: unknown = window.history.state; if (hasMobileHistoryPage(state, "chat")) { window.history.replaceState({ ...state, zcodeMobilePage: "home" }, ""); } }, []); useEffect(() => { // 初始意图只随导航变化消费;断点恢复不能重放旧 chat 意图,覆盖用户已经返回的 Home。 if (initialNavigationIntent === "chat") setCurrentPage("chat"); }, [initialNavigationIntent]); useLayoutEffect(() => { if (isMobileViewport && currentPage === "chat") recordMobileChatHistory(); }, [isMobileViewport, currentPage]); useEffect(() => { if (!isMobileViewport || typeof window === "undefined") { return; } const handlePopState = (event: PopStateEvent) => { const nextPage = hasMobileHistoryPage(event.state, "chat") ? "chat" : "home"; setCurrentPage(nextPage); }; window.addEventListener("popstate", handlePopState); return () => { window.removeEventListener("popstate", handlePopState); }; }, [isMobileViewport]); // Bugfix: 移动端聊天/侧栏都是 flex 子树,默认 min-width:auto 会按长消息的 // min-content 宽度撑开抽屉;窄屏随后从左侧裁掉正文。沿整条宽度链显式允许收缩, // 让 subagent 输出、进程面板和正文在抽屉内换行。 return ( <> {isMobileViewport ? (
{currentPage === "home" ? (
{renderTaskHome({ navigateToChat, setSwitching: setSwitchingWorkspace })}
) : (
{intl.formatMessage({ id: "webRemoteControl.mobileShell.chatTitle" })}
{renderChatHeader({ onBackHome: navigateHome })}
{/* Bugfix: 手机侧栏打开时底部 composer 会被抽屉覆盖。 如果聊天区仍留在交互树里,模型选择等控件会被遮挡但仍可被浏览器命中, 造成“按钮不可点”的误导。这里把被覆盖的聊天表面设为 inert, 让右侧抽屉成为唯一可交互区域。 */} {chatContent}
{sidePaneContent ? (
) : null} {chatOverlay ? (
{/* Bugfix: 查找框属于聊天视觉区域,但不能成为 inert 聊天表面的后代。 独立 overlay 保留抽屉隔离,同时让文件变更查找仍可聚焦、切换和关闭。 */}
{chatOverlay}
) : null}
)} {switchingWorkspace ? (
{intl.formatMessage({ id: "common.loading" })}
) : null} {terminalTransportState === "reconnecting" ? (
{intl.formatMessage({ id: "webRemoteControl.mobileShell.reconnecting" })}
) : null}
) : ( children )} ); }