Skip to content

Commit 5ed0cf6

Browse files
refactor(mobile): attachment, connection and project flows settle promises instead of chaining finally
The video preview loads the latest attachment through an effect event instead of a layout-effect ref sync. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 98b7191 commit 5ed0cf6

9 files changed

Lines changed: 152 additions & 145 deletions

File tree

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2+
import { AsyncResult } from "effect/reactivity";
13
import * as Dialog from "./ui/Dialog";
24
import type { ComposerContextRecord, EnvironmentId } from "@t3tools/contracts";
35
import { Pressable, View } from "react-native";
@@ -61,32 +63,34 @@ export function ComposerContextAttachment(props: {
6163
if (sharing) return;
6264
setSharing(true);
6365
const controller = new AbortController();
64-
await (async () => {
66+
const result = await settlePromise(async () => {
6567
if (local) {
6668
const preview = await loadLocalAttachmentPreview(local, controller.signal);
67-
await preview?.share(controller.signal, shareSourceIdentifier).finally(() => {
68-
preview.dispose();
69-
});
70-
} else {
71-
const url = await refresh();
72-
if (!url) throw new Error("Reconnect to the environment and try again.");
73-
await downloadAndShareAttachment({
74-
url,
75-
attachment: record,
76-
signal: controller.signal,
77-
sourceIdentifier: shareSourceIdentifier,
78-
});
79-
}
80-
})()
81-
.catch((cause: unknown) => {
82-
Dialog.alert(
83-
"Could not open attachment",
84-
cause instanceof Error ? cause.message : "Try again.",
69+
if (!preview) return;
70+
const shared = await settlePromise(() =>
71+
preview.share(controller.signal, shareSourceIdentifier),
8572
);
86-
})
87-
.finally(() => {
88-
setSharing(false);
73+
preview.dispose();
74+
if (AsyncResult.isFailure(shared)) throw squashAtomCommandFailure(shared);
75+
return;
76+
}
77+
const url = await refresh();
78+
if (!url) throw new Error("Reconnect to the environment and try again.");
79+
await downloadAndShareAttachment({
80+
url,
81+
attachment: record,
82+
signal: controller.signal,
83+
sourceIdentifier: shareSourceIdentifier,
8984
});
85+
});
86+
setSharing(false);
87+
if (AsyncResult.isFailure(result)) {
88+
const cause = squashAtomCommandFailure(result);
89+
Dialog.alert(
90+
"Could not open attachment",
91+
cause instanceof Error ? cause.message : "Try again.",
92+
);
93+
}
9094
};
9195
return (
9296
<View className="gap-3">

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as Dialog from "./ui/Dialog";
22
import { ComposerContextId } from "@t3tools/contracts";
33
import { useAtomValue } from "@effect/atom-react";
44
import { AsyncResult } from "effect/reactivity";
5+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
56
import { useNavigation } from "@react-navigation/native";
67
import { useEffect, useRef, useState } from "react";
78
import type { EnvironmentId } from "@t3tools/contracts";
@@ -87,7 +88,7 @@ export function ComposerEditor({
8788
importRef.current = controller;
8889
setImporting(true);
8990
setComposerContextImporting(draftKey, true);
90-
return await (async () => {
91+
const settled = await settlePromise(async () => {
9192
const retained = getComposerDraftAfterSelection(draftKey, insertion);
9293
const result = await importComposerContextClipboard(
9394
clipboard,
@@ -111,19 +112,17 @@ export function ComposerEditor({
111112
"Some attachments could not be copied",
112113
"Reconnect to the source environment and copy them again. References without their files are marked unavailable.",
113114
);
114-
})()
115-
.catch((error: unknown) => {
116-
if (!controller.signal.aborted)
117-
Dialog.alert(
118-
"Could not paste context",
119-
error instanceof Error ? error.message : "Try copying again.",
120-
);
121-
})
122-
.finally(() => {
123-
setComposerContextImporting(draftKey, false);
124-
importRef.current = null;
125-
setImporting(false);
126-
});
115+
});
116+
setComposerContextImporting(draftKey, false);
117+
importRef.current = null;
118+
setImporting(false);
119+
if (AsyncResult.isFailure(settled) && !controller.signal.aborted) {
120+
const error = squashAtomCommandFailure(settled);
121+
Dialog.alert(
122+
"Could not paste context",
123+
error instanceof Error ? error.message : "Try copying again.",
124+
);
125+
}
127126
};
128127
const clipboardFragment =
129128
environmentId && draft.context

‎apps/mobile/src/components/VideoPreviewModal.ios.tsx‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as Dialog from "./ui/Dialog";
22
import { useIsFocused } from "@react-navigation/native";
33
import { requireNativeModule } from "expo";
4+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5+
import { AsyncResult } from "effect/reactivity";
46
import { useEffect, useEffectEvent, useId, useState } from "react";
57
import { Keyboard } from "react-native";
68

@@ -76,20 +78,23 @@ function NativeVideoPreview(props: {
7678
? await loadLocalAttachmentPreview(localAttachment, controller.signal)
7779
: null;
7880
if (localAttachment !== null && !file) return;
79-
return await (async () => {
80-
if (controller.signal.aborted) return;
81-
ready = true;
82-
await NativeControls.presentVideo(
81+
if (controller.signal.aborted) {
82+
file?.dispose();
83+
return;
84+
}
85+
ready = true;
86+
const presented = await settlePromise(() =>
87+
NativeControls.presentVideo(
8388
file?.uri ?? playbackUrl!,
8489
name,
8590
source.sourceIdentifier ?? "",
8691
identifier,
87-
);
88-
if (!controller.signal.aborted) onRequestClose();
89-
})().finally(() => {
90-
// Native completion follows dismissal, so local playback keeps its file lease.
91-
file?.dispose();
92-
});
92+
),
93+
);
94+
// Native completion follows dismissal, so local playback keeps its file lease.
95+
file?.dispose();
96+
if (AsyncResult.isFailure(presented)) throw squashAtomCommandFailure(presented);
97+
if (!controller.signal.aborted) onRequestClose();
9398
})().catch((error: unknown) => {
9499
if (controller.signal.aborted) return;
95100
// AVKit gives no retry, so re-mint now; the cached URL may simply have expired.

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useIsFocused } from "@react-navigation/native";
22
import { videoMimeType } from "@t3tools/shared/video";
3-
import { useEffect, useLayoutEffect, useRef, useState } from "react";
3+
import { useEffect, useEffectEvent, useState } from "react";
44
import { Keyboard, Modal, Pressable, View } from "react-native";
55
import { useSafeAreaInsets } from "react-native-safe-area-context";
66

@@ -60,16 +60,15 @@ function useLocalPlayback(source: LocalVideoPreviewSource): PlaybackState {
6060
const [error, setError] = useState<string | null>(null);
6161
// Only a different file needs a new lease; a metadata update on the same
6262
// draft must not dispose the file Android is still playing.
63-
const attachmentRef = useRef(attachment);
64-
useLayoutEffect(() => {
65-
attachmentRef.current = attachment;
66-
});
63+
const loadAttachment = useEffectEvent((signal: AbortSignal) =>
64+
loadLocalAttachmentPreview(attachment, signal),
65+
);
6766
const { id: attachmentId, fileUri } = attachment;
6867
useEffect(() => {
6968
setUri(null);
7069
setError(null);
7170
const controller = new AbortController();
72-
const loading = loadLocalAttachmentPreview(attachmentRef.current, controller.signal);
71+
const loading = loadAttachment(controller.signal);
7372
void loading.then(
7473
(file) => {
7574
if (file === null) return;

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,22 @@ export function ConnectionsNewRouteScreen({
154154
const connectAndClose = async (pairingUrl: string) => {
155155
setIsSubmitting(true);
156156
onChangeConnectionPairingUrl(pairingUrl);
157-
await (async () => {
158-
const result = await onConnectPress(pairingUrl, params.routeFor);
159-
void Haptics.notificationAsync(
160-
AsyncResult.isSuccess(result)
161-
? Haptics.NotificationFeedbackType.Success
162-
: Haptics.NotificationFeedbackType.Error,
163-
);
164-
if (AsyncResult.isSuccess(result)) {
165-
// Return to whatever opened the sheet. Replacing it with Home would
166-
// nest Home inside any sheet still below it.
167-
if (!navigation.canGoBack()) {
168-
navigation.dispatch(StackActions.replace("Home"));
169-
} else {
170-
navigation.goBack();
171-
}
157+
const result = await onConnectPress(pairingUrl, params.routeFor);
158+
setIsSubmitting(false);
159+
void Haptics.notificationAsync(
160+
AsyncResult.isSuccess(result)
161+
? Haptics.NotificationFeedbackType.Success
162+
: Haptics.NotificationFeedbackType.Error,
163+
);
164+
if (AsyncResult.isSuccess(result)) {
165+
// Return to whatever opened the sheet. Replacing it with Home would
166+
// nest Home inside any sheet still below it.
167+
if (!navigation.canGoBack()) {
168+
navigation.dispatch(StackActions.replace("Home"));
169+
} else {
170+
navigation.goBack();
172171
}
173-
})().finally(() => {
174-
setIsSubmitting(false);
175-
});
172+
}
176173
};
177174

178175
const handleSubmit = async () => {

‎apps/mobile/src/features/projects/AddProjectScreen.tsx‎

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,58 +1041,57 @@ export function AddProjectNewScreen(props: { readonly environmentId?: string | s
10411041
submittingRef.current = true;
10421042
setError(null);
10431043
setIsSubmitting(true);
1044-
return await (async () => {
1045-
const result = await createNew({
1044+
const result = await createNew({
1045+
environmentId: environment.environmentId,
1046+
input: { name: trimmedName },
1047+
});
1048+
if (AsyncResult.isFailure(result)) {
1049+
submittingRef.current = false;
1050+
setIsSubmitting(false);
1051+
setError(errorMessage(Cause.squash(result.cause)));
1052+
return;
1053+
}
1054+
const { projectId, workspaceRoot, commitError } = result.value;
1055+
if (commitError !== undefined) {
1056+
Dialog.alert("Created without a first commit", commitError);
1057+
}
1058+
if (publishes && publishTarget !== null) {
1059+
void publishRepository({
10461060
environmentId: environment.environmentId,
1047-
input: { name: trimmedName },
1061+
input: {
1062+
cwd: workspaceRoot,
1063+
provider: publishTarget.definition.kind,
1064+
repository: getNewProjectRepository(publishTarget, workspaceRoot),
1065+
visibility: "private",
1066+
},
1067+
}).then((publishResult) => {
1068+
if (AsyncResult.isFailure(publishResult)) {
1069+
Dialog.alert(
1070+
`Could not create the ${publishTarget.definition.label} repository`,
1071+
errorMessage(Cause.squash(publishResult.cause)),
1072+
);
1073+
}
10481074
});
1049-
if (AsyncResult.isFailure(result)) {
1050-
setError(errorMessage(Cause.squash(result.cause)));
1051-
return;
1052-
}
1053-
const { projectId, workspaceRoot, commitError } = result.value;
1054-
if (commitError !== undefined) {
1055-
Dialog.alert("Created without a first commit", commitError);
1056-
}
1057-
if (publishes && publishTarget !== null) {
1058-
void publishRepository({
1059-
environmentId: environment.environmentId,
1060-
input: {
1061-
cwd: workspaceRoot,
1062-
provider: publishTarget.definition.kind,
1063-
repository: getNewProjectRepository(publishTarget, workspaceRoot),
1064-
visibility: "private",
1065-
},
1066-
}).then((publishResult) => {
1067-
if (AsyncResult.isFailure(publishResult)) {
1068-
Dialog.alert(
1069-
`Could not create the ${publishTarget.definition.label} repository`,
1070-
errorMessage(Cause.squash(publishResult.cause)),
1071-
);
1072-
}
1073-
});
1074-
}
1075-
// The draft screen resolves its project from the client store, so it
1076-
// must not open before the create event has arrived.
1077-
const project = await waitForProject(
1078-
{ environmentId: environment.environmentId, projectId },
1079-
15_000,
1075+
}
1076+
// The draft screen resolves its project from the client store, so it
1077+
// must not open before the create event has arrived.
1078+
const project = await waitForProject(
1079+
{ environmentId: environment.environmentId, projectId },
1080+
15_000,
1081+
);
1082+
submittingRef.current = false;
1083+
setIsSubmitting(false);
1084+
if (project === null) {
1085+
setName("");
1086+
setError(
1087+
"The project was created but has not reached this device yet. It will appear in the project list once the connection catches up.",
10801088
);
1081-
if (project === null) {
1082-
setName("");
1083-
setError(
1084-
"The project was created but has not reached this device yet. It will appear in the project list once the connection catches up.",
1085-
);
1086-
return;
1087-
}
1088-
openNewTaskDraft(navigation, {
1089-
environmentId: environment.environmentId,
1090-
projectId,
1091-
title: trimmedName,
1092-
});
1093-
})().finally(() => {
1094-
submittingRef.current = false;
1095-
setIsSubmitting(false);
1089+
return;
1090+
}
1091+
openNewTaskDraft(navigation, {
1092+
environmentId: environment.environmentId,
1093+
projectId,
1094+
title: trimmedName,
10961095
});
10971096
};
10981097

‎apps/mobile/src/lib/attachmentDocument.ts‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { AsyncResult } from "effect/reactivity";
2+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
13
import * as Dialog from "../components/ui/Dialog";
24
import { filePreviewDelimiter, parseDelimitedPreview } from "@t3tools/shared/delimitedPreview";
35
import type { EnvironmentId } from "@t3tools/contracts";
@@ -164,21 +166,20 @@ export function useAttachmentDocument(input: {
164166
attachment: { name: input.name, mimeType: input.mimeType },
165167
signal: controller.signal,
166168
};
167-
await (async () => {
169+
const result = await settlePromise(async () => {
168170
if (isLocalUri(uri)) await shareLocalAttachment({ ...request, uri });
169171
else await downloadAndShareAttachment({ ...request, url: (await refresh()) ?? uri });
170-
})()
171-
.catch((cause: unknown) => {
172-
if (controller.signal.aborted) return;
173-
Dialog.alert(
174-
"Could not share file",
175-
cause instanceof Error ? cause.message : "Please try again.",
176-
);
177-
})
178-
.finally(() => {
179-
if (shareController.current === controller) shareController.current = null;
180-
if (!controller.signal.aborted) setSharing(false);
181-
});
172+
});
173+
if (shareController.current === controller) shareController.current = null;
174+
if (controller.signal.aborted) return;
175+
setSharing(false);
176+
if (AsyncResult.isFailure(result)) {
177+
const cause = squashAtomCommandFailure(result);
178+
Dialog.alert(
179+
"Could not share file",
180+
cause instanceof Error ? cause.message : "Please try again.",
181+
);
182+
}
182183
};
183184
return {
184185
kind,

0 commit comments

Comments
 (0)