Skip to content

Commit 845ddd9

Browse files
bmdavis419claudejuliusmarminge
authored
refactor(clients): share opening a machine's No project folder (#14759)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 60a56f1 commit 845ddd9

11 files changed

Lines changed: 220 additions & 85 deletions

File tree

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { MaterialListRow } from "../../components/MaterialListRow";
22
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
33
import type { VcsRef } from "@t3tools/client-runtime/state/vcs";
44
import { type EnvironmentId, resolveEnvironmentMachineKind } from "@t3tools/contracts";
5-
import * as Cause from "effect/Cause";
6-
import { AsyncResult } from "effect/unstable/reactivity";
75
import { LegendList } from "@legendapp/list/react-native";
86
import {
97
isAtomCommandInterrupted,
@@ -31,7 +29,7 @@ import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSym
3129
import { ThemedSwitch } from "../../components/ThemedSwitch";
3230
import { cn } from "../../lib/cn";
3331
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
34-
import { useServerConfigs, waitForProject } from "../../state/entities";
32+
import { useServerConfigs } from "../../state/entities";
3533
import { projectEnvironment } from "../../state/projects";
3634
import { useAtomCommand } from "../../state/use-atom-command";
3735
import { vcsEnvironment } from "../../state/vcs";
@@ -208,7 +206,7 @@ export function NewTaskEnvironmentPickerRouteScreen() {
208206
const navigation = useNavigation();
209207
const insets = useSafeAreaInsets();
210208
const serverConfigs = useServerConfigs();
211-
const ensureScratch = useAtomCommand(projectEnvironment.ensureScratch, {
209+
const openScratch = useAtomCommand(projectEnvironment.openScratch, {
212210
reportFailure: false,
213211
});
214212
const [movingToEnvironmentId, setMovingToEnvironmentId] = useState<EnvironmentId | null>(null);
@@ -218,23 +216,20 @@ export function NewTaskEnvironmentPickerRouteScreen() {
218216
async function moveScratchDraft(environmentId: EnvironmentId): Promise<void> {
219217
setMovingToEnvironmentId(environmentId);
220218
try {
221-
const result = await ensureScratch({ environmentId, input: {} });
222-
if (AsyncResult.isFailure(result)) {
223-
const error = Cause.squash(result.cause);
224-
Alert.alert(
225-
"Could not switch machine",
226-
error instanceof Error
227-
? error.message
228-
: "The folder for threads without a project could not be created.",
229-
);
230-
return;
231-
}
232-
const project = await waitForProject({ environmentId, projectId: result.value.projectId });
233-
if (project === null) {
234-
Alert.alert("Could not switch machine", "It has not reached this device yet. Try again.");
219+
const result = await openScratch({ environmentId, input: {} });
220+
if (result._tag === "Failure") {
221+
if (!isAtomCommandInterrupted(result)) {
222+
const error = squashAtomCommandFailure(result);
223+
Alert.alert(
224+
"Could not switch machine",
225+
error instanceof Error
226+
? error.message
227+
: "The folder for threads without a project could not be created.",
228+
);
229+
}
235230
return;
236231
}
237-
flow.setProject(project);
232+
flow.setProject(result.value);
238233
navigation.goBack();
239234
} finally {
240235
setMovingToEnvironmentId(null);

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import {
77
type StaticScreenProps,
88
} from "@react-navigation/native";
99
import { SymbolView } from "../../components/AppSymbol";
10-
import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
10+
import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
1111
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
12+
import {
13+
isAtomCommandInterrupted,
14+
squashAtomCommandFailure,
15+
} from "@t3tools/client-runtime/state/runtime";
1216
import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
13-
import * as Cause from "effect/Cause";
14-
import { AsyncResult } from "effect/unstable/reactivity";
1517
import { useEffect, useRef, useState } from "react";
1618
import { ActivityIndicator, Alert, Platform, Pressable, View } from "react-native";
1719
import { useSafeAreaInsets } from "react-native-safe-area-context";
@@ -21,7 +23,7 @@ import { MaterialButton } from "../../components/MaterialButton";
2123
import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
2224
import { AppText as Text } from "../../components/AppText";
2325
import { ProjectFavicon } from "../../components/ProjectFavicon";
24-
import { useProjects, useServerConfigs, waitForProject } from "../../state/entities";
26+
import { useProjects, useServerConfigs } from "../../state/entities";
2527
import { projectEnvironment } from "../../state/projects";
2628
import { useAtomCommand } from "../../state/use-atom-command";
2729
import { useRemoteConnectionStatus } from "../../state/use-remote-environment-registry";
@@ -173,16 +175,18 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
173175
) ?? null)
174176
: null;
175177
const { connectedEnvironments } = useRemoteConnectionStatus();
176-
const ensureScratch = useAtomCommand(projectEnvironment.ensureScratch, {
178+
const openScratch = useAtomCommand(projectEnvironment.openScratch, {
177179
reportFailure: false,
178180
});
179181
// Threads without a project need a connected environment that offers them.
180182
// The row starts on the selected environment when it has one, otherwise the
181183
// first that does; the draft page's machine picker moves it from there.
182184
const scratchEnvironments = connectedEnvironments.filter(
183185
(environment) =>
184-
canCreateProjectInEnvironment(environment.connectionState) &&
185-
serverConfigs.get(environment.environmentId)?.scratchWorkspaceRoot !== undefined,
186+
availableScratchWorkspaceRoot(
187+
environment.connectionState,
188+
serverConfigs.get(environment.environmentId),
189+
) !== null,
186190
);
187191
const scratchEnvironment =
188192
scratchEnvironments.find(
@@ -230,26 +234,20 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
230234
const environmentId = scratchEnvironment.environmentId;
231235
scratchStartInFlightRef.current = true;
232236
try {
233-
const result = await ensureScratch({ environmentId, input: {} });
234-
if (AsyncResult.isFailure(result)) {
235-
const error = Cause.squash(result.cause);
236-
Alert.alert(
237-
"Could not start without a project",
238-
error instanceof Error
239-
? error.message
240-
: "The folder for threads without a project could not be created.",
241-
);
242-
return;
243-
}
244-
const project = await waitForProject({ environmentId, projectId: result.value.projectId });
245-
if (project === null) {
246-
Alert.alert(
247-
"Could not start without a project",
248-
"It has not reached this device yet. Pick No project from the list once it appears.",
249-
);
237+
const result = await openScratch({ environmentId, input: {} });
238+
if (result._tag === "Failure") {
239+
if (!isAtomCommandInterrupted(result)) {
240+
const error = squashAtomCommandFailure(result);
241+
Alert.alert(
242+
"Could not start without a project",
243+
error instanceof Error
244+
? error.message
245+
: "The folder for threads without a project could not be created.",
246+
);
247+
}
250248
return;
251249
}
252-
await selectProject(project);
250+
await selectProject(result.value);
253251
} finally {
254252
scratchStartInFlightRef.current = false;
255253
}

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ import {
8989
useRemoteConnectionStatus,
9090
useSavedRemoteConnections,
9191
} from "../../state/use-remote-environment-registry";
92-
import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
92+
import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
9393
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
9494
import { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
9595
import { type VcsRef } from "@t3tools/client-runtime/state/vcs";
@@ -367,8 +367,10 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
367367
() =>
368368
connectedEnvironments.filter(
369369
(environment) =>
370-
canCreateProjectInEnvironment(environment.connectionState) &&
371-
serverConfigs.get(environment.environmentId)?.scratchWorkspaceRoot !== undefined,
370+
availableScratchWorkspaceRoot(
371+
environment.connectionState,
372+
serverConfigs.get(environment.environmentId),
373+
) !== null,
372374
),
373375
[connectedEnvironments, serverConfigs],
374376
);

‎apps/mobile/src/state/projects.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { environmentCatalog } from "../connection/catalog";
55
import { connectionAtomRuntime } from "../connection/runtime";
66
import { environmentSnapshotAtom } from "./shell";
77

8-
export const projectEnvironment = createProjectEnvironmentAtoms(connectionAtomRuntime);
98
export const environmentProjects = createEnvironmentProjectAtoms({
109
catalogValueAtom: environmentCatalog.catalogValueAtom,
1110
snapshotAtom: environmentSnapshotAtom,
1211
});
12+
export const projectEnvironment = createProjectEnvironmentAtoms(connectionAtomRuntime, {
13+
projectAtom: environmentProjects.projectAtom,
14+
});

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4396,7 +4396,7 @@ export default function ChatView(props: ChatViewProps) {
43964396
}
43974397
// Keep send disabled until the destination Scratch project is ready.
43984398
setIsEnvironmentChanging(true);
4399-
void openScratchProject(target.environmentId)
4399+
void openScratchProject(target.environmentId, "Could not switch machine")
44004400
.then((project) => {
44014401
if (project) retarget(project);
44024402
})

‎apps/web/src/hooks/useScratchProject.ts‎

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2+
import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
23
import {
34
isAtomCommandInterrupted,
45
squashAtomCommandFailure,
@@ -8,17 +9,16 @@ import type { EnvironmentId } from "@t3tools/contracts";
89
import { useCallback } from "react";
910

1011
import { stackedThreadToast, toastManager } from "~/components/ui/toast";
11-
import { waitForProject } from "~/state/entities";
1212
import { useEnvironments } from "~/state/environments";
1313
import { projectEnvironment } from "~/state/projects";
1414
import { useAtomCommand } from "~/state/use-atom-command";
1515
import { useNewThreadHandler } from "./useHandleNewThread";
1616

17-
function reportScratchFailure(error: unknown) {
17+
function reportScratchFailure(title: string, error: unknown) {
1818
toastManager.add(
1919
stackedThreadToast({
2020
type: "error",
21-
title: "Could not start without a project",
21+
title,
2222
description: error instanceof Error ? error.message : "An error occurred.",
2323
}),
2424
);
@@ -32,16 +32,17 @@ function reportScratchFailure(error: unknown) {
3232
*/
3333
export function useScratchProject() {
3434
const { environments } = useEnvironments();
35-
const ensureScratch = useAtomCommand(projectEnvironment.ensureScratch, { reportFailure: false });
35+
const openScratch = useAtomCommand(projectEnvironment.openScratch, { reportFailure: false });
3636
const handleNewThread = useNewThreadHandler();
3737

3838
/** The scratch folder of a connected environment, or null when it offers none. */
3939
const scratchWorkspaceRootFor = useCallback(
4040
(environmentId: EnvironmentId | null): string | null => {
4141
const environment = environments.find((entry) => entry.environmentId === environmentId);
42-
return environment?.connection.phase === "connected"
43-
? (environment.serverConfig?.scratchWorkspaceRoot ?? null)
44-
: null;
42+
return availableScratchWorkspaceRoot(
43+
environment?.connection.phase,
44+
environment?.serverConfig,
45+
);
4546
},
4647
[environments],
4748
);
@@ -62,32 +63,26 @@ export function useScratchProject() {
6263

6364
/** Resolves to the scratch project once it is in this client's store. */
6465
const openScratchProject = useCallback(
65-
async (environmentId: EnvironmentId): Promise<EnvironmentProject | null> => {
66-
const result = await ensureScratch({ environmentId, input: {} });
67-
if (result._tag === "Failure") {
68-
if (!isAtomCommandInterrupted(result)) {
69-
reportScratchFailure(squashAtomCommandFailure(result));
70-
}
71-
return null;
66+
async (
67+
environmentId: EnvironmentId,
68+
failureTitle = "Could not start without a project",
69+
): Promise<EnvironmentProject | null> => {
70+
const result = await openScratch({ environmentId, input: {} });
71+
if (result._tag === "Success") return result.value;
72+
if (!isAtomCommandInterrupted(result)) {
73+
reportScratchFailure(failureTitle, squashAtomCommandFailure(result));
7274
}
73-
// Drafts key off the project's stored path and settings, so wait for
74-
// the create event to reach the store before targeting one.
75-
return waitForProject(scopeProjectRef(environmentId, result.value.projectId)).catch(
76-
(error: unknown) => {
77-
reportScratchFailure(error);
78-
return null;
79-
},
80-
);
75+
return null;
8176
},
82-
[ensureScratch],
77+
[openScratch],
8378
);
8479

8580
const startScratchThread = useCallback(
8681
async (environmentId: EnvironmentId) => {
8782
const project = await openScratchProject(environmentId);
8883
if (project) {
8984
await handleNewThread(scopeProjectRef(project.environmentId, project.id)).catch(
90-
reportScratchFailure,
85+
(error: unknown) => reportScratchFailure("Could not start without a project", error),
9186
);
9287
}
9388
},

‎apps/web/src/state/projects.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import { environmentCatalog } from "../connection/catalog";
77
import { connectionAtomRuntime } from "../connection/runtime";
88
import { environmentSnapshotAtom } from "./shell";
99

10-
export const projectEnvironment = createProjectEnvironmentAtoms(connectionAtomRuntime);
10+
export const environmentProjects = createEnvironmentProjectAtoms({
11+
catalogValueAtom: environmentCatalog.catalogValueAtom,
12+
snapshotAtom: environmentSnapshotAtom,
13+
});
14+
export const projectEnvironment = createProjectEnvironmentAtoms(connectionAtomRuntime, {
15+
projectAtom: environmentProjects.projectAtom,
16+
});
1117
/**
1218
* Web-only: project content search backs the ⇧⌘F dialog, which has no mobile
1319
* surface, so the atom family lives here instead of the shared client-runtime
@@ -19,7 +25,3 @@ export const projectContentSearch = createEnvironmentRpcQueryAtomFamily(connecti
1925
staleTimeMs: 5_000,
2026
idleTtlMs: 60_000,
2127
});
22-
export const environmentProjects = createEnvironmentProjectAtoms({
23-
catalogValueAtom: environmentCatalog.catalogValueAtom,
24-
snapshotAtom: environmentSnapshotAtom,
25-
});

‎docs/user/thread-sidebar.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ A thread does not need a project. To start one without a project, click **or
1919
start without a project** under a new thread's heading, pick **No project** from
2020
the project menu in that heading or from **New thread in...** in the command
2121
palette, or press `mod+alt+n`. On mobile, pick **No project** from the project
22-
list. To move a draft into a project, pick the project in the heading.
22+
list. It starts on your current machine; before sending, pick another machine
23+
from the machine menu to move it there. To move a draft into a project, pick the
24+
project in the heading.
2325

2426
Each thread without a project works in its own folder under `~/.t3/scratch` (the
2527
`scratch` folder of your T3 data directory), named after its date, the first words

‎packages/client-runtime/src/operations/projects.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
EnvironmentId,
55
ProjectMutation,
66
ProjectId,
7+
ServerConfig,
78
SourceControlDiscoveryResult,
89
SourceControlProviderKind,
910
SourceControlRepositoryInfo,
@@ -36,6 +37,19 @@ export function canCreateProjectInEnvironment(
3637
return connectionPhase === "connected";
3738
}
3839

40+
/**
41+
* The Scratch folder an environment offers threads without a project right
42+
* now, or null while it is not connected or has none.
43+
*/
44+
export function availableScratchWorkspaceRoot(
45+
connectionPhase: EnvironmentConnectionPhase | null | undefined,
46+
serverConfig: Pick<ServerConfig, "scratchWorkspaceRoot"> | null | undefined,
47+
): string | null {
48+
return canCreateProjectInEnvironment(connectionPhase)
49+
? (serverConfig?.scratchWorkspaceRoot ?? null)
50+
: null;
51+
}
52+
3953
export type AddProjectRemoteSourceReadiness = Record<
4054
AddProjectRemoteSource,
4155
{ readonly ready: boolean; readonly hint: string | null }

0 commit comments

Comments
 (0)