|
| 1 | +import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 2 | +import type { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 3 | +import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 4 | +import * as Haptics from "expo-haptics"; |
| 5 | +import { useState } from "react"; |
| 6 | +import { Platform, Pressable, ScrollView, View } from "react-native"; |
| 7 | +import { Screen, ScreenStack, ScreenStackHeaderConfig } from "react-native-screens"; |
| 8 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 9 | + |
| 10 | +import { AndroidSheetHeader } from "../../components/AndroidScreenHeader"; |
| 11 | +import { AppText as Text } from "../../components/AppText"; |
| 12 | +import { useUniwindTheme } from "../../lib/useUniwindTheme"; |
| 13 | +import { nativeHeaderScrollEdgeEffects } from "../../native/StackHeader"; |
| 14 | +import { useEnvironmentQuery } from "../../state/query"; |
| 15 | +import { threadHandoffEnvironment } from "../../state/threadHandoff"; |
| 16 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 17 | + |
| 18 | +const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version); |
| 19 | + |
| 20 | +type HandoffTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId }; |
| 21 | + |
| 22 | +/** |
| 23 | + * "Continue on…": the linked environments this thread can move to. Picking |
| 24 | + * one starts the move and closes the sheet; the thread's card shows how it goes. |
| 25 | + */ |
| 26 | +export function ThreadHandoffSheet({ route }: StaticScreenProps<HandoffTarget>) { |
| 27 | + const target = route.params; |
| 28 | + const navigation = useNavigation(); |
| 29 | + const insets = useSafeAreaInsets(); |
| 30 | + const theme = useUniwindTheme(); |
| 31 | + const options = useEnvironmentQuery( |
| 32 | + threadHandoffEnvironment.options({ |
| 33 | + environmentId: target.environmentId, |
| 34 | + input: { threadId: target.threadId }, |
| 35 | + }), |
| 36 | + ); |
| 37 | + const start = useAtomCommand(threadHandoffEnvironment.start, { |
| 38 | + label: "move thread", |
| 39 | + reportFailure: false, |
| 40 | + }); |
| 41 | + const [starting, setStarting] = useState<EnvironmentId | null>(null); |
| 42 | + const [error, setError] = useState<string | null>(null); |
| 43 | + |
| 44 | + const choose = async (environmentId: EnvironmentId) => { |
| 45 | + void Haptics.selectionAsync(); |
| 46 | + setStarting(environmentId); |
| 47 | + setError(null); |
| 48 | + const result = await start({ |
| 49 | + environmentId: target.environmentId, |
| 50 | + input: { threadId: target.threadId, environmentId }, |
| 51 | + }); |
| 52 | + setStarting(null); |
| 53 | + if (result._tag === "Failure") { |
| 54 | + setError(String(squashAtomCommandFailure(result))); |
| 55 | + return; |
| 56 | + } |
| 57 | + navigation.goBack(); |
| 58 | + }; |
| 59 | + |
| 60 | + const targets = options.data?.options ?? []; |
| 61 | + const content = ( |
| 62 | + <ScrollView |
| 63 | + className="flex-1" |
| 64 | + contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"} |
| 65 | + contentContainerClassName="px-5 pb-6" |
| 66 | + contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }} |
| 67 | + > |
| 68 | + <Text className="pb-2 pt-3 text-sm text-foreground-muted"> |
| 69 | + Moves the conversation, unpushed commits, and uncommitted changes. This thread becomes |
| 70 | + read-only here. |
| 71 | + </Text> |
| 72 | + {options.error ? ( |
| 73 | + <Text className="pt-4 text-sm text-danger-foreground">{options.error}</Text> |
| 74 | + ) : options.data === null ? ( |
| 75 | + <Text className="pt-4 text-sm text-foreground-muted">Asking linked environments…</Text> |
| 76 | + ) : targets.length === 0 ? ( |
| 77 | + <Text className="pt-4 text-sm text-foreground-muted"> |
| 78 | + No linked environments. Link one from a desktop or with `t3 environment link`. |
| 79 | + </Text> |
| 80 | + ) : ( |
| 81 | + targets.map((option) => ( |
| 82 | + <Pressable |
| 83 | + key={option.environmentId} |
| 84 | + accessibilityRole="button" |
| 85 | + accessibilityState={{ disabled: option.reason !== null || starting !== null }} |
| 86 | + disabled={option.reason !== null || starting !== null} |
| 87 | + onPress={() => void choose(option.environmentId)} |
| 88 | + className="border-b border-border py-3.5 active:opacity-70 disabled:opacity-50" |
| 89 | + > |
| 90 | + <Text className="text-base font-t3-bold text-foreground"> |
| 91 | + {starting === option.environmentId ? `Moving to ${option.label}…` : option.label} |
| 92 | + </Text> |
| 93 | + {option.reason !== null ? ( |
| 94 | + <Text className="text-xs text-foreground-muted">{option.reason}</Text> |
| 95 | + ) : null} |
| 96 | + </Pressable> |
| 97 | + )) |
| 98 | + )} |
| 99 | + {error !== null ? ( |
| 100 | + <Text accessibilityRole="alert" className="pt-3 text-sm text-danger-foreground"> |
| 101 | + {error} |
| 102 | + </Text> |
| 103 | + ) : null} |
| 104 | + </ScrollView> |
| 105 | + ); |
| 106 | + |
| 107 | + if (Platform.OS === "ios") { |
| 108 | + // A plain formSheet screen never renders a stack header, so it comes from |
| 109 | + // a nested native stack inside the sheet (same shape as the agents sheet). |
| 110 | + return ( |
| 111 | + <View collapsable={false} className="flex-1 bg-sheet"> |
| 112 | + <ScreenStack style={{ flex: 1 }}> |
| 113 | + <Screen |
| 114 | + activityState={2} |
| 115 | + enabled |
| 116 | + isNativeStack |
| 117 | + screenId="thread-handoff-sheet-native" |
| 118 | + scrollEdgeEffects={HEADER_SCROLL_EDGE_EFFECTS} |
| 119 | + style={{ backgroundColor: theme["--color-sheet"], flex: 1 }} |
| 120 | + > |
| 121 | + {content} |
| 122 | + <ScreenStackHeaderConfig |
| 123 | + backgroundColor="rgba(0,0,0,0)" |
| 124 | + color={theme["--color-foreground"]} |
| 125 | + hideBackButton |
| 126 | + hideShadow={false} |
| 127 | + title="Continue on…" |
| 128 | + titleColor={theme["--color-foreground"]} |
| 129 | + titleFontSize={18} |
| 130 | + titleFontWeight="800" |
| 131 | + translucent |
| 132 | + /> |
| 133 | + </Screen> |
| 134 | + </ScreenStack> |
| 135 | + </View> |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + return ( |
| 140 | + <View collapsable={false} className="flex-1 bg-sheet"> |
| 141 | + <AndroidSheetHeader title="Continue on…" onBack={() => navigation.goBack()} /> |
| 142 | + {content} |
| 143 | + </View> |
| 144 | + ); |
| 145 | +} |
0 commit comments