Skip to content

Commit 68fb872

Browse files
committed
fix(mobile): iOS trash now removes a persisted environment
Settings → Environments is a nested form sheet, so Alert.alert never presented the remove confirm. Always show an in-tree confirm, surface Keychain persist failures, and keep the catalog row if the write fails. Co-authored-by: maco <[email protected]>
1 parent 7445aa7 commit 68fb872

6 files changed

Lines changed: 301 additions & 82 deletions

File tree

‎apps/mobile/src/components/ConfirmDialogHost.tsx‎

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useCallback, useEffect, useState } from "react";
22
import { Platform, Modal, Pressable, TextInput, View } from "react-native";
3+
import { FullWindowOverlay } from "react-native-screens";
34

45
import { cn } from "../lib/cn";
56
import { AppText } from "./AppText";
@@ -31,10 +32,11 @@ type DialogRequest =
3132
let presentRequest: ((request: DialogRequest) => void) | null = null;
3233

3334
/**
34-
* Imperative confirm dialog, Alert.alert-shaped. Native iOS alerts already
35-
* match the app (and support per-button destructive red), so this is for
36-
* Android, where the native dialog can only theme all confirm buttons at
37-
* once. Requires ConfirmDialogHost to be mounted at the app root.
35+
* Imperative confirm dialog, Alert.alert-shaped. Android uses a themed
36+
* in-tree dialog because the native one cannot color a single destructive
37+
* button. iOS uses a FullWindowOverlay so the confirm stays visible above
38+
* nested form sheets — UIAlertController from the key window does not.
39+
* Requires ConfirmDialogHost to be mounted at the app root.
3840
*/
3941
export function showConfirmDialog(request: ConfirmDialogRequest): void {
4042
presentRequest?.({ kind: "confirm", request });
@@ -97,6 +99,69 @@ export function ConfirmDialogHost() {
9799
/>
98100
) : null;
99101

102+
const dialog =
103+
presented === null ? null : (
104+
<View className="flex-1 items-center justify-center bg-backdrop px-8">
105+
<View className="w-full rounded-[24px] bg-card px-6 pb-4 pt-5">
106+
<AppText className="text-lg font-t3-medium">{presented.request.title}</AppText>
107+
{presented.kind === "confirm" && presented.request.message !== undefined ? (
108+
<AppText className="mt-2 text-sm text-foreground-secondary">
109+
{presented.request.message}
110+
</AppText>
111+
) : null}
112+
{presented.kind === "text-input" ? (
113+
<TextInput
114+
accessibilityLabel={presented.request.title}
115+
autoFocus
116+
className="mt-4 rounded-xl border border-border bg-screen px-3 py-2.5 text-base text-foreground"
117+
onChangeText={setInputValue}
118+
onSubmitEditing={confirmDisabled ? undefined : () => handleConfirm()}
119+
returnKeyType="done"
120+
selectTextOnFocus
121+
value={inputValue}
122+
/>
123+
) : null}
124+
<View className="mt-5 flex-row justify-end gap-1">
125+
<View className="overflow-hidden rounded-full">
126+
<Pressable
127+
accessibilityRole="button"
128+
className="min-h-10 items-center justify-center px-4 active:bg-subtle"
129+
onPress={handleCancel}
130+
>
131+
<AppText className="text-base font-t3-medium">
132+
{presented.request.cancelText ?? "Cancel"}
133+
</AppText>
134+
</Pressable>
135+
</View>
136+
<View className="overflow-hidden rounded-full">
137+
<Pressable
138+
accessibilityRole="button"
139+
disabled={confirmDisabled}
140+
className="min-h-10 items-center justify-center px-4 active:bg-subtle"
141+
onPress={() => handleConfirm()}
142+
>
143+
<AppText
144+
className={cn(
145+
"text-base font-t3-medium",
146+
presented.kind === "confirm" &&
147+
presented.request.destructive &&
148+
"text-danger-foreground",
149+
confirmDisabled && "text-foreground-tertiary",
150+
)}
151+
>
152+
{presented.request.confirmText}
153+
</AppText>
154+
</Pressable>
155+
</View>
156+
</View>
157+
</View>
158+
</View>
159+
);
160+
161+
if (Platform.OS === "ios") {
162+
return dialog === null ? null : <FullWindowOverlay>{dialog}</FullWindowOverlay>;
163+
}
164+
100165
return (
101166
<Modal
102167
visible={presented !== null}
@@ -106,63 +171,7 @@ export function ConfirmDialogHost() {
106171
navigationBarTranslucent
107172
onRequestClose={handleCancel}
108173
>
109-
{presented === null ? null : (
110-
<View className="flex-1 items-center justify-center bg-backdrop px-8">
111-
<View className="w-full rounded-[24px] bg-card px-6 pb-4 pt-5">
112-
<AppText className="text-lg font-t3-medium">{presented.request.title}</AppText>
113-
{presented.kind === "confirm" && presented.request.message !== undefined ? (
114-
<AppText className="mt-2 text-sm text-foreground-secondary">
115-
{presented.request.message}
116-
</AppText>
117-
) : null}
118-
{presented.kind === "text-input" ? (
119-
<TextInput
120-
accessibilityLabel={presented.request.title}
121-
autoFocus
122-
className="mt-4 rounded-xl border border-border bg-screen px-3 py-2.5 text-base text-foreground"
123-
onChangeText={setInputValue}
124-
onSubmitEditing={confirmDisabled ? undefined : () => handleConfirm()}
125-
returnKeyType="done"
126-
selectTextOnFocus
127-
value={inputValue}
128-
/>
129-
) : null}
130-
<View className="mt-5 flex-row justify-end gap-1">
131-
<View className="overflow-hidden rounded-full">
132-
<Pressable
133-
accessibilityRole="button"
134-
className="min-h-10 items-center justify-center px-4 active:bg-subtle"
135-
onPress={handleCancel}
136-
>
137-
<AppText className="text-base font-t3-medium">
138-
{presented.request.cancelText ?? "Cancel"}
139-
</AppText>
140-
</Pressable>
141-
</View>
142-
<View className="overflow-hidden rounded-full">
143-
<Pressable
144-
accessibilityRole="button"
145-
disabled={confirmDisabled}
146-
className="min-h-10 items-center justify-center px-4 active:bg-subtle"
147-
onPress={() => handleConfirm()}
148-
>
149-
<AppText
150-
className={cn(
151-
"text-base font-t3-medium",
152-
presented.kind === "confirm" &&
153-
presented.request.destructive &&
154-
"text-danger-foreground",
155-
confirmDisabled && "text-foreground-tertiary",
156-
)}
157-
>
158-
{presented.request.confirmText}
159-
</AppText>
160-
</Pressable>
161-
</View>
162-
</View>
163-
</View>
164-
</View>
165-
)}
174+
{dialog}
166175
</Modal>
167176
);
168177
}

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { BearerConnectionTarget } from "@t3tools/client-runtime/connection";
2+
import { EnvironmentId } from "@t3tools/contracts";
13
import { describe, expect, it } from "@effect/vitest";
24
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
36
import { vi } from "vite-plus/test";
47

58
vi.mock("react-native", () => ({
@@ -13,7 +16,10 @@ vi.mock("expo-secure-store", () => ({
1316
}));
1417

1518
import { CONNECTION_CATALOG_KEY, LEGACY_CONNECTIONS_KEY, make } from "./catalog-store";
16-
import { MobileSecureStorage } from "../persistence/mobile-secure-storage";
19+
import {
20+
MobileSecureStorage,
21+
MobileSecureStorageError,
22+
} from "../persistence/mobile-secure-storage";
1723

1824
function makeStorage(initial: Readonly<Record<string, string>>) {
1925
const values = new Map(Object.entries(initial));
@@ -94,4 +100,52 @@ describe("mobile connection catalog storage", () => {
94100
expect(memory.values.has(LEGACY_CONNECTIONS_KEY)).toBe(false);
95101
}),
96102
);
103+
104+
it.effect("keeps a saved environment when a catalog write fails", () =>
105+
Effect.gen(function* () {
106+
const values = new Map<string, string>();
107+
let failWrite = false;
108+
const storage = MobileSecureStorage.of({
109+
getItem: (key) => Effect.sync(() => values.get(key) ?? null),
110+
setItem: (key, value) =>
111+
Effect.gen(function* () {
112+
if (failWrite) {
113+
return yield* new MobileSecureStorageError({
114+
operation: "write",
115+
key,
116+
cause: new Error("keychain write failed"),
117+
});
118+
}
119+
values.set(key, value);
120+
}),
121+
removeItem: (key) =>
122+
Effect.sync(() => {
123+
values.delete(key);
124+
}),
125+
});
126+
const catalog = yield* make().pipe(Effect.provideService(MobileSecureStorage, storage));
127+
const environmentId = EnvironmentId.make("offline-direct");
128+
129+
yield* catalog.update((document) => ({
130+
...document,
131+
targets: [
132+
new BearerConnectionTarget({
133+
environmentId,
134+
label: "Stale Mac",
135+
connectionId: "bearer-offline-direct",
136+
}),
137+
],
138+
}));
139+
expect((yield* catalog.read).targets).toHaveLength(1);
140+
141+
failWrite = true;
142+
const removed = yield* catalog
143+
.update((document) => ({ ...document, targets: [] }))
144+
.pipe(Effect.exit);
145+
146+
expect(Exit.isFailure(removed)).toBe(true);
147+
expect((yield* catalog.read).targets).toHaveLength(1);
148+
expect((yield* catalog.read).targets[0]?.environmentId).toBe(environmentId);
149+
}),
150+
);
97151
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ export function ConnectionEnvironmentRow(props: {
235235
</Pressable>
236236

237237
<Pressable
238+
accessibilityLabel="Remove environment"
239+
accessibilityRole="button"
238240
className="h-[42px] w-[42px] items-center justify-center rounded-[14px] border border-danger-border bg-danger active:opacity-70"
239241
onPress={() => props.onRemove(props.environment.environmentId)}
240242
>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { EnvironmentId } from "@t3tools/contracts";
2+
import * as Cause from "effect/Cause";
3+
import { AsyncResult } from "effect/unstable/reactivity";
4+
import { describe, expect, it, vi } from "vite-plus/test";
5+
6+
import {
7+
presentRemoveSavedEnvironment,
8+
REMOVE_ENVIRONMENT_CONFIRM_TITLE,
9+
REMOVE_ENVIRONMENT_FAILURE_TITLE,
10+
} from "./remove-environment-confirm";
11+
12+
const OFFLINE_DIRECT_ID = EnvironmentId.make("offline-direct");
13+
14+
describe("presentRemoveSavedEnvironment", () => {
15+
it("presents a confirm for an offline direct environment even without a list label", () => {
16+
const presentConfirm = vi.fn();
17+
const remove = vi.fn();
18+
19+
presentRemoveSavedEnvironment({
20+
environmentId: OFFLINE_DIRECT_ID,
21+
remove,
22+
presentConfirm,
23+
presentError: vi.fn(),
24+
});
25+
26+
expect(remove).not.toHaveBeenCalled();
27+
expect(presentConfirm).toHaveBeenCalledOnce();
28+
expect(presentConfirm.mock.calls[0]?.[0]).toMatchObject({
29+
title: REMOVE_ENVIRONMENT_CONFIRM_TITLE,
30+
confirmText: "Remove",
31+
destructive: true,
32+
message: expect.stringContaining(OFFLINE_DIRECT_ID),
33+
});
34+
});
35+
36+
it("removes the catalog target after confirm", async () => {
37+
const presentConfirm = vi.fn();
38+
const presentError = vi.fn();
39+
const remove = vi.fn(async () => AsyncResult.success(undefined));
40+
41+
presentRemoveSavedEnvironment({
42+
environmentId: OFFLINE_DIRECT_ID,
43+
environmentLabel: "Stale Mac",
44+
remove,
45+
presentConfirm,
46+
presentError,
47+
});
48+
49+
await presentConfirm.mock.calls[0]?.[0].onConfirm();
50+
51+
expect(remove).toHaveBeenCalledWith(OFFLINE_DIRECT_ID);
52+
expect(presentError).not.toHaveBeenCalled();
53+
});
54+
55+
it("surfaces a persist failure and does not treat the row as removed", async () => {
56+
const presentConfirm = vi.fn();
57+
const presentError = vi.fn();
58+
const remove = vi.fn(async () =>
59+
AsyncResult.failure(
60+
Cause.fail(new Error("Could not save the local connection catalog: keychain write failed")),
61+
),
62+
);
63+
64+
presentRemoveSavedEnvironment({
65+
environmentId: OFFLINE_DIRECT_ID,
66+
environmentLabel: "Stale Mac",
67+
remove,
68+
presentConfirm,
69+
presentError,
70+
});
71+
72+
await presentConfirm.mock.calls[0]?.[0].onConfirm();
73+
74+
expect(remove).toHaveBeenCalledWith(OFFLINE_DIRECT_ID);
75+
expect(presentError).toHaveBeenCalledWith(
76+
REMOVE_ENVIRONMENT_FAILURE_TITLE,
77+
"Could not save the local connection catalog: keychain write failed",
78+
);
79+
});
80+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {
2+
isAtomCommandInterrupted,
3+
squashAtomCommandFailure,
4+
type AtomCommandResult,
5+
} from "@t3tools/client-runtime/state/runtime";
6+
import type { EnvironmentId } from "@t3tools/contracts";
7+
import { AsyncResult } from "effect/unstable/reactivity";
8+
9+
export const REMOVE_ENVIRONMENT_CONFIRM_TITLE = "Remove from this device?";
10+
export const REMOVE_ENVIRONMENT_FAILURE_TITLE = "Could not remove environment";
11+
12+
export type RemoveEnvironmentConfirmRequest = {
13+
readonly title: string;
14+
readonly message: string;
15+
readonly confirmText: string;
16+
readonly destructive?: boolean;
17+
readonly onConfirm: () => void;
18+
};
19+
20+
function resolveRemoveEnvironmentLabel(
21+
environmentLabel: string | undefined,
22+
environmentId: EnvironmentId,
23+
): string {
24+
const trimmed = environmentLabel?.trim();
25+
return trimmed === undefined || trimmed.length === 0 ? environmentId : trimmed;
26+
}
27+
28+
function removeEnvironmentConfirmMessage(label: string): string {
29+
return `Forget ${label} and its cached threads on this device. Switch it off instead to keep it saved.`;
30+
}
31+
32+
function removeEnvironmentFailureMessage(cause: unknown): string {
33+
return cause instanceof Error
34+
? cause.message
35+
: "The environment could not be removed from this device.";
36+
}
37+
38+
/**
39+
* Trash on Settings → Environments lives in a nested iOS form sheet, where
40+
* `Alert.alert` never becomes visible. Always present an in-tree confirm,
41+
* even when the row is missing from the connected list, then surface a
42+
* persist failure instead of leaving a Keychain-backed row that looks gone.
43+
*/
44+
export function presentRemoveSavedEnvironment(input: {
45+
readonly environmentId: EnvironmentId;
46+
readonly environmentLabel?: string;
47+
readonly remove: (environmentId: EnvironmentId) => Promise<AtomCommandResult<unknown, unknown>>;
48+
readonly presentConfirm: (request: RemoveEnvironmentConfirmRequest) => void;
49+
readonly presentError: (title: string, message: string) => void;
50+
}): void {
51+
const label = resolveRemoveEnvironmentLabel(input.environmentLabel, input.environmentId);
52+
input.presentConfirm({
53+
title: REMOVE_ENVIRONMENT_CONFIRM_TITLE,
54+
message: removeEnvironmentConfirmMessage(label),
55+
confirmText: "Remove",
56+
destructive: true,
57+
onConfirm: () => {
58+
void removeSavedEnvironment(input.remove, input.environmentId, input.presentError);
59+
},
60+
});
61+
}
62+
63+
async function removeSavedEnvironment(
64+
remove: (environmentId: EnvironmentId) => Promise<AtomCommandResult<unknown, unknown>>,
65+
environmentId: EnvironmentId,
66+
presentError: (title: string, message: string) => void,
67+
): Promise<void> {
68+
const result = await remove(environmentId);
69+
if (AsyncResult.isSuccess(result) || isAtomCommandInterrupted(result)) {
70+
return;
71+
}
72+
presentError(
73+
REMOVE_ENVIRONMENT_FAILURE_TITLE,
74+
removeEnvironmentFailureMessage(squashAtomCommandFailure(result)),
75+
);
76+
}

0 commit comments

Comments
 (0)