@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 112 lines 4.17 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
3 import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4 import {
5 presentPendingBackgroundWork,
6 type PendingBackgroundWorkItem,
7 } from "@t3tools/client-runtime/state/thread-execution";
8 import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
9 import * as Haptics from "expo-haptics";
10 import { msg, T, useGT, useMessages } from "gt-react-native";
11 import { Pressable, ScrollView, View } from "react-native";
12 import { useSafeAreaInsets } from "react-native-safe-area-context";
13
14 import { SheetHeader } from "../../components/ui/SheetHeader";
15 import { SymbolView } from "../../components/ui/AppSymbol";
16 import { AppText as Text } from "../../components/ui/AppText";
17 import { ScreenContent } from "../../components/ui/ScreenContent";
18 import { environmentThreadShells } from "../../state/threads";
19
20 const KIND_LABELS = {
21 subagent: msg("Subagent"),
22 command: msg("Command", { $context: "noun, a running shell command" }),
23 monitor: msg("Monitor", { $context: "noun, a background watcher task" }),
24 background_task: msg("Background task"),
25 } as const;
26
27 type BackgroundWorkTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
28
29 export function ThreadBackgroundWorkSheet({ route }: StaticScreenProps<BackgroundWorkTarget>) {
30 const navigation = useNavigation();
31 const insets = useSafeAreaInsets();
32 const gt = useGT();
33 const m = useMessages();
34 const target = route.params;
35 const thread = useAtomValue(
36 environmentThreadShells.threadShellAtom(scopeThreadRef(target.environmentId, target.threadId)),
37 );
38 const presentation = presentPendingBackgroundWork(thread?.pendingBackgroundTasks ?? [], m);
39 const openThread = (threadId: ThreadId) => {
40 void Haptics.selectionAsync();
41 // A thread replaces the leaf sheet so workspace back navigation still works.
42 navigation.dispatch(
43 StackActions.replace("Thread", { environmentId: target.environmentId, threadId }),
44 );
45 };
46 const content = (
47 <ScrollView
48 nestedScrollEnabled
49 className="flex-1"
50 contentInsetAdjustmentBehavior="automatic"
51 contentContainerClassName="px-5 pb-6"
52 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
53 >
54 {presentation === null ? (
55 <Text className="pt-6 text-center text-sm text-foreground-muted">
56 <T>Nothing running in the background.</T>
57 </Text>
58 ) : (
59 presentation.items.map((item) => (
60 <BackgroundWorkRow key={item.taskId} item={item} onOpen={openThread} />
61 ))
62 )}
63 </ScrollView>
64 );
65 return (
66 <View collapsable={false} className="flex-1 bg-sheet">
67 <SheetHeader title={gt("Background work")} onClose={() => navigation.goBack()} />
68 <ScreenContent>{content}</ScreenContent>
69 </View>
70 );
71 }
72
73 function BackgroundWorkRow(props: {
74 readonly item: PendingBackgroundWorkItem;
75 readonly onOpen: (threadId: ThreadId) => void;
76 }) {
77 const { item } = props;
78 const gt = useGT();
79 const m = useMessages();
80 const childThreadId = item.childThreadId;
81 const row = (
82 <View className="flex-row items-center gap-3 border-b border-border py-3.5">
83 <SymbolView
84 name={
85 item.kind === "subagent" ? "person.2" : item.kind === "monitor" ? "clock" : "terminal"
86 }
87 size={15}
88 tintColorClassName="accent-foreground-muted"
89 type="monochrome"
90 />
91 <View className="min-w-0 flex-1">
92 <Text className="font-t3-medium text-sm text-foreground">{item.label}</Text>
93 <Text className="text-xs text-foreground-muted">{m(KIND_LABELS[item.kind])}</Text>
94 </View>
95 {childThreadId === undefined ? null : (
96 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
97 )}
98 </View>
99 );
100 return childThreadId === undefined ? (
101 <View accessible>{row}</View>
102 ) : (
103 <Pressable
104 accessibilityRole="link"
105 accessibilityLabel={gt("Open subagent {label}", { label: item.label })}
106 onPress={() => props.onOpen(childThreadId)}
107 className="active:opacity-70"
108 >
109 {row}
110 </Pressable>
111 );
112 }