import { useAtomValue } from "@effect/atom-react"; import { Fragment, useState, type ReactNode } from "react"; import * as Cause from "effect/Cause"; import * as Option from "effect/Option"; import { AsyncResult } from "effect/reactivity"; import { AuthSettingsWriteScope, type DevicePlatform, type DevicePlatformAvailability, type DeviceServiceState, type EasDeviceHostConfig, type EnvironmentId, type ServerSettingsPatch, type SimlockDeviceHostConfig, type SshDeviceHostConfig, } from "@t3tools/contracts"; import { ChevronDownIcon, MoreVertical, PlusIcon } from "lucide-react"; import { cn, randomUUID } from "../../lib/utils"; import { deviceEnvironment } from "../../state/device"; import { environmentAtomList, environmentIdsKey } from "../../state/environmentAtomList"; import { serverEnvironment } from "../../state/server"; import { readEnvironmentScope } from "../../state/session"; import { useAtomCommand } from "../../state/use-atom-command"; import { AndroidIcon, AppleIcon } from "../Icons"; import { Button, InlineButton } from "../ui/button"; import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; import { Popover, PopoverPopup, PopoverTrigger } from "../ui/popover"; import { Spinner } from "../ui/spinner"; import { toastManager } from "../ui/toast"; import { incompleteListings } from "../device/DeviceSetup"; import { DeviceHostEditor } from "./DeviceHostEditor"; import { updateDeviceHosts, groupDeviceHosts } from "./deviceHostsSettings.logic"; import { EasDeviceHostEditor } from "./EasDeviceHostEditor"; import { easSessionLimits, newEasDeviceHost, updateEasDeviceHosts, } from "./easDeviceHostsSettings.logic"; import { useSettingsScope } from "./SettingsScopeContext"; import { SettingsSection } from "./settingsLayout"; import { settingsScopeEnvironmentLabel } from "./settingsScopeAxis"; import { SimlockDeviceHostEditor } from "./SimlockDeviceHostEditor"; import { newSimlockDeviceHost, planSimlockDeviceHostSave, simlockLeaseLimits, updateSimlockDeviceHosts, type SimlockTokenEntry, } from "./simlockDeviceHostsSettings.logic"; import { useHostConnectionChecks } from "./useHostConnectionChecks"; const deviceHostStatesAtom = environmentAtomList("web-device-host-states", (environmentId) => deviceEnvironment.state({ environmentId, input: {} }), ); const failureMessage = (cause: Cause.Cause) => { const error = Cause.squash(cause); return error instanceof Error ? error.message : String(error); }; /** Something on one environment that is in progress or needs a fix; a working source has none. */ interface SourceStatus { readonly tone: "pending" | "warning" | "error"; readonly label: string; readonly detail?: string | undefined; readonly action?: { readonly label: string; readonly run: () => void } | undefined; } /** A source on one environment whose devices are on. */ interface SourceEnvironment { readonly environmentId: EnvironmentId; readonly label: string; readonly added: boolean; readonly status?: SourceStatus | undefined; /** Unknown until the environment's hub has reached the source. */ readonly platforms?: ReadonlyArray | undefined; } interface SourceRow { readonly key: string; readonly title: string; readonly kind?: string; readonly description: ReactNode; readonly environments: ReadonlyArray; /** * One machine or account every environment reaches the same way (an SSH host, EAS, a Simlock * pool), so its platforms are the same for all of them. Local simulators differ per machine. */ readonly shared: boolean; readonly actions?: ReactNode; } type Editing = | { readonly kind: "ssh"; readonly host: SshDeviceHostConfig; readonly original: SshDeviceHostConfig | null; /** Limits the save to these environments, for a host that differs between them. */ readonly only?: ReadonlySet; } | { readonly kind: "eas"; readonly host: EasDeviceHostConfig; readonly isNew: boolean; /** Where the editor checks the Expo account: an environment that holds the host. */ readonly environmentId: EnvironmentId; } | { readonly kind: "simlock"; readonly host: SimlockDeviceHostConfig; readonly isNew: boolean }; /** What a configured host is doing on one environment, when it is not simply working. */ function hostStatus( state: DeviceServiceState | undefined, hostId: string, ): SourceStatus | undefined { if (!state) return { tone: "pending", label: "Loading" }; const status = state.hostStatuses[hostId]; switch (status?.status) { case "ready": { const incomplete = incompleteListings(status); if (incomplete.length === 0) return undefined; const list = incomplete.length > 1 ? "Device" : incomplete[0] === "ios" ? "iOS" : "Android"; return { tone: "warning", label: `${list} list may be incomplete`, detail: status.detail }; } case "installing": case "starting": return { tone: "pending", label: "Starting", detail: status.detail }; case "failed": return { tone: "error", label: "Failed", detail: status.detail }; default: return undefined; } } const hostPlatforms = (state: DeviceServiceState | undefined, hostId: string) => state?.hosts.find((host) => host.id === hostId)?.platforms; /** * Every device source the selected environments can open, one row each. A source configured on * several environments is one row; how it stands on each lives in the row's breakdown. */ export function DeviceSourcesSettings(props: { readonly supportsEas: boolean; readonly supportsSimlock: boolean; }) { const { scope, environments, connectedEnvironments, environment: primary } = useSettingsScope(); const projectScope = scope.kind === "project" || scope.kind === "checkout"; const update = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false }); const list = useAtomCommand(deviceEnvironment.list, { reportFailure: false }); const [editing, setEditing] = useState(null); const [busy, setBusy] = useState(false); const label = (environment: (typeof environments)[number]) => settingsScopeEnvironmentLabel(environment, environments); const targets = environments.map((environment) => ({ environmentId: environment.environmentId, label: label(environment), connected: environment.connection.phase === "connected", })); const { testConnection } = useHostConnectionChecks(targets); const ids = connectedEnvironments.map((environment) => environment.environmentId); const results = useAtomValue(deviceHostStatesAtom(environmentIdsKey(ids))); const members = connectedEnvironments.map((environment, index) => { const result = results[index]; return { environmentId: environment.environmentId, label: label(environment), settings: environment.serverConfig?.settings, state: result ? Option.getOrUndefined(AsyncResult.value(result)) : undefined, }; }); // Environments with devices off are left out: the switch above already says so. const active = members.filter((member) => member.settings?.enableDeviceSupport); const simlockEnabled = members.some((member) => member.settings?.simlockDeviceHostsEnabled); // Like every scoped setting, a change reaches the connected environments and skips the rest. const disabled = projectScope || busy || members.length === 0; /** Writes one settings patch per environment; reports the environments it could not change. */ const write = async ( patches: ReadonlyArray<{ environmentId: EnvironmentId; patch: ServerSettingsPatch }>, failedTitle: string, ) => { if (projectScope) return false; const name = (environmentId: EnvironmentId) => members.find((member) => member.environmentId === environmentId)?.label ?? environmentId; const unwritable = patches.filter( (patch) => !readEnvironmentScope(patch.environmentId, AuthSettingsWriteScope), ); if (unwritable.length > 0) { toastManager.add({ type: "error", title: failedTitle, description: `This connection cannot change settings on ${unwritable.map((patch) => name(patch.environmentId)).join(", ")}.`, }); return false; } setBusy(true); const outcomes = await Promise.all( patches.map((patch) => update({ environmentId: patch.environmentId, input: { patch: patch.patch } }), ), ); setBusy(false); const failed = patches.flatMap((patch, index) => { const outcome = outcomes[index]; return outcome && outcome._tag === "Failure" ? [`${name(patch.environmentId)}: ${failureMessage(outcome.cause)}`] : []; }); if (failed.length === 0) return true; toastManager.add({ type: "error", title: failedTitle, description: failed.join("\n") }); return false; }; const saveSsh = async ( host: SshDeviceHostConfig, remove: boolean, original = host, only?: ReadonlySet, ) => { try { const saved = await write( members .filter((member) => !only || only.has(member.environmentId)) .map((member) => ({ environmentId: member.environmentId, patch: { deviceHosts: updateDeviceHosts( member.settings?.deviceHosts ?? [], host, remove, original, ), }, })), remove ? "Device host not removed everywhere" : "Device host not saved everywhere", ); if (saved) setEditing(null); } catch (error) { toastManager.add({ type: "error", title: "Device host not saved", description: error instanceof Error ? error.message : String(error), }); } }; const saveEas = async ( host: EasDeviceHostConfig, remove: boolean, previous?: EasDeviceHostConfig, ) => { const keepToken = previous !== undefined && host.accessToken === previous.accessToken; const saved = await write( members.map((member) => ({ environmentId: member.environmentId, patch: { easDeviceHosts: updateEasDeviceHosts( member.settings?.easDeviceHosts ?? [], host, remove, keepToken, ), }, })), remove ? "EAS host not removed everywhere" : "EAS host not saved everywhere", ); if (saved) setEditing(null); }; const simlockEnvironments = members.map((member) => ({ environmentId: member.environmentId, label: member.label, hosts: member.settings?.simlockDeviceHosts ?? [], })); const saveSimlock = async ( host: SimlockDeviceHostConfig, tokens: ReadonlyMap, ) => { const plan = planSimlockDeviceHostSave(simlockEnvironments, host, tokens); if ("problem" in plan) return; const saved = await write( plan.patches.map((patch) => ({ environmentId: patch.environmentId, patch: { simlockDeviceHosts: patch.hosts }, })), "Simlock host not saved everywhere", ); if (saved) setEditing(null); }; const removeSimlock = (host: SimlockDeviceHostConfig) => void write( simlockEnvironments .filter((environment) => environment.hosts.some((candidate) => candidate.id === host.id)) .map((environment) => ({ environmentId: environment.environmentId, patch: { simlockDeviceHosts: updateSimlockDeviceHosts(environment.hosts, host, true) }, })), "Simlock host not removed everywhere", ); const [testing, setTesting] = useState>(new Set()); const testSsh = async (host: SshDeviceHostConfig) => { setTesting((current) => new Set(current).add(host.id)); const checked = await testConnection(host).finally(() => setTesting((current) => { const next = new Set(current); next.delete(host.id); return next; }), ); if (!checked) return; const failed = targets.filter((target) => checked[target.environmentId]?.status === "failed"); toastManager.add({ type: failed.length ? "error" : "success", title: failed.length ? `${host.label}: ${failed.length} of ${targets.length} environments failed` : `${host.label}: connection checks passed`, description: failed.length ? failed .map((target) => { const result = checked[target.environmentId]; return `${target.label}: ${result?.status === "failed" ? result.error : "failed"}`; }) .join("\n") : "Connected or already available locally on each selected environment.", }); }; const retry = (environmentId: EnvironmentId, hostId: string) => void list({ environmentId, input: { retryHostId: hostId } }); const rowActions = (onEdit: () => void, onRemove: () => void, extra?: ReactNode) => ( <> } > {extra} Remove ); const rows: SourceRow[] = []; rows.push({ key: "local", shared: false, title: "Local simulators", description: members.length > 1 ? "Simulators and emulators installed on each environment's own machine." : "Simulators and emulators installed on this environment's machine.", environments: active.map((member) => { const local = member.state?.hosts.find((host) => host.kind === "local"); const status = local ? hostStatus(member.state, local.id) : hostStatus(member.state, ""); return { environmentId: member.environmentId, label: member.label, added: true, platforms: local?.platforms, status: status?.tone === "error" ? { ...status, action: { label: "Refresh", run: () => void list({ environmentId: member.environmentId, input: {} }), }, } : status, }; }), }); const grouped = groupDeviceHosts( members.map((member) => ({ environmentId: member.environmentId, label: member.label, hosts: member.settings?.deviceHosts ?? [], })), ); const sshGroups = grouped ?? // Hosts that cannot be matched across environments stay one row per environment. members.flatMap((member) => (member.settings?.deviceHosts ?? []).map((host) => ({ host, members: [{ environmentId: member.environmentId, label: member.label, host }], })), ); for (const group of sshGroups) { const host = group.host; const holders = new Set(group.members.map((member) => member.environmentId)); rows.push({ key: `ssh:${host.id}:${group.members.map((member) => member.environmentId).join(",")}`, shared: true, title: host.label, kind: "SSH", description: host.target, environments: active.map((member) => { const held = group.members.find( (candidate) => candidate.environmentId === member.environmentId, ); if (!held) return { environmentId: member.environmentId, label: member.label, added: false }; const status = testing.has(host.id) ? ({ tone: "pending", label: "Testing connection" } as const) : hostStatus(member.state, held.host.id); return { environmentId: member.environmentId, label: member.label, added: true, platforms: hostPlatforms(member.state, held.host.id), status: status?.tone === "error" && member.state?.supportsHostRetry ? { ...status, label: "Can't connect", action: { label: "Retry", run: () => retry(member.environmentId, held.host.id) }, } : status, }; }), actions: rowActions( () => setEditing({ kind: "ssh", host, original: host, ...(grouped ? {} : { only: holders }) }), () => void saveSsh(host, true, host, holders), void testSsh(host)}>Test connection, ), }); } if (props.supportsEas) { const easHosts = new Map(); for (const member of members) for (const host of member.settings?.easDeviceHosts ?? []) if (!easHosts.has(host.id)) easHosts.set(host.id, host); for (const host of easHosts.values()) { const holders = members.filter((member) => (member.settings?.easDeviceHosts ?? []).some((candidate) => candidate.id === host.id), ); const [firstHolder] = holders; if (!firstHolder) continue; const checkOn = ( holders.find((member) => member.environmentId === primary?.environmentId) ?? firstHolder ).environmentId; rows.push({ key: `eas:${host.id}`, shared: true, title: host.label, kind: "EAS", description: easSessionLimits(host), environments: active.map((member) => { const added = (member.settings?.easDeviceHosts ?? []).some( (candidate) => candidate.id === host.id, ); return { environmentId: member.environmentId, label: member.label, added, ...(added ? { status: hostStatus(member.state, host.id), platforms: hostPlatforms(member.state, host.id), } : {}), }; }), actions: rowActions( () => setEditing({ kind: "eas", host, isNew: false, environmentId: checkOn }), () => void saveEas(host, true), ), }); } } if (props.supportsSimlock && simlockEnabled) { const simlockHosts = new Map(); for (const environment of simlockEnvironments) for (const host of environment.hosts) if (!simlockHosts.has(host.id)) simlockHosts.set(host.id, host); for (const host of simlockHosts.values()) { const edit = () => setEditing({ kind: "simlock", host, isNew: false }); rows.push({ key: `simlock:${host.id}`, shared: true, title: host.label, kind: "Simlock", description: `${host.gatewayUrl.replace(/^https?:\/\//, "")} ยท ${simlockLeaseLimits(host)}`, environments: active.map((member) => { const held = (member.settings?.simlockDeviceHosts ?? []).find( (candidate) => candidate.id === host.id, ); if (!held) return { environmentId: member.environmentId, label: member.label, added: false }; return { environmentId: member.environmentId, label: member.label, added: true, platforms: hostPlatforms(member.state, host.id), // A blank token can still resolve to the machine's own, so the host's answer decides. status: hostStatus(member.state, host.id), }; }), actions: rowActions(edit, () => removeSimlock(host)), }); } } const addMenu = ( } > Add source setEditing({ kind: "ssh", host: { kind: "ssh", id: randomUUID(), label: "", target: "" }, original: null, }) } > Machine over SSH {props.supportsEas ? ( primary && setEditing({ kind: "eas", host: newEasDeviceHost(randomUUID()), isNew: true, environmentId: primary.environmentId, }) } > EAS Simulator ) : null} {props.supportsSimlock && simlockEnabled ? ( setEditing({ kind: "simlock", host: newSimlockDeviceHost(randomUUID()), isNew: true }) } > Simlock pool (alpha) ) : null} ); return ( {rows.map((row) => ( ))} {editing?.kind === "ssh" ? ( void saveSsh(host, false, editing.original ?? host, editing.only)} onClose={() => setEditing(null)} /> ) : editing?.kind === "eas" ? ( void saveEas(host, false, editing.isNew ? undefined : editing.host)} onClose={() => setEditing(null)} /> ) : editing?.kind === "simlock" ? ( void saveSimlock(host, tokens)} onClose={() => setEditing(null)} /> ) : null} ); } const PLATFORMS = [ { platform: "ios", label: "iOS", Icon: AppleIcon }, { platform: "android", label: "Android", Icon: AndroidIcon }, ] as const satisfies ReadonlyArray<{ platform: DevicePlatform; label: string; Icon: unknown }>; /** The platforms a source offers: one icon each, solid where available, muted where not. */ function SourcePlatforms(props: { row: SourceRow }) { const added = props.row.environments.filter((environment) => environment.added); const known = added.filter((environment) => environment.platforms !== undefined); return PLATFORMS.filter(({ platform }) => known.some((environment) => environment.platforms?.some((candidate) => candidate.platform === platform), ), ).map((entry) => ); } function PlatformIcon(props: { platform: DevicePlatform; label: string; Icon: (typeof PLATFORMS)[number]["Icon"]; row: SourceRow; known: ReadonlyArray; }) { const availability = (environment: SourceEnvironment) => environment.platforms?.find((candidate) => candidate.platform === props.platform); const availableOn = props.known.filter((environment) => availability(environment)?.available); const several = props.row.environments.length > 1; const available = availableOn.length > 0; const partial = available && availableOn.length < props.known.length; // A shared source usually answers the same everywhere, so its answer is given once. Each // environment still signs in to it on its own, which is where they can differ. const sharedAnswer = props.known.map(availability).find(Boolean); const uniform = props.known.every( (environment) => availability(environment)?.available === sharedAnswer?.available && availability(environment)?.reason === sharedAnswer?.reason, ); const unavailableOn = props.known.filter((environment) => !availability(environment)?.available); const connected = props.row.environments.filter((environment) => environment.added); const notAdded = props.row.environments.filter((environment) => !environment.added); return ( } > {partial ? ( ) : null}

