Skip to content
TypeScript

fix(mobile): new thread project list no longer shifts when the No project option appears (#23391)

Juan De la Rosa7fe6934ed5d4
diff --git a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx
index 12b2a893e50..8dfe63a95cb 100644
--- a/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx
+++ b/apps/mobile/src/features/threads/NewTaskRouteScreen.tsx
@@ -10,7 +10,6 @@ import {
type StaticScreenProps,
} from "@react-navigation/native";
import { SymbolView } from "../../components/ui/AppSymbol";
-import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
import {
isAtomCommandInterrupted,
@@ -35,7 +34,11 @@ import { useWorkspaceState } from "../../state/workspace";
import { useIncomingShare } from "../sharing/IncomingShareProvider";
import { useNewTaskFlow } from "./new-task-flow-provider";
-import { filterProjectScopes, getProjectScopeSelectionTarget } from "./new-task-project-selection";
+import {
+ filterProjectScopes,
+ getProjectScopeSelectionTarget,
+ resolveNewTaskScratchOption,
+} from "./new-task-project-selection";
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
type NewTaskRouteParams = {
@@ -196,20 +199,15 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
const openScratch = useAtomCommand(projectEnvironment.openScratch, {
reportFailure: false,
});
- const scratchEnvironments = connectedEnvironments.filter(
- (environment) =>
- availableScratchWorkspaceRoot(
- environment.connectionState,
- serverConfigs.get(environment.environmentId),
- ) !== null,
- );
- const scratchEnvironment =
- scratchEnvironments.find(
- (environment) => environment.environmentId === selectedEnvironmentId,
- ) ??
- scratchEnvironments[0] ??
- null;
- const canStartScratch = scratchEnvironment !== null && reservedDestinationProject === null;
+ const scratchOption =
+ reservedDestinationProject === null
+ ? resolveNewTaskScratchOption({
+ environments: connectedEnvironments,
+ serverConfigs,
+ selectedEnvironmentId,
+ })
+ : null;
+ const canStartScratch = scratchOption?.ready === true;
const scratchStartInFlightRef = useRef(false);
async function selectProject(project: EnvironmentProject): Promise<void> {
@@ -249,8 +247,8 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
}
async function startScratch(): Promise<void> {
- if (!scratchEnvironment || scratchStartInFlightRef.current) return;
- const environmentId = scratchEnvironment.environmentId;
+ if (!canStartScratch || !scratchOption || scratchStartInFlightRef.current) return;
+ const environmentId = scratchOption.environmentId;
scratchStartInFlightRef.current = true;
const result = await openScratch({ environmentId, input: {} });
if (result._tag === "Failure") {
@@ -331,13 +329,14 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
: {}),
}}
>
- {canStartScratch && listScopes.length > 0 ? (
+ {scratchOption !== null && listScopes.length > 0 ? (
Platform.OS === "android" ? (
<View collapsable={false} className="overflow-hidden rounded-[28px] bg-grouped-card">
<NavigationRow
className="bg-grouped-card"
title="No project"
subtitle="Start a task without a project"
+ disabled={!canStartScratch}
onPress={() => void startScratch()}
leading={
<SymbolView
@@ -354,8 +353,12 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
<Pressable
accessibilityRole="button"
accessibilityLabel="No project"
+ disabled={!canStartScratch}
onPress={() => void startScratch()}
- className="flex-row items-center gap-3 bg-grouped-card px-4 py-3.5"
+ className={cn(
+ "flex-row items-center gap-3 bg-grouped-card px-4 py-3.5",
+ !canStartScratch && "opacity-45",
+ )}
>
<View className="h-7 w-7 items-center justify-center">
<SymbolView
diff --git a/apps/mobile/src/features/threads/new-task-project-selection.test.ts b/apps/mobile/src/features/threads/new-task-project-selection.test.ts
index 34357436b60..40be74007e8 100644
--- a/apps/mobile/src/features/threads/new-task-project-selection.test.ts
+++ b/apps/mobile/src/features/threads/new-task-project-selection.test.ts
@@ -8,6 +8,7 @@ import {
newTaskDestination,
resolveDraftProjectSelection,
resolveEnvironmentProjectMatch,
+ resolveNewTaskScratchOption,
} from "./new-task-project-selection";
function makeProject(
@@ -250,3 +251,78 @@ describe("newTaskDestination", () => {
).toEqual(picker);
});
});
+
+describe("resolveNewTaskScratchOption", () => {
+ const environmentId = EnvironmentId.make("environment");
+ const otherId = EnvironmentId.make("other");
+ const configs = new Map([[environmentId, { scratchWorkspaceRoot: "/scratch" }]]);
+
+ it("offers the option from the cached config while the environment is still connecting", () => {
+ expect(
+ resolveNewTaskScratchOption({
+ environments: [{ environmentId, connectionState: "connecting" }],
+ serverConfigs: configs,
+ selectedEnvironmentId: null,
+ }),
+ ).toEqual({ environmentId, ready: false });
+ });
+
+ it("becomes ready without changing environment once connected", () => {
+ expect(
+ resolveNewTaskScratchOption({
+ environments: [{ environmentId, connectionState: "connected" }],
+ serverConfigs: configs,
+ selectedEnvironmentId: null,
+ }),
+ ).toEqual({ environmentId, ready: true });
+ });
+
+ it("hides the option when no environment reports a scratch root", () => {
+ expect(
+ resolveNewTaskScratchOption({
+ environments: [{ environmentId, connectionState: "connected" }],
+ serverConfigs: new Map([[environmentId, {}]]),
+ selectedEnvironmentId: null,
+ }),
+ ).toBeNull();
+ expect(
+ resolveNewTaskScratchOption({
+ environments: [{ environmentId, connectionState: "connected" }],
+ serverConfigs: new Map(),
+ selectedEnvironmentId: null,
+ }),
+ ).toBeNull();
+ });
+
+ it("prefers a connected environment over the selected one that is still reconnecting", () => {
+ expect(
+ resolveNewTaskScratchOption({
+ environments: [
+ { environmentId, connectionState: "reconnecting" },
+ { environmentId: otherId, connectionState: "connected" },
+ ],
+ serverConfigs: new Map([
+ [environmentId, { scratchWorkspaceRoot: "/scratch" }],
+ [otherId, { scratchWorkspaceRoot: "/other-scratch" }],
+ ]),
+ selectedEnvironmentId: environmentId,
+ }),
+ ).toEqual({ environmentId: otherId, ready: true });
+ });
+
+ it("keeps the selected environment when every offering environment is connected", () => {
+ expect(
+ resolveNewTaskScratchOption({
+ environments: [
+ { environmentId: otherId, connectionState: "connected" },
+ { environmentId, connectionState: "connected" },
+ ],
+ serverConfigs: new Map([
+ [environmentId, { scratchWorkspaceRoot: "/scratch" }],
+ [otherId, { scratchWorkspaceRoot: "/other-scratch" }],
+ ]),
+ selectedEnvironmentId: environmentId,
+ }),
+ ).toEqual({ environmentId, ready: true });
+ });
+});
diff --git a/apps/mobile/src/features/threads/new-task-project-selection.ts b/apps/mobile/src/features/threads/new-task-project-selection.ts
index 99167a04e38..f6d728ae27c 100644
--- a/apps/mobile/src/features/threads/new-task-project-selection.ts
+++ b/apps/mobile/src/features/threads/new-task-project-selection.ts
@@ -1,5 +1,7 @@
+import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
+import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
-import { repositoryGroupingKeyOf, type EnvironmentId } from "@t3tools/contracts";
+import { repositoryGroupingKeyOf, type EnvironmentId, type ServerConfig } from "@t3tools/contracts";
import { scopedProjectKey } from "../../lib/scopedEntities";
import type { HomeProjectScope } from "../home/homeThreadList";
@@ -46,6 +48,33 @@ export function filterProjectScopes(
);
}
+// Cached configuration reserves the row; connection readiness only gates activation.
+export function resolveNewTaskScratchOption(input: {
+ readonly environments: ReadonlyArray<{
+ readonly environmentId: EnvironmentId;
+ readonly connectionState: EnvironmentConnectionPhase;
+ }>;
+ readonly serverConfigs: ReadonlyMap<EnvironmentId, Pick<ServerConfig, "scratchWorkspaceRoot">>;
+ readonly selectedEnvironmentId: EnvironmentId | null;
+}) {
+ const offering = input.environments.filter(
+ (environment) =>
+ (input.serverConfigs.get(environment.environmentId)?.scratchWorkspaceRoot ?? null) !== null,
+ );
+ const ready = offering.filter(
+ (environment) =>
+ availableScratchWorkspaceRoot(
+ environment.connectionState,
+ input.serverConfigs.get(environment.environmentId),
+ ) !== null,
+ );
+ const candidates = ready.length > 0 ? ready : offering;
+ const environment =
+ candidates.find((candidate) => candidate.environmentId === input.selectedEnvironmentId) ??
+ candidates[0];
+ return environment ? { environmentId: environment.environmentId, ready: ready.length > 0 } : null;
+}
+
export function resolveEnvironmentProjectMatch(
projectsOnTarget: ReadonlyArray<EnvironmentProject>,
selectedProject: EnvironmentProject | null,