Skip to content

Commit 2997fa0

Browse files
author
T3 Code Test
committed
Merge remote-tracking branch 'upstream/main' into fix/mobile-send-error-banner
2 parents 237bb20 + 1e2ecbd commit 2997fa0

95 files changed

Lines changed: 5528 additions & 809 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/src/connection/onboarding.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,20 @@ const onboardingScheduler = createAtomCommandScheduler();
1313
export const connectPairingUrl = createRuntimeCommand(connectionAtomRuntime, {
1414
label: "mobile:connection:connect-pairing-url",
1515
scheduler: onboardingScheduler,
16-
concurrency: { mode: "singleFlight", key: (pairingUrl: string) => pairingUrl },
17-
execute: (pairingUrl: string) =>
16+
concurrency: {
17+
mode: "singleFlight",
18+
// Adding a route to a different machine with the same link is its own
19+
// operation: it must check its own expected machine.
20+
key: (input: { readonly pairingUrl: string; readonly expectedEnvironmentId?: EnvironmentId }) =>
21+
JSON.stringify([input.pairingUrl, input.expectedEnvironmentId ?? null]),
22+
},
23+
execute: (input: {
24+
readonly pairingUrl: string;
25+
/** Set when adding a route to this saved machine. */
26+
readonly expectedEnvironmentId?: EnvironmentId;
27+
}) =>
1828
ConnectionOnboarding.ConnectionOnboarding.pipe(
19-
Effect.flatMap((onboarding) => onboarding.registerPairing({ pairingUrl })),
29+
Effect.flatMap((onboarding) => onboarding.registerPairing(input)),
2030
),
2131
});
2232

‎apps/mobile/src/connection/platform.ts‎

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,75 @@ const connectivityLayer = Connectivity.layer({
8383
),
8484
});
8585

86+
/**
87+
* Wakes connections when the device moves between networks while staying
88+
* online, such as Wi-Fi to cellular. Connectivity only reports online or
89+
* offline, so leaving home on cellular would otherwise go unnoticed until the
90+
* LAN socket times out.
91+
*/
92+
const networkPathChanges = Stream.callback<"network-changed">((queue) =>
93+
Effect.acquireRelease(
94+
Effect.sync(() => {
95+
let active = true;
96+
let previous: Network.NetworkStateType | undefined;
97+
const record = (state: Network.NetworkState) => {
98+
const type = state.isConnected === true ? state.type : undefined;
99+
if (previous !== undefined && type !== undefined && type !== previous) {
100+
Queue.offerUnsafe(queue, "network-changed");
101+
}
102+
previous = type ?? previous;
103+
};
104+
// The listener reports changes only, so seed the current type; without
105+
// it the first Wi-Fi to cellular move would go unnoticed.
106+
void Network.getNetworkStateAsync()
107+
.then((state) => {
108+
if (active && previous === undefined && state.isConnected === true) {
109+
previous = state.type;
110+
}
111+
})
112+
.catch(() => undefined);
113+
const subscription = Network.addNetworkStateListener(record);
114+
return {
115+
remove: () => {
116+
active = false;
117+
subscription.remove();
118+
},
119+
};
120+
}),
121+
(subscription) => Effect.sync(() => subscription.remove()),
122+
).pipe(Effect.asVoid),
123+
);
124+
86125
const wakeupsLayer = Wakeups.layer({
87-
changes: Stream.merge(
88-
Stream.callback<"application-active-probe" | "application-active-reconnect">((queue) =>
89-
Effect.acquireRelease(
90-
Effect.sync(() => {
91-
let backgroundedAtMs = AppState.currentState === "background" ? Date.now() : null;
92-
return AppState.addEventListener("change", (state) => {
93-
if (state === "background") {
94-
backgroundedAtMs = Date.now();
95-
return;
96-
}
97-
if (state === "active") {
98-
Queue.offerUnsafe(queue, mobileApplicationActiveWakeup(backgroundedAtMs, Date.now()));
99-
backgroundedAtMs = null;
100-
}
101-
});
102-
}),
103-
(subscription) => Effect.sync(() => subscription.remove()),
104-
).pipe(Effect.asVoid),
105-
),
106-
managedRelayAccountChanges(appAtomRegistry).pipe(
107-
Stream.map(() => "credentials-changed" as const),
108-
),
126+
changes: Stream.mergeAll(
127+
[
128+
Stream.callback<"application-active-probe" | "application-active-reconnect">((queue) =>
129+
Effect.acquireRelease(
130+
Effect.sync(() => {
131+
let backgroundedAtMs = AppState.currentState === "background" ? Date.now() : null;
132+
return AppState.addEventListener("change", (state) => {
133+
if (state === "background") {
134+
backgroundedAtMs = Date.now();
135+
return;
136+
}
137+
if (state === "active") {
138+
Queue.offerUnsafe(
139+
queue,
140+
mobileApplicationActiveWakeup(backgroundedAtMs, Date.now()),
141+
);
142+
backgroundedAtMs = null;
143+
}
144+
});
145+
}),
146+
(subscription) => Effect.sync(() => subscription.remove()),
147+
).pipe(Effect.asVoid),
148+
),
149+
managedRelayAccountChanges(appAtomRegistry).pipe(
150+
Stream.map(() => "credentials-changed" as const),
151+
),
152+
networkPathChanges,
153+
],
154+
{ concurrency: "unbounded" },
109155
),
110156
});
111157

‎apps/mobile/src/connection/storage.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
registerConnectionInCatalog,
44
removeConnectionFromCatalog,
55
setConnectionEnabledInCatalog,
6+
setRoutesInCatalog,
67
removeCatalogValue,
78
replaceCatalogValue,
89
Persistence,
@@ -27,6 +28,7 @@ function targetPersistenceError(
2728
| "list-targets"
2829
| "list-disabled-targets"
2930
| "register-connection"
31+
| "set-connection-routes"
3032
| "remove-connection"
3133
| "set-connection-enabled",
3234
error: ConnectionTransientError,
@@ -57,13 +59,17 @@ export const connectionStorageLayer = Layer.effectContext(
5759
),
5860
});
5961
const registrationStore = Persistence.ConnectionRegistrationStore.of({
60-
register: (registration) =>
62+
register: (registration, routes) =>
6163
catalog
62-
.update((document) => registerConnectionInCatalog(document, registration))
64+
.update((document) => registerConnectionInCatalog(document, registration, routes))
6365
.pipe(Effect.mapError((error) => targetPersistenceError("register-connection", error))),
64-
remove: (target) =>
66+
setRoutes: (environmentId, routes) =>
6567
catalog
66-
.update((document) => removeConnectionFromCatalog(document, target))
68+
.update((document) => setRoutesInCatalog(document, environmentId, routes))
69+
.pipe(Effect.mapError((error) => targetPersistenceError("set-connection-routes", error))),
70+
remove: (environmentId) =>
71+
catalog
72+
.update((document) => removeConnectionFromCatalog(document, environmentId))
6773
.pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))),
6874
setEnabled: (environmentId, enabled) =>
6975
catalog

