WorkspaceWebRemoteControlTrigger.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { useState } from "react";
  2. import { Smartphone } from "lucide-react";
  3. import type { WebRemoteControlStatus } from "@zcode/shared";
  4. import {
  5. buildWebRemoteControlEntryViewTelemetry,
  6. parseRemoteWorkspaceIdentity,
  7. } from "@zcode/shared";
  8. import { Button } from "@/components/ui/button.js";
  9. import { cn } from "@/components/lib/utils.js";
  10. import { ControlHintTooltip } from "@/ControlHintTooltip.js";
  11. import { useResolvedRemoteWorkspaceSessionId } from "@/hooks/useResolvedRemoteWorkspaceSessionId.js";
  12. import { useOptionalPlatform } from "@/hooks/usePlatform.js";
  13. import { useWebRemoteControlStatus } from "@/hooks/useWebRemoteControlStatus.js";
  14. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  15. import { logger } from "@/logger.js";
  16. import { reportAppTelemetryEvent } from "@/lib/appTelemetry.js";
  17. import { WebRemoteControlDialog } from "@/WebRemoteControlDialog.js";
  18. import { useWebRemoteControlFeatureEnabled } from "@/WebRemoteControlFeatureProvider.js";
  19. type FormatMessage = ReturnType<typeof useZCodeIntl>["intl"]["formatMessage"];
  20. export function getWebRemoteControlTriggerPresentation(
  21. status: Pick<WebRemoteControlStatus, "status" | "mobileConnected">,
  22. formatMessage: FormatMessage,
  23. ): { iconClassName: string; tooltip: string } {
  24. if (status.status === "error") {
  25. return {
  26. iconClassName: "text-destructive",
  27. tooltip: formatMessage({ id: "webRemoteControl.triggerStatus.error" }),
  28. };
  29. }
  30. if (status.status === "active") {
  31. // Bugfix: relay 重连时 main 可能先把 status 降到 starting/running,
  32. // 但 mobileConnected 仍保留上一轮配对状态。按钮只按 active 显示已连接,
  33. // 避免和打开弹层后的真实连接阶段文案不一致。
  34. return {
  35. iconClassName: "text-success",
  36. tooltip: formatMessage({
  37. id: "webRemoteControl.triggerStatus.connected",
  38. }),
  39. };
  40. }
  41. if (status.status === "starting") {
  42. return {
  43. iconClassName: "text-warning",
  44. tooltip: formatMessage({
  45. id: "webRemoteControl.triggerStatus.starting",
  46. }),
  47. };
  48. }
  49. if (status.status === "connecting") {
  50. return {
  51. iconClassName: "text-warning",
  52. tooltip: formatMessage({
  53. id: "webRemoteControl.triggerStatus.connecting",
  54. }),
  55. };
  56. }
  57. if (status.status === "running") {
  58. return {
  59. iconClassName: "text-warning",
  60. tooltip: formatMessage({ id: "webRemoteControl.triggerStatus.waiting" }),
  61. };
  62. }
  63. return {
  64. iconClassName: "text-foreground-subtle",
  65. tooltip: formatMessage({ id: "webRemoteControl.triggerStatus.idle" }),
  66. };
  67. }
  68. export function WorkspaceWebRemoteControlTrigger({
  69. workspacePath,
  70. workspaceIdentity,
  71. remoteSessionId,
  72. initialTaskId,
  73. compact = false,
  74. className,
  75. }: {
  76. workspacePath: string;
  77. workspaceIdentity?: string;
  78. remoteSessionId?: string;
  79. initialTaskId?: string;
  80. compact?: boolean;
  81. className?: string;
  82. }) {
  83. const enabled = useWebRemoteControlFeatureEnabled();
  84. // Bugfix: 生产默认隐藏时如果继续执行远程 session 解析 hook,会依赖 TabStoreProvider 并导致静态渲染提前抛错。
  85. // 这里先读无 store 依赖的 feature provider,再决定是否进入真正的入口内容。
  86. if (!enabled) {
  87. return null;
  88. }
  89. return (
  90. <WorkspaceWebRemoteControlTriggerContent
  91. workspacePath={workspacePath}
  92. workspaceIdentity={workspaceIdentity}
  93. remoteSessionId={remoteSessionId}
  94. initialTaskId={initialTaskId}
  95. compact={compact}
  96. className={className}
  97. />
  98. );
  99. }
  100. function WorkspaceWebRemoteControlTriggerContent({
  101. workspacePath,
  102. workspaceIdentity,
  103. remoteSessionId,
  104. initialTaskId,
  105. compact = false,
  106. className,
  107. }: {
  108. workspacePath: string;
  109. workspaceIdentity?: string;
  110. remoteSessionId?: string;
  111. initialTaskId?: string;
  112. compact?: boolean;
  113. className?: string;
  114. }) {
  115. const { intl } = useZCodeIntl();
  116. const platform = useOptionalPlatform();
  117. const mappedRemoteSessionId = useResolvedRemoteWorkspaceSessionId(
  118. workspacePath,
  119. remoteSessionId,
  120. workspaceIdentity,
  121. );
  122. // 修复原因:tab 上的 remoteSessionId 可能在断线重连期间失效;远控链路同时携带
  123. // identity 与 session,若继续透传未注册的旧值会组合成错误 endpoint。这里只采用
  124. // resolver 按 identity 校验后的 live session,未恢复时让弹层保持 remote-waiting。
  125. const currentRemoteSessionId = mappedRemoteSessionId ?? undefined;
  126. const [webRemoteControlOpen, setWebRemoteControlOpen] = useState(false);
  127. const remoteControlStatus = useWebRemoteControlStatus();
  128. const triggerPresentation = getWebRemoteControlTriggerPresentation(
  129. remoteControlStatus,
  130. intl.formatMessage,
  131. );
  132. return (
  133. <>
  134. <ControlHintTooltip
  135. title={intl.formatMessage({ id: "webRemoteControl.trigger" })}
  136. description={triggerPresentation.tooltip}
  137. side="top"
  138. align="center"
  139. triggerClassName={compact ? undefined : "w-full"}
  140. >
  141. <Button
  142. variant="ghost"
  143. onClick={() => {
  144. if (platform) {
  145. void reportAppTelemetryEvent(
  146. platform,
  147. buildWebRemoteControlEntryViewTelemetry({
  148. workspaceKind:
  149. workspaceIdentity?.trim() || currentRemoteSessionId ? "remote" : "local",
  150. remoteKind: workspaceIdentity
  151. ? parseRemoteWorkspaceIdentity(workspaceIdentity)?.kind
  152. : undefined,
  153. }),
  154. "web-remote-control-entry",
  155. );
  156. }
  157. logger.info("[WorkspaceWebRemoteControlTrigger] 打开 Web 远程控制弹层", {
  158. workspacePath,
  159. workspaceIdentity: workspaceIdentity ?? "none",
  160. remoteSessionId: currentRemoteSessionId ?? "none",
  161. initialTaskId: initialTaskId ?? "none",
  162. });
  163. setWebRemoteControlOpen(true);
  164. }}
  165. size={compact ? "icon-lg" : "lg"}
  166. aria-label={intl.formatMessage({ id: "webRemoteControl.trigger" })}
  167. className={cn(
  168. compact
  169. ? "text-foreground hover:bg-surface-hover hover:text-foreground"
  170. : "w-full justify-start gap-2 text-foreground hover:bg-surface-hover hover:text-foreground",
  171. className,
  172. )}
  173. >
  174. {/* Bugfix: 触发入口以前只显示固定手机图标,用户无法从桌面端判断手机是否已连上。
  175. 这里复用远控状态轮询结果,用语义色表达连接状态,并把入口名称和状态文案合进同一个 tooltip,
  176. 避免桌面端 footer 外层和按钮内层同时弹出两份提示。 */}
  177. <Smartphone className={cn("size-4", triggerPresentation.iconClassName)} />
  178. {compact ? (
  179. <span className="sr-only">
  180. {intl.formatMessage({ id: "webRemoteControl.trigger" })}
  181. </span>
  182. ) : (
  183. intl.formatMessage({ id: "webRemoteControl.trigger" })
  184. )}
  185. </Button>
  186. </ControlHintTooltip>
  187. <WebRemoteControlDialog
  188. open={webRemoteControlOpen}
  189. onOpenChange={setWebRemoteControlOpen}
  190. workspacePath={workspacePath}
  191. workspaceIdentity={workspaceIdentity}
  192. remoteSessionId={currentRemoteSessionId}
  193. initialTaskId={initialTaskId}
  194. />
  195. </>
  196. );
  197. }