The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 400 lines 14.1 KB
Raw
1 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2 import { changeRequestTermKind } from "../../lib/changeRequestTerm";
3 import Animated from "react-native-reanimated";
4 import { PR_ROW_LAYOUT } from "./pullRequestAnimations";
5 import { useNavigation } from "@react-navigation/native";
6 import type { PullRequestDetail } from "@t3tools/contracts";
7 import type { MenuAction } from "@react-native-menu/menu";
8 import { useGT } from "gt-react-native";
9 import type { ReactNode } from "react";
10 import { Platform, Pressable, View } from "react-native";
11 import { useSafeAreaInsets } from "react-native-safe-area-context";
12 import { AppText as Text } from "../../components/ui/AppText";
13 import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
14 import { Button } from "../../components/ui/Button";
15 import { IconButton } from "../../components/ui/IconButton";
16 import { Toolbar } from "../../components/ui/Toolbar";
17 import { Menu } from "../../components/ui/Menu";
18 import type { AppBarMenu, AppBarMenuItem } from "../../components/ui/AppBar.types";
19 import { createNativeHeaderMenu } from "../../components/ui/nativeHeaderMenu.ios";
20
21 import { useNativeLayoutMetrics } from "../../native/native-layout-metrics";
22 import { type PullRequestParams } from "./usePullRequestDiff";
23 import { usePullRequestPermissions } from "./usePullRequestPermissions";
24 import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
25 import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft";
26 import { usePullRequestAgentActions } from "./usePullRequestAgentActions";
27
28 export function PullRequestCardGroup(props: { readonly children: ReactNode }) {
29 return (
30 <Animated.View
31 layout={PR_ROW_LAYOUT}
32 className="overflow-hidden rounded-[22px] border border-composer-border border-continuous bg-grouped-card"
33 >
34 {props.children}
35 </Animated.View>
36 );
37 }
38
39 export function PullRequestCardRow(props: {
40 readonly title: string;
41 readonly icon: AppSymbolName;
42 readonly tint?: string;
43 readonly value?: string;
44 readonly detail?: string;
45 readonly onPress?: () => void;
46 readonly accessibilityLabel?: string;
47 readonly destructive?: boolean;
48 readonly last?: boolean;
49 readonly compact?: boolean;
50 readonly menu?: boolean;
51 readonly expanded?: boolean;
52 readonly children?: ReactNode;
53 }) {
54 return (
55 <Pressable
56 accessibilityRole={props.onPress || props.menu ? "button" : undefined}
57 accessibilityLabel={props.accessibilityLabel}
58 accessibilityState={props.expanded === undefined ? undefined : { expanded: props.expanded }}
59 onPress={props.onPress}
60 disabled={!props.onPress && !props.menu}
61 className={`${props.compact ? "min-h-11 py-2.5" : "min-h-13 py-3"} flex-row items-center gap-3 px-4 ${props.last || props.compact ? "" : "border-b border-border-subtle"} ${props.onPress ? "active:opacity-70" : ""}`}
62 >
63 <SymbolView
64 name={props.icon}
65 size={18}
66 tintColorClassName={
67 props.destructive ? "accent-danger-foreground" : (props.tint ?? "accent-icon-muted")
68 }
69 />
70 <View className="min-w-0 flex-1 gap-0.5">
71 <Text
72 className={
73 props.destructive
74 ? "text-sm font-t3-medium text-danger-foreground"
75 : "text-sm font-t3-medium"
76 }
77 >
78 {props.title}
79 </Text>
80 {props.detail ? (
81 <Text className="text-2xs text-foreground-muted" numberOfLines={2}>
82 {props.detail}
83 </Text>
84 ) : null}
85 </View>
86 {props.children ??
87 (props.value ? (
88 <Text
89 className={`${props.compact ? "max-w-[38%] text-2xs" : "max-w-[60%] text-xs"} shrink text-right text-foreground-muted`}
90 numberOfLines={2}
91 >
92 {props.value}
93 </Text>
94 ) : null)}
95 {props.onPress || props.menu ? (
96 <SymbolView
97 name={
98 props.expanded === undefined
99 ? "chevron.right"
100 : props.expanded
101 ? "chevron.up"
102 : "chevron.down"
103 }
104 size={11}
105 tintColorClassName="accent-icon-muted"
106 />
107 ) : null}
108 </Pressable>
109 );
110 }
111
112 /** The Duo's vertical bar, where this toolbar and the screen's bar share one column of buttons. */
113 export function usePullRequestVerticalControls() {
114 const nativeMetrics = useNativeLayoutMetrics();
115 return nativeMetrics !== null && nativeMetrics.verticalBarEdge !== "none";
116 }
117
118 export function PullRequestActionToolbar(props: {
119 readonly pr: PullRequestDetail | null;
120 readonly params: PullRequestParams;
121 readonly filePath?: string;
122 /** The screen's actions menu; the vertical bar shows it here with the toolbar's tools. */
123 readonly menu: AppBarMenu;
124 readonly onOpenFiles?: () => void;
125 readonly onHeightChange?: (height: number) => void;
126 }) {
127 const navigation = useNavigation();
128 const gt = useGT();
129 const { draft } = usePullRequestReviewDraft(props.params);
130 const { themeVariables: colors } = useAppearancePreferences();
131 const insets = useSafeAreaInsets();
132 const verticalControls = usePullRequestVerticalControls();
133 const permissions = usePullRequestPermissions(props.params, props.pr);
134 const agent = usePullRequestAgentActions(props.params, props.pr, props.filePath);
135 const canReview = permissions.verdicts.length > 0;
136 const pendingReview = canReview && draft.comments.length > 0;
137 const showAgentControls = agent.canAsk || !permissions.connected;
138 const canOpenThread = agent.threads.length > 0 || showAgentControls;
139 const term = changeRequestTermKind(
140 sourceControlClients.get((props.pr ?? props.params.initialSummary)?.provider).changeRequest,
141 );
142 const threadLabel = agent.threads.length
143 ? gt("Continue thread")
144 : gt("Work on {term, select, mr {MR} cr {change request} other {PR}}", { term });
145 const primaryLabel = pendingReview
146 ? gt("Submit review · {count}", { count: draft.comments.length })
147 : threadLabel;
148 const agentActionsLabel = gt(
149 "{term, select, mr {MR} cr {change request} other {PR}} agent actions",
150 { term },
151 );
152 const chooseThread = agent.chooseThread;
153 const agentActions = agent.actions.filter((action) => action.task !== "ask");
154 const open = (mode: "review" | "comment") =>
155 navigation.navigate("PullRequestReview", { ...props.params, mode });
156 const primaryPress = () => (pendingReview ? open("review") : agent.start("ask"));
157 const threadMenuItems = agent.threadActions("ask");
158 const openFiles =
159 props.onOpenFiles ?? (() => navigation.navigate("PullRequestFiles", props.params));
160 if (!props.pr)
161 return verticalControls ? (
162 <Toolbar placement="bottom">{createNativeHeaderMenu(props.menu)}</Toolbar>
163 ) : null;
164 const primaryDisabled = pendingReview ? false : agent.disabled;
165 const showPrimary = pendingReview || canOpenThread;
166 if (Platform.OS === "android") {
167 const primary = (
168 <Button
169 label={primaryLabel}
170 tone="primary"
171 disabled={primaryDisabled}
172 onPress={primaryPress}
173 />
174 );
175 return (
176 <View
177 pointerEvents="box-none"
178 className="absolute inset-x-0 bottom-0 z-10 items-center px-4 pt-2"
179 style={{ paddingBottom: insets.bottom + 16 }}
180 onLayout={(event) => props.onHeightChange?.(event.nativeEvent.layout.height)}
181 >
182 <Toolbar>
183 <View className="flex-row items-center gap-2">
184 {showAgentControls ? (
185 <Menu
186 title={gt("AI helpers")}
187 actions={agentActions.map((action) => agent.menuAction(action.task))}
188 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
189 >
190 <View pointerEvents="none">
191 <IconButton
192 icon="ellipsis.bubble"
193 accessibilityLabel={agentActionsLabel}
194 disabled={agent.disabled}
195 />
196 </View>
197 </Menu>
198 ) : null}
199 {showPrimary ? (
200 <View className="min-w-0 shrink">
201 {!pendingReview && chooseThread ? (
202 <Menu
203 title={gt("Open thread")}
204 actions={threadMenuItems}
205 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
206 >
207 <View pointerEvents="none">{primary}</View>
208 </Menu>
209 ) : (
210 primary
211 )}
212 </View>
213 ) : null}
214 <IconButton
215 icon="file.diff"
216 accessibilityLabel={gt("Changed files")}
217 onPress={openFiles}
218 />
219 </View>
220 </Toolbar>
221 </View>
222 );
223 }
224 const primaryControl = showPrimary ? (
225 !pendingReview && chooseThread ? (
226 <Toolbar.Menu
227 identifier="pull-request-primary-action"
228 title={primaryLabel}
229 accessibilityLabel={primaryLabel}
230 variant="prominent"
231 icon={
232 verticalControls
233 ? pendingReview
234 ? "text.bubble"
235 : "bubble.left.and.bubble.right"
236 : undefined
237 }
238 tintColor={colors["--color-prominent-action"]}
239 disabled={primaryDisabled}
240 separateBackground
241 >
242 {renderThreadMenuItems(threadMenuItems, agent.select)}
243 </Toolbar.Menu>
244 ) : (
245 <Toolbar.Button
246 identifier="pull-request-primary-action"
247 label={primaryLabel}
248 accessibilityLabel={primaryLabel}
249 variant="prominent"
250 icon={
251 verticalControls
252 ? pendingReview
253 ? "text.bubble"
254 : "bubble.left.and.bubble.right"
255 : undefined
256 }
257 tintColor={colors["--color-prominent-action"]}
258 disabled={primaryDisabled}
259 onPress={primaryPress}
260 separateBackground
261 />
262 )
263 ) : null;
264 const agentControl = showAgentControls ? (
265 <Toolbar.Menu
266 identifier="pull-request-agent"
267 icon="ellipsis.bubble"
268 title={gt("AI helpers")}
269 accessibilityLabel={agentActionsLabel}
270 disabled={agent.disabled}
271 separateBackground
272 >
273 {agentActions.map((action) =>
274 chooseThread && !action.disabled ? (
275 <Toolbar.Menu key={action.task} title={action.title} icon={action.icon}>
276 {renderThreadMenuItems(agent.threadActions(action.task), agent.select)}
277 </Toolbar.Menu>
278 ) : (
279 <Toolbar.MenuAction
280 key={action.task}
281 icon={action.icon}
282 disabled={action.disabled}
283 onPress={() => agent.start(action.task)}
284 >
285 <Toolbar.Label>{action.title}</Toolbar.Label>
286 </Toolbar.MenuAction>
287 ),
288 )}
289 </Toolbar.Menu>
290 ) : null;
291 const filesControl = (
292 <Toolbar.Button
293 identifier="workspace-trailing-action"
294 icon={{ type: "image", source: require("../../../assets/icons/file-diff.png") }}
295 accessibilityLabel={gt("Changed files")}
296 onPress={openFiles}
297 separateBackground
298 />
299 );
300 // The vertical bar never overflows into a menu, and with a Live Activity it fits about three
301 // buttons above the workspace tabs: back, the primary action and one menu.
302 if (verticalControls)
303 return (
304 <Toolbar placement="bottom">
305 {primaryControl}
306 {createNativeHeaderMenu({
307 ...props.menu,
308 items: [
309 {
310 id: "changed-files",
311 title: gt("Changed files"),
312 icon: "doc.text.magnifyingglass",
313 onPress: openFiles,
314 },
315 ...(showAgentControls
316 ? [
317 {
318 id: "ai-helpers",
319 title: gt("AI helpers"),
320 icon: "ellipsis.bubble",
321 items: agentActions.map((action): AppBarMenuItem =>
322 chooseThread && !action.disabled
323 ? {
324 id: action.task,
325 title: action.title,
326 icon: action.icon,
327 items: appBarThreadMenuItems(
328 agent.threadActions(action.task),
329 agent.select,
330 ),
331 }
332 : {
333 id: action.task,
334 title: action.title,
335 icon: action.icon,
336 disabled: action.disabled || agent.disabled,
337 onPress: () => agent.start(action.task),
338 },
339 ),
340 },
341 ]
342 : []),
343 ...props.menu.items,
344 ],
345 })}
346 </Toolbar>
347 );
348 return (
349 <Toolbar placement="bottom">
350 {agentControl}
351 <Toolbar.Spacer flexible />
352 {primaryControl}
353 <Toolbar.Spacer flexible />
354 {filesControl}
355 </Toolbar>
356 );
357 }
358
359 function appBarThreadMenuItems(
360 items: MenuAction[],
361 select: (id: string) => void,
362 ): ReadonlyArray<AppBarMenuItem> {
363 return items.map((item) =>
364 item.subactions
365 ? {
366 id: item.id ?? item.title,
367 title: item.title,
368 icon: item.image,
369 items: appBarThreadMenuItems(item.subactions, select),
370 }
371 : {
372 id: item.id ?? item.title,
373 title: item.title,
374 icon: item.image,
375 subtitle: item.subtitle,
376 disabled: item.attributes?.disabled,
377 onPress: () => item.id && select(item.id),
378 },
379 );
380 }
381
382 function renderThreadMenuItems(items: MenuAction[], select: (id: string) => void): ReactNode {
383 return items.map((item) =>
384 item.subactions ? (
385 <Toolbar.Menu key={item.id} title={item.title} icon={item.image}>
386 {renderThreadMenuItems(item.subactions, select)}
387 </Toolbar.Menu>
388 ) : (
389 <Toolbar.MenuAction
390 key={item.id}
391 icon={item.image}
392 subtitle={item.subtitle}
393 disabled={item.attributes?.disabled}
394 onPress={() => item.id && select(item.id)}
395 >
396 <Toolbar.Label>{item.title}</Toolbar.Label>
397 </Toolbar.MenuAction>
398 ),
399 );
400 }