Skip to content

Commit f921b9b

Browse files
feat(web,mobile): continue a thread on a linked environment
1 parent 1e24145 commit f921b9b

39 files changed

Lines changed: 1987 additions & 93 deletions

‎apps/mobile/src/Stack.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ThreadHandoffSheet } from "./features/threads/ThreadHandoffSheet";
12
import { ComponentCatalogScreen } from "./features/development/ComponentCatalogScreen";
23
import { PullRequestGuideStopScreen } from "./features/pull-requests/guide/PullRequestGuideScreen";
34
import { PullRequestInlineCommentScreen } from "./features/pull-requests/PullRequestInlineCommentScreen";
@@ -550,6 +551,7 @@ const WORKSPACE_OVERLAY_ROUTES = new Set([
550551
"NewTaskSheet",
551552
"SettingsLegal",
552553
"SettingsSheet",
554+
"ThreadHandoff",
553555
"ThreadAgents",
554556
"ThreadPullRequests",
555557
"ThreadQueue",
@@ -855,6 +857,15 @@ const RootStackConfig = createWorkspaceStackNavigator({
855857
sheetGrabberVisible: true,
856858
},
857859
}),
860+
ThreadHandoff: createNativeStackScreen({
861+
screen: ThreadHandoffSheet,
862+
options: {
863+
...FORM_SHEET_PRESENTATION_OPTIONS,
864+
headerShown: false,
865+
sheetAllowedDetents: [0.65, 0.95],
866+
sheetGrabberVisible: true,
867+
},
868+
}),
858869
ThreadAgents: createNativeStackScreen({
859870
screen: ThreadAgentsSheet,
860871
options: {

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ import {
1111
threadSearchMatchKey,
1212
type EnvironmentThreadSearchMatch,
1313
} from "@t3tools/client-runtime/state/thread-search";
14-
import { type EnvironmentId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
14+
import {
15+
type EnvironmentId,
16+
type SidebarProjectGroupingMode,
17+
type ScopedThreadRef,
18+
} from "@t3tools/contracts";
1519
import { useAtomValue } from "@effect/atom-react";
16-
import { useFocusEffect } from "@react-navigation/native";
20+
import { useFocusEffect, useNavigation } from "@react-navigation/native";
1721
import { use, useEffect, useRef, useState, type ComponentProps } from "react";
1822
import {
1923
Platform,
@@ -250,6 +254,10 @@ function renderHomeScrollView(props: ScrollViewProps) {
250254
}
251255

252256
export function HomeScreen(props: HomeScreenProps) {
257+
const navigation = useNavigation();
258+
const handleContinueOn = (ref: ScopedThreadRef) => {
259+
navigation.navigate("ThreadHandoff", ref);
260+
};
253261
const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported();
254262
const workspace = useWorkspaceTabs();
255263
const [initialScrollOffset] = useState(() => workspace?.getScrollOffset("threads") ?? 0);
@@ -631,6 +639,7 @@ export function HomeScreen(props: HomeScreenProps) {
631639
selected={scopedThreadKey(thread.environmentId, thread.id) === selectedThreadKey}
632640
fullSwipeWidth={fullSwipeWidth}
633641
onNewThreadOnBranch={props.onNewThreadOnBranch}
642+
onContinueOn={handleContinueOn}
634643
thread={thread}
635644
variant={item.item.variant}
636645
hasQueuedMessages={item.hasQueuedMessages}

‎apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type {
22
EnvironmentProject,
33
EnvironmentThreadShell,
44
} from "@t3tools/client-runtime/state/shell";
5-
import { EnvironmentId, ThreadId, type SidebarProjectGroupingMode } from "@t3tools/contracts";
5+
import {
6+
EnvironmentId,
7+
ThreadId,
8+
type SidebarProjectGroupingMode,
9+
type ScopedThreadRef,
10+
} from "@t3tools/contracts";
611
import { useAtomValue } from "@effect/atom-react";
712
import { useFocusEffect } from "@react-navigation/native";
813
import {
@@ -416,6 +421,10 @@ function AdaptiveWorkspaceLayoutContent(
416421
});
417422
};
418423

424+
const handleContinueOn = (ref: ScopedThreadRef) => {
425+
navigation.navigate("ThreadHandoff", ref);
426+
};
427+
419428
const handleOpenThreadPullRequests = useOpenThreadPullRequests();
420429

421430
const handleNewThreadOnBranch = (thread: EnvironmentThreadShell) => {
@@ -683,6 +692,7 @@ function AdaptiveWorkspaceLayoutContent(
683692
onOpenEnvironmentSettings={handleOpenEnvironmentSettings}
684693
onNewThreadInProject={handleNewThreadInProject}
685694
onNewThreadOnBranch={handleNewThreadOnBranch}
695+
onContinueOn={handleContinueOn}
686696
onSelectThread={handleSelectThread}
687697
onOpenThreadPullRequests={handleOpenThreadPullRequests}
688698
onSearchQueryChange={setPrimarySidebarSearchQuery}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ThreadHandoffCard } from "./ThreadHandoffCard";
2+
import { threadHandoffIsReadOnly } from "@t3tools/client-runtime/state/threads";
13
import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
24
import { AsyncResult } from "effect/reactivity";
35
import * as Dialog from "../../components/ui/Dialog";
@@ -612,7 +614,9 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
612614
useState<RuntimeRequestId | null>(null);
613615
const activeUserInputRequestId = props.activePendingUserInput?.requestId ?? null;
614616
const composerSlotHidden =
615-
activeUserInputRequestId !== null || props.creationState?.kind === "failed";
617+
activeUserInputRequestId !== null ||
618+
props.creationState?.kind === "failed" ||
619+
threadHandoffIsReadOnly(props.selectedThread);
616620
const [usageLimitsPanel, setUsageLimitsPanel] = useState<{
617621
readonly key: string;
618622
readonly threadKey: string;
@@ -1284,6 +1288,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
12841288
) : null}
12851289
</View>
12861290

1291+
<ThreadHandoffCard thread={props.selectedThread} />
12871292
{/*
12881293
* Hidden (not unmounted) while a user-input request owns the composer slot, so
12891294
* composer drafts and editor state survive.

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function compactMenuStatus(gitStatus: VcsStatusResult | null): string {
7272
type HeaderItem = Record<string, unknown>;
7373
type HeaderItems = HeaderItem[];
7474
type ThreadGitHeaderActionItems = {
75+
readonly handoff: HeaderItem | null;
7576
readonly terminal: HeaderItem;
7677
readonly files: HeaderItem;
7778
readonly git: HeaderItem;
@@ -96,6 +97,7 @@ export type ThreadGitMenuProps = {
9697
};
9798

9899
type ThreadGitControlsProps = ThreadGitMenuProps & {
100+
readonly onContinueOn?: () => void;
99101
readonly auxiliaryPaneControl?: {
100102
readonly accessibilityLabel: string;
101103
readonly onPress: () => void;
@@ -264,6 +266,17 @@ function useThreadGitHeaderActionItems(props: ThreadGitControlsProps): ThreadGit
264266
const model = useThreadGitControlModel(props);
265267

266268
return {
269+
handoff:
270+
props.onContinueOn !== undefined
271+
? {
272+
type: "button" as const,
273+
identifier: "thread-continue-on",
274+
accessibilityLabel: "Continue on…",
275+
label: "Continue on…",
276+
icon: { name: "arrow.right", type: "sfSymbol" as const },
277+
onPress: props.onContinueOn,
278+
}
279+
: null,
267280
terminal: {
268281
accessibilityLabel: "Open terminal",
269282
disabled: !props.canOpenTerminal,
@@ -397,12 +410,22 @@ function useThreadGitHeaderActionItems(props: ThreadGitControlsProps): ThreadGit
397410

398411
export function useThreadGitRightHeaderItems(props: ThreadGitControlsProps): HeaderItems {
399412
const actionItems = useThreadGitHeaderActionItems(props);
400-
return [actionItems.git, actionItems.files, actionItems.terminal] as HeaderItems;
413+
return [
414+
...(actionItems.handoff ? [actionItems.handoff] : []),
415+
actionItems.git,
416+
actionItems.files,
417+
actionItems.terminal,
418+
] as HeaderItems;
401419
}
402420

403421
export function useThreadGitCenterHeaderItems(props: ThreadGitControlsProps): HeaderItems {
404422
const actionItems = useThreadGitHeaderActionItems(props);
405-
return [actionItems.files, actionItems.git, actionItems.terminal] as HeaderItems;
423+
return [
424+
...(actionItems.handoff ? [actionItems.handoff] : []),
425+
actionItems.files,
426+
actionItems.git,
427+
actionItems.terminal,
428+
] as HeaderItems;
406429
}
407430

408431
export function ThreadGitControls(props: ThreadGitControlsProps) {
@@ -415,6 +438,13 @@ export function ThreadGitControls(props: ThreadGitControlsProps) {
415438

416439
return (
417440
<Toolbar placement="right">
441+
{props.onContinueOn !== undefined ? (
442+
<Toolbar.Button
443+
accessibilityLabel="Continue on…"
444+
icon="arrow.right"
445+
onPress={props.onContinueOn}
446+
/>
447+
) : null}
418448
{showActionControls && props.auxiliaryPaneControl ? (
419449
<Toolbar.Button
420450
accessibilityLabel={props.auxiliaryPaneControl.accessibilityLabel}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
import { useNavigation } from "@react-navigation/native";
3+
import { threadHandoffNotice } from "@t3tools/client-runtime/state/threads";
4+
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
5+
import {
6+
isAtomCommandInterrupted,
7+
squashAtomCommandFailure,
8+
} from "@t3tools/client-runtime/state/runtime";
9+
import { useEffect, useRef, useState } from "react";
10+
import { View } from "react-native";
11+
import { AppText as Text } from "../../components/ui/AppText";
12+
import { Button } from "../../components/ui/Button";
13+
import * as Dialog from "../../components/ui/Dialog";
14+
import { appAtomRegistry } from "../../state/atom-registry";
15+
import { environmentPresentations } from "../../state/presentation";
16+
import { environmentThreadShells } from "../../state/threads";
17+
import { threadHandoffEnvironment } from "../../state/threadHandoff";
18+
19+
export function ThreadHandoffCard({ thread }: { thread: EnvironmentThreadShell }) {
20+
return thread.handoff ? (
21+
<Notice
22+
key={`${thread.environmentId}:${thread.id}:${thread.handoff.handoffId}`}
23+
thread={thread}
24+
/>
25+
) : null;
26+
}
27+
function Notice({ thread }: { thread: EnvironmentThreadShell }) {
28+
const handoff = thread.handoff!;
29+
const ref = { environmentId: thread.environmentId, threadId: thread.id };
30+
const notice = threadHandoffNotice(handoff)!;
31+
const canCancel = useAtomValue(
32+
threadHandoffEnvironment.cancel.permissionAtom(thread.environmentId),
33+
);
34+
const canRetry = useAtomValue(
35+
threadHandoffEnvironment.start.permissionAtom(thread.environmentId),
36+
);
37+
const presentations = useAtomValue(environmentPresentations.presentationsAtom);
38+
const sourceConnected = presentations.get(thread.environmentId)?.connection.phase === "connected";
39+
const navigation = useNavigation();
40+
const [busy, setBusy] = useState(false);
41+
const [error, setError] = useState<string | null>(null);
42+
const working = useRef(false);
43+
const alive = useRef(true);
44+
useEffect(() => {
45+
alive.current = true;
46+
return () => {
47+
alive.current = false;
48+
};
49+
}, []);
50+
const current = () => {
51+
const live = appAtomRegistry.get(environmentThreadShells.threadShellAtom(ref))?.handoff;
52+
return alive.current && live?.handoffId === handoff.handoffId && live.state === handoff.state;
53+
};
54+
const act = async (action: "cancel" | "retry" | "abandon") => {
55+
if (!current() || working.current) return;
56+
const command =
57+
action === "retry" ? threadHandoffEnvironment.start : threadHandoffEnvironment.cancel;
58+
if (!appAtomRegistry.get(command.permissionAtom(thread.environmentId))) return;
59+
working.current = true;
60+
setBusy(true);
61+
setError(null);
62+
try {
63+
const result =
64+
action === "retry"
65+
? await threadHandoffEnvironment.start.run(appAtomRegistry, {
66+
environmentId: thread.environmentId,
67+
input: {
68+
threadId: thread.id,
69+
environmentId: handoff.environmentId,
70+
projectId: handoff.projectId,
71+
handoffId: handoff.handoffId,
72+
isCurrent: current,
73+
},
74+
})
75+
: await threadHandoffEnvironment.cancel.run(appAtomRegistry, {
76+
environmentId: thread.environmentId,
77+
input: {
78+
threadId: thread.id,
79+
handoffId: handoff.handoffId,
80+
isCurrent: current,
81+
...(action === "abandon" ? { abandon: true } : {}),
82+
},
83+
});
84+
if (current() && result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
85+
const failure = squashAtomCommandFailure(result);
86+
setError(
87+
failure instanceof Error ? failure.message : "Could not update the move. Try again.",
88+
);
89+
}
90+
} finally {
91+
if (alive.current) {
92+
working.current = false;
93+
setBusy(false);
94+
}
95+
}
96+
};
97+
const abandon = () =>
98+
Dialog.confirm({
99+
title: "Continue here?",
100+
message:
101+
"The move outcome is unknown. The destination may already have a copy or finish accepting it later. Continue working here anyway?",
102+
confirmText: "Continue here",
103+
destructive: true,
104+
onConfirm: () => {
105+
if (current()) void act("abandon");
106+
},
107+
});
108+
return (
109+
<View className="gap-3 rounded-2xl border border-border bg-card p-4">
110+
<Text className="font-t3-medium">{notice.title}</Text>
111+
<Text className="text-sm text-foreground-muted">{notice.detail}</Text>
112+
{handoff.state === "pending" ? (
113+
<Button
114+
label="Cancel move"
115+
disabled={!canCancel || !sourceConnected || busy}
116+
onPress={() => void act("cancel")}
117+
/>
118+
) : null}
119+
{handoff.state === "departing" ? (
120+
<>
121+
<Button
122+
label="Retry move"
123+
disabled={!canRetry || !sourceConnected || busy}
124+
onPress={() => void act("retry")}
125+
/>
126+
<Button
127+
label="Continue here…"
128+
tone="text"
129+
disabled={!canCancel || !sourceConnected || busy}
130+
onPress={abandon}
131+
/>
132+
</>
133+
) : null}
134+
{handoff.state === "departed" &&
135+
presentations.get(handoff.environmentId)?.connection.phase === "connected" ? (
136+
<Button
137+
label={`Open on ${handoff.label}`}
138+
onPress={() => {
139+
if (
140+
!current() ||
141+
appAtomRegistry.get(environmentPresentations.presentationAtom(handoff.environmentId))
142+
?.connection.phase !== "connected"
143+
)
144+
return;
145+
navigation.navigate("Thread", {
146+
environmentId: handoff.environmentId,
147+
threadId: handoff.threadId,
148+
});
149+
}}
150+
/>
151+
) : null}
152+
{error ? (
153+
<Text accessibilityRole="alert" className="text-sm text-danger">
154+
{error}
155+
</Text>
156+
) : null}
157+
</View>
158+
);
159+
}

0 commit comments

Comments
 (0)