Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

easDeviceHostsSettings.logic.ts

54 lines1.9 KB

1import {
2 EasDeviceHostConfig,
3 type EasDeviceHostConfig as EasDeviceHostConfigType,
4} from "@t3tools/contracts";
5import * as Schema from "effect/Schema";
6import { msg, plural, part } from "gt-react";
7​
8import { m, type MessageResolver } from "../../i18n/messages";
9​
10export const parseEasDeviceHostDraft = Schema.decodeUnknownOption(EasDeviceHostConfig);
11​
12export const newEasDeviceHost = (
13 id: string,
14 resolve: MessageResolver = m,
15): EasDeviceHostConfigType => ({
16 kind: "eas",
17 id,
18 label: resolve(msg("Expo Simulators")),
19 accessToken: "",
20 maxSessionMinutes: 30,
21 idleMinutes: 10,
22 maxConcurrentSessions: 2,
23});
24​
25/** Adds, replaces, or removes one Expo host. */
26/**
27 * With `keepToken`, the environment keeps the token it holds: a saved token arrives redacted, and
28 * one environment's token says nothing about another's. One new to the host uses its own Expo login.
29 */
30export function updateEasDeviceHosts(
31 hosts: ReadonlyArray<EasDeviceHostConfigType>,
32 host: EasDeviceHostConfigType,
33 remove: boolean,
34 keepToken = false,
35): ReadonlyArray<EasDeviceHostConfigType> {
36 if (remove) return hosts.filter((candidate) => candidate.id !== host.id);
37 const existing = hosts.find((candidate) => candidate.id === host.id);
38 const next = keepToken ? { ...host, accessToken: existing?.accessToken ?? "" } : host;
39 return existing
40 ? hosts.map((candidate) => (candidate.id === host.id ? next : candidate))
41 : [...hosts, next];
43​
44export function easSessionLimits(host: EasDeviceHostConfigType, resolve: MessageResolver = m) {
45 return resolve(
46 msg`Up to ${plural(
47 { sessions: host.maxConcurrentSessions },
48 {
49 one: part`${{ sessions: host.maxConcurrentSessions }} session`,
50 other: part`${{ sessions: host.maxConcurrentSessions }} sessions`,
51 },
52 )} at once, each stopped after ${{ maxMinutes: host.maxSessionMinutes }} min or ${{ idleMinutes: host.idleMinutes }} idle min`,
53 );