RemoteConnectionFields.tsx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* eslint-disable max-lines -- 远程连接字段较多,SSH/WSL/Docker 分支暂集中在单文件维护。 */
  2. import { useCallback, useEffect, useRef, useState } from "react";
  3. import type {
  4. DockerContainerInfo,
  5. RemoteAssetInstallMode,
  6. RemoteTarget,
  7. RemoteWorkspaceSessionEntry,
  8. SSHConfigAliasOption,
  9. WSLDistro,
  10. } from "@zcode/shared";
  11. import { AlertTriangleIcon, CheckIcon, ChevronDownIcon, LoaderIcon, Plus } from "lucide-react";
  12. import {
  13. TID_DOCKER_CONTAINER_INPUT,
  14. TID_DOCKER_CONTAINER_SELECT,
  15. TID_SSH_CONFIG_ALIAS_SELECT,
  16. TID_SSH_AUTH_PASSWORD,
  17. TID_SSH_AUTH_PRIVATE_KEY,
  18. TID_SSH_HOST_INPUT,
  19. TID_SSH_PASSWORD_INPUT,
  20. TID_SSH_PORT_INPUT,
  21. TID_SSH_PRIVATE_KEY_INPUT,
  22. TID_SSH_USERNAME_INPUT,
  23. TID_SERVER_NAME_INPUT,
  24. TID_SERVER_TOKEN_INPUT,
  25. TID_SERVER_URL_INPUT,
  26. TID_SERVER_WORKSPACE_PATH_INPUT,
  27. TID_WSL_DISTRO_SELECT,
  28. TID_WSL_USER_INPUT,
  29. isValidWslUser,
  30. } from "@zcode/shared";
  31. import type { SSHAuthMethod } from "@/hooks/useRemoteConnectionForm.js";
  32. import { usePlatform } from "@/hooks/usePlatform.js";
  33. import { cn } from "@/components/lib/utils.js";
  34. import { Button } from "@/components/ui/button.js";
  35. import { Input } from "@/components/ui/input.js";
  36. import { RemoteConnectionHistoryInput } from "@/remote-connection/RemoteConnectionHistoryInput.js";
  37. import { buildSshConnectionHistorySuggestions } from "@/remote-connection/sshHistorySuggestions.js";
  38. import {
  39. Command,
  40. CommandEmpty,
  41. CommandGroup,
  42. CommandInput,
  43. CommandItem,
  44. CommandList,
  45. } from "@/components/ui/command.js";
  46. import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover.js";
  47. import {
  48. Select,
  49. SelectContent,
  50. SelectItem,
  51. SelectTrigger,
  52. SelectValue,
  53. } from "@/components/ui/select.js";
  54. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  55. const DEFAULT_WSL_DISTRO_VALUE = "__default_wsl_distro__";
  56. const NO_SSH_CONFIG_ALIAS_VALUE = "__ssh_config_alias_none__";
  57. function formatSshConfigAliasSummary(aliasOption: SSHConfigAliasOption): string {
  58. const host = aliasOption.host?.trim() || aliasOption.alias;
  59. const username = aliasOption.username?.trim();
  60. const withUser = username ? `${username}@${host}` : host;
  61. return aliasOption.port != null ? `${withUser}:${aliasOption.port}` : withUser;
  62. }
  63. export function RemoteConnectionFields({
  64. kind,
  65. host,
  66. port,
  67. username,
  68. sshAuthMethod,
  69. assetInstallMode,
  70. password,
  71. privateKeyPath,
  72. privateKeyPassphrase,
  73. wslDistro,
  74. wslUser = "",
  75. wslDistros,
  76. dockerContainer,
  77. manualDockerContainer,
  78. serverUrl,
  79. serverName,
  80. serverToken,
  81. serverWorkspacePath,
  82. dockerContainers,
  83. dockerAvailable,
  84. sshConfigAliases,
  85. sshConfigAliasesLoading,
  86. sshConfigAliasesError,
  87. selectedSshConfigAlias,
  88. runtimeOptionsLoading,
  89. remoteWorkspaceSessions = [],
  90. refreshDockerContainers,
  91. applySshConfigAlias,
  92. clearSelectedSshConfigAlias,
  93. setHost,
  94. setPort,
  95. setUsername,
  96. setSshAuthMethod,
  97. setAssetInstallMode,
  98. setPassword,
  99. setPrivateKeyPath,
  100. setPrivateKeyPassphrase,
  101. setWslDistro,
  102. setWslUser,
  103. setDockerContainer,
  104. setManualDockerContainer,
  105. setServerUrl,
  106. setServerName,
  107. setServerToken,
  108. setServerWorkspacePath,
  109. }: {
  110. kind: RemoteTarget["kind"];
  111. host: string;
  112. port: string;
  113. username: string;
  114. sshAuthMethod: SSHAuthMethod;
  115. assetInstallMode: RemoteAssetInstallMode;
  116. password: string;
  117. privateKeyPath: string;
  118. privateKeyPassphrase: string;
  119. wslDistro: string;
  120. wslUser?: string;
  121. wslDistros: WSLDistro[];
  122. dockerContainer: string;
  123. manualDockerContainer: string;
  124. serverUrl: string;
  125. serverName: string;
  126. serverToken: string;
  127. serverWorkspacePath: string;
  128. dockerContainers: DockerContainerInfo[];
  129. dockerAvailable: boolean | null;
  130. sshConfigAliases: SSHConfigAliasOption[];
  131. sshConfigAliasesLoading: boolean;
  132. sshConfigAliasesError: string;
  133. selectedSshConfigAlias: string | null;
  134. runtimeOptionsLoading: boolean;
  135. remoteWorkspaceSessions?: RemoteWorkspaceSessionEntry[];
  136. refreshDockerContainers?: () => void;
  137. applySshConfigAlias: (value: SSHConfigAliasOption) => void;
  138. clearSelectedSshConfigAlias: () => void;
  139. setHost: (value: string) => void;
  140. setPort: (value: string) => void;
  141. setUsername: (value: string) => void;
  142. setSshAuthMethod: (value: SSHAuthMethod) => void;
  143. setAssetInstallMode: (value: RemoteAssetInstallMode) => void;
  144. setPassword: (value: string) => void;
  145. setPrivateKeyPath: (value: string) => void;
  146. setPrivateKeyPassphrase: (value: string) => void;
  147. setWslDistro: (value: string) => void;
  148. setWslUser?: (value: string) => void;
  149. setDockerContainer: (value: string) => void;
  150. setManualDockerContainer: (value: string) => void;
  151. setServerUrl: (value: string) => void;
  152. setServerName: (value: string) => void;
  153. setServerToken: (value: string) => void;
  154. setServerWorkspacePath: (value: string) => void;
  155. }) {
  156. const { intl } = useZCodeIntl();
  157. const platform = usePlatform();
  158. const [sshAliasPopoverOpen, setSshAliasPopoverOpen] = useState(false);
  159. const [dockerContainerPopoverOpen, setDockerContainerPopoverOpen] = useState(false);
  160. const sshAliasListRef = useRef<HTMLDivElement | null>(null);
  161. const sshHistorySuggestions = buildSshConnectionHistorySuggestions(remoteWorkspaceSessions);
  162. const selectedSshAliasOption =
  163. selectedSshConfigAlias == null
  164. ? null
  165. : (sshConfigAliases.find((option) => option.alias === selectedSshConfigAlias) ?? null);
  166. const sshAliasTriggerLabel = sshConfigAliasesLoading
  167. ? intl.formatMessage({ id: "common.loading" })
  168. : (selectedSshAliasOption?.alias ??
  169. (sshConfigAliases.length === 0
  170. ? intl.formatMessage({ id: "ssh.configAliasEmpty" })
  171. : intl.formatMessage({ id: "ssh.configAliasPlaceholder" })));
  172. const handleSshAliasListWheel = useCallback((event: React.WheelEvent<HTMLDivElement>) => {
  173. const listElement = event.currentTarget;
  174. if (listElement.scrollHeight <= listElement.clientHeight) {
  175. return;
  176. }
  177. // Popover 嵌在 Dialog 中时,外层滚动锁会吞掉默认滚轮行为,
  178. // 导致 alias CommandList 只能拖滚动条、不能直接滚轮滚动。
  179. // 这里显式驱动列表自身 scrollTop,确保鼠标滚轮和触控板都能滚动候选项。
  180. listElement.scrollTop += event.deltaY;
  181. event.preventDefault();
  182. event.stopPropagation();
  183. }, []);
  184. const dockerContainerEmptyText =
  185. dockerAvailable === false
  186. ? intl.formatMessage({ id: "docker.unavailable" })
  187. : intl.formatMessage({ id: "docker.noContainers" });
  188. const dockerContainerTriggerLabel =
  189. dockerContainer.trim() ||
  190. (runtimeOptionsLoading
  191. ? intl.formatMessage({ id: "docker.loading" })
  192. : dockerContainers.length === 0
  193. ? dockerContainerEmptyText
  194. : intl.formatMessage({ id: "docker.selectContainer" }));
  195. const dockerContainerTriggerIsPlaceholder =
  196. !dockerContainer.trim() && dockerContainers.length === 0;
  197. const manualDockerContainerIsEmpty = manualDockerContainer.trim().length === 0;
  198. const wslUserIsRoot = wslUser.trim().toLowerCase() === "root";
  199. const wslUserIsInvalid = wslUser.trim().length > 0 && !isValidWslUser(wslUser);
  200. const handleDockerContainerPopoverOpenChange = useCallback(
  201. (nextOpen: boolean) => {
  202. setDockerContainerPopoverOpen(nextOpen);
  203. if (nextOpen) {
  204. refreshDockerContainers?.();
  205. }
  206. },
  207. [refreshDockerContainers],
  208. );
  209. useEffect(() => {
  210. if (kind !== "ssh" && sshAliasPopoverOpen) {
  211. setSshAliasPopoverOpen(false);
  212. }
  213. }, [kind, sshAliasPopoverOpen]);
  214. useEffect(() => {
  215. if (kind !== "docker" && dockerContainerPopoverOpen) {
  216. setDockerContainerPopoverOpen(false);
  217. }
  218. }, [dockerContainerPopoverOpen, kind]);
  219. useEffect(() => {
  220. if (!sshAliasPopoverOpen) {
  221. return;
  222. }
  223. const frameId = window.requestAnimationFrame(() => {
  224. const selectedAliasItem = sshAliasListRef.current?.querySelector<HTMLElement>(
  225. '[data-ssh-config-alias-selected="true"]',
  226. );
  227. selectedAliasItem?.scrollIntoView({ block: "nearest" });
  228. });
  229. return () => {
  230. window.cancelAnimationFrame(frameId);
  231. };
  232. }, [sshAliasPopoverOpen, selectedSshConfigAlias, sshConfigAliases.length]);
  233. switch (kind) {
  234. case "ssh":
  235. return (
  236. <div className="space-y-3">
  237. <div className="space-y-1">
  238. <label className="mb-1 block text-ui-base text-foreground-subtle">
  239. {intl.formatMessage({ id: "ssh.configAlias" })}
  240. </label>
  241. <Popover open={sshAliasPopoverOpen} onOpenChange={setSshAliasPopoverOpen}>
  242. <PopoverTrigger asChild>
  243. <Button
  244. type="button"
  245. variant="outline"
  246. size="lg"
  247. disabled={sshConfigAliasesLoading || sshConfigAliases.length === 0}
  248. data-testid={TID_SSH_CONFIG_ALIAS_SELECT}
  249. className="h-9 w-full max-w-80 justify-between rounded-lg border-input-border bg-input px-3 text-ui-base font-normal hover:border-input-border-hover hover:bg-input aria-expanded:border-input-border-focused aria-expanded:bg-input-focused"
  250. >
  251. <span className="min-w-0 truncate text-left">{sshAliasTriggerLabel}</span>
  252. <ChevronDownIcon className="size-3.5 text-foreground-subtle" />
  253. </Button>
  254. </PopoverTrigger>
  255. <PopoverContent align="start" sideOffset={6} className="w-80 gap-0 bg-menu p-0">
  256. <Command className="bg-transparent p-0 text-foreground">
  257. <CommandInput
  258. placeholder={intl.formatMessage({
  259. id: "ssh.configAliasSearchPlaceholder",
  260. })}
  261. className="h-8"
  262. />
  263. <CommandList
  264. ref={sshAliasListRef}
  265. className="max-h-60 overscroll-contain"
  266. onWheel={handleSshAliasListWheel}
  267. >
  268. <CommandEmpty className="px-4 py-5 text-foreground-subtle">
  269. {intl.formatMessage({ id: "ssh.configAliasEmpty" })}
  270. </CommandEmpty>
  271. <CommandGroup className="p-1">
  272. <CommandItem
  273. value={NO_SSH_CONFIG_ALIAS_VALUE}
  274. data-checked={selectedSshConfigAlias == null ? "true" : undefined}
  275. data-ssh-config-alias-selected={
  276. selectedSshConfigAlias == null ? "true" : undefined
  277. }
  278. className="min-h-8 cursor-pointer px-2 text-ui-base"
  279. onSelect={() => {
  280. clearSelectedSshConfigAlias();
  281. setSshAliasPopoverOpen(false);
  282. }}
  283. >
  284. <span className="truncate">
  285. {intl.formatMessage({ id: "ssh.configAliasPlaceholder" })}
  286. </span>
  287. </CommandItem>
  288. {sshConfigAliases.map((aliasOption) => (
  289. <CommandItem
  290. key={aliasOption.alias}
  291. value={`${aliasOption.alias} ${formatSshConfigAliasSummary(aliasOption)}`}
  292. data-checked={
  293. selectedSshConfigAlias === aliasOption.alias ? "true" : undefined
  294. }
  295. data-ssh-config-alias-selected={
  296. selectedSshConfigAlias === aliasOption.alias ? "true" : undefined
  297. }
  298. className="min-h-8 cursor-pointer px-2 text-ui-base"
  299. onSelect={() => {
  300. applySshConfigAlias(aliasOption);
  301. setSshAliasPopoverOpen(false);
  302. }}
  303. >
  304. <span className="flex min-w-0 flex-1 flex-col text-left">
  305. <span className="truncate">{aliasOption.alias}</span>
  306. <span className="truncate text-ui-base text-foreground-subtle">
  307. {formatSshConfigAliasSummary(aliasOption)}
  308. </span>
  309. </span>
  310. </CommandItem>
  311. ))}
  312. </CommandGroup>
  313. </CommandList>
  314. </Command>
  315. </PopoverContent>
  316. </Popover>
  317. <p
  318. className={cn(
  319. "text-ui-base",
  320. sshConfigAliasesError ? "text-warning" : "text-foreground-subtle",
  321. )}
  322. >
  323. {sshConfigAliasesLoading
  324. ? intl.formatMessage({ id: "common.loading" })
  325. : sshConfigAliasesError
  326. ? intl.formatMessage({ id: "ssh.configAliasLoadFailed" })
  327. : sshConfigAliases.length === 0
  328. ? intl.formatMessage({ id: "ssh.configAliasEmpty" })
  329. : intl.formatMessage({ id: "ssh.configAliasDescription" })}
  330. </p>
  331. </div>
  332. {/* Bugfix: Electron / Chromium 的原生 autocomplete 在 SSH 向导里不稳定,
  333. 而且默认值(如 localhost / 22)会把历史候选提前过滤掉。
  334. 这里改成用应用自身持久化的远程连接历史做显式候选,focus 时先展示完整历史;密码仍然不参与历史回填。 */}
  335. {/* Bugfix: 建议列表之前直接跟着全宽输入框展开,在大对话框里会变成长条。
  336. 这里把宽度限制在字段语义范围内,只收窄建议列表,不改变原输入框布局。 */}
  337. {/* Bugfix: 示例值直接作为 placeholder 会被误认为已有默认值。
  338. 这里改成“输入提示 + 示例”,让用户知道仍需手动填写必填字段。 */}
  339. <div className="grid gap-3 md:grid-cols-[minmax(0,1fr)_6.5rem]">
  340. <RemoteConnectionHistoryInput
  341. className="h-9 text-ui-base"
  342. label={intl.formatMessage({ id: "ssh.host" })}
  343. value={host}
  344. onChange={setHost}
  345. placeholder={intl.formatMessage({ id: "ssh.hostPlaceholder" })}
  346. suggestions={sshHistorySuggestions.hosts}
  347. emptyText={intl.formatMessage({ id: "remote.history.empty" })}
  348. suggestionWidth="min(30ch, calc(100vw - 2rem))"
  349. autoCapitalize="none"
  350. spellCheck={false}
  351. data-testid={TID_SSH_HOST_INPUT}
  352. />
  353. <RemoteConnectionHistoryInput
  354. className="h-9 text-ui-base"
  355. label={intl.formatMessage({ id: "ssh.port" })}
  356. value={port}
  357. onChange={setPort}
  358. placeholder="22"
  359. suggestions={sshHistorySuggestions.ports}
  360. emptyText={intl.formatMessage({ id: "remote.history.empty" })}
  361. suggestionWidth="min(8ch, calc(100vw - 2rem))"
  362. inputMode="numeric"
  363. data-testid={TID_SSH_PORT_INPUT}
  364. />
  365. </div>
  366. {/* Bugfix: 认证方式之前复用了端口的 6.5rem 窄列,两个选项扣除 padding 后会把中英文文案挤到换行或溢出。
  367. 这里单独给认证方式保留 12rem,并禁止选项文字换行。 */}
  368. <div className="grid gap-3 sm:grid-cols-[minmax(0,1fr)_12rem] sm:items-end">
  369. <RemoteConnectionHistoryInput
  370. className="h-9 text-ui-base"
  371. label={intl.formatMessage({ id: "ssh.username" })}
  372. value={username}
  373. onChange={setUsername}
  374. placeholder={intl.formatMessage({ id: "ssh.usernamePlaceholder" })}
  375. suggestions={sshHistorySuggestions.usernames}
  376. emptyText={intl.formatMessage({ id: "remote.history.empty" })}
  377. suggestionWidth="min(30ch, calc(100vw - 2rem))"
  378. autoCapitalize="none"
  379. spellCheck={false}
  380. data-testid={TID_SSH_USERNAME_INPUT}
  381. />
  382. <div>
  383. <label className="mb-1 block text-ui-base text-foreground-subtle">
  384. {intl.formatMessage({ id: "ssh.authMethod" })}
  385. </label>
  386. <div className="inline-flex w-full items-center rounded-lg border border-input-border bg-input p-[3px]">
  387. {(["password", "privateKey"] as const).map((value) => {
  388. const selected = sshAuthMethod === value;
  389. return (
  390. <button
  391. key={value}
  392. type="button"
  393. onClick={() => setSshAuthMethod(value)}
  394. data-testid={
  395. value === "password" ? TID_SSH_AUTH_PASSWORD : TID_SSH_AUTH_PRIVATE_KEY
  396. }
  397. className={cn(
  398. "inline-flex h-7 flex-1 items-center justify-center rounded-md px-3 text-ui-base font-medium whitespace-nowrap transition-colors",
  399. selected
  400. ? "bg-background text-foreground"
  401. : "text-foreground-subtle hover:text-foreground",
  402. )}
  403. >
  404. {intl.formatMessage({ id: `ssh.auth.${value}` })}
  405. </button>
  406. );
  407. })}
  408. </div>
  409. </div>
  410. </div>
  411. {sshAuthMethod === "password" ? (
  412. <div>
  413. <label className="mb-1 block text-ui-base text-foreground-subtle">
  414. {intl.formatMessage({ id: "ssh.password" })}
  415. </label>
  416. <Input
  417. size="lg"
  418. className="h-9 text-ui-base"
  419. type="password"
  420. value={password}
  421. onChange={(e) => setPassword(e.target.value)}
  422. placeholder={intl.formatMessage({
  423. id: "ssh.passwordPlaceholder",
  424. })}
  425. name="remote-ssh-password"
  426. autoComplete="off"
  427. data-testid={TID_SSH_PASSWORD_INPUT}
  428. />
  429. </div>
  430. ) : (
  431. <div>
  432. <label className="mb-1 block text-ui-base text-foreground-subtle">
  433. {intl.formatMessage({ id: "ssh.privateKey" })}
  434. </label>
  435. <div className="relative mb-3">
  436. <RemoteConnectionHistoryInput
  437. className="h-9 pr-10 text-ui-base"
  438. value={privateKeyPath}
  439. onChange={setPrivateKeyPath}
  440. placeholder={intl.formatMessage({
  441. id: "ssh.privateKeyPlaceholder",
  442. })}
  443. suggestions={sshHistorySuggestions.privateKeyPaths}
  444. emptyText={intl.formatMessage({
  445. id: "remote.history.empty",
  446. })}
  447. autoCapitalize="none"
  448. spellCheck={false}
  449. data-testid={TID_SSH_PRIVATE_KEY_INPUT}
  450. />
  451. <Button
  452. type="button"
  453. variant="ghost"
  454. size="icon-lg"
  455. className="absolute top-1/2 right-0.5 -translate-y-1/2"
  456. title={intl.formatMessage({ id: "ssh.privateKeySelect" })}
  457. onClick={() => {
  458. void (async () => {
  459. const selectedPath = await platform.selectFile();
  460. if (selectedPath) {
  461. setPrivateKeyPath(selectedPath);
  462. }
  463. })();
  464. }}
  465. >
  466. <Plus className="size-4" />
  467. </Button>
  468. </div>
  469. <label className="mb-1 block text-ui-base text-foreground-subtle">
  470. {intl.formatMessage({ id: "ssh.privateKeyPassphrase" })}
  471. </label>
  472. <Input
  473. size="lg"
  474. className="h-9 text-ui-base"
  475. type="password"
  476. value={privateKeyPassphrase}
  477. onChange={(e) => setPrivateKeyPassphrase(e.target.value)}
  478. placeholder={intl.formatMessage({
  479. id: "ssh.privateKeyPassphrasePlaceholder",
  480. })}
  481. name="remote-ssh-private-key-passphrase"
  482. autoComplete="off"
  483. />
  484. </div>
  485. )}
  486. <div>
  487. <label className="mb-1 block text-ui-base text-foreground-subtle">
  488. {intl.formatMessage({ id: "ssh.assetInstallMode" })}
  489. </label>
  490. <div className="inline-flex w-full max-w-md flex-col items-stretch rounded-lg border border-input-border bg-input p-[3px] sm:w-fit sm:flex-row sm:items-center">
  491. {(["local-download-upload", "remote-download"] as const).map((value) => {
  492. const selected = assetInstallMode === value;
  493. return (
  494. <button
  495. key={value}
  496. type="button"
  497. onClick={() => setAssetInstallMode(value)}
  498. className={cn(
  499. "inline-flex min-h-7 min-w-0 flex-1 items-center justify-center rounded-md px-3 py-1 text-center text-ui-base font-medium transition-colors sm:flex-none",
  500. selected
  501. ? "bg-background text-foreground"
  502. : "text-foreground-subtle hover:text-foreground",
  503. )}
  504. >
  505. <span className="min-w-0 break-words">
  506. {intl.formatMessage({
  507. id: `ssh.assetInstallMode.${value}`,
  508. })}
  509. </span>
  510. </button>
  511. );
  512. })}
  513. </div>
  514. <p className="mt-1 text-ui-base text-foreground-subtle">
  515. {intl.formatMessage({ id: "ssh.assetInstallModeDescription" })}
  516. </p>
  517. </div>
  518. </div>
  519. );
  520. case "server":
  521. return (
  522. <div className="space-y-3">
  523. <p className="text-ui-base text-foreground-subtle">
  524. {intl.formatMessage({ id: "server.description" })}
  525. </p>
  526. <div>
  527. <label className="mb-1 block text-ui-base text-foreground-subtle">
  528. {intl.formatMessage({ id: "server.url" })}
  529. </label>
  530. <Input
  531. size="lg"
  532. className="h-9 text-ui-base"
  533. value={serverUrl}
  534. onChange={(event) => setServerUrl(event.target.value)}
  535. placeholder={intl.formatMessage({ id: "server.urlPlaceholder" })}
  536. autoCapitalize="none"
  537. spellCheck={false}
  538. data-testid={TID_SERVER_URL_INPUT}
  539. />
  540. </div>
  541. <div className="grid gap-3 md:grid-cols-2">
  542. <div>
  543. <label className="mb-1 block text-ui-base text-foreground-subtle">
  544. {intl.formatMessage({ id: "server.name" })}
  545. </label>
  546. <Input
  547. size="lg"
  548. className="h-9 text-ui-base"
  549. value={serverName}
  550. onChange={(event) => setServerName(event.target.value)}
  551. placeholder={intl.formatMessage({ id: "server.namePlaceholder" })}
  552. data-testid={TID_SERVER_NAME_INPUT}
  553. />
  554. </div>
  555. <div>
  556. <label className="mb-1 block text-ui-base text-foreground-subtle">
  557. {intl.formatMessage({ id: "server.token" })}
  558. </label>
  559. <Input
  560. size="lg"
  561. className="h-9 text-ui-base"
  562. type="password"
  563. value={serverToken}
  564. onChange={(event) => setServerToken(event.target.value)}
  565. placeholder={intl.formatMessage({ id: "server.tokenPlaceholder" })}
  566. autoComplete="off"
  567. data-testid={TID_SERVER_TOKEN_INPUT}
  568. />
  569. </div>
  570. </div>
  571. <div>
  572. <label className="mb-1 block text-ui-base text-foreground-subtle">
  573. {intl.formatMessage({ id: "server.workspacePath" })}
  574. </label>
  575. <Input
  576. size="lg"
  577. className="h-9 text-ui-base"
  578. value={serverWorkspacePath}
  579. onChange={(event) => setServerWorkspacePath(event.target.value)}
  580. placeholder={intl.formatMessage({ id: "server.workspacePathPlaceholder" })}
  581. autoCapitalize="none"
  582. spellCheck={false}
  583. data-testid={TID_SERVER_WORKSPACE_PATH_INPUT}
  584. />
  585. <p className="mt-1 text-ui-base text-foreground-subtle">
  586. {intl.formatMessage({ id: "server.workspacePathDescription" })}
  587. </p>
  588. </div>
  589. </div>
  590. );
  591. case "wsl":
  592. return (
  593. <div className="space-y-3">
  594. <p className="text-ui-base text-foreground-subtle">
  595. {intl.formatMessage({ id: "wsl.description" })}
  596. </p>
  597. <div>
  598. <label className="mb-1 block text-ui-base text-foreground-subtle">
  599. {intl.formatMessage({ id: "wsl.distro" })}
  600. </label>
  601. {wslDistros.length > 0 ? (
  602. <Select
  603. value={wslDistro || DEFAULT_WSL_DISTRO_VALUE}
  604. onValueChange={(value) => {
  605. setWslDistro(value === DEFAULT_WSL_DISTRO_VALUE ? "" : value);
  606. }}
  607. >
  608. <SelectTrigger size="lg" className="h-9 w-full" data-testid={TID_WSL_DISTRO_SELECT}>
  609. <SelectValue />
  610. </SelectTrigger>
  611. <SelectContent align="start">
  612. <SelectItem value={DEFAULT_WSL_DISTRO_VALUE}>
  613. {intl.formatMessage({ id: "wsl.defaultDistro" })}
  614. </SelectItem>
  615. {wslDistros.map((distro) => (
  616. <SelectItem key={distro.name} value={distro.name}>
  617. {distro.name}
  618. </SelectItem>
  619. ))}
  620. </SelectContent>
  621. </Select>
  622. ) : (
  623. <Input
  624. size="lg"
  625. className="h-9 text-ui-base"
  626. value={wslDistro}
  627. onChange={(e) => setWslDistro(e.target.value)}
  628. placeholder={intl.formatMessage({ id: "wsl.defaultDistro" })}
  629. />
  630. )}
  631. </div>
  632. <p className="text-ui-base text-foreground-subtle">
  633. {runtimeOptionsLoading
  634. ? intl.formatMessage({ id: "wsl.loading" })
  635. : wslDistros.length > 0
  636. ? intl.formatMessage(
  637. { id: "wsl.detectedCount" },
  638. {
  639. count: String(wslDistros.length),
  640. },
  641. )
  642. : intl.formatMessage({ id: "wsl.noDistros" })}
  643. </p>
  644. <div>
  645. <label className="mb-1 block text-ui-base text-foreground-subtle">
  646. {intl.formatMessage({ id: "wsl.user" })}
  647. </label>
  648. <Input
  649. size="lg"
  650. className="h-9 text-ui-base"
  651. value={wslUser}
  652. onChange={(event) => setWslUser?.(event.target.value)}
  653. placeholder={intl.formatMessage({ id: "wsl.defaultUser" })}
  654. autoCapitalize="none"
  655. spellCheck={false}
  656. aria-invalid={wslUserIsInvalid}
  657. data-testid={TID_WSL_USER_INPUT}
  658. />
  659. <p
  660. className={cn(
  661. "mt-1 text-ui-base",
  662. wslUserIsInvalid ? "text-destructive" : "text-foreground-subtle",
  663. )}
  664. >
  665. {intl.formatMessage({
  666. id: wslUserIsInvalid
  667. ? "wsl.validation.invalidUser"
  668. : "wsl.userDescription",
  669. })}
  670. </p>
  671. </div>
  672. {wslUserIsRoot ? (
  673. <div className="flex items-start gap-2 rounded-lg border border-warning bg-warning px-3 py-2 text-ui-base text-warning-foreground">
  674. <AlertTriangleIcon className="mt-0.5 size-4 shrink-0" />
  675. <span>{intl.formatMessage({ id: "wsl.rootWarning" })}</span>
  676. </div>
  677. ) : null}
  678. </div>
  679. );
  680. case "docker":
  681. return (
  682. <div className="space-y-3">
  683. <p className="text-ui-base text-foreground-subtle">
  684. {intl.formatMessage({ id: "docker.description" })}
  685. </p>
  686. <div>
  687. <label className="mb-1 block text-ui-base text-foreground-subtle">
  688. {intl.formatMessage({ id: "docker.selectContainer" })}
  689. </label>
  690. <Popover
  691. open={dockerContainerPopoverOpen}
  692. onOpenChange={handleDockerContainerPopoverOpenChange}
  693. >
  694. <PopoverTrigger asChild>
  695. <Button
  696. type="button"
  697. variant="outline"
  698. size="lg"
  699. data-testid={TID_DOCKER_CONTAINER_SELECT}
  700. className="h-9 w-full justify-between rounded-lg border-input-border bg-input px-3 text-ui-base font-normal hover:border-input-border-hover hover:bg-input aria-expanded:border-input-border-focused aria-expanded:bg-input-focused"
  701. >
  702. <span
  703. className={cn(
  704. "min-w-0 truncate text-left",
  705. dockerContainerTriggerIsPlaceholder
  706. ? "text-foreground-subtlest"
  707. : "text-foreground",
  708. )}
  709. >
  710. {dockerContainerTriggerLabel}
  711. </span>
  712. {runtimeOptionsLoading ? (
  713. <LoaderIcon className="size-3.5 shrink-0 animate-spin text-foreground-subtle" />
  714. ) : (
  715. <ChevronDownIcon className="size-3.5 shrink-0 text-foreground-subtle" />
  716. )}
  717. </Button>
  718. </PopoverTrigger>
  719. <PopoverContent
  720. align="start"
  721. sideOffset={6}
  722. className="max-w-[calc(100vw-2rem)] rounded-xl border border-border bg-menu p-1 shadow-lg"
  723. style={{ width: "var(--radix-popover-trigger-width)" }}
  724. >
  725. <Command className="rounded-lg bg-transparent p-0 text-foreground">
  726. <CommandList className="max-h-56 scroll-py-1">
  727. {runtimeOptionsLoading ? (
  728. <div className="flex min-h-8 items-center gap-2 rounded-lg px-3 py-1.5 text-ui-base text-foreground-subtle">
  729. <LoaderIcon className="size-3.5 shrink-0 animate-spin" />
  730. <span className="truncate">
  731. {intl.formatMessage({ id: "docker.loading" })}
  732. </span>
  733. </div>
  734. ) : null}
  735. {dockerContainers.length === 0 && !runtimeOptionsLoading ? (
  736. <CommandEmpty className="px-3 py-5 text-ui-base text-foreground-subtle">
  737. {dockerContainerEmptyText}
  738. </CommandEmpty>
  739. ) : (
  740. dockerContainers.map((container) => {
  741. const selected = container.name === dockerContainer;
  742. return (
  743. <CommandItem
  744. key={container.id}
  745. value={container.name}
  746. className="min-h-8 cursor-pointer rounded-lg px-3 py-1.5 text-ui-base text-foreground data-selected:bg-menu-hover data-selected:text-foreground"
  747. onSelect={() => {
  748. setDockerContainer(container.name);
  749. setDockerContainerPopoverOpen(false);
  750. }}
  751. >
  752. <span className="min-w-0 flex-1 truncate">{container.name}</span>
  753. <CheckIcon
  754. className={cn(
  755. "size-4 shrink-0 text-foreground-subtle",
  756. selected ? "opacity-100" : "opacity-0",
  757. )}
  758. />
  759. </CommandItem>
  760. );
  761. })
  762. )}
  763. </CommandList>
  764. </Command>
  765. </PopoverContent>
  766. </Popover>
  767. </div>
  768. <div>
  769. <label className="mb-1 block text-ui-base text-foreground-subtle">
  770. {intl.formatMessage({ id: "docker.container" })}
  771. </label>
  772. <Input
  773. size="lg"
  774. className="h-9 text-ui-base"
  775. value={manualDockerContainer}
  776. onChange={(event) => setManualDockerContainer(event.target.value)}
  777. placeholder={intl.formatMessage({ id: "docker.containerPlaceholder" })}
  778. data-testid={TID_DOCKER_CONTAINER_INPUT}
  779. />
  780. {manualDockerContainerIsEmpty ? (
  781. <p className="mt-1 text-ui-base text-foreground-subtle">
  782. {intl.formatMessage({ id: "docker.manualContainerHint" })}
  783. </p>
  784. ) : null}
  785. </div>
  786. </div>
  787. );
  788. }
  789. }