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