@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 458 lines 16.6 KB
Raw
1 import { NativeScrollView } from "../../native/NativeScrollViewMarker";
2 import { Button } from "../../components/ui/Button";
3 import type {
4 EnvironmentProject,
5 EnvironmentThreadShell,
6 } from "@t3tools/client-runtime/state/shell";
7 import { LegendList } from "@legendapp/list/react-native";
8 import {
9 AuthOrchestrationOperateScope,
10 type EnvironmentId,
11 type EnvironmentMachineKind,
12 resolveEnvironmentMachineKind,
13 } from "@t3tools/contracts";
14 import { useNavigation } from "@react-navigation/native";
15 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
16 import { AppBar } from "../../components/ui/AppBar";
17 import { SymbolView } from "../../components/ui/AppSymbol";
18 import { T, useGT } from "gt-react-native";
19 import { useMemo, useRef, type ComponentProps } from "react";
20 import { useWindowDimensions, View } from "react-native";
21 import { RefreshControl } from "../../components/ui/RefreshControl";
22 import { GestureDetector, useNativeGesture } from "react-native-gesture-handler";
23 import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable";
24
25 import { AppText as Text } from "../../components/ui/AppText";
26 import { EmptyState } from "../../components/ui/EmptyState";
27 import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
28 import { ProjectFavicon } from "../../components/ProjectFavicon";
29 import { RelativeTime } from "../../components/RelativeTime";
30 import { useUniwindTheme } from "../../lib/useUniwindTheme";
31 import { useServerConfigs } from "../../state/entities";
32 import { useEnvironmentScope } from "../../state/session";
33 import { ThreadSwipeable } from "../home/thread-swipe-actions";
34 import type { ArchivedThreadGroup, ArchivedThreadSortOrder } from "./archivedThreadList";
35 import { SettingsScreenContent } from "../settings/components/SettingsScreen";
36
37 export interface ArchivedThreadsHeaderEnvironment {
38 readonly environmentId: EnvironmentId;
39 readonly label: string;
40 }
41
42 function ArchivedThreadsHeader(props: {
43 readonly environments: ReadonlyArray<ArchivedThreadsHeaderEnvironment>;
44 readonly searchQuery: string;
45 readonly selectedEnvironmentId: EnvironmentId | null;
46 readonly sortOrder: ArchivedThreadSortOrder;
47 readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void;
48 readonly onRefresh: () => void;
49 readonly onSearchQueryChange: (query: string) => void;
50 readonly onSortOrderChange: (sortOrder: ArchivedThreadSortOrder) => void;
51 }) {
52 const navigation = useNavigation();
53 const gt = useGT();
54 const { width } = useWindowDimensions();
55 const hasCustomFilter = props.selectedEnvironmentId !== null || props.sortOrder !== "newest";
56 return (
57 <AppBar
58 title={gt("Archived threads")}
59 onBack={() => navigation.goBack()}
60 search={{
61 value: props.searchQuery,
62 onChangeText: props.onSearchQueryChange,
63 placeholder: gt("Search archived threads"),
64 compactPlaceholder: gt("Search", { $context: "search field placeholder" }),
65 mode: "inline",
66 compactToolbar: width < 700,
67 }}
68 menus={[
69 {
70 title: gt("Archived thread options"),
71 icon: hasCustomFilter
72 ? "line.3.horizontal.decrease.circle.fill"
73 : "line.3.horizontal.decrease.circle",
74 items: [
75 {
76 id: "environment",
77 title: gt("Environment", { $context: "filter menu heading" }),
78 items: [
79 {
80 id: "environment:all",
81 title: gt("All environments"),
82 selected: props.selectedEnvironmentId === null,
83 onPress: () => props.onEnvironmentChange(null),
84 },
85 ...props.environments.map((environment) => ({
86 id: `environment:${environment.environmentId}`,
87 title: environment.label,
88 selected: props.selectedEnvironmentId === environment.environmentId,
89 onPress: () => props.onEnvironmentChange(environment.environmentId),
90 })),
91 ],
92 },
93 {
94 id: "sort",
95 title: gt("Sort by archived date"),
96 items: [
97 {
98 id: "sort:newest",
99 title: gt("Newest first"),
100 selected: props.sortOrder === "newest",
101 onPress: () => props.onSortOrderChange("newest"),
102 },
103 {
104 id: "sort:oldest",
105 title: gt("Oldest first"),
106 selected: props.sortOrder === "oldest",
107 onPress: () => props.onSortOrderChange("oldest"),
108 },
109 ],
110 },
111 {
112 id: "refresh",
113 title: gt("Refresh archived threads"),
114 onPress: props.onRefresh,
115 },
116 ],
117 },
118 ]}
119 />
120 );
121 }
122
123 type ArchivedThreadListItem =
124 | {
125 readonly kind: "project";
126 readonly key: string;
127 readonly environmentLabel: string | null;
128 readonly environmentMachine: EnvironmentMachineKind;
129 readonly project: EnvironmentProject;
130 }
131 | {
132 readonly kind: "thread";
133 readonly key: string;
134 readonly environmentLabel: string | null;
135 readonly isFirst: boolean;
136 readonly isLast: boolean;
137 readonly thread: EnvironmentThreadShell;
138 };
139
140 function ProjectGroupLabel(props: {
141 readonly environmentLabel: string | null;
142 readonly environmentMachine: EnvironmentMachineKind;
143 readonly project: EnvironmentProject;
144 }) {
145 return (
146 <View className="flex-row items-center gap-2.5 px-1 pb-2">
147 <ProjectFavicon
148 environmentId={props.project.environmentId}
149 faviconPath={props.project.faviconPath}
150 projectIcon={props.project.projectIcon}
151 projectTitle={props.project.title}
152 size={18}
153 workspaceRoot={props.project.workspaceRoot}
154 />
155 <Text
156 className="flex-1 text-xs font-t3-medium tracking-[0.5px] uppercase text-foreground-muted"
157 numberOfLines={1}
158 >
159 {props.project.title}
160 </Text>
161 {props.environmentLabel ? (
162 <View className="max-w-[42%] flex-row items-center gap-1">
163 <EnvironmentMachineSymbol
164 kind={props.environmentMachine}
165 size={10}
166 tintColorClassName="accent-foreground-tertiary"
167 />
168 <Text className="shrink text-2xs text-foreground-tertiary" numberOfLines={1}>
169 {props.environmentLabel}
170 </Text>
171 </View>
172 ) : null}
173 </View>
174 );
175 }
176
177 function ArchivedThreadRow(props: {
178 readonly environmentLabel: string | null;
179 readonly isFirst: boolean;
180 readonly isLast: boolean;
181 readonly onDelete: () => void;
182 readonly onSwipeableClose: (methods: SwipeableMethods) => void;
183 readonly onSwipeableWillOpen: (methods: SwipeableMethods) => void;
184 readonly simultaneousSwipeGesture?: ComponentProps<typeof ThreadSwipeable>["simultaneousWith"];
185 readonly onUnarchive: () => void;
186 readonly thread: EnvironmentThreadShell;
187 }) {
188 const { width: windowWidth } = useWindowDimensions();
189 const gt = useGT();
190 const cardColor = useUniwindTheme()["--color-grouped-card"];
191 const canOperateThread = useEnvironmentScope(
192 props.thread.environmentId,
193 AuthOrchestrationOperateScope,
194 );
195 const subtitle = [props.environmentLabel, props.thread.branch].filter((part): part is string =>
196 Boolean(part),
197 );
198 const rowContent = (
199 <View
200 className={`flex-row items-center gap-3 bg-grouped-card px-4 py-3 ${props.isLast ? "" : "border-b border-separator"}`}
201 >
202 <View className="h-[34px] w-[34px] items-center justify-center rounded-[11px] bg-subtle">
203 <SymbolView
204 name="archivebox.fill"
205 size={15}
206 tintColorClassName="accent-icon-subtle"
207 type="monochrome"
208 />
209 </View>
210
211 <View className="min-w-0 flex-1 gap-1">
212 <View className="flex-row items-center gap-2">
213 <Text
214 className="min-w-0 flex-1 text-base font-t3-bold leading-snug text-foreground"
215 numberOfLines={1}
216 >
217 {props.thread.title}
218 </Text>
219 <Text className="min-w-[30px] text-right text-xs tabular-nums text-foreground-tertiary">
220 <RelativeTime timestamp={props.thread.archivedAt ?? props.thread.updatedAt} />
221 </Text>
222 </View>
223 {subtitle.length > 0 ? (
224 <View className="flex-row items-center gap-1.5">
225 <SymbolView
226 name="arrow.triangle.branch"
227 size={10}
228 tintColorClassName="accent-icon-subtle"
229 type="monochrome"
230 />
231 <Text
232 className="min-w-0 flex-1 font-mono text-2xs text-foreground-tertiary"
233 numberOfLines={1}
234 >
235 {subtitle.join(" · ")}
236 </Text>
237 </View>
238 ) : null}
239 </View>
240 </View>
241 );
242 const containerStyle = {
243 borderTopLeftRadius: props.isFirst ? 20 : 0,
244 borderTopRightRadius: props.isFirst ? 20 : 0,
245 borderBottomLeftRadius: props.isLast ? 20 : 0,
246 borderBottomRightRadius: props.isLast ? 20 : 0,
247 overflow: "hidden" as const,
248 };
249 if (!canOperateThread) return <View style={containerStyle}>{rowContent}</View>;
250
251 return (
252 <ThreadSwipeable
253 resetKey={`${props.thread.environmentId}:${props.thread.id}`}
254 threadKey={`${props.thread.environmentId}:${props.thread.id}`}
255 backgroundColor={cardColor}
256 containerStyle={containerStyle}
257 fullSwipeWidth={windowWidth - 32}
258 onDelete={props.onDelete}
259 onSwipeableClose={props.onSwipeableClose}
260 onSwipeableWillOpen={props.onSwipeableWillOpen}
261 primaryAction={{
262 accessibilityLabel: gt("Unarchive {title}", { title: props.thread.title }),
263 icon: "arrow.uturn.backward",
264 label: gt("Unarchive", { $context: "verb, swipe action" }),
265 onPress: props.onUnarchive,
266 }}
267 simultaneousWith={props.simultaneousSwipeGesture}
268 threadTitle={props.thread.title}
269 >
270 {() => rowContent}
271 </ThreadSwipeable>
272 );
273 }
274
275 function ArchiveError(props: { readonly message: string; readonly onRetry: () => void }) {
276 const gt = useGT();
277 return (
278 <View className="rounded-[20px] border border-danger-border bg-danger p-4">
279 <Text className="text-base font-t3-bold text-danger-foreground">
280 <T>Could not load every archive</T>
281 </Text>
282 <Text className="mt-1 text-sm text-foreground-muted">{props.message}</Text>
283 <View className="mt-3">
284 <Button label={gt("Try again")} tone="secondary" onPress={props.onRetry} />
285 </View>
286 </View>
287 );
288 }
289
290 export function ArchivedThreadsScreen(props: {
291 readonly environments: ReadonlyArray<ArchivedThreadsHeaderEnvironment>;
292 readonly error: string | null;
293 readonly groups: ReadonlyArray<ArchivedThreadGroup>;
294 readonly isLoading: boolean;
295 readonly searchQuery: string;
296 readonly selectedEnvironmentId: EnvironmentId | null;
297 readonly sortOrder: ArchivedThreadSortOrder;
298 readonly onDeleteThread: (thread: EnvironmentThreadShell) => void;
299 readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void;
300 readonly onRefresh: () => void;
301 readonly onSearchQueryChange: (query: string) => void;
302 readonly onSortOrderChange: (sortOrder: ArchivedThreadSortOrder) => void;
303 readonly onUnarchiveThread: (thread: EnvironmentThreadShell) => void;
304 }) {
305 const { onDeleteThread, onUnarchiveThread } = props;
306 const gt = useGT();
307 const openSwipeableRef = useRef<SwipeableMethods | null>(null);
308 const archiveScrollGesture = useNativeGesture();
309 const environmentLabelsById = useMemo(
310 () =>
311 new Map(
312 props.environments.map((environment) => [environment.environmentId, environment.label]),
313 ),
314 [props.environments],
315 );
316 const serverConfigs = useServerConfigs();
317 const listItems = useMemo<ReadonlyArray<ArchivedThreadListItem>>(() => {
318 const items: ArchivedThreadListItem[] = [];
319 for (const group of props.groups) {
320 const environmentLabel = environmentLabelsById.get(group.project.environmentId) ?? null;
321 items.push({
322 kind: "project",
323 key: `${group.key}:project`,
324 environmentLabel,
325 environmentMachine: resolveEnvironmentMachineKind(
326 serverConfigs.get(group.project.environmentId) ?? null,
327 ),
328 project: group.project,
329 });
330
331 group.threads.forEach((thread, index) => {
332 items.push({
333 kind: "thread",
334 key: `${thread.environmentId}:${thread.id}`,
335 environmentLabel,
336 isFirst: index === 0,
337 isLast: index === group.threads.length - 1,
338 thread,
339 });
340 });
341 }
342 return items;
343 }, [environmentLabelsById, props.groups, serverConfigs]);
344 const handleSwipeableWillOpen = (methods: SwipeableMethods) => {
345 if (openSwipeableRef.current && openSwipeableRef.current !== methods) {
346 openSwipeableRef.current.close();
347 }
348 openSwipeableRef.current = methods;
349 };
350 const handleSwipeableClose = (methods: SwipeableMethods) => {
351 if (openSwipeableRef.current === methods) {
352 openSwipeableRef.current = null;
353 }
354 };
355 const isInitialLoad = props.isLoading && props.groups.length === 0 && props.error === null;
356 const isFiltered = props.searchQuery.trim().length > 0 || props.selectedEnvironmentId !== null;
357 const renderListItem = ({ item }: { item: ArchivedThreadListItem }) => {
358 if (item.kind === "project") {
359 return (
360 <View className="pt-4">
361 <ProjectGroupLabel
362 environmentLabel={item.environmentLabel}
363 environmentMachine={item.environmentMachine}
364 project={item.project}
365 />
366 </View>
367 );
368 }
369
370 return (
371 <ArchivedThreadRow
372 environmentLabel={item.environmentLabel}
373 isFirst={item.isFirst}
374 isLast={item.isLast}
375 onDelete={() => onDeleteThread(item.thread)}
376 onSwipeableClose={handleSwipeableClose}
377 onSwipeableWillOpen={handleSwipeableWillOpen}
378 onUnarchive={() => onUnarchiveThread(item.thread)}
379 simultaneousSwipeGesture={archiveScrollGesture}
380 thread={item.thread}
381 />
382 );
383 };
384 const listEmptyComponent = useMemo(() => {
385 if (isInitialLoad) {
386 return (
387 <View className="items-center py-16">
388 <AppActivityIndicator colorClassName="accent-icon" />
389 <Text className="mt-3 text-sm text-foreground-muted">
390 <T>Loading archive...</T>
391 </Text>
392 </View>
393 );
394 }
395
396 return (
397 <EmptyState
398 detail={
399 isFiltered
400 ? gt("Try another search or environment.")
401 : gt("Threads you archive will appear here.")
402 }
403 title={isFiltered ? gt("No matching threads") : gt("No archived threads")}
404 />
405 );
406 }, [gt, isFiltered, isInitialLoad]);
407
408 return (
409 // Without this native container, formSheet resizing mistakes the background for a header and collapses the list.
410 <View collapsable={false} className="flex-1 bg-sheet">
411 <ArchivedThreadsHeader
412 environments={props.environments}
413 searchQuery={props.searchQuery}
414 onEnvironmentChange={props.onEnvironmentChange}
415 onRefresh={props.onRefresh}
416 onSearchQueryChange={props.onSearchQueryChange}
417 onSortOrderChange={props.onSortOrderChange}
418 selectedEnvironmentId={props.selectedEnvironmentId}
419 sortOrder={props.sortOrder}
420 />
421
422 <SettingsScreenContent>
423 <GestureDetector gesture={archiveScrollGesture}>
424 <LegendList
425 renderScrollComponent={NativeScrollView}
426 className="flex-1"
427 contentContainerStyle={{
428 paddingBottom: 32,
429 paddingHorizontal: 16,
430 paddingTop: 4,
431 }}
432 contentInsetAdjustmentBehavior="automatic"
433 data={listItems}
434 estimatedItemSize={62}
435 getItemType={(item) => item.kind}
436 keyboardDismissMode="on-drag"
437 keyboardShouldPersistTaps="handled"
438 keyExtractor={(item) => item.key}
439 ListEmptyComponent={listEmptyComponent}
440 ListHeaderComponent={
441 props.error ? <ArchiveError message={props.error} onRetry={props.onRefresh} /> : null
442 }
443 onScrollBeginDrag={() => openSwipeableRef.current?.close()}
444 refreshControl={
445 <RefreshControl
446 onRefresh={props.onRefresh}
447 refreshing={props.isLoading && !isInitialLoad}
448 tintColorClassName={String("accent-icon")}
449 />
450 }
451 renderItem={renderListItem}
452 showsVerticalScrollIndicator={false}
453 />
454 </GestureDetector>
455 </SettingsScreenContent>
456 </View>
457 );
458 }