{props.label}{" "} {available ? (partial ? "available on some" : "available") : "not available"}

{props.row.shared ? ( <> {uniform && !available && sharedAnswer?.reason ? (

{sharedAnswer.reason}

) : null} {several ? (
{( [ ...(uniform ? ([["Connected", connected]] as const) : ([ ["Available", availableOn], ["Not available", unavailableOn], ] as const)), ["Not added", notAdded], ] as const ).map(([heading, environments]) => environments.length > 0 ? (
{heading}
{environments.map((environment) => { const reason = heading === "Not available" ? availability(environment)?.reason : undefined; return (
{environment.label} {reason ? ( {reason} ) : null}
); })}
) : null, )}
) : null} ) : several ? (
{props.known.map((environment) => { const entry = availability(environment); return (
{environment.label}
{entry?.available ? "Available" : (entry?.reason ?? "Not available")}
); })}
) : !available ? (

{availability(props.known[0]!)?.reason ?? "Not set up on this machine."}

) : null}
); } function DeviceSourceRow(props: { row: SourceRow }) { const { row } = props; const several = row.environments.length > 1; const issues = row.environments.filter( (environment): environment is SourceEnvironment & { status: SourceStatus } => environment.status !== undefined, ); const problems = issues.filter((environment) => environment.status.tone !== "pending"); const pending = issues.find((environment) => environment.status.tone === "pending"); return (

{row.title}

{row.kind ? ( {row.kind} ) : null}

{row.description}

{problems.map((environment) => (

{several ? `${environment.label}: ` : ""} {!several && environment.status.detail ? environment.status.detail : environment.status.label} {environment.status.action ? ( {environment.status.action.label} ) : null}

))}
{pending ? ( {pending.status.label} ) : (
)} {row.actions ?
{row.actions}
: null}
); }