Skip to content

Commit 3b6ac34

Browse files
refactor(mobile): pull request actions branch on command results instead of throw-and-catch chains
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9ae327d commit 3b6ac34

9 files changed

Lines changed: 157 additions & 165 deletions

‎apps/mobile/src/features/pull-requests/PullRequestDiscussionScreen.tsx‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { Toolbar } from "../../components/ui/Toolbar";
22
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
33
import { useAtomRefresh } from "@effect/atom-react";
44
import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
5-
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5+
import {
6+
type AtomCommandResult,
7+
squashAtomCommandFailure,
8+
} from "@t3tools/client-runtime/state/runtime";
9+
import { AsyncResult } from "effect/reactivity";
610
import type { PullRequestThreadComment } from "@t3tools/contracts";
711
import { useRef, useState } from "react";
812
import { Platform, ScrollView, View } from "react-native";
@@ -59,59 +63,58 @@ function Discussion({ params }: { params: PullRequestParams & { readonly discuss
5963
const [error, setError] = useState<string | null>(null);
6064
const [notice, setNotice] = useState<string | null>(null);
6165
const [composerHeight, setComposerHeight] = useState(64);
62-
const operation = async (run: () => Promise<void>) => {
66+
const operation = async (run: () => Promise<AtomCommandResult<unknown, unknown> | undefined>) => {
6367
if (pending.current) return;
6468
pending.current = true;
6569
setBusy(true);
6670
setError(null);
6771
setNotice(null);
68-
await (async () => {
69-
await run();
70-
})()
71-
.catch((cause: unknown) => {
72-
setError(
73-
cause instanceof Error ? cause.message : "The provider could not complete this action.",
74-
);
75-
})
76-
.finally(() => {
77-
pending.current = false;
78-
setBusy(false);
79-
});
72+
const result = await run();
73+
pending.current = false;
74+
setBusy(false);
75+
if (result && AsyncResult.isFailure(result)) {
76+
const cause = squashAtomCommandFailure(result);
77+
setError(
78+
cause instanceof Error ? cause.message : "The provider could not complete this action.",
79+
);
80+
}
8081
};
8182
const post = () =>
8283
operation(async () => {
83-
if (!permissions.canReply || !body.trim() || !thread) return;
84+
if (!permissions.canReply || !body.trim() || !thread) return undefined;
8485
const submitted = body;
8586
const response = await reply({
8687
...target,
8788
input: { ...target.input, threadId: thread.id, body: submitted },
8889
});
89-
if (response._tag === "Failure") throw squashAtomCommandFailure(response);
90+
if (AsyncResult.isFailure(response)) return response;
9091
setBody((current) => (current === submitted ? "" : current));
9192
setNotice("Reply posted.");
9293
refresh();
94+
return response;
9395
});
9496
const toggleResolved = () =>
9597
operation(async () => {
96-
if (!thread || !permissions.canResolve) return;
98+
if (!thread || !permissions.canResolve) return undefined;
9799
const response = await resolve({
98100
...target,
99101
input: { ...target.input, threadId: thread.id, resolved: !thread.isResolved },
100102
});
101-
if (response._tag === "Failure") throw squashAtomCommandFailure(response);
102-
refresh();
103+
if (AsyncResult.isSuccess(response)) refresh();
104+
return response;
103105
});
104106
const nextCursor = cursor === undefined ? thread?.nextCommentsCursor : cursor;
105107
const more = () =>
106108
operation(async () => {
107-
if (!thread || !nextCursor) return;
109+
if (!thread || !nextCursor) return undefined;
108110
const response = await loadMore({
109111
...target,
110112
input: { ...target.input, threadId: thread.id, cursor: nextCursor },
111113
});
112-
if (response._tag === "Failure") throw squashAtomCommandFailure(response);
114+
if (AsyncResult.isFailure(response)) return response;
113115
setExtra((comments) => [...comments, ...response.value.comments]);
114116
setCursor(response.value.nextCursor);
117+
return response;
115118
});
116119
const comments = [
117120
...new Map(

‎apps/mobile/src/features/pull-requests/PullRequestInlineCommentScreen.tsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { settlePromise } from "@t3tools/client-runtime/state/runtime";
2+
import { AsyncResult } from "effect/reactivity";
13
import * as Dialog from "../../components/ui/Dialog";
24
import { useFocusEffect, useNavigation, type StaticScreenProps } from "@react-navigation/native";
35
import { useRef, useState } from "react";
@@ -64,13 +66,11 @@ export function PullRequestInlineCommentScreen({ route }: StaticScreenProps<Inli
6466
},
6567
],
6668
}));
67-
await (async () => {
68-
await store.flush();
69-
navigation.goBack();
70-
})().finally(() => {
71-
pending.current = false;
72-
setBusy(false);
73-
});
69+
// flush reports write failures through the draft store's error field.
70+
const result = await settlePromise(() => store.flush());
71+
pending.current = false;
72+
setBusy(false);
73+
if (AsyncResult.isSuccess(result)) navigation.goBack();
7474
};
7575
return (
7676
<PullRequestSheetContent

‎apps/mobile/src/features/pull-requests/PullRequestMergeControls.tsx‎

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -98,50 +98,46 @@ export function PullRequestMergeControls(props: {
9898
return;
9999
pending.current = true;
100100
setBusy(true);
101-
await (async () => {
102-
const response = await runAction({
103-
...target,
104-
input: {
105-
...target.input,
106-
action,
107-
...(action === "merge" && method && stack.stack && scope
101+
const response = await runAction({
102+
...target,
103+
input: {
104+
...target.input,
105+
action,
106+
...(action === "merge" && method && stack.stack && scope
107+
? {
108+
stackNumber: stack.stack.number,
109+
expectedStackHeads: scope.heads,
110+
mergeMethod: method,
111+
}
112+
: action === "merge" && method
108113
? {
109-
stackNumber: stack.stack.number,
110-
expectedStackHeads: scope.heads,
111-
mergeMethod: method,
114+
resolveMergeMethod: (fresh) => {
115+
if (
116+
!canPerformPullRequestAction(fresh, "merge") ||
117+
!allowedMergeMethods(fresh).includes(method)
118+
)
119+
throw new Error("The pull request changed. Refresh before merging.");
120+
return method;
121+
},
112122
}
113-
: action === "merge" && method
114-
? {
115-
resolveMergeMethod: (fresh) => {
116-
if (
117-
!canPerformPullRequestAction(fresh, "merge") ||
118-
!allowedMergeMethods(fresh).includes(method)
119-
)
120-
throw new Error("The pull request changed. Refresh before merging.");
121-
return method;
122-
},
123-
}
124-
: action === "enable-auto-merge" && method
125-
? { mergeMethod: method }
126-
: {}),
127-
},
128-
});
129-
if (response._tag === "Failure") throw squashAtomCommandFailure(response);
130-
})()
131-
.catch((cause: unknown) => {
132-
Dialog.alert(
133-
"Couldn't complete the merge action",
134-
cause instanceof Error ? cause.message : "Try again.",
135-
);
136-
})
137-
.finally(() => {
138-
// A stack can partly merge before the provider reports a failure.
139-
refreshDetail();
140-
refreshActivity();
141-
stack.refresh();
142-
pending.current = false;
143-
setBusy(false);
144-
});
123+
: action === "enable-auto-merge" && method
124+
? { mergeMethod: method }
125+
: {}),
126+
},
127+
});
128+
// A stack can partly merge before the provider reports a failure.
129+
refreshDetail();
130+
refreshActivity();
131+
stack.refresh();
132+
pending.current = false;
133+
setBusy(false);
134+
if (AsyncResult.isFailure(response)) {
135+
const cause = squashAtomCommandFailure(response);
136+
Dialog.alert(
137+
"Couldn't complete the merge action",
138+
cause instanceof Error ? cause.message : "Try again.",
139+
);
140+
}
145141
};
146142
const confirm = (action: "merge" | "enable-auto-merge" | "disable-auto-merge") => {
147143
const title =

‎apps/mobile/src/features/pull-requests/PullRequestReviewSheet.tsx‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { AsyncResult } from "effect/reactivity";
12
import { useAtomRefresh } from "@effect/atom-react";
23
import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
34
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
4-
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
5+
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
56
import type { PullRequestReviewVerdict } from "@t3tools/contracts";
67
import { useRef, useState } from "react";
78
import { useFocusEffect } from "@react-navigation/native";
@@ -31,23 +32,22 @@ function useActionSubmission(params: PullRequestParams) {
3132
pending.current = true;
3233
setBusy(true);
3334
setError(null);
34-
await (async () => {
35+
const result = await settlePromise(async () => {
3536
await operation();
3637
refreshDetail();
3738
refreshActivity();
3839
navigation.goBack();
39-
})()
40-
.catch((cause: unknown) => {
41-
setError(
42-
cause instanceof Error
43-
? cause.message
44-
: "The provider could not complete this action. Try again.",
45-
);
46-
})
47-
.finally(() => {
48-
pending.current = false;
49-
setBusy(false);
50-
});
40+
});
41+
pending.current = false;
42+
setBusy(false);
43+
if (AsyncResult.isFailure(result)) {
44+
const cause = squashAtomCommandFailure(result);
45+
setError(
46+
cause instanceof Error
47+
? cause.message
48+
: "The provider could not complete this action. Try again.",
49+
);
50+
}
5151
};
5252
return { busy, error, submit };
5353
}

‎apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx‎

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AsyncResult } from "effect/reactivity";
12
import { Toolbar } from "../../components/ui/Toolbar";
23
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
34
import { usePullRequestConnected } from "./usePullRequestConnected";
@@ -103,32 +104,27 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
103104
inFlight.current = true;
104105
setPending(`${candidate.kind}:${candidate.id}`);
105106
setError(null);
106-
await (async () => {
107-
const response = await request({
108-
...target,
109-
input: {
110-
...target.input,
111-
reviewers: [{ id: candidate.id, kind: candidate.kind }],
112-
requested: !candidate.isRequested,
113-
},
114-
});
115-
if (response._tag === "Failure") throw squashAtomCommandFailure(response);
116-
if (!pr?.capabilities.reviewers.listCandidates) {
117-
setQuery("");
118-
detail.refresh();
119-
}
120-
})()
121-
.catch((cause: unknown) => {
122-
setError(
123-
cause instanceof Error
124-
? cause.message
125-
: "Couldn't update this review request. Try again.",
126-
);
127-
})
128-
.finally(() => {
129-
inFlight.current = false;
130-
setPending(null);
131-
});
107+
const response = await request({
108+
...target,
109+
input: {
110+
...target.input,
111+
reviewers: [{ id: candidate.id, kind: candidate.kind }],
112+
requested: !candidate.isRequested,
113+
},
114+
});
115+
inFlight.current = false;
116+
setPending(null);
117+
if (AsyncResult.isFailure(response)) {
118+
const cause = squashAtomCommandFailure(response);
119+
setError(
120+
cause instanceof Error ? cause.message : "Couldn't update this review request. Try again.",
121+
);
122+
return;
123+
}
124+
if (!pr?.capabilities.reviewers.listCandidates) {
125+
setQuery("");
126+
detail.refresh();
127+
}
132128
};
133129
const listCandidates = pr?.capabilities.reviewers.listCandidates === true;
134130
const searchOptions = {

‎apps/mobile/src/features/pull-requests/guide/useReviewGuide.ts‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import { useFocusEffect } from "@react-navigation/native";
33
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
4+
import { AsyncResult } from "effect/reactivity";
45
import { useRef, useState } from "react";
56
import { usePullRequestConnected } from "../usePullRequestConnected";
67
import { composerPullRequests } from "../../../state/pull-requests";
@@ -45,16 +46,13 @@ export function useReviewGuide(params: PullRequestParams) {
4546
pending.current = true;
4647
setStarting(true);
4748
setError(null);
48-
await (async () => {
49-
const result = await generate(target);
50-
if (result._tag === "Failure") {
51-
const cause = squashAtomCommandFailure(result);
52-
setError(cause instanceof Error ? cause.message : "Could not start the review guide.");
53-
}
54-
})().finally(() => {
55-
pending.current = false;
56-
setStarting(false);
57-
});
49+
const result = await generate(target);
50+
pending.current = false;
51+
setStarting(false);
52+
if (AsyncResult.isFailure(result)) {
53+
const cause = squashAtomCommandFailure(result);
54+
setError(cause instanceof Error ? cause.message : "Could not start the review guide.");
55+
}
5856
};
5957
return { ...query, enabled, canWrite, running, write, startError: error };
6058
}

‎apps/mobile/src/features/pull-requests/usePullRequestAction.ts‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AsyncResult } from "effect/reactivity";
12
import * as Dialog from "../../components/ui/Dialog";
23
import { useAtomRefresh } from "@effect/atom-react";
34
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
@@ -27,25 +28,22 @@ export function usePullRequestAction(params: PullRequestParams, pr: PullRequestD
2728
if (pending.current || !available(action)) return;
2829
pending.current = true;
2930
setBusy(true);
30-
await (async () => {
31-
const response = await run({
32-
...target,
33-
input: { ...target.input, action, ...(updateMethod ? { updateMethod } : {}) },
34-
});
35-
if (response._tag === "Failure") throw squashAtomCommandFailure(response);
36-
refreshDetail();
37-
refreshActivity();
38-
})()
39-
.catch((cause: unknown) => {
40-
Dialog.alert(
41-
"Couldn't update this pull request",
42-
cause instanceof Error ? cause.message : "Try again.",
43-
);
44-
})
45-
.finally(() => {
46-
pending.current = false;
47-
setBusy(false);
48-
});
31+
const response = await run({
32+
...target,
33+
input: { ...target.input, action, ...(updateMethod ? { updateMethod } : {}) },
34+
});
35+
pending.current = false;
36+
setBusy(false);
37+
if (AsyncResult.isFailure(response)) {
38+
const cause = squashAtomCommandFailure(response);
39+
Dialog.alert(
40+
"Couldn't update this pull request",
41+
cause instanceof Error ? cause.message : "Try again.",
42+
);
43+
return;
44+
}
45+
refreshDetail();
46+
refreshActivity();
4947
};
5048
const confirm = (action: PullRequestAction, title: string, message: string) =>
5149
Dialog.alert(title, message, [

0 commit comments

Comments
 (0)