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

ThreadNavigationDrawer.tsx

261 lines9.2 KB

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