Skip to content

Commit 3888bce

Browse files
committed
fix(mobile): reject branch routes for unavailable projects
1 parent c8011da commit 3888bce

3 files changed

Lines changed: 33 additions & 12 deletions

File tree

‎apps/mobile/src/features/threads/NewTaskDraftRouteScreen.tsx‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ type NewTaskDraftRouteParams = {
2727

2828
export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraftRouteParams>) {
2929
const params = useMemo(() => route.params ?? {}, [route.params]);
30+
const pendingTaskId = Array.isArray(params.pendingTaskId)
31+
? params.pendingTaskId[0]
32+
: params.pendingTaskId;
33+
const draftId = Array.isArray(params.draftId) ? params.draftId[0] : params.draftId;
3034
const projects = useProjects();
3135
const navigation = useNavigation();
3236
const switchRef = useAtomCommand(vcsEnvironment.switchRef, { reportFailure: false });
@@ -49,7 +53,7 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
4953
const [preparedProject, setPreparedProject] = useState<{
5054
request: typeof initialProjectRef;
5155
projectRef: typeof initialProjectRef;
52-
workspaceRoot: string;
56+
workspaceRoot: string | undefined;
5357
} | null>(null);
5458
const project = projects.find(
5559
(candidate) =>
@@ -58,12 +62,10 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
5862
);
5963
const environmentId = project?.environmentId;
6064
const workspaceRoot = project?.workspaceRoot;
61-
const needsPreparation = Boolean(
62-
initialProjectRef.branch && !params.pendingTaskId && !params.draftId,
63-
);
65+
const needsPreparation = Boolean(initialProjectRef.branch && !pendingTaskId && !draftId);
6466

6567
useEffect(() => {
66-
if (!needsPreparation || !initialProjectRef.branch || !environmentId || !workspaceRoot) return;
68+
if (!needsPreparation || !initialProjectRef.branch) return;
6769
let active = true;
6870
void checkoutNewTaskBranch({
6971
// A thread's branch is historical; only switchRef can establish that
@@ -74,7 +76,7 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
7476
isDefault: false,
7577
worktreePath: initialProjectRef.worktreePath ?? null,
7678
},
77-
project: { environmentId, workspaceRoot },
79+
project: environmentId && workspaceRoot ? { environmentId, workspaceRoot } : null,
7880
workspaceMode: "local",
7981
switchRef,
8082
}).then((result) => {
@@ -127,10 +129,8 @@ export function NewTaskDraftRouteScreen({ route }: StaticScreenProps<NewTaskDraf
127129
? params.incomingShareId[0]
128130
: params.incomingShareId
129131
}
130-
pendingTaskId={
131-
Array.isArray(params.pendingTaskId) ? params.pendingTaskId[0] : params.pendingTaskId
132-
}
133-
draftId={Array.isArray(params.draftId) ? params.draftId[0] : params.draftId}
132+
pendingTaskId={pendingTaskId}
133+
draftId={draftId}
134134
/>
135135
)}
136136
</>

‎apps/mobile/src/features/threads/checkout-new-task-branch.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ describe("new-task branch checkout", () => {
9595
);
9696
});
9797

98+
it("fails when the source project is unavailable instead of releasing a composer selection", async () => {
99+
const result = await checkoutNewTaskBranch({
100+
branch,
101+
project: null,
102+
workspaceMode: "local",
103+
switchRef: () => {
104+
throw new Error("An unavailable project must not run checkout");
105+
},
106+
});
107+
expect(result._tag).toBe("Failure");
108+
if (result._tag !== "Failure") throw new Error("Expected an unavailable-project failure");
109+
expect(String(squashAtomCommandFailure(result))).toContain("selected project is unavailable");
110+
expect((await git("branch", "--show-current")).stdout.trim()).toBe("main");
111+
});
112+
98113
it("reuses an existing worktree without switching the project checkout", async () => {
99114
const worktreePath = NodePath.join(directory, "worktree");
100115
await git("worktree", "add", worktreePath, "feature/a");

‎apps/mobile/src/features/threads/checkout-new-task-branch.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
mapAtomCommandResult,
66
} from "@t3tools/client-runtime/state/runtime";
77
import type { VcsSwitchRefInput, VcsSwitchRefResult } from "@t3tools/contracts";
8+
import * as Cause from "effect/Cause";
89
import { AsyncResult } from "effect/unstable/reactivity";
910

1011
import { shouldCheckoutNewTaskBranch } from "./new-task-context-presentation";
@@ -13,13 +14,18 @@ import { shouldCheckoutNewTaskBranch } from "./new-task-context-presentation";
1314
* and new-worktree base selections already identify a separate workspace. */
1415
export async function checkoutNewTaskBranch<E>(input: {
1516
readonly branch: VcsRef;
16-
readonly project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">;
17+
readonly project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot"> | null;
1718
readonly workspaceMode: "local" | "worktree";
1819
readonly switchRef: (request: {
1920
readonly environmentId: EnvironmentProject["environmentId"];
2021
readonly input: VcsSwitchRefInput;
2122
}) => Promise<AtomCommandResult<VcsSwitchRefResult, E>>;
22-
}): Promise<AtomCommandResult<VcsRef, E>> {
23+
}): Promise<AtomCommandResult<VcsRef, E | Error>> {
24+
if (!input.project) {
25+
return AsyncResult.failure(
26+
Cause.fail(new Error("The selected project is unavailable. Reconnect and try again.")),
27+
);
28+
}
2329
if (
2430
!shouldCheckoutNewTaskBranch({
2531
branchIsCurrent: input.branch.current,

0 commit comments

Comments
 (0)