Skip to content

Commit c924284

Browse files
committed
feat(web,mobile): move a thread to another project from its menu
1 parent 1614621 commit c924284

13 files changed

Lines changed: 249 additions & 10 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { threadMoveDestinations } from "@t3tools/client-runtime/state/projects";
2+
import {
3+
isAtomCommandInterrupted,
4+
runAtomCommand,
5+
squashAtomCommandFailure,
6+
} from "@t3tools/client-runtime/state/runtime";
7+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
8+
import { AuthOrchestrationOperateScope, ProjectId } from "@t3tools/contracts";
9+
import * as Haptics from "expo-haptics";
10+
11+
import * as Dialog from "../../components/ui/Dialog";
12+
import { appAtomRegistry } from "../../state/atom-registry";
13+
import { environmentProjects, projectEnvironment } from "../../state/projects";
14+
import { environmentServerConfigsAtom } from "../../state/server";
15+
import { readEnvironmentScope } from "../../state/session";
16+
17+
/** Projects in the thread's environment it can move to, on a server that supports moving. */
18+
export function threadMoveDestinationsFor(
19+
thread: EnvironmentThreadShell,
20+
): ReadonlyArray<{ readonly id: ProjectId; readonly title: string }> {
21+
const config = appAtomRegistry.get(environmentServerConfigsAtom).get(thread.environmentId);
22+
if (config?.environment.capabilities.threadProjectMove !== true) return [];
23+
return threadMoveDestinations(
24+
thread,
25+
appAtomRegistry.get(environmentProjects.projectsAtom),
26+
config.scratchWorkspaceRoot,
27+
);
28+
}
29+
30+
export async function moveThreadToProject(
31+
thread: EnvironmentThreadShell,
32+
projectId: string,
33+
): Promise<void> {
34+
const title = "Could not move thread";
35+
if (!readEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope)) {
36+
Dialog.alert(title, "This connection cannot change threads.");
37+
return;
38+
}
39+
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
40+
const result = await runAtomCommand(
41+
appAtomRegistry,
42+
projectEnvironment.moveThread,
43+
{
44+
environmentId: thread.environmentId,
45+
input: { threadId: thread.id, projectId: ProjectId.make(projectId) },
46+
},
47+
{ reportFailure: false },
48+
);
49+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
50+
const error = squashAtomCommandFailure(result);
51+
Dialog.alert(title, error instanceof Error ? error.message : "Try again.");
52+
}
53+
}

