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

ThreadBackgroundWorkSheet.tsx

143 lines5.4 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 { Platform, Pressable, ScrollView, View } from "react-native";
11import { ScreenStack } from "react-native-screens";
12import { useSafeAreaInsets } from "react-native-safe-area-context";
13​
14import { AndroidSheetHeader } from "../../components/ui/AndroidScreenHeader";
15import { SymbolView } from "../../components/ui/AppSymbol";
16import { AppText as Text } from "../../components/ui/AppText";
17import { ScreenContent } from "../../components/ui/ScreenContent";
18import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
19import { NativeHeader, NativeScreen } from "../../native/ThemedScreenStack";
20import { environmentThreadShells } from "../../state/threads";
21import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
22​
23const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
24const KIND_LABELS = {
25 subagent: "Subagent",
26 command: "Command",
27 monitor: "Monitor",
28 background_task: "Background task",
29} as const;
30​
31type BackgroundWorkTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
32​
33export function ThreadBackgroundWorkSheet({ route }: StaticScreenProps<BackgroundWorkTarget>) {
34 const { layout } = useAdaptiveWorkspaceLayout();
35 const navigation = useNavigation();
36 const insets = useSafeAreaInsets();
37 const target = route.params;
38 const thread = useAtomValue(
39 environmentThreadShells.threadShellAtom(scopeThreadRef(target.environmentId, target.threadId)),
40 );
41 const presentation = presentPendingBackgroundWork(thread?.pendingBackgroundTasks ?? []);
42 const openThread = (threadId: ThreadId) => {
43 void Haptics.selectionAsync();
44 // A thread replaces the leaf sheet so workspace back navigation still works.
45 navigation.dispatch(
46 StackActions.replace("Thread", { environmentId: target.environmentId, threadId }),
47 );
48 };
49 const content = (
50 <ScrollView
51 nestedScrollEnabled
52 className="flex-1"
53 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
54 contentContainerClassName="px-5 pb-6"
55 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
56 >
57 {presentation === null ? (
58 <Text className="pt-6 text-center text-sm text-foreground-muted">
59 Nothing running in the background.
60 </Text>
61 ) : (
62 presentation.items.map((item) => (
63 <BackgroundWorkRow key={item.taskId} item={item} onOpen={openThread} />
64 ))
65 )}
66 </ScrollView>
67 );
68 if (Platform.OS === "ios") {
69 return (
70 <View collapsable={false} className="flex-1 bg-sheet">
71 <ScreenStack style={{ flex: 1 }}>
72 <NativeScreen
73 activityState={2}
74 enabled
75 isNativeStack
76 screenId="thread-background-work-sheet-native"
77 scrollEdgeEffects={HEADER_SCROLL_EDGE_EFFECTS}
78 className="flex-1 bg-sheet"
79 >
80 {content}
81 <NativeHeader
82 backgroundColor="rgba(0,0,0,0)"
83 tintColorClassName="accent-foreground"
84 hideBackButton
85 hideShadow={false}
86 title="Background work"
87 titleColorClassName="accent-foreground"
88 titleFontSize={18}
89 titleFontWeight="800"
90 translucent
91 />
92 </NativeScreen>
93 </ScreenStack>
94 </View>
95 );
96 }
97 return (
98 <View collapsable={false} className="flex-1 bg-sheet">
99 <NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} />
100 <AndroidSheetHeader title="Background work" onBack={() => navigation.goBack()} />
101 <ScreenContent insetHorizontal={layout.usesSplitView}>{content}</ScreenContent>
102 </View>
103 );
105​
106function BackgroundWorkRow(props: {
107 readonly item: PendingBackgroundWorkItem;
108 readonly onOpen: (threadId: ThreadId) => void;
109}) {
110 const { item } = props;
111 const childThreadId = item.childThreadId;
112 const row = (
113 <View className="flex-row items-center gap-3 border-b border-border py-3.5">
114 <SymbolView
115 name={
116 item.kind === "subagent" ? "person.2" : item.kind === "monitor" ? "clock" : "terminal"
117 }
118 size={15}
119 tintColorClassName="accent-foreground-muted"
120 type="monochrome"
121 />
122 <View className="min-w-0 flex-1">
123 <Text className="font-t3-medium text-sm text-foreground">{item.label}</Text>
124 <Text className="text-xs text-foreground-muted">{KIND_LABELS[item.kind]}</Text>
125 </View>
126 {childThreadId === undefined ? null : (
127 <SymbolView name="chevron.right" size={12} tintColorClassName="accent-icon-subtle" />
128 )}
129 </View>
130 );
131 return childThreadId === undefined ? (
132 <View accessible>{row}</View>
133 ) : (
134 <Pressable
135 accessibilityRole="link"
136 accessibilityLabel={`Open subagent ${item.label}`}
137 onPress={() => props.onOpen(childThreadId)}
138 className="active:opacity-70"
139 >
140 {row}
141 </Pressable>
142 );