Open History to see the last commit
1import { SymbolView } from "expo-symbols";
2import { useCallback, useEffect, useMemo, useState } from "react";
3import {
4 type ColorValue,
5 Modal,
6 Pressable,
7 ScrollView,
8 useWindowDimensions,
9 View,
10} from "react-native";
11import * as Arr from "effect/Array";
12import * as Order from "effect/Order";
13import { Gesture, GestureDetector } from "react-native-gesture-handler";
14import { useSafeAreaInsets } from "react-native-safe-area-context";
15import Animated, {
16 runOnJS,
17 useAnimatedStyle,
18 useSharedValue,
19 withTiming,
20} from "react-native-reanimated";
21import { useThemeColor } from "../../lib/useThemeColor";
22
23import { AppText as Text } from "../../components/AppText";
24import { StatusPill } from "../../components/StatusPill";
25import { useProjects, useThreadShells } from "../../state/entities";
26import { groupProjectsByRepository } from "../../lib/repositoryGroups";
27import { scopedThreadKey } from "../../lib/scopedEntities";
28import { relativeTime } from "../../lib/time";
29import { threadStatusTone } from "./threadPresentation";
30import { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
31
32const threadActivityOrder = Order.mapInput(
33 Order.Struct({
34 activityAt: Order.flip(Order.Number),
35 title: Order.String,
36 }),
37 (thread: EnvironmentThreadShell) => ({
38 activityAt: new Date(thread.updatedAt ?? thread.createdAt).getTime(),
39 title: thread.title,
40 }),
41);
42
43export function ThreadNavigationDrawer(props: {
44 readonly visible: boolean;
45 readonly selectedThreadKey: string | null;
46 readonly onClose: () => void;
47 readonly onSelectThread: (thread: EnvironmentThreadShell) => void;
48 readonly onStartNewTask: () => void;
49}) {
50 const insets = useSafeAreaInsets();
51 const { width } = useWindowDimensions();
52 const drawerWidth = Math.min(width * 0.84, 360);
53 const [mounted, setMounted] = useState(props.visible);
54 const translateX = useSharedValue(-drawerWidth);
55 const overlayOpacity = useSharedValue(0);
56
57 const backdropColor = useThemeColor("--color-backdrop");
58 const drawerBg = useThemeColor("--color-drawer");
59 const drawerShadow = useThemeColor("--color-drawer-shadow");
60 const primaryForeground = useThemeColor("--color-primary-foreground");
61 const borderSubtleColor = useThemeColor("--color-border-subtle");
62
63 useEffect(() => {
64 if (props.visible) {
65 setMounted(true);
66 translateX.value = withTiming(0, { duration: 240 });
67 overlayOpacity.value = withTiming(1, { duration: 220 });
68 return;
69 }
70
71 overlayOpacity.value = withTiming(0, { duration: 180 });
72 translateX.value = withTiming(-drawerWidth, { duration: 220 }, (finished) => {
73 if (finished) {
74 runOnJS(setMounted)(false);
75 }
76 });
77 }, [drawerWidth, overlayOpacity, props.visible, translateX]);
78
79 const closeDrawer = useCallback(() => {
80 props.onClose();
81 }, [props]);
82
83 const panGesture = useMemo(
84 () =>
85 Gesture.Pan()
86 .activeOffsetX([-12, 12])
87 .failOffsetY([-24, 24])
88 .onUpdate((event) => {
89 translateX.value = Math.min(0, event.translationX);
90 })
91 .onEnd((event) => {
92 const shouldClose = event.translationX < -drawerWidth * 0.2 || event.velocityX < -500;
93 if (shouldClose) {
94 runOnJS(closeDrawer)();
95 return;
96 }
97
98 translateX.value = withTiming(0, { duration: 180 });
99 }),
100 [closeDrawer, drawerWidth, translateX],
101 );
102
103 const drawerStyle = useAnimatedStyle(() => ({
104 transform: [{ translateX: translateX.value }],
105 }));
106
107 const backdropStyle = useAnimatedStyle(() => ({
108 opacity: overlayOpacity.value,
109 }));
110
111 if (!mounted) {
112 return null;
113 }
114
115 return (
116 <Modal transparent visible={mounted} onRequestClose={props.onClose} statusBarTranslucent>
117 <View style={{ flex: 1 }}>
118 <Animated.View
119 pointerEvents="none"
120 style={[
121 {
122 position: "absolute",
123 left: 0,
124 right: 0,
125 top: 0,
126 bottom: 0,
127 backgroundColor: backdropColor,
128 },
129 backdropStyle,
130 ]}
131 />
132 <Pressable
133 style={{ position: "absolute", left: 0, right: 0, top: 0, bottom: 0 }}
134 onPress={props.onClose}
135 />
136
137 <GestureDetector gesture={panGesture}>
138 <Animated.View
139 style={[
140 {
141 position: "absolute",
142 left: 0,
143 top: 0,
144 bottom: 0,
145 width: drawerWidth,
146 backgroundColor: drawerBg,
147 paddingTop: insets.top + 10,
148 paddingBottom: Math.max(insets.bottom, 18),
149 boxShadow: `20px 0 36px ${String(drawerShadow)}`,
150 },
151 drawerStyle,
152 ]}
153 >
154 <View className="flex-row items-center justify-between px-4 pb-5">
155 <Text className="text-2xl font-t3-bold">Threads</Text>
156 <Pressable
157 onPress={() => {
158 props.onClose();
159 props.onStartNewTask();
160 }}
161 className="h-11 w-11 items-center justify-center rounded-full bg-primary"
162 >
163 <SymbolView
164 name="square.and.pencil"
165 size={17}
166 tintColor={primaryForeground}
167 type="monochrome"
168 />
169 </Pressable>
170 </View>
171
172 <ThreadNavigationDrawerContent
173 bottomInset={Math.max(insets.bottom, 18)}
174 borderSubtleColor={borderSubtleColor}
175 selectedThreadKey={props.selectedThreadKey}
176 onClose={props.onClose}
177 onSelectThread={props.onSelectThread}
178 />
179 </Animated.View>
180 </GestureDetector>
181 </View>
182 </Modal>
183 );
184}
185
186function ThreadNavigationDrawerContent(props: {
187 readonly bottomInset: number;
188 readonly borderSubtleColor: ColorValue;
189 readonly selectedThreadKey: string | null;
190 readonly onClose: () => void;
191 readonly onSelectThread: (thread: EnvironmentThreadShell) => void;
192}) {
193 const projects = useProjects();
194 const threads = useThreadShells();
195 const repositoryGroups = useMemo(
196 () => groupProjectsByRepository({ projects, threads }),
197 [projects, threads],
198 );
199 const groupedThreads = useMemo(
200 () =>
201 repositoryGroups.map((group) => {
202 const threads: EnvironmentThreadShell[] = [];
203 for (const projectGroup of group.projects) {
204 threads.push(...projectGroup.threads);
205 }
206 return {
207 key: group.key,
208 title: group.projects[0]?.project.title ?? group.title,
209 threads: Arr.sort(threads, threadActivityOrder),
210 };
211 }),
212 [repositoryGroups],
213 );
214
215 return (
216 <ScrollView
217 showsVerticalScrollIndicator={false}
218 contentInset={{ bottom: props.bottomInset + 12 }}
219 contentContainerStyle={{
220 gap: 20,
221 paddingHorizontal: 14,
222 }}
223 >
224 {groupedThreads.map((group) => (
225 <View key={group.key} className="gap-3">
226 <Text
227 className="px-1 text-base font-t3-bold text-foreground-muted"
228 style={{ letterSpacing: -0.2 }}
229 >
230 {group.title}
231 </Text>
232
233 <View className="overflow-hidden rounded-[22px] bg-card">
234 {group.threads.length === 0 ? (
235 <View className="px-4 py-4">
236 <Text className="text-sm font-medium text-foreground-tertiary">No threads yet</Text>
237 </View>
238 ) : (
239 group.threads.map((thread, index) => {
240 const threadKey = scopedThreadKey(thread.environmentId, thread.id);
241 const selected = props.selectedThreadKey === threadKey;
242
243 return (
244 <Pressable
245 key={threadKey}
246 onPress={() => {
247 props.onSelectThread(thread);
248 props.onClose();
249 }}
250 style={{
251 paddingHorizontal: 16,
252 paddingVertical: 15,
253 borderTopWidth: index === 0 ? 0 : 1,
254 borderTopColor: props.borderSubtleColor,
255 backgroundColor: selected ? undefined : "transparent",
256 }}
257 className={selected ? "bg-subtle" : undefined}
258 >
259 <View className="flex-row items-start justify-between gap-3">
260 <View className="flex-1 gap-1">
261 <Text className="text-base font-t3-bold" numberOfLines={1}>
262 {thread.title}
263 </Text>
264 <Text
265 className="text-sm font-medium text-foreground-muted"
266 numberOfLines={1}
267 >
268 {relativeTime(thread.updatedAt ?? thread.createdAt)}
269 </Text>
270 </View>
271 <StatusPill {...threadStatusTone(thread)} />
272 </View>
273 </Pressable>
274 );
275 })
276 )}
277 </View>
278 </View>
279 ))}
280 </ScrollView>
281 );
282}