‎apps/mobile/src/features/threads/thread-list-v2-items.tsx‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import { useSwipeRowDormant } from "../home/swipe-row-activation";
6363
import { ThreadSwipeable } from "../home/thread-swipe-actions";
6464
import { buildThreadTitleRegenerationMenuItems } from "./thread-title-regeneration-menu";
6565
import { canTurnThreadIntoProject, turnThreadIntoProject } from "./turnThreadIntoProject";
66+
import { moveThreadToProject, threadMoveDestinationsFor } from "./moveThreadToProject";
6667
import { threadCanArchive } from "../home/threadArchive";
6768
import {
6869
THREAD_LIST_V2_SETTLED_PAGE_COUNT,
@@ -889,6 +890,10 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
889890
[props.autoSettleOptOutSupported, thread.autoSettleDisabledAt],
890891
);
891892
const canTurnIntoProject = canTurnThreadIntoProject(thread);
893+
// A fresh array every render; the joined key keeps the menu memo stable.
894+
const moveDestinationsKey = threadMoveDestinationsFor(thread)
895+
.map((project) => `${project.id}\u0000${project.title}`)
896+
.join("\n");
892897
const titleMenuItems = useMemo<MenuAction[]>(
893898
() => [
894899
{ id: "rename", title: "Rename", image: "square.and.pencil" },
@@ -906,8 +911,28 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
906911
} satisfies MenuAction,
907912
]
908913
: []),
914+
...(moveDestinationsKey !== ""
915+
? [
916+
{
917+
id: "move-to-project",
918+
title: "Move to project",
919+
image: "folder",
920+
attributes: { disabled: !threadCanArchive(thread.runtime) },
921+
subactions: moveDestinationsKey.split("\n").map((entry) => {
922+
const [id, title] = entry.split("\u0000");
923+
return { id: `move-to-project:${id}`, title: title ?? "" };
924+
}),
925+
} satisfies MenuAction,
926+
]
927+
: []),
928+
],
929+
[
930+
canTurnIntoProject,
931+
moveDestinationsKey,
932+
props.titleRegenerationSupported,
933+
thread.runtime,
934+
isRegeneratingTitle,
909935
],
910-
[canTurnIntoProject, props.titleRegenerationSupported, thread.runtime, isRegeneratingTitle],
911936
);
912937
const menuActions = useMemo<MenuAction[]>(() => {
913938
const nested = Platform.OS === "ios";
@@ -1010,6 +1035,9 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
10101035
if (nativeEvent.event === "rename") handleRename();
10111036
if (nativeEvent.event === "regenerate-title") handleRegenerateTitle();
10121037
if (nativeEvent.event === "turn-into-project") void turnThreadIntoProject(thread);
1038+
if (nativeEvent.event.startsWith("move-to-project:")) {
1039+
void moveThreadToProject(thread, nativeEvent.event.slice("move-to-project:".length));
1040+
}
10131041
if (nativeEvent.event === "copy-thread") {
10141042
const target = threadChipCopyTarget(
10151043
{ environmentId: thread.environmentId, threadId: thread.id },

‎apps/server/src/mcp/WorktreeMcpService.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ const makeHarness = (options: HarnessOptions = {}) => {
347347
}),
348348
Layer.mock(ManagedProjectFolders.ManagedProjectFolders)({
349349
isScratchProject: () => Effect.succeed(false),
350+
namedProjectsRoot: "/projects",
350351
}),
351352
ServerSettings.layerTest({
352353
newWorktreesStartFromOrigin: options.newWorktreesStartFromOrigin ?? false,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ import {
122122
} from "../threadSelectionStore";
123123
import {
124124
canTurnThreadIntoProject,
125+
threadMoveDestinationsFor,
125126
useAcknowledgeThreadWoke,
126127
useThreadActions,
127128
} from "../hooks/useThreadActions";
@@ -925,6 +926,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
925926
confirmAndUnpinThread,
926927
setThreadAutoSettle,
927928
turnThreadIntoProject,
929+
moveThreadToProject,
928930
reorderPinnedThread,
929931
reorderActiveThread,
930932
markThreadUnread,
@@ -2646,6 +2648,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
26462648
canShareWithGuestAtom(threadRef.environmentId),
26472649
),
26482650
canTurnIntoProject: canTurnThreadIntoProject(threadRef),
2651+
moveDestinations: threadMoveDestinationsFor(threadRef),
26492652
supports: {
26502653
settlement: configLoaded,
26512654
autoSettleOptOut: supportsAutoSettleOptOut,
@@ -2673,6 +2676,13 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
26732676
if (preset) attemptSnooze(threadRef, preset);
26742677
return;
26752678
}
2679+
if (clicked.value.startsWith("move-to-project:")) {
2680+
await moveThreadToProject(
2681+
threadRef,
2682+
ProjectId.make(clicked.value.slice("move-to-project:".length)),
2683+
);
2684+
return;
2685+
}
26762686
switch (clicked.value) {
26772687
case "filter-by-project":
26782688
if (threadProjectGroup) {

‎apps/web/src/components/threadActionMenu.logic.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ describe("buildThreadActionMenuItems", () => {
6565
},
6666
);
6767

68-
it("disables archive and turning into a project while the thread is running", () => {
68+
it("disables archive and project moves while the thread is running", () => {
6969
const items = buildThreadActionMenuItems({
7070
...baseState,
7171
isRunning: true,
7272
canTurnIntoProject: true,
73+
moveDestinations: [{ id: "project-b", label: "B" }],
7374
});
7475
expect(items.find((item) => item.id === "archive")?.disabled).toBe(true);
7576
expect(items.find((item) => item.id === "turn-into-project")?.disabled).toBe(true);
77+
expect(items.find((item) => item.id === "move-to-project")?.disabled).toBe(true);
7678
});
7779
});

‎apps/web/src/components/threadActionMenu.logic.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type ThreadActionMenuId =
66
| "new-thread-on-branch"
77
| "filter-by-project"
88
| "turn-into-project"
9+
| "move-to-project"
10+
| `move-to-project:${string}`
911
| "project-settings"
1012
| "pin"
1113
| "unpin"
@@ -95,6 +97,8 @@ export interface ThreadActionMenuState {
9597
readonly canShareWithGuest?: boolean;
9698
/** A No project thread with its own folder, on a server that can promote it. */
9799
readonly canTurnIntoProject?: boolean;
100+
/** Projects this thread can be filed under; empty hides the item. */
101+
readonly moveDestinations?: ReadonlyArray<{ readonly id: string; readonly label: string }>;
98102
}
99103

100104
/** Local navigation, read markers, and copying remain available to read-only clients. */
@@ -202,6 +206,20 @@ export function buildThreadActionMenuItems(
202206
},
203207
]
204208
: []),
209+
...(state.moveDestinations && state.moveDestinations.length > 0
210+
? [
211+
{
212+
id: "move-to-project" as const,
213+
label: "Move to project",
214+
icon: "folder-input",
215+
disabled: state.isRunning,
216+
children: state.moveDestinations.map((destination) => ({
217+
id: `move-to-project:${destination.id}` as const,
218+
label: destination.label,
219+
})),
220+
},
221+
]
222+
: []),
205223
...(state.supports.autoSettleOptOut
206224
? [
207225
{

‎apps/web/src/contextMenuFallback.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ const ICON_PATHS: Record<string, ReadonlyArray<{ tag: string; attrs: Record<stri
134134
},
135135
},
136136
],
137+
"folder-input": [
138+
{
139+
tag: "path",
140+
attrs: {
141+
d: "M2 9V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H20a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2v-1",
142+
},
143+
},
144+
{ tag: "path", attrs: { d: "M2 13h10" } },
145+
{ tag: "path", attrs: { d: "m9 16 3-3-3-3" } },
146+
],
137147
"folder-tree": [
138148
{
139149
tag: "path",

‎apps/web/src/hooks/useThreadActionMenu.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ vi.mock("./useSettings", () => ({
120120
}));
121121
vi.mock("./useThreadActions", () => ({
122122
canTurnThreadIntoProject: () => false,
123+
threadMoveDestinationsFor: () => [],
123124
useThreadActions: () =>
124125
Object.fromEntries(
125126
[

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
1111
import {
1212
AuthOrchestrationOperateScope,
13+
ProjectId,
1314
type ScopedThreadRef,
1415
type ThreadId,
1516
} from "@t3tools/contracts";
@@ -46,7 +47,11 @@ import { threadRuntimeCanArchive } from "@t3tools/client-runtime/state/models";
4647
import { useCopyToClipboard } from "./useCopyToClipboard";
4748
import { useNewThreadHandler } from "./useHandleNewThread";
4849
import { useClientSettings } from "./useSettings";
49-
import { canTurnThreadIntoProject, useThreadActions } from "./useThreadActions";
50+
import {
51+
canTurnThreadIntoProject,
52+
threadMoveDestinationsFor,
53+
useThreadActions,
54+
} from "./useThreadActions";
5055

5156
function failureToast(title: string, error: unknown) {
5257
toastManager.add(
@@ -87,6 +92,7 @@ export function useThreadActionMenu(input: {
8792
deleteThread,
8893
markThreadUnread,
8994
turnThreadIntoProject,
95+
moveThreadToProject,
9096
} = useThreadActions();
9197
const updateThreadMetadata = useOrchestrationCommand(threadEnvironment.updateMetadata, {
9298
reportFailure: false,
@@ -149,6 +155,7 @@ export function useThreadActionMenu(input: {
149155
snoozePresets,
150156
canShareWithGuest,
151157
canTurnIntoProject: canTurnThreadIntoProject(threadRef),
158+
moveDestinations: threadMoveDestinationsFor(threadRef),
152159
});
153160
const clicked = await settlePromise(() => api.contextMenu.show(items, position));
154161
if (clicked._tag === "Failure" || clicked.value === null) return;
@@ -180,6 +187,13 @@ export function useThreadActionMenu(input: {
180187
}
181188
return;
182189
}
190+
if (action.startsWith("move-to-project:")) {
191+
await moveThreadToProject(
192+
threadRef,
193+
ProjectId.make(action.slice("move-to-project:".length)),
194+
);
195+
return;
196+
}
183197
const reportFailure = async (
184198
title: string,
185199
run: () => Promise<AtomCommandResult<unknown, unknown>>,

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ import {
44
scopeThreadRef,
55
scopedThreadKey,
66
} from "@t3tools/client-runtime/environment";
7-
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
7+
import {
8+
isAtomCommandInterrupted,
9+
settlePromise,
10+
squashAtomCommandFailure,
11+
} from "@t3tools/client-runtime/state/runtime";
812
import { canSnooze, threadWokeAt } from "@t3tools/client-runtime/state/thread-settled";
913
import { threadRuntimeCanArchive } from "@t3tools/client-runtime/state/models";
1014
import {
1115
AuthOrchestrationOperateScope,
1216
AuthSourceControlWriteScope,
1317
EnvironmentAuthorizationError,
1418
EnvironmentId,
19+
ProjectId,
1520
type ScopedThreadRef,
1621
ThreadId,
1722
sessionGrantsScope,
@@ -27,7 +32,7 @@ import { useComposerDraftStore } from "../composerDraftStore";
2732
import { environmentSession, readEnvironmentScope } from "../state/session";
2833
import { appAtomRegistry } from "../rpc/atomRegistry";
2934
import { environmentServerConfigsAtom } from "../state/server";
30-
import { isScratchProject } from "@t3tools/client-runtime/state/projects";
35+
import { isScratchProject, threadMoveDestinations } from "@t3tools/client-runtime/state/projects";
3136
import { orchestrationEnvironment } from "../state/orchestration";
3237
import { projectEnvironment } from "../state/projects";
3338
import { threadEnvironment } from "../state/threads";
@@ -41,6 +46,7 @@ import {
4146
readEnvironmentSupportsAutoSettleOptOut,
4247
readEnvironmentThreadRefs,
4348
readProject,
49+
readProjects,
4450
readThreadShell,
4551
readThreadShells,
4652
} from "../state/entities";
@@ -260,6 +266,20 @@ export function canTurnThreadIntoProject(threadRef: ScopedThreadRef): boolean {
260266
return project !== null && isScratchProject(project, config.scratchWorkspaceRoot);
261267
}
262268

269+
/** Projects in the thread's environment it can move to, on a server that supports moving. */
270+
export function threadMoveDestinationsFor(
271+
threadRef: ScopedThreadRef,
272+
): ReadonlyArray<{ readonly id: ProjectId; readonly label: string }> {
273+
const thread = readThreadShell(threadRef);
274+
const config = appAtomRegistry.get(environmentServerConfigsAtom).get(threadRef.environmentId);
275+
if (!thread || config?.environment.capabilities.threadProjectMove !== true) return [];
276+
return threadMoveDestinations(
277+
{ ...thread, worktreePath: thread.worktreePath ?? null },
278+
readProjects(),
279+
config.scratchWorkspaceRoot,
280+
).map((project) => ({ id: project.id, label: project.title }));
281+
}
282+
263283
function threadOperationFailure(target: ScopedThreadRef) {
264284
return readEnvironmentScope(target.environmentId, AuthOrchestrationOperateScope)
265285
? null
@@ -314,6 +334,9 @@ export function useThreadActions() {
314334
const createProjectFromThread = useAtomCommand(projectEnvironment.createFromThread, {
315335
reportFailure: false,
316336
});
337+
const moveThreadMutation = useAtomCommand(projectEnvironment.moveThread, {
338+
reportFailure: false,
339+
});
317340
const stopThreadSession = useOrchestrationCommand(threadEnvironment.stopSession);
318341
const removeWorktree = useAtomCommand(vcsEnvironment.removeWorktree, {
319342
reportFailure: false,
@@ -970,5 +993,28 @@ export function useThreadActions() {
970993
environmentId: target.environmentId,
971994
input: { threadId: target.threadId },
972995
}),
996+
moveThreadToProject: async (target: ScopedThreadRef, projectId: ProjectId) => {
997+
const result = await moveThreadMutation({
998+
environmentId: target.environmentId,
999+
input: { threadId: target.threadId, projectId },
1000+
});
1001+
if (result._tag === "Success") {
1002+
const project = readProject({ environmentId: target.environmentId, projectId });
1003+
toastManager.add({
1004+
type: "success",
1005+
title: "Thread moved",
1006+
description: project ? `Its next turn runs in ${project.title}.` : undefined,
1007+
});
1008+
} else if (!isAtomCommandInterrupted(result)) {
1009+
const error = squashAtomCommandFailure(result);
1010+
toastManager.add(
1011+
stackedThreadToast({
1012+
type: "error",
1013+
title: "Could not move thread",
1014+
description: error instanceof Error ? error.message : "An error occurred.",
1015+
}),
1016+
);
1017+
}
1018+
},
9731019
};
9741020
}

0 commit comments

Comments
 (0)