WebRemoteControlMobileShell.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import { useCallback, useEffect, useLayoutEffect, useState, type ReactNode } from "react";
  2. import { ArrowLeft, LoaderCircle } from "lucide-react";
  3. import { type WebRemoteControlMobileNavigationIntent } from "@zcode/shared";
  4. import { Button } from "@/components/ui/button.js";
  5. import { WebRemoteControlThemeMenu } from "@/WebRemoteControlThemeMenu.js";
  6. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  7. import { WebRemoteControlTaskViewReporter } from "@/web-remote-control/index.js";
  8. import type { WebRemoteControlTerminalTransportState } from "@/root/types.js";
  9. import type { WebRemoteControlWorkspaceSwitcherApi } from "@/root/types.js";
  10. import { useWebRemoteControlTerminalTransportState } from "@/webRemoteControlTerminalTransportState.js";
  11. type WebRemoteControlMobilePage = "home" | "chat";
  12. function hasMobileHistoryPage(
  13. state: unknown,
  14. page: WebRemoteControlMobilePage,
  15. ): state is { zcodeMobilePage: WebRemoteControlMobilePage } {
  16. return (
  17. typeof state === "object" &&
  18. state !== null &&
  19. "zcodeMobilePage" in state &&
  20. state.zcodeMobilePage === page
  21. );
  22. }
  23. function recordMobileChatHistory() {
  24. if (typeof window === "undefined") {
  25. return;
  26. }
  27. const state: unknown = window.history.state;
  28. if (hasMobileHistoryPage(state, "chat")) {
  29. return;
  30. }
  31. if (hasMobileHistoryPage(state, "home")) {
  32. window.history.replaceState({ ...state, zcodeMobilePage: "chat" }, "");
  33. } else {
  34. window.history.pushState({ zcodeMobilePage: "chat" }, "");
  35. }
  36. }
  37. function resolveInitialWebRemoteControlMobilePage(
  38. initialNavigationIntent?: WebRemoteControlMobileNavigationIntent,
  39. ): WebRemoteControlMobilePage {
  40. if (initialNavigationIntent === "chat") {
  41. return "chat";
  42. }
  43. // Bugfix: 手机端在聊天页刷新时,React 内存状态会丢失,但浏览器会保留当前 history.state。
  44. // 这里复用点击进入聊天页时写入的 history state,避免刷新后只恢复 task 选中态却回到任务首页。
  45. if (typeof window !== "undefined" && hasMobileHistoryPage(window.history.state, "chat")) {
  46. return "chat";
  47. }
  48. return "home";
  49. }
  50. export function WebRemoteControlMobileShell({
  51. children,
  52. isMobileViewport = true,
  53. activeTaskId,
  54. activeWorkspaceIdentity,
  55. activeWorkspacePath,
  56. taskViewEnabled = true,
  57. chatContent,
  58. chatOverlay,
  59. initialNavigationIntent,
  60. isSidePaneOpen = false,
  61. onCloseSidePane,
  62. renderTaskHome,
  63. renderChatHeader,
  64. sidePaneContent,
  65. switcher,
  66. webRemoteControlTerminalTransportState,
  67. }: {
  68. children?: ReactNode;
  69. isMobileViewport?: boolean;
  70. switcher: WebRemoteControlWorkspaceSwitcherApi;
  71. activeWorkspacePath: string;
  72. activeWorkspaceIdentity?: string;
  73. activeTaskId: string | null;
  74. taskViewEnabled?: boolean;
  75. initialNavigationIntent?: WebRemoteControlMobileNavigationIntent;
  76. isSidePaneOpen?: boolean;
  77. onCloseSidePane?: () => void;
  78. renderTaskHome: (options: {
  79. navigateToChat(): void;
  80. setSwitching(switching: boolean): void;
  81. }) => ReactNode;
  82. renderChatHeader: (options: { onBackHome: () => void }) => ReactNode;
  83. chatContent: ReactNode;
  84. chatOverlay?: ReactNode;
  85. sidePaneContent?: ReactNode;
  86. webRemoteControlTerminalTransportState?: WebRemoteControlTerminalTransportState;
  87. }) {
  88. const { intl } = useZCodeIntl();
  89. const [currentPage, setCurrentPage] = useState<WebRemoteControlMobilePage>(() =>
  90. resolveInitialWebRemoteControlMobilePage(initialNavigationIntent),
  91. );
  92. const [switchingWorkspace, setSwitchingWorkspace] = useState(false);
  93. const terminalTransportState = useWebRemoteControlTerminalTransportState(
  94. webRemoteControlTerminalTransportState,
  95. );
  96. const shouldInertChatSurface = Boolean(sidePaneContent) && isSidePaneOpen;
  97. const navigateToChat = useCallback(() => {
  98. setCurrentPage("chat");
  99. }, []);
  100. const navigateHome = useCallback(() => {
  101. // 聊天可能是历史首项,浏览器无法后退。箭头直接回列表,并同步历史供刷新恢复。
  102. setCurrentPage("home");
  103. const state: unknown = window.history.state;
  104. if (hasMobileHistoryPage(state, "chat")) {
  105. window.history.replaceState({ ...state, zcodeMobilePage: "home" }, "");
  106. }
  107. }, []);
  108. useEffect(() => {
  109. // 初始意图只随导航变化消费;断点恢复不能重放旧 chat 意图,覆盖用户已经返回的 Home。
  110. if (initialNavigationIntent === "chat") setCurrentPage("chat");
  111. }, [initialNavigationIntent]);
  112. useLayoutEffect(() => {
  113. if (isMobileViewport && currentPage === "chat") recordMobileChatHistory();
  114. }, [isMobileViewport, currentPage]);
  115. useEffect(() => {
  116. if (!isMobileViewport || typeof window === "undefined") {
  117. return;
  118. }
  119. const handlePopState = (event: PopStateEvent) => {
  120. const nextPage = hasMobileHistoryPage(event.state, "chat") ? "chat" : "home";
  121. setCurrentPage(nextPage);
  122. };
  123. window.addEventListener("popstate", handlePopState);
  124. return () => {
  125. window.removeEventListener("popstate", handlePopState);
  126. };
  127. }, [isMobileViewport]);
  128. // Bugfix: 移动端聊天/侧栏都是 flex 子树,默认 min-width:auto 会按长消息的
  129. // min-content 宽度撑开抽屉;窄屏随后从左侧裁掉正文。沿整条宽度链显式允许收缩,
  130. // 让 subagent 输出、进程面板和正文在抽屉内换行。
  131. return (
  132. <>
  133. <WebRemoteControlTaskViewReporter
  134. enabled={
  135. taskViewEnabled &&
  136. (!isMobileViewport ||
  137. (currentPage === "chat" && !shouldInertChatSurface && !switchingWorkspace))
  138. }
  139. activeTaskId={activeTaskId}
  140. workspacePath={activeWorkspacePath}
  141. workspaceIdentity={activeWorkspaceIdentity}
  142. switcher={switcher}
  143. />
  144. {isMobileViewport ? (
  145. <div className="relative flex h-full min-h-0 min-w-0 flex-col bg-background">
  146. {currentPage === "home" ? (
  147. <section className="flex h-full min-h-0 min-w-0 flex-col" data-mobile-page="home">
  148. {renderTaskHome({ navigateToChat, setSwitching: setSwitchingWorkspace })}
  149. </section>
  150. ) : (
  151. <section
  152. className="flex h-full min-h-0 min-w-0 flex-col bg-background text-foreground"
  153. data-mobile-page="chat"
  154. >
  155. <div className="flex h-11 shrink-0 items-center gap-2 border-b border-border bg-header px-2">
  156. <Button
  157. type="button"
  158. variant="ghost"
  159. size="icon-sm"
  160. aria-label={intl.formatMessage({
  161. id: "webRemoteControl.mobileShell.backHome",
  162. })}
  163. onClick={navigateHome}
  164. >
  165. <ArrowLeft className="size-4" />
  166. </Button>
  167. <span className="min-w-0 flex-1 truncate text-ui-base font-medium">
  168. {intl.formatMessage({ id: "webRemoteControl.mobileShell.chatTitle" })}
  169. </span>
  170. <WebRemoteControlThemeMenu />
  171. </div>
  172. {renderChatHeader({ onBackHome: navigateHome })}
  173. <div className="relative flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
  174. <main
  175. className="flex h-full min-h-0 min-w-0 flex-1 flex-col overflow-hidden"
  176. aria-hidden={shouldInertChatSurface ? true : undefined}
  177. data-mobile-chat-surface={shouldInertChatSurface ? "inert" : undefined}
  178. inert={shouldInertChatSurface ? true : undefined}
  179. >
  180. {/* Bugfix: 手机侧栏打开时底部 composer 会被抽屉覆盖。
  181. 如果聊天区仍留在交互树里,模型选择等控件会被遮挡但仍可被浏览器命中,
  182. 造成“按钮不可点”的误导。这里把被覆盖的聊天表面设为 inert,
  183. 让右侧抽屉成为唯一可交互区域。 */}
  184. {chatContent}
  185. </main>
  186. {sidePaneContent ? (
  187. <div
  188. className={[
  189. "absolute inset-0 z-30 transition-opacity duration-200 ease-out",
  190. isSidePaneOpen ? "opacity-100" : "pointer-events-none opacity-0",
  191. ].join(" ")}
  192. aria-hidden={!isSidePaneOpen}
  193. data-mobile-side-pane-overlay="true"
  194. >
  195. <button
  196. type="button"
  197. className="absolute inset-0 bg-background/60 backdrop-blur-[1px]"
  198. aria-label={intl.formatMessage({ id: "sidePane.collapse" })}
  199. onClick={onCloseSidePane}
  200. />
  201. <div
  202. className={[
  203. "absolute top-0 right-0 h-full w-[min(88vw,28rem)] max-w-full border-l border-border bg-background shadow-2xl transition-transform duration-200 ease-out",
  204. isSidePaneOpen ? "translate-x-0" : "translate-x-full",
  205. ].join(" ")}
  206. >
  207. {/* Bugfix: 手机远控之前把 side pane 作为底部纵向面板渲染,
  208. 会挤压聊天区且不像桌面右侧栏。这里改成右侧覆盖抽屉,
  209. 只覆盖手机聊天区,不改变桌面端分栏布局。 */}
  210. {sidePaneContent}
  211. </div>
  212. </div>
  213. ) : null}
  214. {chatOverlay ? (
  215. <div
  216. className="pointer-events-none absolute inset-0 z-40"
  217. data-mobile-chat-overlay="true"
  218. >
  219. {/* Bugfix: 查找框属于聊天视觉区域,但不能成为 inert 聊天表面的后代。
  220. 独立 overlay 保留抽屉隔离,同时让文件变更查找仍可聚焦、切换和关闭。 */}
  221. <div className="pointer-events-auto">{chatOverlay}</div>
  222. </div>
  223. ) : null}
  224. </div>
  225. </section>
  226. )}
  227. {switchingWorkspace ? (
  228. <div
  229. className="absolute inset-0 z-40 flex items-center justify-center bg-background/72 backdrop-blur-[1px]"
  230. aria-live="polite"
  231. aria-busy="true"
  232. >
  233. <div className="flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-ui-base text-foreground-subtle shadow-sm">
  234. <LoaderCircle className="size-4 animate-spin" />
  235. {intl.formatMessage({ id: "common.loading" })}
  236. </div>
  237. </div>
  238. ) : null}
  239. {terminalTransportState === "reconnecting" ? (
  240. <div
  241. className="pointer-events-none absolute top-3 left-1/2 z-50 flex -translate-x-1/2 justify-center px-3"
  242. aria-live="polite"
  243. aria-busy="true"
  244. data-web-remote-control-reconnect-notice="true"
  245. >
  246. <div className="flex max-w-[calc(100vw-1.5rem)] items-center gap-2 rounded-lg border border-border bg-toast px-3 py-2 text-ui-base text-foreground shadow-lg">
  247. <LoaderCircle className="size-4 shrink-0 animate-spin text-foreground-subtle" />
  248. <span className="min-w-0 whitespace-nowrap">
  249. {intl.formatMessage({ id: "webRemoteControl.mobileShell.reconnecting" })}
  250. </span>
  251. </div>
  252. </div>
  253. ) : null}
  254. </div>
  255. ) : (
  256. children
  257. )}
  258. </>
  259. );
  260. }