Skip to content

Commit 6290f6a

Browse files
fix(web): show device hosts from every selected environment
1 parent 49a08db commit 6290f6a

5 files changed

Lines changed: 229 additions & 156 deletions

File tree

‎apps/web/src/components/settings/DeviceHostsSettings.tsx‎

Lines changed: 185 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,17 @@ import { useSettingsScope } from "./SettingsScopeContext";
2323
import { toastManager } from "../ui/toast";
2424
import { updateDeviceHosts } from "./deviceHostsSettings.logic";
2525

26-
export function DeviceHostsSettings(props: {
27-
environmentId: EnvironmentId | null;
28-
hosts: ReadonlyArray<SshDeviceHostConfig>;
29-
}) {
30-
const { scope, environments } = useSettingsScope();
26+
export function DeviceHostsSettings(props: { environmentId: EnvironmentId | null }) {
27+
const { scope, environments, connectedEnvironments, environment: selected } = useSettingsScope();
3128
const projectScope = scope.kind === "project" || scope.kind === "checkout";
32-
const update = useAtomCommand(serverEnvironment.updateSettings);
33-
const test = useAtomCommand(deviceEnvironment.testHost, { reportFailure: false });
34-
const { state } = useDeviceState(props.environmentId);
29+
const update = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false });
3530
const [editing, setEditing] = useState<SshDeviceHostConfig | null>(null);
31+
const [originalHost, setOriginalHost] = useState<SshDeviceHostConfig | null>(null);
3632
const [busy, setBusy] = useState(false);
3733
const validPort = (port: number | undefined) =>
3834
port === undefined || (Number.isInteger(port) && port >= 1 && port <= 65535);
39-
const [checks, setChecks] = useState<
40-
Record<
41-
string,
42-
{ pending?: boolean; platforms?: ReadonlyArray<DevicePlatformAvailability>; error?: string }
43-
>
44-
>({});
45-
const setCheck = (id: string, value: (typeof checks)[string]) =>
46-
setChecks((current) => ({ ...current, [id]: value }));
47-
const save = async (host: SshDeviceHostConfig, remove = false) => {
35+
const { checks, testConnection } = useHostConnectionChecks(props.environmentId);
36+
const save = async (host: SshDeviceHostConfig, remove = false, original = host) => {
4837
if (!props.environmentId || projectScope) return;
4938
setBusy(true);
5039
try {
@@ -61,6 +50,7 @@ export function DeviceHostsSettings(props: {
6150
environment.serverConfig.settings.deviceHosts,
6251
host,
6352
remove,
53+
original,
6454
),
6555
},
6656
},
@@ -84,21 +74,6 @@ export function DeviceHostsSettings(props: {
8474
setBusy(false);
8575
}
8676
};
87-
const testConnection = async (host: SshDeviceHostConfig) => {
88-
if (!props.environmentId || checks[host.id]?.pending) return;
89-
setCheck(host.id, { pending: true });
90-
try {
91-
const summary = await test({ environmentId: props.environmentId, input: host });
92-
setCheck(
93-
host.id,
94-
summary._tag === "Failure"
95-
? { error: Cause.pretty(summary.cause) }
96-
: { platforms: summary.value.platforms },
97-
);
98-
} catch (error) {
99-
setCheck(host.id, { error: error instanceof Error ? error.message : String(error) });
100-
}
101-
};
10277
return (
10378
<SettingsRow
10479
id="device-hosts"
@@ -112,6 +87,7 @@ export function DeviceHostsSettings(props: {
11287
variant="outline"
11388
disabled={projectScope || busy || !props.environmentId || editing !== null}
11489
onClick={() => {
90+
setOriginalHost(null);
11591
setEditing({ id: randomUUID(), label: "", target: "" });
11692
}}
11793
>
@@ -126,122 +102,31 @@ export function DeviceHostsSettings(props: {
126102
</p>
127103
) : (
128104
<>
129-
{props.hosts.map((host) => {
130-
const status = state.hostStatuses[host.id];
131-
const check = checks[host.id];
132-
const platforms =
133-
check?.platforms ??
134-
state.hosts.find((value) => value.id === host.id)?.platforms ??
135-
[];
136-
const progress = check?.pending
137-
? "Checking connection…"
138-
: status?.status === "installing"
139-
? "Installing device support…"
140-
: status?.status === "starting"
141-
? "Connecting…"
142-
: null;
143-
const error =
144-
check?.error ?? (status?.status === "failed" ? status.detail : undefined);
145-
return (
146-
<div
147-
key={host.id}
148-
className="flex items-center gap-2 border-t border-border/50 py-2.5"
149-
>
150-
<div className="min-w-0 flex-1">
151-
<div className="flex min-w-0 items-center gap-2">
152-
<p className="truncate text-sm font-medium">{host.label}</p>
153-
{platforms
154-
.filter((platform) => platform.available)
155-
.map((platform) => (
156-
<Tooltip key={platform.platform}>
157-
<TooltipTrigger
158-
render={
159-
<span
160-
tabIndex={0}
161-
role="img"
162-
aria-label={
163-
platform.platform === "ios"
164-
? "iOS available"
165-
: "Android available"
166-
}
167-
className="shrink-0 text-muted-foreground"
168-
/>
169-
}
170-
>
171-
{platform.platform === "ios" ? (
172-
<AppleIcon className="size-3.5" />
173-
) : (
174-
<AndroidIcon className="size-3.5" />
175-
)}
176-
</TooltipTrigger>
177-
<TooltipPopup>
178-
{platform.platform === "ios" ? "iOS available" : "Android available"}
179-
</TooltipPopup>
180-
</Tooltip>
181-
))}
182-
</div>
183-
<p className="truncate text-xs text-muted-foreground">{host.target}</p>
184-
{error ? (
185-
<div className="mt-1" role="status">
186-
<details className="text-xs text-destructive">
187-
<summary>Connection failed</summary>
188-
<p className="mt-1 whitespace-pre-wrap break-words">{error}</p>
189-
</details>
190-
</div>
191-
) : null}
192-
</div>
193-
{progress ? (
194-
<span
195-
role="status"
196-
className="inline-flex items-center gap-1.5 text-xs text-muted-foreground"
197-
>
198-
<Spinner className="size-3" />
199-
{progress}
200-
</span>
201-
) : null}
202-
<Menu>
203-
<MenuTrigger
204-
render={
205-
<Button
206-
size="icon-sm"
207-
variant="ghost-muted"
208-
disabled={projectScope || busy}
209-
aria-label={host.label + " options"}
210-
/>
211-
}
212-
>
213-
<MoreVertical />
214-
</MenuTrigger>
215-
<MenuPopup align="end">
216-
<MenuItem
217-
onClick={() => {
218-
setEditing(host);
219-
}}
220-
>
221-
Edit
222-
</MenuItem>
223-
<MenuItem variant="destructive" onClick={() => void save(host, true)}>
224-
Remove
225-
</MenuItem>
226-
</MenuPopup>
227-
</Menu>
228-
<Button
229-
size="sm"
230-
variant="outline"
231-
disabled={busy || progress !== null}
232-
onClick={() => void testConnection(host)}
233-
>
234-
Test connection
235-
</Button>
236-
</div>
237-
);
238-
})}
105+
{connectedEnvironments.map((environment) => (
106+
<div key={environment.environmentId}>
107+
{connectedEnvironments.length > 1 ? (
108+
<p className="pt-3 pb-1 text-xs font-medium text-muted-foreground">
109+
{environment.label}
110+
</p>
111+
) : null}
112+
<DeviceHostList
113+
environmentId={environment.environmentId}
114+
hosts={environment.serverConfig?.settings.deviceHosts ?? []}
115+
busy={projectScope || busy}
116+
onEdit={(host) => {
117+
setOriginalHost(host);
118+
setEditing(host);
119+
}}
120+
onRemove={(host) => void save(host, true)}
121+
/>
122+
</div>
123+
))}
239124
{editing ? (
240125
<form
241126
className="space-y-3 border-t border-border/50 py-3"
242127
onSubmit={(event) => {
243128
event.preventDefault();
244-
void save(editing);
129+
void save(editing, false, originalHost ?? editing);
245130
}}
246131
>
247132
<label className="block space-y-1 text-sm">
@@ -320,7 +205,9 @@ export function DeviceHostsSettings(props: {
320205
}
321206
onClick={() => void testConnection(editing)}
322207
>
323-
Test connection
208+
{connectedEnvironments.length > 1
209+
? `Test from ${selected?.label}`
210+
: "Test connection"}
324211
</Button>
325212
<Button
326213
size="sm"
@@ -359,3 +246,157 @@ export function DeviceHostsSettings(props: {
359246
</SettingsRow>
360247
);
361248
}
249+
250+
function useHostConnectionChecks(environmentId: EnvironmentId | null) {
251+
const test = useAtomCommand(deviceEnvironment.testHost, { reportFailure: false });
252+
const [checks, setChecks] = useState<
253+
Record<
254+
string,
255+
{ pending?: boolean; platforms?: ReadonlyArray<DevicePlatformAvailability>; error?: string }
256+
>
257+
>({});
258+
const setCheck = (id: string, value: (typeof checks)[string]) =>
259+
setChecks((current) => ({ ...current, [id]: value }));
260+
const testConnection = async (host: SshDeviceHostConfig) => {
261+
if (!environmentId || checks[host.id]?.pending) return;
262+
setCheck(host.id, { pending: true });
263+
try {
264+
const summary = await test({ environmentId: environmentId, input: host });
265+
setCheck(
266+
host.id,
267+
summary._tag === "Failure"
268+
? { error: Cause.pretty(summary.cause) }
269+
: { platforms: summary.value.platforms },
270+
);
271+
} catch (error) {
272+
setCheck(host.id, { error: error instanceof Error ? error.message : String(error) });
273+
}
274+
};
275+
return { checks, testConnection };
276+
}
277+
278+
function DeviceHostList({
279+
environmentId,
280+
hosts,
281+
busy,
282+
onEdit,
283+
onRemove,
284+
}: {
285+
environmentId: EnvironmentId;
286+
hosts: ReadonlyArray<SshDeviceHostConfig>;
287+
busy: boolean;
288+
onEdit: (host: SshDeviceHostConfig) => void;
289+
onRemove: (host: SshDeviceHostConfig) => void;
290+
}) {
291+
const { state } = useDeviceState(environmentId);
292+
const { checks, testConnection } = useHostConnectionChecks(environmentId);
293+
return (
294+
<>
295+
{hosts.length === 0 ? (
296+
<p className="py-2 text-sm text-muted-foreground">No device hosts.</p>
297+
) : null}
298+
{hosts.map((host) => {
299+
const status = state.hostStatuses[host.id];
300+
const check = checks[host.id];
301+
const platforms =
302+
check?.platforms ?? state.hosts.find((value) => value.id === host.id)?.platforms ?? [];
303+
const progress = check?.pending
304+
? "Checking connection…"
305+
: status?.status === "installing"
306+
? "Installing device support…"
307+
: status?.status === "starting"
308+
? "Connecting…"
309+
: null;
310+
const error = check?.error ?? (status?.status === "failed" ? status.detail : undefined);
311+
return (
312+
<div key={host.id} className="flex items-center gap-2 border-t border-border/50 py-2.5">
313+
<div className="min-w-0 flex-1">
314+
<div className="flex min-w-0 items-center gap-2">
315+
<p className="truncate text-sm font-medium">{host.label}</p>
316+
{platforms
317+
.filter((platform) => platform.available)
318+
.map((platform) => (
319+
<Tooltip key={platform.platform}>
320+
<TooltipTrigger
321+
render={
322+
<span
323+
tabIndex={0}
324+
role="img"
325+
aria-label={
326+
platform.platform === "ios" ? "iOS available" : "Android available"
327+
}
328+
className="shrink-0 text-muted-foreground"
329+
/>
330+
}
331+
>
332+
{platform.platform === "ios" ? (
333+
<AppleIcon className="size-3.5" />
334+
) : (
335+
<AndroidIcon className="size-3.5" />
336+
)}
337+
</TooltipTrigger>
338+
<TooltipPopup>
339+
{platform.platform === "ios" ? "iOS available" : "Android available"}
340+
</TooltipPopup>
341+
</Tooltip>
342+
))}
343+
</div>
344+
<p className="truncate text-xs text-muted-foreground">{host.target}</p>
345+
{error ? (
346+
<div className="mt-1" role="status">
347+
<details className="text-xs text-destructive">
348+
<summary>Connection failed</summary>
349+
<p className="mt-1 whitespace-pre-wrap break-words">{error}</p>
350+
</details>
351+
</div>
352+
) : null}
353+
</div>
354+
{progress ? (
355+
<span
356+
role="status"
357+
className="inline-flex items-center gap-1.5 text-xs text-muted-foreground"
358+
>
359+
<Spinner className="size-3" />
360+
{progress}
361+
</span>
362+
) : null}
363+
<Menu>
364+
<MenuTrigger
365+
render={
366+
<Button
367+
size="icon-sm"
368+
variant="ghost-muted"
369+
disabled={busy}
370+
aria-label={host.label + " options"}
371+
/>
372+
}
373+
>
374+
<MoreVertical />
375+
</MenuTrigger>
376+
<MenuPopup align="end">
377+
<MenuItem
378+
onClick={() => {
379+
onEdit(host);
380+
}}
381+
>
382+
Edit
383+
</MenuItem>
384+
<MenuItem variant="destructive" onClick={() => onRemove(host)}>
385+
Remove
386+
</MenuItem>
387+
</MenuPopup>
388+
</Menu>
389+
<Button
390+
size="sm"
391+
variant="outline"
392+
disabled={busy || progress !== null}
393+
onClick={() => void testConnection(host)}
394+
>
395+
Test connection
396+
</Button>
397+
</div>
398+
);
399+
})}
400+
</>
401+
);
402+
}

0 commit comments

Comments
 (0)