The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import { Fragment, useState, type ReactNode } from "react"; |
| 4 | import { msg, T, useGT, useMessages } from "gt-react"; |
| 5 | import { m as resolveNow, type MessageResolver } from "../../i18n/messages"; |
| 6 | import * as Cause from "effect/Cause"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import { AsyncResult } from "effect/reactivity"; |
| 9 | import { |
| 10 | AuthSettingsWriteScope, |
| 11 | type DevicePlatform, |
| 12 | type DevicePlatformAvailability, |
| 13 | type DeviceServiceState, |
| 14 | type EasDeviceHostConfig, |
| 15 | type EnvironmentId, |
| 16 | type ServerSettingsPatch, |
| 17 | type SimlockDeviceHostConfig, |
| 18 | type SshDeviceHostConfig, |
| 19 | } from "@t3tools/contracts"; |
| 20 | import { ChevronDownIcon, MoreVertical, PlusIcon } from "lucide-react"; |
| 21 | |
| 22 | import { cn, randomUUID } from "../../lib/utils"; |
| 23 | import { deviceEnvironment } from "../../state/device"; |
| 24 | import { environmentAtomList, environmentIdsKey } from "../../state/environmentAtomList"; |
| 25 | import { serverEnvironment } from "../../state/server"; |
| 26 | import { readEnvironmentScope } from "../../state/session"; |
| 27 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 28 | import { AndroidIcon, AppleIcon } from "../Icons"; |
| 29 | import { Button, InlineButton } from "../ui/button"; |
| 30 | import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 31 | import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; |
| 32 | import { Spinner } from "../ui/spinner"; |
| 33 | import { toastManager } from "../ui/toast"; |
| 34 | import { incompleteListings } from "../device/DeviceSetup"; |
| 35 | import { DeviceHostEditor } from "./DeviceHostEditor"; |
| 36 | import { |
| 37 | updateDeviceHosts, |
| 38 | groupDeviceHosts, |
| 39 | type DeviceHostGroup, |
| 40 | } from "./deviceHostsSettings.logic"; |
| 41 | import { EasDeviceHostEditor } from "./EasDeviceHostEditor"; |
| 42 | import { |
| 43 | easSessionLimits, |
| 44 | newEasDeviceHost, |
| 45 | updateEasDeviceHosts, |
| 46 | } from "./easDeviceHostsSettings.logic"; |
| 47 | import { useSettingsScope } from "./SettingsScopeContext"; |
| 48 | import { SettingsSection } from "./settingsLayout"; |
| 49 | import { settingsScopeEnvironmentLabel } from "./settingsScopeAxis"; |
| 50 | import { SimlockDeviceHostEditor } from "./SimlockDeviceHostEditor"; |
| 51 | import { |
| 52 | newSimlockDeviceHost, |
| 53 | planSimlockDeviceHostSave, |
| 54 | simlockLeaseLimits, |
| 55 | updateSimlockDeviceHosts, |
| 56 | type SimlockTokenEntry, |
| 57 | } from "./simlockDeviceHostsSettings.logic"; |
| 58 | import { useHostConnectionChecks } from "./useHostConnectionChecks"; |
| 59 | |
| 60 | const deviceHostStatesAtom = environmentAtomList("web-device-host-states", (environmentId) => |
| 61 | deviceEnvironment.state({ environmentId, input: {} }), |
| 62 | ); |
| 63 | |
| 64 | const failureMessage = (cause: Cause.Cause<unknown>) => { |
| 65 | const error = Cause.squash(cause); |
| 66 | return error instanceof Error |
| 67 | ? (thrownErrorMessage(error, resolveNow) ?? error.message) |
| 68 | : String(error); |
| 69 | }; |
| 70 | |
| 71 | /** Something on one environment that is in progress or needs a fix; a working source has none. */ |
| 72 | interface SourceStatus { |
| 73 | readonly tone: "pending" | "warning" | "error"; |
| 74 | readonly label: string; |
| 75 | readonly detail?: string | undefined; |
| 76 | readonly action?: { readonly label: string; readonly run: () => void } | undefined; |
| 77 | } |
| 78 | |
| 79 | /** A source on one environment whose devices are on. */ |
| 80 | interface SourceEnvironment { |
| 81 | readonly environmentId: EnvironmentId; |
| 82 | readonly label: string; |
| 83 | readonly added: boolean; |
| 84 | readonly status?: SourceStatus | undefined; |
| 85 | /** Unknown until the environment's hub has reached the source. */ |
| 86 | readonly platforms?: ReadonlyArray<DevicePlatformAvailability> | undefined; |
| 87 | } |
| 88 | |
| 89 | interface SourceRow { |
| 90 | readonly key: string; |
| 91 | readonly title: string; |
| 92 | readonly kind?: string; |
| 93 | readonly description: ReactNode; |
| 94 | readonly environments: ReadonlyArray<SourceEnvironment>; |
| 95 | /** |
| 96 | * One machine or account every environment reaches the same way (an SSH host, Expo, a Simlock |
| 97 | * pool), so its platforms are the same for all of them. Local simulators differ per machine. |
| 98 | */ |
| 99 | readonly shared: boolean; |
| 100 | readonly actions?: ReactNode; |
| 101 | } |
| 102 | |
| 103 | type Editing = |
| 104 | | { |
| 105 | readonly kind: "ssh"; |
| 106 | readonly host: SshDeviceHostConfig; |
| 107 | readonly original: SshDeviceHostConfig | null; |
| 108 | /** Limits the save to these environments, for a host that differs between them. */ |
| 109 | readonly only?: ReadonlySet<EnvironmentId>; |
| 110 | } |
| 111 | | { |
| 112 | readonly kind: "eas"; |
| 113 | readonly host: EasDeviceHostConfig; |
| 114 | readonly isNew: boolean; |
| 115 | /** Where the editor checks the Expo account: an environment that holds the host. */ |
| 116 | readonly environmentId: EnvironmentId; |
| 117 | } |
| 118 | | { readonly kind: "simlock"; readonly host: SimlockDeviceHostConfig; readonly isNew: boolean }; |
| 119 | |
| 120 | /** What a configured host is doing on one environment, when it is not simply working. */ |
| 121 | function hostStatus( |
| 122 | state: DeviceServiceState | undefined, |
| 123 | hostId: string, |
| 124 | resolve: MessageResolver = resolveNow, |
| 125 | ): SourceStatus | undefined { |
| 126 | if (!state) return { tone: "pending", label: resolve(msg("Loading")) }; |
| 127 | const status = state.hostStatuses[hostId]; |
| 128 | switch (status?.status) { |
| 129 | case "ready": { |
| 130 | const incomplete = incompleteListings(status); |
| 131 | if (incomplete.length === 0) return undefined; |
| 132 | const label = |
| 133 | incomplete.length > 1 |
| 134 | ? resolve(msg("Device list may be incomplete")) |
| 135 | : incomplete[0] === "ios" |
| 136 | ? resolve(msg("iOS list may be incomplete")) |
| 137 | : resolve(msg("Android list may be incomplete")); |
| 138 | return { tone: "warning", label, detail: status.detail }; |
| 139 | } |
| 140 | case "installing": |
| 141 | case "starting": |
| 142 | return { tone: "pending", label: resolve(msg("Starting")), detail: status.detail }; |
| 143 | case "failed": |
| 144 | return { |
| 145 | tone: "error", |
| 146 | label: resolve(msg("Failed", { $context: "device host status" })), |
| 147 | detail: status.detail, |
| 148 | }; |
| 149 | default: |
| 150 | return undefined; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | const hostPlatforms = (state: DeviceServiceState | undefined, hostId: string) => |
| 155 | state?.hosts.find((host) => host.id === hostId)?.platforms; |
| 156 | |
| 157 | /** |
| 158 | * Every device source the selected environments can open, one row each. A source configured on |
| 159 | * several environments is one row; how it stands on each lives in the row's breakdown. |
| 160 | */ |
| 161 | export function DeviceSourcesSettings(props: { |
| 162 | readonly supportsEas: boolean; |
| 163 | readonly supportsSimlock: boolean; |
| 164 | }) { |
| 165 | const gt = useGT(); |
| 166 | const m = useMessages(); |
| 167 | const { scope, environments, connectedEnvironments, environment: primary } = useSettingsScope(); |
| 168 | const projectScope = scope.kind === "project" || scope.kind === "checkout"; |
| 169 | const update = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false }); |
| 170 | const list = useAtomCommand(deviceEnvironment.list, { reportFailure: false }); |
| 171 | const [editing, setEditing] = useState<Editing | null>(null); |
| 172 | const [busy, setBusy] = useState(false); |
| 173 | const label = (environment: (typeof environments)[number]) => |
| 174 | settingsScopeEnvironmentLabel(environment, environments); |
| 175 | const targets = environments.map((environment) => ({ |
| 176 | environmentId: environment.environmentId, |
| 177 | label: label(environment), |
| 178 | connected: environment.connection.phase === "connected", |
| 179 | })); |
| 180 | const { testConnection } = useHostConnectionChecks(targets); |
| 181 | const ids = connectedEnvironments.map((environment) => environment.environmentId); |
| 182 | const results = useAtomValue(deviceHostStatesAtom(environmentIdsKey(ids))); |
| 183 | const members = connectedEnvironments.map((environment, index) => { |
| 184 | const result = results[index]; |
| 185 | return { |
| 186 | environmentId: environment.environmentId, |
| 187 | label: label(environment), |
| 188 | settings: environment.serverConfig?.settings, |
| 189 | state: result ? Option.getOrUndefined(AsyncResult.value(result)) : undefined, |
| 190 | }; |
| 191 | }); |
| 192 | // Environments with devices off are left out: the switch above already says so. |
| 193 | const active = members.filter((member) => member.settings?.enableDeviceSupport); |
| 194 | const simlockEnabled = members.some((member) => member.settings?.simlockDeviceHostsEnabled); |
| 195 | // Like every scoped setting, a change reaches the connected environments and skips the rest. |
| 196 | const disabled = projectScope || busy || members.length === 0; |
| 197 | |
| 198 | /** Writes one settings patch per environment; reports the environments it could not change. */ |
| 199 | const write = async ( |
| 200 | patches: ReadonlyArray<{ environmentId: EnvironmentId; patch: ServerSettingsPatch }>, |
| 201 | failedTitle: string, |
| 202 | ) => { |
| 203 | if (projectScope) return false; |
| 204 | const name = (environmentId: EnvironmentId) => |
| 205 | members.find((member) => member.environmentId === environmentId)?.label ?? environmentId; |
| 206 | const unwritable = patches.filter( |
| 207 | (patch) => !readEnvironmentScope(patch.environmentId, AuthSettingsWriteScope), |
| 208 | ); |
| 209 | if (unwritable.length > 0) { |
| 210 | toastManager.add({ |
| 211 | type: "error", |
| 212 | title: failedTitle, |
| 213 | description: gt("This connection cannot change settings on {environments}.", { |
| 214 | environments: unwritable.map((patch) => name(patch.environmentId)).join(", "), |
| 215 | }), |
| 216 | }); |
| 217 | return false; |
| 218 | } |
| 219 | setBusy(true); |
| 220 | const outcomes = await Promise.all( |
| 221 | patches.map((patch) => |
| 222 | update({ environmentId: patch.environmentId, input: { patch: patch.patch } }), |
| 223 | ), |
| 224 | ); |
| 225 | setBusy(false); |
| 226 | const failed = patches.flatMap((patch, index) => { |
| 227 | const outcome = outcomes[index]; |
| 228 | return outcome && outcome._tag === "Failure" |
| 229 | ? [`${name(patch.environmentId)}: ${failureMessage(outcome.cause)}`] |
| 230 | : []; |
| 231 | }); |
| 232 | if (failed.length === 0) return true; |
| 233 | toastManager.add({ type: "error", title: failedTitle, description: failed.join("\n") }); |
| 234 | return false; |
| 235 | }; |
| 236 | |
| 237 | const saveSsh = async ( |
| 238 | host: SshDeviceHostConfig, |
| 239 | remove: boolean, |
| 240 | original = host, |
| 241 | only?: ReadonlySet<EnvironmentId>, |
| 242 | ) => { |
| 243 | try { |
| 244 | const saved = await write( |
| 245 | members |
| 246 | .filter((member) => !only || only.has(member.environmentId)) |
| 247 | .map((member) => ({ |
| 248 | environmentId: member.environmentId, |
| 249 | patch: { |
| 250 | deviceHosts: updateDeviceHosts( |
| 251 | member.settings?.deviceHosts ?? [], |
| 252 | host, |
| 253 | remove, |
| 254 | original, |
| 255 | ), |
| 256 | }, |
| 257 | })), |
| 258 | remove ? gt("Device host not removed everywhere") : gt("Device host not saved everywhere"), |
| 259 | ); |
| 260 | if (saved) setEditing(null); |
| 261 | } catch (error) { |
| 262 | toastManager.add({ |
| 263 | type: "error", |
| 264 | title: gt("Device host not saved"), |
| 265 | description: |
| 266 | error instanceof Error |
| 267 | ? (thrownErrorMessage(error, resolveNow) ?? error.message) |
| 268 | : String(error), |
| 269 | }); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | const saveEas = async ( |
| 274 | host: EasDeviceHostConfig, |
| 275 | remove: boolean, |
| 276 | previous?: EasDeviceHostConfig, |
| 277 | ) => { |
| 278 | const keepToken = previous !== undefined && host.accessToken === previous.accessToken; |
| 279 | const saved = await write( |
| 280 | members.map((member) => ({ |
| 281 | environmentId: member.environmentId, |
| 282 | patch: { |
| 283 | easDeviceHosts: updateEasDeviceHosts( |
| 284 | member.settings?.easDeviceHosts ?? [], |
| 285 | host, |
| 286 | remove, |
| 287 | keepToken, |
| 288 | ), |
| 289 | }, |
| 290 | })), |
| 291 | remove ? gt("Expo host not removed everywhere") : gt("Expo host not saved everywhere"), |
| 292 | ); |
| 293 | if (saved) setEditing(null); |
| 294 | }; |
| 295 | |
| 296 | const simlockEnvironments = members.map((member) => ({ |
| 297 | environmentId: member.environmentId, |
| 298 | label: member.label, |
| 299 | hosts: member.settings?.simlockDeviceHosts ?? [], |
| 300 | })); |
| 301 | const saveSimlock = async ( |
| 302 | host: SimlockDeviceHostConfig, |
| 303 | tokens: ReadonlyMap<EnvironmentId, SimlockTokenEntry>, |
| 304 | ) => { |
| 305 | const plan = planSimlockDeviceHostSave(simlockEnvironments, host, tokens); |
| 306 | if ("problem" in plan) return; |
| 307 | const saved = await write( |
| 308 | plan.patches.map((patch) => ({ |
| 309 | environmentId: patch.environmentId, |
| 310 | patch: { simlockDeviceHosts: patch.hosts }, |
| 311 | })), |
| 312 | gt("Simlock host not saved everywhere"), |
| 313 | ); |
| 314 | if (saved) setEditing(null); |
| 315 | }; |
| 316 | const removeSimlock = (host: SimlockDeviceHostConfig) => |
| 317 | void write( |
| 318 | simlockEnvironments |
| 319 | .filter((environment) => environment.hosts.some((candidate) => candidate.id === host.id)) |
| 320 | .map((environment) => ({ |
| 321 | environmentId: environment.environmentId, |
| 322 | patch: { simlockDeviceHosts: updateSimlockDeviceHosts(environment.hosts, host, true) }, |
| 323 | })), |
| 324 | gt("Simlock host not removed everywhere"), |
| 325 | ); |
| 326 | |
| 327 | const [testingHosts, setTestingHosts] = useState<ReadonlySet<string>>(new Set()); |
| 328 | const testSsh = async ({ host, members }: DeviceHostGroup) => { |
| 329 | const check = testConnection(host); |
| 330 | if (!check) return; |
| 331 | // Every member's id, so the row that remains after an environment drops out stays pending. |
| 332 | const ids = members.map((member) => member.host.id); |
| 333 | setTestingHosts((current) => new Set([...current, ...ids])); |
| 334 | const checked = await check.finally(() => |
| 335 | setTestingHosts((current) => { |
| 336 | const next = new Set(current); |
| 337 | for (const id of ids) next.delete(id); |
| 338 | return next; |
| 339 | }), |
| 340 | ); |
| 341 | const failed = targets.filter((target) => checked[target.environmentId]?.status === "failed"); |
| 342 | toastManager.add({ |
| 343 | type: failed.length ? "error" : "success", |
| 344 | title: failed.length |
| 345 | ? gt( |
| 346 | "{count, plural, one {{host}: {count} of {total} environments failed} other {{host}: {count} of {total} environments failed}}", |
| 347 | { host: host.label, count: failed.length, total: targets.length }, |
| 348 | ) |
| 349 | : gt("{host}: connection checks passed", { host: host.label }), |
| 350 | description: failed.length |
| 351 | ? failed |
| 352 | .map((target) => { |
| 353 | const result = checked[target.environmentId]; |
| 354 | return `${target.label}: ${result?.status === "failed" ? result.error : gt("failed", { $context: "connection check result" })}`; |
| 355 | }) |
| 356 | .join("\n") |
| 357 | : gt("Connected or already available locally on each selected environment."), |
| 358 | }); |
| 359 | }; |
| 360 | |
| 361 | const retry = (environmentId: EnvironmentId, hostId: string) => |
| 362 | void list({ environmentId, input: { retryHostId: hostId } }); |
| 363 | |
| 364 | const rowActions = (onEdit: () => void, onRemove: () => void, extra?: ReactNode) => ( |
| 365 | <> |
| 366 | <Button size="sm" variant="outline" disabled={disabled} onClick={onEdit}> |
| 367 | <T>Edit</T> |
| 368 | </Button> |
| 369 | <Menu> |
| 370 | <MenuTrigger |
| 371 | render={ |
| 372 | <Button |
| 373 | size="icon-sm" |
| 374 | variant="ghost-muted" |
| 375 | disabled={disabled} |
| 376 | aria-label={gt("More")} |
| 377 | /> |
| 378 | } |
| 379 | > |
| 380 | <MoreVertical /> |
| 381 | </MenuTrigger> |
| 382 | <MenuPopup align="end"> |
| 383 | {extra} |
| 384 | <MenuItem variant="destructive" onClick={onRemove}> |
| 385 | {gt("Remove", { $context: "verb, removes a device source" })} |
| 386 | </MenuItem> |
| 387 | </MenuPopup> |
| 388 | </Menu> |
| 389 | </> |
| 390 | ); |
| 391 | |
| 392 | const rows: SourceRow[] = []; |
| 393 | |
| 394 | rows.push({ |
| 395 | key: "local", |
| 396 | shared: false, |
| 397 | title: gt("Local simulators"), |
| 398 | description: |
| 399 | members.length > 1 |
| 400 | ? gt("Simulators and emulators installed on each environment's own machine.") |
| 401 | : gt("Simulators and emulators installed on this environment's machine."), |
| 402 | environments: active.map((member) => { |
| 403 | const local = member.state?.hosts.find((host) => host.kind === "local"); |
| 404 | const status = local |
| 405 | ? hostStatus(member.state, local.id, m) |
| 406 | : hostStatus(member.state, "", m); |
| 407 | return { |
| 408 | environmentId: member.environmentId, |
| 409 | label: member.label, |
| 410 | added: true, |
| 411 | platforms: local?.platforms, |
| 412 | status: |
| 413 | status?.tone === "error" |
| 414 | ? { |
| 415 | ...status, |
| 416 | action: { |
| 417 | label: gt("Refresh"), |
| 418 | run: () => void list({ environmentId: member.environmentId, input: {} }), |
| 419 | }, |
| 420 | } |
| 421 | : status, |
| 422 | }; |
| 423 | }), |
| 424 | }); |
| 425 | |
| 426 | const grouped = groupDeviceHosts( |
| 427 | members.map((member) => ({ |
| 428 | environmentId: member.environmentId, |
| 429 | label: member.label, |
| 430 | hosts: member.settings?.deviceHosts ?? [], |
| 431 | })), |
| 432 | ); |
| 433 | const sshGroups = |
| 434 | grouped ?? |
| 435 | // Hosts that cannot be matched across environments stay one row per environment. |
| 436 | members.flatMap((member) => |
| 437 | (member.settings?.deviceHosts ?? []).map((host) => ({ |
| 438 | host, |
| 439 | members: [{ environmentId: member.environmentId, label: member.label, host }], |
| 440 | })), |
| 441 | ); |
| 442 | for (const group of sshGroups) { |
| 443 | const host = group.host; |
| 444 | const holders = new Set(group.members.map((member) => member.environmentId)); |
| 445 | const testing = group.members.some((member) => testingHosts.has(member.host.id)); |
| 446 | rows.push({ |
| 447 | key: `ssh:${host.id}:${group.members.map((member) => member.environmentId).join(",")}`, |
| 448 | shared: true, |
| 449 | title: host.label, |
| 450 | kind: "SSH", |
| 451 | description: host.target, |
| 452 | environments: active.map((member) => { |
| 453 | const held = group.members.find( |
| 454 | (candidate) => candidate.environmentId === member.environmentId, |
| 455 | ); |
| 456 | if (!held) |
| 457 | return { environmentId: member.environmentId, label: member.label, added: false }; |
| 458 | const status = testing |
| 459 | ? ({ tone: "pending", label: gt("Testing connection") } as const) |
| 460 | : hostStatus(member.state, held.host.id, m); |
| 461 | return { |
| 462 | environmentId: member.environmentId, |
| 463 | label: member.label, |
| 464 | added: true, |
| 465 | platforms: hostPlatforms(member.state, held.host.id), |
| 466 | status: |
| 467 | status?.tone === "error" && member.state?.supportsHostRetry |
| 468 | ? { |
| 469 | ...status, |
| 470 | label: gt("Can't connect"), |
| 471 | action: { |
| 472 | label: gt("Retry"), |
| 473 | run: () => retry(member.environmentId, held.host.id), |
| 474 | }, |
| 475 | } |
| 476 | : status, |
| 477 | }; |
| 478 | }), |
| 479 | actions: rowActions( |
| 480 | () => |
| 481 | setEditing({ kind: "ssh", host, original: host, ...(grouped ? {} : { only: holders }) }), |
| 482 | () => void saveSsh(host, true, host, holders), |
| 483 | <MenuItem onClick={() => void testSsh(group)}> |
| 484 | <T>Test connection</T> |
| 485 | </MenuItem>, |
| 486 | ), |
| 487 | }); |
| 488 | } |
| 489 | |
| 490 | if (props.supportsEas) { |
| 491 | const easHosts = new Map<string, EasDeviceHostConfig>(); |
| 492 | for (const member of members) |
| 493 | for (const host of member.settings?.easDeviceHosts ?? []) |
| 494 | if (!easHosts.has(host.id)) easHosts.set(host.id, host); |
| 495 | for (const host of easHosts.values()) { |
| 496 | const holders = members.filter((member) => |
| 497 | (member.settings?.easDeviceHosts ?? []).some((candidate) => candidate.id === host.id), |
| 498 | ); |
| 499 | const [firstHolder] = holders; |
| 500 | if (!firstHolder) continue; |
| 501 | const checkOn = ( |
| 502 | holders.find((member) => member.environmentId === primary?.environmentId) ?? firstHolder |
| 503 | ).environmentId; |
| 504 | rows.push({ |
| 505 | key: `eas:${host.id}`, |
| 506 | shared: true, |
| 507 | title: host.label, |
| 508 | kind: "Expo", |
| 509 | description: easSessionLimits(host, m), |
| 510 | environments: active.map((member) => { |
| 511 | const added = (member.settings?.easDeviceHosts ?? []).some( |
| 512 | (candidate) => candidate.id === host.id, |
| 513 | ); |
| 514 | return { |
| 515 | environmentId: member.environmentId, |
| 516 | label: member.label, |
| 517 | added, |
| 518 | ...(added |
| 519 | ? { |
| 520 | status: hostStatus(member.state, host.id, m), |
| 521 | platforms: hostPlatforms(member.state, host.id), |
| 522 | } |
| 523 | : {}), |
| 524 | }; |
| 525 | }), |
| 526 | actions: rowActions( |
| 527 | () => setEditing({ kind: "eas", host, isNew: false, environmentId: checkOn }), |
| 528 | () => void saveEas(host, true), |
| 529 | ), |
| 530 | }); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (props.supportsSimlock && simlockEnabled) { |
| 535 | const simlockHosts = new Map<string, SimlockDeviceHostConfig>(); |
| 536 | for (const environment of simlockEnvironments) |
| 537 | for (const host of environment.hosts) |
| 538 | if (!simlockHosts.has(host.id)) simlockHosts.set(host.id, host); |
| 539 | for (const host of simlockHosts.values()) { |
| 540 | const edit = () => setEditing({ kind: "simlock", host, isNew: false }); |
| 541 | rows.push({ |
| 542 | key: `simlock:${host.id}`, |
| 543 | shared: true, |
| 544 | title: host.label, |
| 545 | kind: "Simlock", |
| 546 | description: `${host.gatewayUrl.replace(/^https?:\/\//, "")} · ${simlockLeaseLimits(host, m)}`, |
| 547 | environments: active.map((member) => { |
| 548 | const held = (member.settings?.simlockDeviceHosts ?? []).find( |
| 549 | (candidate) => candidate.id === host.id, |
| 550 | ); |
| 551 | if (!held) |
| 552 | return { environmentId: member.environmentId, label: member.label, added: false }; |
| 553 | return { |
| 554 | environmentId: member.environmentId, |
| 555 | label: member.label, |
| 556 | added: true, |
| 557 | platforms: hostPlatforms(member.state, host.id), |
| 558 | // A blank token can still resolve to the machine's own, so the host's answer decides. |
| 559 | status: hostStatus(member.state, host.id, m), |
| 560 | }; |
| 561 | }), |
| 562 | actions: rowActions(edit, () => removeSimlock(host)), |
| 563 | }); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | const addMenu = ( |
| 568 | <Menu> |
| 569 | <MenuTrigger |
| 570 | render={<Button size="sm" variant="outline" disabled={disabled || editing !== null} />} |
| 571 | > |
| 572 | <PlusIcon className="size-3.5" /> <T>Add source</T> |
| 573 | <ChevronDownIcon className="size-3.5" /> |
| 574 | </MenuTrigger> |
| 575 | <MenuPopup align="end"> |
| 576 | <MenuItem |
| 577 | onClick={() => |
| 578 | setEditing({ |
| 579 | kind: "ssh", |
| 580 | host: { kind: "ssh", id: randomUUID(), label: "", target: "" }, |
| 581 | original: null, |
| 582 | }) |
| 583 | } |
| 584 | > |
| 585 | <T>Machine over SSH</T> |
| 586 | </MenuItem> |
| 587 | {props.supportsEas ? ( |
| 588 | <MenuItem |
| 589 | onClick={() => |
| 590 | primary && |
| 591 | setEditing({ |
| 592 | kind: "eas", |
| 593 | host: newEasDeviceHost(randomUUID()), |
| 594 | isNew: true, |
| 595 | environmentId: primary.environmentId, |
| 596 | }) |
| 597 | } |
| 598 | > |
| 599 | <T>Expo Simulators</T> |
| 600 | </MenuItem> |
| 601 | ) : null} |
| 602 | {props.supportsSimlock && simlockEnabled ? ( |
| 603 | <MenuItem |
| 604 | onClick={() => |
| 605 | setEditing({ kind: "simlock", host: newSimlockDeviceHost(randomUUID()), isNew: true }) |
| 606 | } |
| 607 | > |
| 608 | <T>Simlock pool (alpha)</T> |
| 609 | </MenuItem> |
| 610 | ) : null} |
| 611 | </MenuPopup> |
| 612 | </Menu> |
| 613 | ); |
| 614 | |
| 615 | return ( |
| 616 | <SettingsSection id="device-sources" title={gt("Device sources")} headerAction={addMenu}> |
| 617 | {rows.map((row) => ( |
| 618 | <DeviceSourceRow key={row.key} row={row} /> |
| 619 | ))} |
| 620 | {editing?.kind === "ssh" ? ( |
| 621 | <DeviceHostEditor |
| 622 | key={editing.host.id} |
| 623 | host={editing.host} |
| 624 | isNew={editing.original === null} |
| 625 | targets={targets} |
| 626 | busy={busy} |
| 627 | onSave={(host) => void saveSsh(host, false, editing.original ?? host, editing.only)} |
| 628 | onClose={() => setEditing(null)} |
| 629 | /> |
| 630 | ) : editing?.kind === "eas" ? ( |
| 631 | <EasDeviceHostEditor |
| 632 | key={editing.host.id} |
| 633 | environmentId={editing.environmentId} |
| 634 | host={editing.host} |
| 635 | isNew={editing.isNew} |
| 636 | busy={busy} |
| 637 | onSave={(host) => void saveEas(host, false, editing.isNew ? undefined : editing.host)} |
| 638 | onClose={() => setEditing(null)} |
| 639 | /> |
| 640 | ) : editing?.kind === "simlock" ? ( |
| 641 | <SimlockDeviceHostEditor |
| 642 | key={editing.host.id} |
| 643 | host={editing.host} |
| 644 | isNew={editing.isNew} |
| 645 | environments={simlockEnvironments} |
| 646 | busy={busy} |
| 647 | onSave={(host, tokens) => void saveSimlock(host, tokens)} |
| 648 | onClose={() => setEditing(null)} |
| 649 | /> |
| 650 | ) : null} |
| 651 | </SettingsSection> |
| 652 | ); |
| 653 | } |
| 654 | |
| 655 | const PLATFORMS = [ |
| 656 | { platform: "ios", label: "iOS", Icon: AppleIcon }, |
| 657 | { platform: "android", label: "Android", Icon: AndroidIcon }, |
| 658 | ] as const satisfies ReadonlyArray<{ platform: DevicePlatform; label: string; Icon: unknown }>; |
| 659 | |
| 660 | /** The platforms a source offers: one icon each, solid where available, muted where not. */ |
| 661 | function SourcePlatforms(props: { row: SourceRow }) { |
| 662 | const added = props.row.environments.filter((environment) => environment.added); |
| 663 | const known = added.filter((environment) => environment.platforms !== undefined); |
| 664 | return PLATFORMS.filter(({ platform }) => |
| 665 | known.some((environment) => |
| 666 | environment.platforms?.some((candidate) => candidate.platform === platform), |
| 667 | ), |
| 668 | ).map((entry) => <PlatformIcon key={entry.platform} {...entry} row={props.row} known={known} />); |
| 669 | } |
| 670 | |
| 671 | function PlatformIcon(props: { |
| 672 | platform: DevicePlatform; |
| 673 | label: string; |
| 674 | Icon: (typeof PLATFORMS)[number]["Icon"]; |
| 675 | row: SourceRow; |
| 676 | known: ReadonlyArray<SourceEnvironment>; |
| 677 | }) { |
| 678 | const gt = useGT(); |
| 679 | const headingLabels = { |
| 680 | Connected: gt("Connected"), |
| 681 | Available: gt("Available", { $context: "device platform status" }), |
| 682 | "Not available": gt("Not available"), |
| 683 | "Not added": gt("Not added"), |
| 684 | }; |
| 685 | const availability = (environment: SourceEnvironment) => |
| 686 | environment.platforms?.find((candidate) => candidate.platform === props.platform); |
| 687 | const availableOn = props.known.filter((environment) => availability(environment)?.available); |
| 688 | const several = props.row.environments.length > 1; |
| 689 | const available = availableOn.length > 0; |
| 690 | const partial = available && availableOn.length < props.known.length; |
| 691 | // A shared source usually answers the same everywhere, so its answer is given once. Each |
| 692 | // environment still signs in to it on its own, which is where they can differ. |
| 693 | const sharedAnswer = props.known.map(availability).find(Boolean); |
| 694 | const uniform = props.known.every( |
| 695 | (environment) => |
| 696 | availability(environment)?.available === sharedAnswer?.available && |
| 697 | availability(environment)?.reason === sharedAnswer?.reason, |
| 698 | ); |
| 699 | const unavailableOn = props.known.filter((environment) => !availability(environment)?.available); |
| 700 | const connected = props.row.environments.filter((environment) => environment.added); |
| 701 | const notAdded = props.row.environments.filter((environment) => !environment.added); |
| 702 | return ( |
| 703 | <Popover> |
| 704 | <PopoverTrigger |
| 705 | openOnHover |
| 706 | delay={150} |
| 707 | closeDelay={100} |
| 708 | render={ |
| 709 | <button |
| 710 | type="button" |
| 711 | aria-label={ |
| 712 | available |
| 713 | ? gt("{source}: {platform} available", { |
| 714 | source: props.row.title, |
| 715 | platform: props.label, |
| 716 | }) |
| 717 | : gt("{source}: {platform} not available", { |
| 718 | source: props.row.title, |
| 719 | platform: props.label, |
| 720 | }) |
| 721 | } |
| 722 | className={cn( |
| 723 | "relative grid size-6 shrink-0 place-items-center rounded-md outline-hidden hover:bg-accent focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring", |
| 724 | available ? "text-foreground" : "text-muted-foreground/45", |
| 725 | )} |
| 726 | /> |
| 727 | } |
| 728 | > |
| 729 | <props.Icon className={cn("size-3.5", props.platform === "ios" && "-translate-y-px")} /> |
| 730 | {partial ? ( |
| 731 | <span |
| 732 | aria-hidden |
| 733 | className="absolute top-0.5 right-0.5 size-1.5 rounded-full bg-warning ring-2 ring-background" |
| 734 | /> |
| 735 | ) : null} |
| 736 | </PopoverTrigger> |
| 737 | <PopoverPopup side="top" align="end" width="sm"> |
| 738 | <p className="text-sm font-medium"> |
| 739 | {available |
| 740 | ? partial |
| 741 | ? gt("{platform} available on some", { platform: props.label }) |
| 742 | : gt("{platform} available", { platform: props.label }) |
| 743 | : gt("{platform} not available", { platform: props.label })} |
| 744 | </p> |
| 745 | {props.row.shared ? ( |
| 746 | <> |
| 747 | {uniform && !available && sharedAnswer?.reason ? ( |
| 748 | <p className="mt-1 text-xs text-muted-foreground">{sharedAnswer.reason}</p> |
| 749 | ) : null} |
| 750 | {several ? ( |
| 751 | <dl className="mt-2 space-y-1.5 text-xs"> |
| 752 | {( |
| 753 | [ |
| 754 | ...(uniform |
| 755 | ? ([["Connected", connected]] as const) |
| 756 | : ([ |
| 757 | ["Available", availableOn], |
| 758 | ["Not available", unavailableOn], |
| 759 | ] as const)), |
| 760 | ["Not added", notAdded], |
| 761 | ] as const |
| 762 | ).map(([heading, environments]) => |
| 763 | environments.length > 0 ? ( |
| 764 | <div key={heading}> |
| 765 | <dt className="text-muted-foreground">{headingLabels[heading]}</dt> |
| 766 | {environments.map((environment) => { |
| 767 | const reason = |
| 768 | heading === "Not available" |
| 769 | ? availability(environment)?.reason |
| 770 | : undefined; |
| 771 | return ( |
| 772 | <dd key={environment.environmentId}> |
| 773 | <span className="block truncate">{environment.label}</span> |
| 774 | {reason ? ( |
| 775 | <span className="block text-muted-foreground">{reason}</span> |
| 776 | ) : null} |
| 777 | </dd> |
| 778 | ); |
| 779 | })} |
| 780 | </div> |
| 781 | ) : null, |
| 782 | )} |
| 783 | </dl> |
| 784 | ) : null} |
| 785 | </> |
| 786 | ) : several ? ( |
| 787 | <dl className="mt-2 grid grid-cols-[auto_1fr] gap-x-4 gap-y-1 text-xs"> |
| 788 | {props.known.map((environment) => { |
| 789 | const entry = availability(environment); |
| 790 | return ( |
| 791 | <Fragment key={environment.environmentId}> |
| 792 | <dt className="text-muted-foreground">{environment.label}</dt> |
| 793 | <dd className={entry?.available ? "" : "text-muted-foreground"}> |
| 794 | {entry?.available |
| 795 | ? gt("Available", { $context: "device platform status" }) |
| 796 | : (entry?.reason ?? gt("Not available"))} |
| 797 | </dd> |
| 798 | </Fragment> |
| 799 | ); |
| 800 | })} |
| 801 | </dl> |
| 802 | ) : !available ? ( |
| 803 | <p className="mt-1 text-xs text-muted-foreground"> |
| 804 | {availability(props.known[0]!)?.reason ?? gt("Not set up on this machine.")} |
| 805 | </p> |
| 806 | ) : null} |
| 807 | </PopoverPopup> |
| 808 | </Popover> |
| 809 | ); |
| 810 | } |
| 811 | |
| 812 | function DeviceSourceRow(props: { row: SourceRow }) { |
| 813 | const { row } = props; |
| 814 | const several = row.environments.length > 1; |
| 815 | const issues = row.environments.filter( |
| 816 | (environment): environment is SourceEnvironment & { status: SourceStatus } => |
| 817 | environment.status !== undefined, |
| 818 | ); |
| 819 | const problems = issues.filter((environment) => environment.status.tone !== "pending"); |
| 820 | const pending = issues.find((environment) => environment.status.tone === "pending"); |
| 821 | return ( |
| 822 | <div className="flex items-center gap-3 border-t border-border/60 px-3 py-3 first:border-t-0 sm:px-4"> |
| 823 | <div className="min-w-0 flex-1"> |
| 824 | <div className="flex min-w-0 items-center gap-1.5"> |
| 825 | <p className="truncate text-sm font-medium">{row.title}</p> |
| 826 | {row.kind ? ( |
| 827 | <span className="shrink-0 rounded border border-border px-1 text-3xs uppercase tracking-wide text-muted-foreground"> |
| 828 | {row.kind} |
| 829 | </span> |
| 830 | ) : null} |
| 831 | </div> |
| 832 | <p className="truncate text-xs text-muted-foreground">{row.description}</p> |
| 833 | {problems.map((environment) => ( |
| 834 | <p |
| 835 | key={environment.environmentId} |
| 836 | role="status" |
| 837 | className={cn( |
| 838 | "mt-1 flex flex-wrap items-center gap-1.5 text-xs", |
| 839 | environment.status.tone === "error" ? "text-destructive" : "text-warning", |
| 840 | )} |
| 841 | > |
| 842 | <span className="min-w-0 break-words"> |
| 843 | {several ? `${environment.label}: ` : ""} |
| 844 | {environment.status.tone === "error" && !several && environment.status.detail |
| 845 | ? environment.status.detail |
| 846 | : environment.status.label} |
| 847 | </span> |
| 848 | {environment.status.tone === "warning" && environment.status.detail ? ( |
| 849 | <Popover> |
| 850 | <PopoverTrigger render={<InlineButton />}> |
| 851 | <T>Details</T> |
| 852 | </PopoverTrigger> |
| 853 | <PopoverPopup side="bottom" align="start" width="lg" padding="compact"> |
| 854 | <p className="whitespace-pre-wrap break-words font-mono text-xs text-muted-foreground"> |
| 855 | {environment.status.detail} |
| 856 | </p> |
| 857 | </PopoverPopup> |
| 858 | </Popover> |
| 859 | ) : null} |
| 860 | {environment.status.action ? ( |
| 861 | <InlineButton onClick={environment.status.action.run}> |
| 862 | {environment.status.action.label} |
| 863 | </InlineButton> |
| 864 | ) : null} |
| 865 | </p> |
| 866 | ))} |
| 867 | </div> |
| 868 | {pending ? ( |
| 869 | <span |
| 870 | role="status" |
| 871 | className="flex shrink-0 items-center gap-1.5 text-xs text-muted-foreground" |
| 872 | > |
| 873 | <Spinner size="xs" /> |
| 874 | {pending.status.label} |
| 875 | </span> |
| 876 | ) : ( |
| 877 | <div className="flex shrink-0 items-center gap-0.5"> |
| 878 | <SourcePlatforms row={row} /> |
| 879 | </div> |
| 880 | )} |
| 881 | {row.actions ? <div className="flex shrink-0 items-center gap-1">{row.actions}</div> : null} |
| 882 | </div> |
| 883 | ); |
| 884 | } |