Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

index.tsx

218 lines7.5 KB

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