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

ThreadBackgroundWorkSheet.tsx

150 lines5.9 KB

1import { useAtomValue } from "@effect/atom-react";
2import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native";
3import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4import {
5 presentPendingBackgroundWork,
6 type PendingBackgroundWorkItem,
7} from "@t3tools/client-runtime/state/thread-execution";
8import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
9import * as Haptics from "expo-haptics";
10import { msg, T, useGT, useMessages } from "gt-react-native";
11import { Pressable, ScrollView, View } from "react-native";
12import { useSafeAreaInsets } from "react-native-safe-area-context";
13​
14import { SheetHeader } from "../../components/ui/SheetHeader";
15import { SymbolView } from "../../components/ui/AppSymbol";
16import { AppText as Text } from "../../components/ui/AppText";
17import { ScreenContent } from "../../components/ui/ScreenContent";
18import { useLinkedThreadReach } from "../../state/linkedThread";
19import { environmentThreadDetails, environmentThreadShells } from "../../state/threads";
20import { subagentThreadTarget, type SubagentThreadTarget } from "./threadAgentsPresentation";
21import { useOpenLinkedThreadRead } from "./useOpenLinkedThread";
22​
23const KIND_LABELS = {
24 subagent: msg("Subagent"),
25 command: msg({ context: "noun, a running shell command" })`Command`,
26 monitor: msg({ context: "noun, a background watcher task" })`Monitor`,
27 background_task: msg("Background task"),
28} as const;
29​
30type BackgroundWorkTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
31​
32export function ThreadBackgroundWorkSheet({ route }: StaticScreenProps<BackgroundWorkTarget>) {
33 const navigation = useNavigation();
34 const openLinkedRead = useOpenLinkedThreadRead();
35 const insets = useSafeAreaInsets();
36 const gt = useGT();
37 const m = useMessages();
38 const target = route.params;
39 const threadRef = scopeThreadRef(target.environmentId, target.threadId);
40 const thread = useAtomValue(environmentThreadShells.threadShellAtom(threadRef));
41 const detail = useAtomValue(environmentThreadDetails.threadAtom(threadRef));
42 const pendingTasks = thread?.pendingBackgroundTasks ?? [];
43 const turnItems = detail?.projection.turnItems ?? [];
44 const presentation = presentPendingBackgroundWork(pendingTasks, m);
45 const rows = (presentation?.items ?? []).map((item) => {
46 const pending = pendingTasks.find((candidate) => candidate.taskId === item.taskId);
47 const itemKey = pending?.nativeItemId ?? item.taskId;
48 const turnItem = turnItems.findLast((candidate) =>
49 pending?.itemId === undefined
50 ? (candidate.nativeItemRef?.nativeId || candidate.id) === itemKey
51 : candidate.id === pending.itemId,
52 );
53 const remoteChild =
54 item.kind === "subagent" && turnItem?.type === "subagent" ? turnItem.remoteChild : undefined;
55 return { item, childThreadId: item.childThreadId ?? null, remoteChild };
56 });
57 const reach = useLinkedThreadReach(target.environmentId, rows);
58 const openThread = (child: SubagentThreadTarget, title: string) => {
59 void Haptics.selectionAsync();
60 if (child.linked !== undefined) {
61 openLinkedRead(child, child.linked, title, { source: target, replace: true });
62 return;
63 }
64 // A thread replaces the leaf sheet so workspace back navigation still works.
65 navigation.dispatch(
66 StackActions.replace("Thread", {
67 environmentId: child.environmentId,
68 threadId: child.threadId,
69 }),
70 );
71 };
72 const content = (
73 <ScrollView
74 nestedScrollEnabled
75 className="flex-1"
76 contentInsetAdjustmentBehavior="automatic"
77 contentContainerClassName="px-5 pb-6"
78 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
79 >
80 {presentation === null ? (
81 <Text className="pt-6 text-center text-sm text-foreground-muted">
82 <T>Nothing running in the background.</T>
83 </Text>
84 ) : (
85 rows.map((row) => {
86 const { item } = row;
87 const openTarget =
88 item.kind === "subagent"
89 ? subagentThreadTarget(row, target.environmentId, reach.ownConnection, reach.isLinked)
90 : null;
91 return (
92 <BackgroundWorkRow
93 key={item.taskId}
94 item={item}
95 openTarget={openTarget}
96 onOpen={openThread}
97 />
98 );
99 })
100 )}
101 </ScrollView>
102 );
103 return (
104 <View collapsable={false} className="flex-1 bg-sheet">
105 <SheetHeader title={gt`Background work`} onClose={() => navigation.goBack()} />
106 <ScreenContent>{content}</ScreenContent>
107 </View>
108 );
110​
111function BackgroundWorkRow(props: {
112 readonly item: PendingBackgroundWorkItem;
113 readonly openTarget: SubagentThreadTarget | null;
114 readonly onOpen: (child: SubagentThreadTarget, title: string) => void;
115}) {
116 const { item, openTarget } = props;
117 const gt = useGT();
118 const m = useMessages();
119 const row = (
120 <View className="flex-row items-center gap-3 border-b border-border py-3.5">
121 <SymbolView
122 name={
123 item.kind === "subagent" ? "person.2" : item.kind === "monitor" ? "clock" : "terminal"
124 }
125 size={15}
126 tintColorClassName="accent-foreground-muted"
127 type="monochrome"
128 />
129 <View className="min-w-0 flex-1">
130 <Text className="font-t3-medium text-sm text-foreground">{item.label}</Text>
131 <Text className="text-xs text-foreground-muted">{m(KIND_LABELS[item.kind])}</Text>
132 </View>
133 {openTarget === null ? null : (
134 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
135 )}
136 </View>
137 );
138 return openTarget === null ? (
139 <View accessible>{row}</View>
140 ) : (
141 <Pressable
142 accessibilityRole="link"
143 accessibilityLabel={gt`Open subagent ${{ label: item.label }}`}
144 onPress={() => props.onOpen(openTarget, item.label)}
145 className="active:opacity-70"
146 >
147 {row}
148 </Pressable>
149 );