‎apps/mobile/src/features/cloud/cloud-drafts.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
1+
import { EnvironmentRegistry, removedWithRelay } from "@t3tools/client-runtime/connection";
22
import { createRuntimeCommand } from "@t3tools/client-runtime/state/runtime";
33
import * as Effect from "effect/Effect";
44
import * as Schema from "effect/Schema";
@@ -27,7 +27,8 @@ export const removeCloudEnvironments = createRuntimeCommand(connectionAtomRuntim
2727
const entries = yield* SubscriptionRef.get(registry.entries);
2828
const environmentIds = new Set(
2929
[...entries.values()]
30-
.filter((entry) => entry.target._tag === "RelayConnectionTarget")
30+
// Only environments that sign-out removes entirely lose their drafts.
31+
.filter(removedWithRelay)
3132
.map((entry) => entry.target.environmentId),
3233
);
3334
// Credentials are already revoked. A failed backup must leave the local

‎apps/mobile/src/features/connection/ConnectionsNewRouteScreen.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
useRoute,
77
type StaticScreenProps,
88
} from "@react-navigation/native";
9+
import type { EnvironmentId } from "@t3tools/contracts";
910
import { AsyncResult } from "effect/unstable/reactivity";
1011
import { useCallback, useEffect, useRef, useState } from "react";
1112
import { Alert, Linking, Platform, View } from "react-native";
@@ -23,6 +24,8 @@ type ConnectionsNewRouteParams = {
2324
readonly mode?: string;
2425
readonly pairingUrl?: string;
2526
readonly autoConnect?: string;
27+
/** Adds a route to this saved machine instead of a new environment. */
28+
readonly routeFor?: EnvironmentId;
2629
};
2730

2831
export function ConnectionsNewRouteScreen({
@@ -158,7 +161,7 @@ export function ConnectionsNewRouteScreen({
158161
setIsSubmitting(true);
159162
onChangeConnectionPairingUrl(pairingUrl);
160163
try {
161-
const result = await onConnectPress(pairingUrl);
164+
const result = await onConnectPress(pairingUrl, params.routeFor);
162165
if (AsyncResult.isSuccess(result)) {
163166
if (replaceWithHome || !navigation.canGoBack()) {
164167
navigation.dispatch(StackActions.replace("Home"));
@@ -170,7 +173,7 @@ export function ConnectionsNewRouteScreen({
170173
setIsSubmitting(false);
171174
}
172175
},
173-
[navigation, onChangeConnectionPairingUrl, onConnectPress],
176+
[navigation, onChangeConnectionPairingUrl, onConnectPress, params.routeFor],
174177
);
175178

176179
const handleSubmit = useCallback(async () => {

‎apps/mobile/src/features/connection/environmentSections.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,21 @@ describe("mobile environment settings sections", () => {
157157
expect(sections.connectedCloudEnvironments).toEqual([cloud]);
158158
expect(sections.availableCloudEnvironments).toEqual([]);
159159
});
160+
161+
it("offers T3 Connect for a machine saved only over the LAN, as an added route", () => {
162+
const local = connectedEnvironment({
163+
environmentId: "environment-desk",
164+
isRelayManaged: false,
165+
});
166+
167+
const sections = splitEnvironmentSections({
168+
connectedEnvironments: [local],
169+
cloudEnvironments: [cloudEnvironment("environment-desk")],
170+
});
171+
172+
expect(sections.localEnvironments).toEqual([local]);
173+
expect(
174+
sections.availableCloudEnvironments.map((environment) => environment.environmentId),
175+
).toEqual([EnvironmentId.make("environment-desk")]);
176+
});
160177
});

‎apps/mobile/src/features/connection/useConnectionController.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export function useConnectionController() {
6969
);
7070

7171
const connectPairingUrl = useCallback(
72-
(pairingUrl: string) => connectPairingUrlMutation(pairingUrl),
72+
(pairingUrl: string, expectedEnvironmentId?: EnvironmentId) =>
73+
connectPairingUrlMutation({
74+
pairingUrl,
75+
...(expectedEnvironmentId === undefined ? {} : { expectedEnvironmentId }),
76+
}),
7377
[connectPairingUrlMutation],
7478
);
7579
const connectRelayEnvironment = useCallback(

0 commit comments

Comments
 (0)