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