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

ThreadHandoffSheet.tsx

172 lines6.9 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import {
3 isAtomCommandInterrupted,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6import type { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
7import * as Haptics from "expo-haptics";
8import { msg, T, useGT, useMessages } from "gt-react-native";
9import { useState } from "react";
10import { Platform, Pressable, ScrollView, View } from "react-native";
11import { useSafeAreaInsets } from "react-native-safe-area-context";
12​
13import { Button } from "../../components/ui/Button";
14import { useHandoffProject } from "./useHandoffProject";
15import { AppBar } from "../../components/ui/AppBar";
16import { AppText as Text } from "../../components/ui/AppText";
17import { ScreenContent } from "../../components/ui/ScreenContent";
18import { SheetHeader } from "../../components/ui/SheetHeader";
19import { scopedThreadKey } from "../../lib/scopedEntities";
20import { NativeStackScreenOptions } from "../../native/StackHeader";
21import { useEnvironmentQuery } from "../../state/query";
22import { threadHandoffEnvironment } from "../../state/threadHandoff";
23import { useAtomCommand } from "../../state/use-atom-command";
24import { useQueuedThreadKeys } from "../../state/use-thread-outbox";
25​
26const TITLE = msg("Continue on another machine");
27​
28type HandoffTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };
29​
30/**
31 * The linked machines a thread can move to. Picking one starts the move and closes the sheet;
32 * the thread's own notice shows how it goes.
33 */
34export function ThreadHandoffSheet({ route }: StaticScreenProps<HandoffTarget>) {
35 const { environmentId, threadId } = route.params;
36 const gt = useGT();
37 const m = useMessages();
38 const navigation = useNavigation();
39 const insets = useSafeAreaInsets();
40 const targets = useEnvironmentQuery(
41 threadHandoffEnvironment.options({ environmentId, input: { threadId } }),
42 );
43 const start = useAtomCommand(threadHandoffEnvironment.start, { reportFailure: false });
44 const [picked, setPicked] = useState<EnvironmentId>();
45 const [starting, setStarting] = useState<EnvironmentId | null>(null);
46 const [error, setError] = useState<string | null>(null);
47 // A message in the outbox exists only on this device, and a moved thread refuses it.
48 const outboxWaiting = useQueuedThreadKeys().has(scopedThreadKey(environmentId, threadId));
49 const close = () => navigation.goBack();
50​
51 const choose = async (target: EnvironmentId, projectId?: ProjectId) => {
52 void Haptics.selectionAsync();
53 setStarting(target);
54 setError(null);
55 const result = await start({
56 environmentId,
57 input: { threadId, environmentId: target, ...(projectId === undefined ? {} : { projectId }) },
58 });
59 setStarting(null);
60 if (result._tag === "Success") {
61 // Dismissed meanwhile, a plain goBack would pop the screen underneath instead.
62 if (navigation.isFocused()) close();
63 return;
64 }
65 if (isAtomCommandInterrupted(result)) return;
66 const failure = squashAtomCommandFailure(result);
67 setError(failure instanceof Error ? failure.message : gt`Could not move the thread.`);
68 };
69​
70 const options = targets.data?.options ?? [];
71 const chosen = options.find((option) => option.environmentId === picked);
72 const needsProject = chosen?.projectSelectionRequired === true;
73 const project = useHandoffProject(
74 { environmentId, threadId },
75 picked,
76 needsProject,
77 starting !== null || outboxWaiting,
78 chosen?.projectChoices,
79 );
80 const content = (
81 <ScrollView
82 className="flex-1"
83 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
84 contentContainerClassName="px-5 pt-4"
85 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 16) + 8 }}
86 >
87 <Text className="pb-2 text-sm leading-normal text-foreground-secondary">
88 <T>
89 The thread moves with its conversation and its code, including commits you have not pushed
90 and uncommitted changes. This copy becomes read-only.
91 </T>
92 </Text>
93 {outboxWaiting ? (
94 <Text accessibilityRole="alert" className="pt-2 text-sm text-danger-foreground">
95 <T>A message is still waiting to send from this device. Send or discard it first.</T>
96 </Text>
97 ) : null}
98 {targets.data === null ? (
99 <Text className="pt-4 text-sm text-foreground-muted">
100 {targets.error ?? gt`Asking linked machines…`}
101 </Text>
102 ) : options.length === 0 ? (
103 <Text className="pt-4 text-sm text-foreground-muted">
104 <T>This machine has no linked machines. Link one in Settings on desktop or web.</T>
105 </Text>
106 ) : (
107 options.map((option) => {
108 const disabled =
109 (option.reason !== null && !option.projectSelectionRequired) ||
110 starting !== null ||
111 outboxWaiting;
112 return (
113 <Pressable
114 key={option.environmentId}
115 accessibilityRole="button"
116 accessibilityState={{ disabled, busy: starting === option.environmentId }}
117 disabled={disabled}
118 onPress={() => {
119 if (option.projectSelectionRequired) setPicked(option.environmentId);
120 else void choose(option.environmentId);
121 }}
122 className="border-b border-border py-3.5 active:opacity-70 disabled:opacity-50"
123 >
124 <Text className="text-base font-t3-bold text-foreground">
125 {starting === option.environmentId
126 ? gt`Moving to ${{ machine: option.label }}…`
127 : option.label}
128 </Text>
129 {option.reason === null ? null : (
130 <Text className="text-xs text-foreground-muted">{option.reason}</Text>
131 )}
132 </Pressable>
133 );
134 })
135 )}
136 {project.picker}
137 {needsProject && picked !== undefined && (project.clone || project.projectId) ? (
138 <Button
139 label={project.clone ? gt`Add project and move` : gt`Move`}
140 tone="primary"
141 disabled={starting !== null || outboxWaiting}
142 onPress={() => {
143 if (project.clone) project.clone();
144 else void choose(picked, project.projectId);
145 }}
146 />
147 ) : null}
148 {error === null ? null : (
149 <Text accessibilityRole="alert" className="pt-3 text-sm text-danger-foreground">
150 {error}
151 </Text>
152 )}
153 </ScrollView>
154 );
155​
156 if (Platform.OS === "ios") {
157 return (
158 <View collapsable={false} className="flex-1 bg-sheet">
159 <SheetHeader presentation="native" title={m(TITLE)} onClose={close} />
160 {content}
161 </View>
162 );
163 }
164​
165 return (
166 <View collapsable={false} className="flex-1 bg-header">
167 <NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} />
168 <AppBar sheet title={m(TITLE)} hideBottomBorder onBack={close} />
169 <ScreenContent>{content}</ScreenContent>
170 </View>
171 );