Skip to content

Commit 950c532

Browse files
fix(mobile): one No project row at the top, machine chosen on the draft
The picker showed a No project row per machine, below the list, and once a machine's Scratch project existed it showed up as an ordinary project row instead. Now the picker has a single No project row at the top and never lists Scratch projects. The draft page's machine picker chooses where the thread runs, as it does for projects: for a thread without a project it lists every connected machine that offers one, and switching creates that machine's Scratch project first if it does not exist yet. This replaces the per-machine rows and the shared resolveScratchEnvironmentId helper from the previous commits, so web is untouched again. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent af12cdf commit 950c532

6 files changed

Lines changed: 181 additions & 194 deletions

File tree

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { MaterialListRow } from "../../components/MaterialListRow";
22
import type { VcsRef } from "@t3tools/client-runtime/state/vcs";
3-
import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
3+
import { type EnvironmentId, resolveEnvironmentMachineKind } from "@t3tools/contracts";
4+
import * as Cause from "effect/Cause";
5+
import { AsyncResult } from "effect/unstable/reactivity";
46
import { LegendList } from "@legendapp/list/react-native";
57
import {
68
isAtomCommandInterrupted,
@@ -28,7 +30,8 @@ import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSym
2830
import { ThemedSwitch } from "../../components/ThemedSwitch";
2931
import { cn } from "../../lib/cn";
3032
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
31-
import { useServerConfigs } from "../../state/entities";
33+
import { useServerConfigs, waitForProject } from "../../state/entities";
34+
import { projectEnvironment } from "../../state/projects";
3235
import { useAtomCommand } from "../../state/use-atom-command";
3336
import { vcsEnvironment } from "../../state/vcs";
3437
import {
@@ -203,6 +206,38 @@ export function NewTaskEnvironmentPickerRouteScreen() {
203206
const navigation = useNavigation();
204207
const insets = useSafeAreaInsets();
205208
const serverConfigs = useServerConfigs();
209+
const ensureScratch = useAtomCommand(projectEnvironment.ensureScratch, {
210+
reportFailure: false,
211+
});
212+
const [movingToEnvironmentId, setMovingToEnvironmentId] = useState<EnvironmentId | null>(null);
213+
214+
// A thread without a project moves to the other machine's own Scratch
215+
// project, which is created there first if it does not exist yet.
216+
async function moveScratchDraft(environmentId: EnvironmentId): Promise<void> {
217+
setMovingToEnvironmentId(environmentId);
218+
try {
219+
const result = await ensureScratch({ environmentId, input: {} });
220+
if (AsyncResult.isFailure(result)) {
221+
const error = Cause.squash(result.cause);
222+
Alert.alert(
223+
"Could not switch machine",
224+
error instanceof Error
225+
? error.message
226+
: "The folder for threads without a project could not be created.",
227+
);
228+
return;
229+
}
230+
const project = await waitForProject({ environmentId, projectId: result.value.projectId });
231+
if (project === null) {
232+
Alert.alert("Could not switch machine", "It has not reached this device yet. Try again.");
233+
return;
234+
}
235+
flow.setProject(project);
236+
navigation.goBack();
237+
} finally {
238+
setMovingToEnvironmentId(null);
239+
}
240+
}
206241

207242
return (
208243
<View className="flex-1 bg-sheet" collapsable={false}>
@@ -243,9 +278,17 @@ export function NewTaskEnvironmentPickerRouteScreen() {
243278
/>
244279
}
245280
isLast={index === flow.environments.length - 1}
281+
disabled={movingToEnvironmentId !== null}
246282
onPress={() => {
247283
void Haptics.selectionAsync();
248-
flow.selectEnvironment(environment.environmentId);
284+
if (flow.isScratchDraft) {
285+
if (environment.environmentId !== flow.selectedEnvironmentId) {
286+
void moveScratchDraft(environment.environmentId);
287+
return;
288+
}
289+
} else {
290+
flow.selectEnvironment(environment.environmentId);
291+
}
249292
navigation.goBack();
250293
}}
251294
selected={flow.selectedEnvironmentId === environment.environmentId}

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

Lines changed: 82 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ import {
88
} from "@react-navigation/native";
99
import { SymbolView } from "../../components/AppSymbol";
1010
import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
11-
import {
12-
isScratchProject,
13-
resolveScratchEnvironmentId,
14-
} from "@t3tools/client-runtime/state/projects";
11+
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
1512
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
16-
import type { EnvironmentId } from "@t3tools/contracts";
1713
import * as Cause from "effect/Cause";
1814
import { AsyncResult } from "effect/unstable/reactivity";
1915
import { useEffect, useRef, useState } from "react";
@@ -139,8 +135,7 @@ function NewTaskHeader(props: {
139135
export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRouteParams | undefined>) {
140136
const projects = useProjects();
141137
const [searchText, setSearchText] = useState("");
142-
const { projectScopes, selectedEnvironmentId, chosenEnvironmentId, setProject } =
143-
useNewTaskFlow();
138+
const { projectScopes, selectedEnvironmentId, setProject } = useNewTaskFlow();
144139
const { state: catalogState } = useWorkspaceState();
145140
const navigation = useNavigation();
146141
const isFocused = useIsFocused();
@@ -160,8 +155,8 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
160155
const screenTitle = incomingShare ? "Start a task" : "Choose project";
161156
const projectEmptyState = deriveProjectEmptyState(catalogState);
162157
const serverConfigs = useServerConfigs();
163-
// The Scratch project is reached through the No project entry, never as a
164-
// project row of its own.
158+
// Scratch projects are reached through the No project row, never as rows
159+
// of their own.
165160
const listScopes = projectScopes.filter(
166161
(scope) =>
167162
!scope.projects.every((project) =>
@@ -182,35 +177,20 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
182177
reportFailure: false,
183178
});
184179
// Threads without a project need a connected environment that offers them.
185-
// The machine follows web's useScratchProject rule; when that leaves it
186-
// open, every machine that offers one gets its own entry so the user picks.
180+
// The row starts on the selected environment when it has one, otherwise the
181+
// first that does; the draft page's machine picker moves it from there.
187182
const scratchEnvironments = connectedEnvironments.filter(
188183
(environment) =>
189184
canCreateProjectInEnvironment(environment.connectionState) &&
190185
serverConfigs.get(environment.environmentId)?.scratchWorkspaceRoot !== undefined,
191186
);
192-
const scratchEnvironmentId = resolveScratchEnvironmentId(
193-
chosenEnvironmentId,
194-
scratchEnvironments.map((environment) => environment.environmentId),
195-
);
196-
const scratchChoices = (
197-
scratchEnvironmentId === null
198-
? scratchEnvironments
199-
: scratchEnvironments.filter(
200-
(environment) => environment.environmentId === scratchEnvironmentId,
201-
)
202-
).map((environment) => {
203-
const machineLabel = connectedEnvironments.length > 1 ? environment.environmentLabel : null;
204-
return {
205-
environmentId: environment.environmentId,
206-
startLabel: machineLabel
207-
? `Start without a project on ${machineLabel}`
208-
: "Start without a project",
209-
rowLabel: machineLabel ? `No project on ${machineLabel}` : "No project",
210-
rowSubtitle: machineLabel ? `On ${machineLabel}` : "Start a task without a project",
211-
};
212-
});
213-
const canStartScratch = reservedDestinationProject === null;
187+
const scratchEnvironment =
188+
scratchEnvironments.find(
189+
(environment) => environment.environmentId === selectedEnvironmentId,
190+
) ??
191+
scratchEnvironments[0] ??
192+
null;
193+
const canStartScratch = scratchEnvironment !== null && reservedDestinationProject === null;
214194
const scratchStartInFlightRef = useRef(false);
215195

216196
async function selectProject(project: EnvironmentProject): Promise<void> {
@@ -245,8 +225,9 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
245225
);
246226
}
247227

248-
async function startScratch(environmentId: EnvironmentId): Promise<void> {
249-
if (scratchStartInFlightRef.current) return;
228+
async function startScratch(): Promise<void> {
229+
if (!scratchEnvironment || scratchStartInFlightRef.current) return;
230+
const environmentId = scratchEnvironment.environmentId;
250231
scratchStartInFlightRef.current = true;
251232
try {
252233
const result = await ensureScratch({ environmentId, input: {} });
@@ -331,6 +312,55 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
331312
: {}),
332313
}}
333314
>
315+
{canStartScratch && listScopes.length > 0 ? (
316+
Platform.OS === "android" ? (
317+
<View collapsable={false} className="overflow-hidden rounded-[28px] bg-card">
318+
<MaterialListRow
319+
title="No project"
320+
subtitle="Start a task without a project"
321+
onPress={() => void startScratch()}
322+
leading={
323+
<SymbolView
324+
name="text.bubble"
325+
size={22}
326+
tintColorClassName="accent-icon-muted"
327+
type="monochrome"
328+
/>
329+
}
330+
/>
331+
</View>
332+
) : (
333+
<View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
334+
<Pressable
335+
accessibilityRole="button"
336+
accessibilityLabel="No project"
337+
onPress={() => void startScratch()}
338+
className="flex-row items-center gap-3 bg-card px-4 py-3.5"
339+
>
340+
<View className="h-7 w-7 items-center justify-center">
341+
<SymbolView
342+
name="text.bubble"
343+
size={18}
344+
tintColorClassName="accent-icon-muted"
345+
type="monochrome"
346+
/>
347+
</View>
348+
<View className="min-w-0 flex-1">
349+
<Text className="text-base font-t3-bold leading-snug">No project</Text>
350+
<Text className="text-xs leading-snug text-foreground-muted" numberOfLines={1}>
351+
Start a task without a project
352+
</Text>
353+
</View>
354+
<SymbolView
355+
name="chevron.right"
356+
size={14}
357+
tintColorClassName="accent-chevron"
358+
type="monochrome"
359+
/>
360+
</Pressable>
361+
</View>
362+
)
363+
) : null}
334364
{listScopes.length === 0 ? (
335365
<View
336366
collapsable={false}
@@ -359,16 +389,13 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
359389
: navigation.navigate("ConnectionsNew")
360390
}
361391
/>
362-
{canStartScratch
363-
? scratchChoices.map((choice) => (
364-
<MaterialButton
365-
key={choice.environmentId}
366-
label={choice.startLabel}
367-
tone="secondary"
368-
onPress={() => void startScratch(choice.environmentId)}
369-
/>
370-
))
371-
: null}
392+
{canStartScratch ? (
393+
<MaterialButton
394+
label="Start without a project"
395+
tone="secondary"
396+
onPress={() => void startScratch()}
397+
/>
398+
) : null}
372399
</>
373400
) : !catalogState.hasReadyEnvironment ? (
374401
<Pressable
@@ -389,19 +416,16 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
389416
Add new project
390417
</Text>
391418
</Pressable>
392-
{canStartScratch
393-
? scratchChoices.map((choice) => (
394-
<Pressable
395-
key={choice.environmentId}
396-
className="rounded-full bg-subtle px-4 py-2.5 active:opacity-70"
397-
onPress={() => void startScratch(choice.environmentId)}
398-
>
399-
<Text className="text-sm font-t3-bold text-foreground">
400-
{choice.startLabel}
401-
</Text>
402-
</Pressable>
403-
))
404-
: null}
419+
{canStartScratch ? (
420+
<Pressable
421+
className="rounded-full bg-subtle px-4 py-2.5 active:opacity-70"
422+
onPress={() => void startScratch()}
423+
>
424+
<Text className="text-sm font-t3-bold text-foreground">
425+
Start without a project
426+
</Text>
427+
</Pressable>
428+
) : null}
405429
</>
406430
)}
407431
</View>
@@ -502,68 +526,6 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
502526
})}
503527
</View>
504528
)}
505-
{canStartScratch && scratchChoices.length > 0 && listScopes.length > 0 ? (
506-
Platform.OS === "android" ? (
507-
<View collapsable={false} className="overflow-hidden rounded-[28px] bg-card">
508-
{scratchChoices.map((choice) => (
509-
<MaterialListRow
510-
key={choice.environmentId}
511-
title="No project"
512-
subtitle={choice.rowSubtitle}
513-
onPress={() => void startScratch(choice.environmentId)}
514-
leading={
515-
<SymbolView
516-
name="text.bubble"
517-
size={22}
518-
tintColorClassName="accent-icon-muted"
519-
type="monochrome"
520-
/>
521-
}
522-
/>
523-
))}
524-
</View>
525-
) : (
526-
<View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
527-
{scratchChoices.map((choice, choiceIndex) => (
528-
<View
529-
key={choice.environmentId}
530-
className={cn(choiceIndex > 0 && "border-t border-border-subtle")}
531-
>
532-
<Pressable
533-
accessibilityRole="button"
534-
accessibilityLabel={choice.rowLabel}
535-
onPress={() => void startScratch(choice.environmentId)}
536-
className="flex-row items-center gap-3 bg-card px-4 py-3.5"
537-
>
538-
<View className="h-7 w-7 items-center justify-center">
539-
<SymbolView
540-
name="text.bubble"
541-
size={18}
542-
tintColorClassName="accent-icon-muted"
543-
type="monochrome"
544-
/>
545-
</View>
546-
<View className="min-w-0 flex-1">
547-
<Text className="text-base font-t3-bold leading-snug">No project</Text>
548-
<Text
549-
className="text-xs leading-snug text-foreground-muted"
550-
numberOfLines={1}
551-
>
552-
{choice.rowSubtitle}
553-
</Text>
554-
</View>
555-
<SymbolView
556-
name="chevron.right"
557-
size={14}
558-
tintColorClassName="accent-chevron"
559-
type="monochrome"
560-
/>
561-
</Pressable>
562-
</View>
563-
))}
564-
</View>
565-
)
566-
) : null}
567529
</ScrollView>
568530
</MaterialScreenContent>
569531
</View>

0 commit comments

Comments
 (0)