Open History to see the last commit
1import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
3import { scopeProjectRef } from "@t3tools/client-runtime/environment";
4import { threadCanStartHandoff } from "@t3tools/client-runtime/state/peerLinks";
5import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
6import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
7import type { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
8import { T, useGT, useMessages } from "gt-react-native";
9import { useState } from "react";
10import { Platform, Pressable, ScrollView, View } from "react-native";
11import { useSafeAreaInsets } from "react-native-safe-area-context";
12
13import { AppBar } from "../../components/ui/AppBar";
14import { AppText as Text } from "../../components/ui/AppText";
15import { Button } from "../../components/ui/Button";
16import { ScreenContent } from "../../components/ui/ScreenContent";
17import { SheetHeader } from "../../components/ui/SheetHeader";
18import { useProject, useProjects, useThreadShell } from "../../state/entities";
19import { projectEnvironment } from "../../state/projects";
20import { useAtomCommand } from "../../state/use-atom-command";
21import { useCanMoveThreadWorkspace, type useMissingWorkspace } from "./useThreadWorkspace";
22
23type WorkspaceTarget = {
24 readonly environmentId: EnvironmentId;
25 readonly threadId: ThreadId;
26 readonly mode: "project" | "scratch";
27};
28
29export function MissingWorkspaceCard({
30 thread,
31 workspace,
32}: {
33 readonly thread: EnvironmentThreadShell;
34 readonly workspace: NonNullable<ReturnType<typeof useMissingWorkspace>>;
35}) {
36 const gt = useGT();
37 const navigation = useNavigation();
38 const permitted = useCanMoveThreadWorkspace(thread.environmentId);
39 const canMove =
40 permitted &&
41 thread.archivedAt === null &&
42 thread.lineage.relationshipToParent !== "subagent" &&
43 threadCanStartHandoff(thread.handoff);
44 const open = (mode: WorkspaceTarget["mode"]) =>
45 navigation.navigate("ThreadWorkspace", {
46 environmentId: thread.environmentId,
47 threadId: thread.id,
48 mode,
49 });
50
51 return (
52 <View className="mb-2 gap-2 rounded-2xl border border-warning-border bg-warning p-3">
53 <Text accessibilityRole="alert" className="font-t3-medium text-warning-foreground">
54 <T>Workspace folder missing</T>
55 </Text>
56 <Text className="font-mono text-xs text-warning-foreground">{workspace.path}</Text>
57 {canMove ? (
58 <View className="gap-2">
59 <Button
60 label={workspace.changeProject ? gt`Change project folder` : gt`Choose folder`}
61 onPress={() =>
62 navigation.dispatch(
63 StackActions.push("ThreadWorkspaceFolder", {
64 environmentId: thread.environmentId,
65 threadId: thread.id,
66 mode: workspace.changeProject ? "project-directory" : "directory",
67 }),
68 )
69 }
70 />
71 <Button label={gt`Continue without project`} onPress={() => open("scratch")} />
72 </View>
73 ) : null}
74 <Button
75 label={workspace.checking ? gt`Checking…` : gt`Check again`}
76 disabled={workspace.checking}
77 tone="text"
78 onPress={workspace.check}
79 />
80 </View>
81 );
82}
83
84export function ThreadWorkspaceSheet({ route }: StaticScreenProps<WorkspaceTarget>) {
85 const { environmentId, threadId, mode } = route.params;
86 const gt = useGT();
87 const m = useMessages();
88 const navigation = useNavigation();
89 const insets = useSafeAreaInsets();
90 const thread = useThreadShell({ environmentId, threadId });
91 const current = useProject(thread ? scopeProjectRef(environmentId, thread.projectId) : null);
92 const projects = useProjects().filter(
93 (project) =>
94 project.environmentId === environmentId &&
95 project.archivedAt == null &&
96 !project.isScratch &&
97 (project.id !== thread?.projectId || thread.worktreePath !== null),
98 );
99 const move = useAtomCommand(projectEnvironment.moveThreadWorkspace, { reportFailure: false });
100 const [selection, setSelection] = useState<ProjectId | "scratch" | null>(null);
101 const [pending, setPending] = useState(false);
102 const [error, setError] = useState<string | null>(null);
103 const source = thread?.worktreePath ?? current?.workspaceRoot;
104 const close = () => navigation.goBack();
105 const title = mode === "scratch" ? gt`Continue without project` : gt`Move thread to project`;
106 const submit = async () => {
107 const target =
108 mode === "scratch" || selection === "scratch"
109 ? ({ type: "scratch" } as const)
110 : selection === null
111 ? null
112 : ({ type: "project", projectId: selection } as const);
113 if (target === null || pending) return;
114 setPending(true);
115 setError(null);
116 const result = await move({ environmentId, input: { threadId, target } });
117 setPending(false);
118 if (result._tag === "Failure") {
119 setError(
120 errorDisplayMessage(squashAtomCommandFailure(result), m) ??
121 gt`Could not change the workspace.`,
122 );
123 } else if (navigation.isFocused()) {
124 close();
125 }
126 };
127 const content = (
128 <ScrollView
129 className="flex-1"
130 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
131 contentContainerClassName="gap-4 px-5 pt-4"
132 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) }}
133 keyboardShouldPersistTaps="handled"
134 >
135 <Text className="text-sm text-foreground-secondary">
136 {mode === "scratch" ? (
137 <T>This thread will continue in an empty folder. Its conversation stays here.</T>
138 ) : (
139 <T>The conversation stays with the thread. Files stay in their current folder.</T>
140 )}
141 </Text>
142 {mode === "project" && source ? (
143 <View className="gap-1">
144 <Text className="font-t3-medium">
145 <T>Current folder</T>
146 </Text>
147 <Text className="font-mono text-xs text-foreground-secondary">{source}</Text>
148 </View>
149 ) : null}
150 {mode === "project" ? (
151 <View className="gap-2">
152 {projects.map((project) => (
153 <Pressable
154 key={project.id}
155 accessibilityRole="radio"
156 accessibilityState={{ checked: selection === project.id }}
157 disabled={pending}
158 onPress={() => setSelection(project.id)}
159 className={
160 selection === project.id
161 ? "rounded-xl border border-primary bg-subtle p-3"
162 : "rounded-xl border border-subtle-strong p-3"
163 }
164 >
165 <Text className="font-t3-medium">{project.title}</Text>
166 <Text className="font-mono text-xs text-foreground-secondary">
167 {project.workspaceRoot}
168 </Text>
169 </Pressable>
170 ))}
171 {!current?.isScratch ? (
172 <Pressable
173 accessibilityRole="radio"
174 accessibilityState={{ checked: selection === "scratch" }}
175 disabled={pending}
176 onPress={() => setSelection("scratch")}
177 className={
178 selection === "scratch"
179 ? "rounded-xl border border-primary bg-subtle p-3"
180 : "rounded-xl border border-subtle-strong p-3"
181 }
182 >
183 <Text className="font-t3-medium">
184 <T>No project</T>
185 </Text>
186 </Pressable>
187 ) : null}
188 </View>
189 ) : null}
190 {error ? (
191 <Text accessibilityRole="alert" className="text-sm text-danger-foreground">
192 {error}
193 </Text>
194 ) : null}
195 <Button
196 label={mode === "scratch" ? gt`Continue` : gt`Move thread`}
197 tone="primary"
198 onPress={() => void submit()}
199 loading={pending}
200 disabled={mode === "project" && selection === null}
201 fullWidth
202 />
203 </ScrollView>
204 );
205 return Platform.OS === "ios" ? (
206 <View collapsable={false} className="flex-1 bg-sheet">
207 <SheetHeader presentation="native" title={title} onClose={close} />
208 {content}
209 </View>
210 ) : (
211 <View collapsable={false} className="flex-1 bg-header">
212 <AppBar sheet title={title} hideBottomBorder onBack={close} />
213 <ScreenContent>{content}</ScreenContent>
214 </View>
215 );
216}