RemoteConnectionDialogContent.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* eslint-disable max-lines -- 远程连接向导的多个步骤暂集中在同一文件,避免拆分时扩大 SSH/Docker/WSL 回归面。 */
  2. import { useState } from "react";
  3. import type {
  4. DockerContainerInfo,
  5. RemoteAssetInstallMode,
  6. RemoteTarget,
  7. RemoteWorkspaceSessionEntry,
  8. SSHConfigAliasOption,
  9. WSLDistro,
  10. } from "@zcode/shared";
  11. import { TID_REMOTE_KIND_DOCKER, TID_REMOTE_KIND_SSH, TID_REMOTE_KIND_WSL } from "@zcode/shared";
  12. import type {
  13. IMcpSyncService,
  14. IPluginSyncService,
  15. IServiceAccessor,
  16. ISkillSyncService,
  17. IZCodeAgentService,
  18. } from "@zcode/services";
  19. import {
  20. AlertTriangleIcon,
  21. ChevronRightIcon,
  22. LoaderIcon,
  23. MonitorCogIcon,
  24. ServerIcon,
  25. TerminalIcon,
  26. } from "lucide-react";
  27. import { DirectoryBrowser } from "@/DirectoryBrowser.js";
  28. import { RemoteConnectionFields } from "@/RemoteConnectionFields.js";
  29. import type { SSHAuthMethod } from "@/hooks/useRemoteConnectionForm.js";
  30. import { Button } from "@/components/ui/button.js";
  31. import { cn } from "@/components/lib/utils.js";
  32. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  33. import {
  34. RemoteSyncDialogs,
  35. RemoteSyncDropdownButton,
  36. shouldShowRemoteSyncActions,
  37. } from "@/settings/RemoteSyncActions.js";
  38. export { RemoteConnectionConnectingStep } from "@/remote-connection/RemoteConnectionConnectingStep.js";
  39. function getKindIcon(kind: RemoteTarget["kind"]) {
  40. switch (kind) {
  41. case "ssh":
  42. return ServerIcon;
  43. case "docker":
  44. return MonitorCogIcon;
  45. case "wsl":
  46. return TerminalIcon;
  47. }
  48. }
  49. export function RemoteConnectionKindStep({
  50. kind,
  51. availableKinds,
  52. onKindChange,
  53. onCancel,
  54. onNext,
  55. }: {
  56. kind: RemoteTarget["kind"];
  57. availableKinds: RemoteTarget["kind"][];
  58. onKindChange: (value: RemoteTarget["kind"]) => void;
  59. onCancel: () => void;
  60. onNext: () => void;
  61. }) {
  62. const { intl } = useZCodeIntl();
  63. return (
  64. <div className="flex min-h-0 flex-1 flex-col gap-6 h-full">
  65. <div className="flex-1 min-h-0 grid content-start gap-3 md:grid-cols-2">
  66. {availableKinds.map((value) => {
  67. const Icon = getKindIcon(value);
  68. const selected = kind === value;
  69. return (
  70. <button
  71. key={value}
  72. type="button"
  73. onClick={() => onKindChange(value)}
  74. data-testid={
  75. value === "ssh"
  76. ? TID_REMOTE_KIND_SSH
  77. : value === "wsl"
  78. ? TID_REMOTE_KIND_WSL
  79. : TID_REMOTE_KIND_DOCKER
  80. }
  81. className={cn(
  82. "flex min-h-32 flex-col items-start gap-4 rounded-2xl border p-4 text-left transition-colors",
  83. selected
  84. ? "border-border-hover bg-selected"
  85. : "border-border bg-card hover:border-border-hover hover:bg-surface",
  86. )}
  87. >
  88. <div
  89. className={cn(
  90. "flex size-10 items-center justify-center rounded-xl border",
  91. selected
  92. ? "border-primary bg-primary text-primary-foreground"
  93. : "border-border bg-background-alt text-foreground-subtle",
  94. )}
  95. >
  96. <Icon className="size-5" />
  97. </div>
  98. <div className="min-w-0 space-y-1">
  99. <p className="text-ui-lg font-medium text-foreground">
  100. {intl.formatMessage({ id: `remote.kind.${value}` })}
  101. </p>
  102. <p className="text-ui-base leading-5 text-foreground-subtle">
  103. {intl.formatMessage({
  104. id: `remote.kind.${value}.wizardDescription`,
  105. })}
  106. </p>
  107. </div>
  108. </button>
  109. );
  110. })}
  111. </div>
  112. <div className="flex gap-3 justify-end">
  113. <Button
  114. type="button"
  115. variant="secondary"
  116. size="lg"
  117. className="h-10 min-w-0 px-5"
  118. onClick={onCancel}
  119. >
  120. {intl.formatMessage({ id: "common.cancel" })}
  121. </Button>
  122. <Button type="button" size="lg" className="h-10 min-w-0 px-5" onClick={onNext}>
  123. {intl.formatMessage({ id: "common.next" })}
  124. <ChevronRightIcon className="size-4" />
  125. </Button>
  126. </div>
  127. </div>
  128. );
  129. }
  130. export function RemoteConnectionSettingsStep({
  131. kind,
  132. host,
  133. port,
  134. username,
  135. sshAuthMethod,
  136. assetInstallMode,
  137. password,
  138. privateKeyPath,
  139. privateKeyPassphrase,
  140. wslDistro,
  141. wslUser = "",
  142. wslDistros,
  143. dockerContainer,
  144. manualDockerContainer,
  145. dockerContainers,
  146. dockerAvailable,
  147. sshConfigAliases,
  148. sshConfigAliasesLoading,
  149. sshConfigAliasesError,
  150. selectedSshConfigAlias,
  151. currentRuntimeOptionsLoading,
  152. currentRuntimeOptionsError,
  153. remoteWorkspaceSessions = [],
  154. validationMessage,
  155. loading,
  156. onBack,
  157. onHostChange,
  158. onPortChange,
  159. onUsernameChange,
  160. onSshAuthMethodChange,
  161. onAssetInstallModeChange,
  162. onPasswordChange,
  163. onPrivateKeyPathChange,
  164. onPrivateKeyPassphraseChange,
  165. onWslDistroChange,
  166. onWslUserChange,
  167. onDockerContainerChange,
  168. onManualDockerContainerChange,
  169. onDockerContainersRefresh,
  170. onApplySshConfigAlias,
  171. onClearSelectedSshConfigAlias,
  172. onConnect,
  173. }: {
  174. kind: RemoteTarget["kind"];
  175. host: string;
  176. port: string;
  177. username: string;
  178. sshAuthMethod: SSHAuthMethod;
  179. assetInstallMode: RemoteAssetInstallMode;
  180. password: string;
  181. privateKeyPath: string;
  182. privateKeyPassphrase: string;
  183. wslDistro: string;
  184. wslUser?: string;
  185. wslDistros: WSLDistro[];
  186. dockerContainer: string;
  187. manualDockerContainer: string;
  188. dockerContainers: DockerContainerInfo[];
  189. dockerAvailable: boolean | null;
  190. sshConfigAliases: SSHConfigAliasOption[];
  191. sshConfigAliasesLoading: boolean;
  192. sshConfigAliasesError: string;
  193. selectedSshConfigAlias: string | null;
  194. currentRuntimeOptionsLoading: boolean;
  195. currentRuntimeOptionsError: string;
  196. remoteWorkspaceSessions?: RemoteWorkspaceSessionEntry[];
  197. validationMessage: string;
  198. loading: boolean;
  199. onBack: () => void;
  200. onHostChange: (value: string) => void;
  201. onPortChange: (value: string) => void;
  202. onUsernameChange: (value: string) => void;
  203. onSshAuthMethodChange: (value: SSHAuthMethod) => void;
  204. onAssetInstallModeChange: (value: RemoteAssetInstallMode) => void;
  205. onPasswordChange: (value: string) => void;
  206. onPrivateKeyPathChange: (value: string) => void;
  207. onPrivateKeyPassphraseChange: (value: string) => void;
  208. onWslDistroChange: (value: string) => void;
  209. onWslUserChange?: (value: string) => void;
  210. onDockerContainerChange: (value: string) => void;
  211. onManualDockerContainerChange: (value: string) => void;
  212. onDockerContainersRefresh?: () => void;
  213. onApplySshConfigAlias: (value: SSHConfigAliasOption) => void;
  214. onClearSelectedSshConfigAlias: () => void;
  215. onConnect: () => void;
  216. }) {
  217. const { intl } = useZCodeIntl();
  218. return (
  219. <div className="flex min-h-0 flex-1 flex-col gap-6 h-full">
  220. <div
  221. data-testid="remote-connection-settings-scroll"
  222. // SSH 配置项增多时,表单内容以前没有自己的滚动边界,会继续绘制到下方按钮区域。
  223. // 这里把中间内容区限定为可滚动区域,让 footer 始终占据独立空间,不遮挡最后几项配置。
  224. className="flex-1 min-h-0 space-y-4 overflow-y-auto pr-1"
  225. >
  226. {currentRuntimeOptionsError ? (
  227. <div className="flex items-start gap-3 rounded-2xl border border-warning bg-warning px-4 py-3 text-ui-base text-warning-foreground">
  228. <AlertTriangleIcon className="mt-0.5 size-4 shrink-0" />
  229. <span>{intl.formatMessage({ id: "remote.optionsLoadFailed" })}</span>
  230. </div>
  231. ) : null}
  232. <RemoteConnectionFields
  233. kind={kind}
  234. host={host}
  235. port={port}
  236. username={username}
  237. sshAuthMethod={sshAuthMethod}
  238. assetInstallMode={assetInstallMode}
  239. password={password}
  240. privateKeyPath={privateKeyPath}
  241. privateKeyPassphrase={privateKeyPassphrase}
  242. wslDistro={wslDistro}
  243. wslUser={wslUser}
  244. wslDistros={wslDistros}
  245. dockerContainer={dockerContainer}
  246. manualDockerContainer={manualDockerContainer}
  247. dockerContainers={dockerContainers}
  248. dockerAvailable={dockerAvailable}
  249. sshConfigAliases={sshConfigAliases}
  250. sshConfigAliasesLoading={sshConfigAliasesLoading}
  251. sshConfigAliasesError={sshConfigAliasesError}
  252. selectedSshConfigAlias={selectedSshConfigAlias}
  253. runtimeOptionsLoading={currentRuntimeOptionsLoading}
  254. remoteWorkspaceSessions={remoteWorkspaceSessions}
  255. applySshConfigAlias={onApplySshConfigAlias}
  256. clearSelectedSshConfigAlias={onClearSelectedSshConfigAlias}
  257. setHost={onHostChange}
  258. setPort={onPortChange}
  259. setUsername={onUsernameChange}
  260. setSshAuthMethod={onSshAuthMethodChange}
  261. setAssetInstallMode={onAssetInstallModeChange}
  262. setPassword={onPasswordChange}
  263. setPrivateKeyPath={onPrivateKeyPathChange}
  264. setPrivateKeyPassphrase={onPrivateKeyPassphraseChange}
  265. setWslDistro={onWslDistroChange}
  266. setWslUser={onWslUserChange}
  267. setDockerContainer={onDockerContainerChange}
  268. setManualDockerContainer={onManualDockerContainerChange}
  269. refreshDockerContainers={onDockerContainersRefresh}
  270. />
  271. </div>
  272. <div className="flex items-center justify-end gap-3">
  273. {validationMessage ? (
  274. <div className="flex flex-1 min-w-0 items-center gap-2">
  275. <AlertTriangleIcon className="size-4 shrink-0 text-warning" />
  276. <span className="text-warning">{validationMessage}</span>
  277. </div>
  278. ) : null}
  279. <div className="flex shrink-0 justify-end gap-3">
  280. <Button
  281. type="button"
  282. variant="secondary"
  283. size="lg"
  284. className="h-10 min-w-0 px-5"
  285. onClick={onBack}
  286. disabled={loading}
  287. >
  288. {intl.formatMessage({ id: "common.back" })}
  289. </Button>
  290. <Button
  291. type="button"
  292. size="lg"
  293. className="h-10 min-w-0 px-5"
  294. onClick={onConnect}
  295. disabled={loading}
  296. >
  297. {loading ? (
  298. <>
  299. <LoaderIcon className="size-4 animate-spin" />
  300. {intl.formatMessage({ id: "remote.connecting" })}
  301. </>
  302. ) : (
  303. intl.formatMessage({ id: "remote.startConnection" })
  304. )}
  305. </Button>
  306. </div>
  307. </div>
  308. </div>
  309. );
  310. }
  311. export function RemoteConnectionDirectoryStep({
  312. services,
  313. remoteTarget,
  314. localSkillSyncService,
  315. remoteSkillSyncService,
  316. localMcpSyncService,
  317. remoteMcpSyncService,
  318. localPluginSyncService,
  319. remotePluginSyncService,
  320. localZCodeAgentService,
  321. remoteZCodeAgentService,
  322. localWorkspacePath,
  323. selecting = false,
  324. onSelect,
  325. onBack,
  326. onCancel,
  327. onSkillsSynced,
  328. onMcpSynced,
  329. onPluginsSynced,
  330. }: {
  331. services: IServiceAccessor | null;
  332. remoteTarget?: RemoteTarget | null;
  333. localSkillSyncService?: ISkillSyncService;
  334. remoteSkillSyncService?: ISkillSyncService | null;
  335. localMcpSyncService?: IMcpSyncService;
  336. remoteMcpSyncService?: IMcpSyncService | null;
  337. localPluginSyncService?: IPluginSyncService;
  338. remotePluginSyncService?: IPluginSyncService | null;
  339. localZCodeAgentService?: IZCodeAgentService;
  340. remoteZCodeAgentService?: IZCodeAgentService | null;
  341. localWorkspacePath?: string;
  342. selecting?: boolean;
  343. onSelect: (path: string) => void;
  344. onBack: () => void;
  345. onCancel: () => void;
  346. onSkillsSynced?: () => Promise<void> | void;
  347. onMcpSynced?: () => Promise<void> | void;
  348. onPluginsSynced?: () => Promise<void> | void;
  349. }) {
  350. const { intl } = useZCodeIntl();
  351. const [selectedPath, setSelectedPath] = useState("");
  352. const [remoteSkillSyncOpen, setRemoteSkillSyncOpen] = useState(false);
  353. const [remoteMcpSyncOpen, setRemoteMcpSyncOpen] = useState(false);
  354. const [remotePluginSyncOpen, setRemotePluginSyncOpen] = useState(false);
  355. const canShowRemoteSyncActions = shouldShowRemoteSyncActions({
  356. remoteSessionId: remoteTarget ? "directory-step" : null,
  357. remoteTarget,
  358. clientMode: "desktop-continuous",
  359. hasLocalSourceService: Boolean(
  360. localSkillSyncService || localMcpSyncService || localPluginSyncService,
  361. ),
  362. });
  363. const canSyncRemoteSkills = Boolean(
  364. canShowRemoteSyncActions && localSkillSyncService && remoteSkillSyncService,
  365. );
  366. const canSyncRemoteMcp = Boolean(
  367. canShowRemoteSyncActions && localMcpSyncService && remoteMcpSyncService,
  368. );
  369. const canSyncRemotePlugins = Boolean(
  370. canShowRemoteSyncActions && localPluginSyncService && remotePluginSyncService,
  371. );
  372. if (!services) {
  373. return (
  374. <div className="flex min-h-0 flex-1 flex-col gap-6 h-full">
  375. <div className="flex min-h-[22rem] flex-1 items-center justify-center rounded-2xl border border-border bg-card text-ui-base text-foreground-subtle">
  376. <LoaderIcon className="mr-2 size-4 animate-spin" />
  377. {intl.formatMessage({ id: "common.loading" })}
  378. </div>
  379. <div className="flex justify-end gap-3">
  380. <Button
  381. type="button"
  382. variant="secondary"
  383. size="lg"
  384. className="h-10 min-w-0 w-32"
  385. onClick={onBack}
  386. >
  387. {intl.formatMessage({ id: "common.back" })}
  388. </Button>
  389. </div>
  390. </div>
  391. );
  392. }
  393. return (
  394. <div className="flex min-h-0 flex-1 flex-col gap-6 h-full" aria-busy={selecting}>
  395. <div className="flex flex-col flex-1 min-h-0 space-y-4">
  396. <div className="flex flex-wrap items-center justify-between gap-2 rounded-lg border border-success bg-success px-4 py-3 text-ui-base text-success-foreground">
  397. <span>{intl.formatMessage({ id: "remote.success" })}</span>
  398. <RemoteSyncDropdownButton
  399. canSyncSkills={canSyncRemoteSkills}
  400. canSyncMcp={canSyncRemoteMcp}
  401. canSyncPlugins={canSyncRemotePlugins}
  402. mcpDisabled={!selectedPath.trim()}
  403. onOpenSkillSync={() => setRemoteSkillSyncOpen(true)}
  404. onOpenMcpSync={() => setRemoteMcpSyncOpen(true)}
  405. onOpenPluginSync={() => setRemotePluginSyncOpen(true)}
  406. />
  407. </div>
  408. {/* 该容器之前不是 flex,导致子级 DirectoryBrowser 的 flex-1 无法拿到有效高度,
  409. 目录项变多时 overflow-y-auto 不生效,列表不能垂直滚动。 */}
  410. <div
  411. className={cn(
  412. "min-h-0 flex flex-1 overflow-hidden",
  413. selecting ? "pointer-events-none opacity-70" : "",
  414. )}
  415. >
  416. <DirectoryBrowser
  417. services={services}
  418. embedded
  419. onSelect={onSelect}
  420. onCancel={onCancel}
  421. onPathChange={setSelectedPath}
  422. />
  423. </div>
  424. </div>
  425. <div className="flex items-center justify-end gap-3">
  426. <Button
  427. type="button"
  428. variant="secondary"
  429. size="lg"
  430. className="h-10 min-w-0 px-5"
  431. onClick={onBack}
  432. disabled={selecting}
  433. >
  434. {intl.formatMessage({ id: "common.back" })}
  435. </Button>
  436. <Button
  437. type="button"
  438. size="lg"
  439. className="h-10 min-w-0 px-5"
  440. onClick={() => selectedPath && !selecting && onSelect(selectedPath)}
  441. disabled={!selectedPath || selecting}
  442. >
  443. {selecting ? (
  444. <>
  445. <LoaderIcon className="size-4 animate-spin" />
  446. {intl.formatMessage({ id: "common.loading" })}
  447. </>
  448. ) : (
  449. intl.formatMessage({ id: "directoryBrowser.selectDir" })
  450. )}
  451. </Button>
  452. </div>
  453. <RemoteSyncDialogs
  454. canSyncSkills={canSyncRemoteSkills}
  455. canSyncMcp={canSyncRemoteMcp}
  456. canSyncPlugins={canSyncRemotePlugins}
  457. skillOpen={remoteSkillSyncOpen}
  458. mcpOpen={remoteMcpSyncOpen}
  459. pluginOpen={remotePluginSyncOpen}
  460. onSkillOpenChange={setRemoteSkillSyncOpen}
  461. onMcpOpenChange={setRemoteMcpSyncOpen}
  462. onPluginOpenChange={setRemotePluginSyncOpen}
  463. localSkillSyncService={localSkillSyncService}
  464. remoteSkillSyncService={remoteSkillSyncService}
  465. localMcpSyncService={localMcpSyncService}
  466. remoteMcpSyncService={remoteMcpSyncService}
  467. localPluginSyncService={localPluginSyncService}
  468. remotePluginSyncService={remotePluginSyncService}
  469. localZCodeAgentService={localZCodeAgentService}
  470. remoteZCodeAgentService={remoteZCodeAgentService}
  471. remoteTarget={remoteTarget}
  472. skillWorkspacePath=""
  473. mcpWorkspacePath={selectedPath.trim()}
  474. pluginWorkspacePath={selectedPath.trim()}
  475. pluginLocalWorkspacePath={localWorkspacePath}
  476. mcpLocalWorkspacePath={localWorkspacePath}
  477. onSkillsSynced={async () => {
  478. await onSkillsSynced?.();
  479. }}
  480. onMcpSynced={async () => {
  481. await onMcpSynced?.();
  482. }}
  483. onPluginsSynced={async () => {
  484. await onPluginsSynced?.();
  485. }}
  486. />
  487. </div>
  488. );
  489. }