Open History to see the last commit
1import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
2import { useNavigation } from "@react-navigation/native";
3import { SymbolView } from "../../components/AppSymbol";
4import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
5import { useMemo } from "react";
6import { ActivityIndicator, Platform, Pressable, ScrollView, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8import { useThemeColor } from "../../lib/useThemeColor";
9import { cn } from "../../lib/cn";
10
11import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
12import { AppText as Text } from "../../components/AppText";
13import { ProjectFavicon } from "../../components/ProjectFavicon";
14import { useProjects, useThreadShells } from "../../state/entities";
15import type { WorkspaceState } from "../../state/workspaceModel";
16import { useWorkspaceState } from "../../state/workspace";
17import { groupProjectsByRepository } from "../../lib/repositoryGroups";
18import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
19
20function deriveProjectEmptyState(catalogState: WorkspaceState): {
21 readonly title: string;
22 readonly detail: string;
23 readonly loading: boolean;
24} {
25 if (catalogState.isLoadingConnections) {
26 return {
27 title: "Loading environments",
28 detail: "Checking saved environments on this device.",
29 loading: true,
30 };
31 }
32
33 if (!catalogState.hasConnections) {
34 return {
35 title: "No environments connected",
36 detail: "Add an environment before creating a task.",
37 loading: false,
38 };
39 }
40
41 if (
42 (catalogState.connectionState === "available" ||
43 catalogState.connectionState === "offline" ||
44 catalogState.connectionState === "error") &&
45 !catalogState.hasLoadedShellSnapshot
46 ) {
47 return {
48 title: "Environment unavailable",
49 detail:
50 catalogState.connectionError ??
51 "The saved environment is offline. Check the URL or start the environment, then retry.",
52 loading: false,
53 };
54 }
55
56 if (
57 catalogState.hasConnectingEnvironment &&
58 !catalogState.hasLoadedShellSnapshot &&
59 catalogState.connectionError === null
60 ) {
61 return {
62 title: "Connecting to environment",
63 detail: "Loading projects from the saved environment.",
64 loading: true,
65 };
66 }
67
68 return {
69 title: "No projects found",
70 detail: "The connected environment did not report any projects.",
71 loading: false,
72 };
73}
74
75export function NewTaskRouteScreen() {
76 const projects = useProjects();
77 const threads = useThreadShells();
78 const { state: catalogState } = useWorkspaceState();
79 const navigation = useNavigation();
80 const { layout } = useAdaptiveWorkspaceLayout();
81 const insets = useSafeAreaInsets();
82 const chevronColor = useThemeColor("--color-chevron");
83 const accentColor = useThemeColor("--color-icon-muted");
84 const repositoryGroups = useMemo(
85 () => groupProjectsByRepository({ projects, threads }),
86 [projects, threads],
87 );
88 const items = useMemo(() => {
89 const nextItems: Array<{
90 readonly environmentId: EnvironmentId;
91 readonly id: ProjectId;
92 readonly key: string;
93 readonly title: string;
94 readonly workspaceRoot: string;
95 }> = [];
96 for (const group of repositoryGroups) {
97 const project = group.projects[0]?.project;
98 if (!project) {
99 continue;
100 }
101 nextItems.push({
102 environmentId: project.environmentId,
103 id: project.id,
104 key: group.key,
105 title: project.title,
106 workspaceRoot: project.workspaceRoot,
107 });
108 }
109 return nextItems;
110 }, [repositoryGroups]);
111 const projectEmptyState = deriveProjectEmptyState(catalogState);
112
113 return (
114 <View collapsable={false} className="flex-1 bg-sheet">
115 {Platform.OS === "android" ? (
116 <>
117 {/* Android renders its own in-screen header instead of the native bar. */}
118 <NativeStackScreenOptions options={{ headerShown: false }} />
119 <AndroidScreenHeader
120 title="Choose project"
121 onBack={layout.usesSplitView ? () => navigation.goBack() : undefined}
122 actions={[
123 {
124 accessibilityLabel: "Add project",
125 icon: "plus",
126 onPress: () => navigation.navigate("NewTaskSheet", { screen: "AddProject" }),
127 },
128 ]}
129 />
130 </>
131 ) : (
132 <NativeHeaderToolbar placement="right">
133 {layout.usesSplitView ? (
134 <NativeHeaderToolbar.Button
135 accessibilityLabel="Close new task"
136 icon="xmark"
137 onPress={() => navigation.goBack()}
138 separateBackground
139 />
140 ) : null}
141 <NativeHeaderToolbar.Button
142 icon="plus"
143 onPress={() => navigation.navigate("NewTaskSheet", { screen: "AddProject" })}
144 separateBackground
145 />
146 </NativeHeaderToolbar>
147 )}
148
149 <ScrollView
150 contentInsetAdjustmentBehavior="automatic"
151 showsVerticalScrollIndicator={false}
152 className="flex-1"
153 contentInset={{ bottom: Math.max(insets.bottom, 18) + 18 }}
154 contentContainerStyle={{
155 paddingHorizontal: 20,
156 paddingTop: 8,
157 }}
158 >
159 {items.length === 0 ? (
160 <View collapsable={false} className="items-center gap-3 rounded-[24px] bg-card px-6 py-8">
161 {projectEmptyState.loading ? <ActivityIndicator color={accentColor} /> : null}
162 <Text className="text-center text-lg font-t3-bold text-foreground">
163 {projectEmptyState.title}
164 </Text>
165 <Text className="text-center text-sm leading-normal text-foreground-muted">
166 {projectEmptyState.detail}
167 </Text>
168 {!catalogState.hasReadyEnvironment ? (
169 <Pressable
170 className="mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
171 onPress={() => navigation.navigate("ConnectionsNew")}
172 >
173 <Text className="text-sm font-t3-bold text-primary-foreground">
174 Add environment
175 </Text>
176 </Pressable>
177 ) : (
178 <Pressable
179 className="mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
180 onPress={() => navigation.navigate("NewTaskSheet", { screen: "AddProject" })}
181 >
182 <Text className="text-sm font-t3-bold text-primary-foreground">
183 Add new project
184 </Text>
185 </Pressable>
186 )}
187 </View>
188 ) : (
189 <View collapsable={false} className="overflow-hidden rounded-[24px] bg-card">
190 {items.map((item, index) => {
191 const isFirst = index === 0;
192 const isLast = index === items.length - 1;
193
194 return (
195 <Pressable
196 key={item.key}
197 onPress={() =>
198 navigation.navigate("NewTaskSheet", {
199 screen: "NewTaskDraft",
200 params: {
201 environmentId: item.environmentId,
202 projectId: item.id,
203 title: item.title,
204 },
205 })
206 }
207 className={cn(
208 "bg-card px-4 py-3.5",
209 !isFirst && "border-t border-border-subtle",
210 isFirst && "rounded-t-[24px]",
211 isLast && "rounded-b-[24px]",
212 )}
213 >
214 <View className="flex-row items-center justify-between gap-3">
215 <View className="h-7 w-7 items-center justify-center">
216 <ProjectFavicon
217 environmentId={item.environmentId}
218 size={20}
219 projectTitle={item.title}
220 workspaceRoot={item.workspaceRoot}
221 />
222 </View>
223 <View className="flex-1">
224 <Text className="text-base leading-snug font-t3-bold">{item.title}</Text>
225 </View>
226 <SymbolView
227 name="chevron.right"
228 size={14}
229 tintColor={chevronColor}
230 type="monochrome"
231 />
232 </View>
233 </Pressable>
234 );
235 })}
236 </View>
237 )}
238 </ScrollView>
239 </View>
240 );
241}