1import type { DeviceServiceState, ThreadId } from "@t3tools/contracts";
2
3/** Host identity is part of the selection because Android serials repeat across hosts. */
4export function threadDevicePreviews(state: DeviceServiceState | null, threadId: ThreadId) {
5 return (state?.sessions ?? [])
6 .filter((session) => session.threadId === threadId)
7 .map((session) => {
8 const device = state?.devices.find(
9 (device) => device.hostId === session.hostId && device.id === session.deviceId,
10 );
11 const host = state?.hosts.find((host) => host.id === session.hostId);
12 return {
13 key: JSON.stringify([session.hostId, session.deviceId]),
14 session,
15 name: device?.name ?? (session.platform === "ios" ? "iOS Simulator" : "Android Emulator"),
16 description: [device?.version, host?.label].filter(Boolean).join(" · "),
17 };
18 });
19}
20
21export type ThreadDevicePreview = ReturnType<typeof threadDevicePreviews>[number];
22
23export function selectedThreadDevicePreview(
24 previews: ReadonlyArray<ThreadDevicePreview>,
25 selectedKey: string | null,
26) {
27 return previews.find((preview) => preview.key === selectedKey) ?? previews[0] ?? null;
28}