Open History to see the last commit
1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import { changeRequestTermKind } from "../../lib/changeRequestTerm";
3import Animated from "react-native-reanimated";
4import { PR_ROW_LAYOUT } from "./pullRequestAnimations";
5import { useNavigation } from "@react-navigation/native";
6import type { PullRequestDetail } from "@t3tools/contracts";
7import type { MenuAction } from "@react-native-menu/menu";
8import { useGT, select as gtSelect } from "gt-react-native";
9import type { ReactNode } from "react";
10import { Platform, Pressable, View } from "react-native";
11import { useSafeAreaInsets } from "react-native-safe-area-context";
12import { AppText as Text } from "../../components/ui/AppText";
13import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
14import { Button } from "../../components/ui/Button";
15import { IconButton } from "../../components/ui/IconButton";
16import { Toolbar } from "../../components/ui/Toolbar";
17import { Menu } from "../../components/ui/Menu";
18import type { AppBarMenu, AppBarMenuItem } from "../../components/ui/AppBar.types";
19import { createNativeHeaderMenu } from "../../components/ui/nativeHeaderMenu.ios";
20
21import { useNativeLayoutMetrics } from "../../native/native-layout-metrics";
22import { type PullRequestParams } from "./usePullRequestDiff";
23import { usePullRequestPermissions } from "./usePullRequestPermissions";
24import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
25import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft";
26import { usePullRequestAgentActions } from "./usePullRequestAgentActions";
27
28export 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
39export 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. */
113export function usePullRequestVerticalControls() {
114 const nativeMetrics = useNativeLayoutMetrics();
115 return nativeMetrics !== null && nativeMetrics.verticalBarEdge !== "none";
116}
117
118export function PullRequestActionToolbar(props: {
119 readonly pr: PullRequestDetail | null;
120 readonly params: PullRequestParams;
121 readonly filePath?: string;
122 /** The head of the diff on screen, when the screen shows one. */
123 readonly reviewedHeadSha?: string | undefined;
124 /** The screen's actions menu; the vertical bar shows it here with the toolbar's tools. */
125 readonly menu: AppBarMenu;
126 readonly onOpenFiles?: () => void;
127 readonly onHeightChange?: (height: number) => void;
128}) {
129 const navigation = useNavigation();
130 const gt = useGT();
131 const { draft } = usePullRequestReviewDraft(props.params);
132 const { themeVariables: colors } = useAppearancePreferences();
133 const insets = useSafeAreaInsets();
134 const verticalControls = usePullRequestVerticalControls();
135 const permissions = usePullRequestPermissions(props.params, props.pr);
136 const agent = usePullRequestAgentActions(props.params, props.pr, props.filePath);
137 const canReview = permissions.verdicts.length > 0;
138 const pendingReview = canReview && draft.comments.length > 0;
139 const showAgentControls = agent.canAsk || !permissions.connected;
140 const canOpenThread = agent.threads.length > 0 || showAgentControls;
141 const term = changeRequestTermKind(
142 sourceControlClients.get((props.pr ?? props.params.initialSummary)?.provider).changeRequest,
143 );
144 const threadLabel = agent.threads.length
145 ? gt`Continue thread`
146 : gt`Work on ${gtSelect({ term }, { mr: "MR", cr: "change request", other: "PR" })}`;
147 const primaryLabel = pendingReview
148 ? gt`Submit review · ${{ count: draft.comments.length }}`
149 : threadLabel;
150 const agentActionsLabel = gt`${gtSelect({ term }, { mr: "MR", cr: "change request", other: "PR" })} agent actions`;
151 const chooseThread = agent.chooseThread;
152 const agentActions = agent.actions.filter((action) => action.task !== "ask");
153 const open = (mode: "review" | "comment") =>
154 navigation.navigate("PullRequestReview", {
155 ...props.params,
156 mode,
157 reviewedHeadSha: props.reviewedHeadSha ?? props.pr?.headSha,
158 });
159 const primaryPress = () => (pendingReview ? open("review") : agent.start("ask"));
160 const threadMenuItems = agent.threadActions("ask");
161 const openFiles =
162 props.onOpenFiles ?? (() => navigation.navigate("PullRequestFiles", props.params));
163 if (!props.pr)
164 return verticalControls ? (
165 <Toolbar placement="bottom">{createNativeHeaderMenu(props.menu)}</Toolbar>
166 ) : null;
167 const primaryDisabled = pendingReview ? false : agent.disabled;
168 const showPrimary = pendingReview || canOpenThread;
169 if (Platform.OS === "android") {
170 const primary = (
171 <Button
172 label={primaryLabel}
173 tone="primary"
174 disabled={primaryDisabled}
175 onPress={primaryPress}
176 />
177 );
178 return (
179 <View
180 pointerEvents="box-none"
181 className="absolute inset-x-0 bottom-0 z-10 items-center px-4 pt-2"
182 style={{ paddingBottom: insets.bottom + 16 }}
183 onLayout={(event) => props.onHeightChange?.(event.nativeEvent.layout.height)}
184 >
185 <Toolbar>
186 <View className="flex-row items-center gap-2">
187 {showAgentControls ? (
188 <Menu
189 title={gt`AI helpers`}
190 actions={agentActions.map((action) => agent.menuAction(action.task))}
191 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
192 >
193 <View pointerEvents="none">
194 <IconButton
195 icon="ellipsis.bubble"
196 accessibilityLabel={agentActionsLabel}
197 disabled={agent.disabled}
198 />
199 </View>
200 </Menu>
201 ) : null}
202 {showPrimary ? (
203 <View className="min-w-0 shrink">
204 {!pendingReview && chooseThread ? (
205 <Menu
206 title={gt`Open thread`}
207 actions={threadMenuItems}
208 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
209 >
210 <View pointerEvents="none">{primary}</View>
211 </Menu>
212 ) : (
213 primary
214 )}
215 </View>
216 ) : null}
217 <IconButton
218 icon="file.diff"
219 accessibilityLabel={gt`Changed files`}
220 onPress={openFiles}
221 />
222 </View>
223 </Toolbar>
224 </View>
225 );
226 }
227 const primaryControl = showPrimary ? (
228 !pendingReview && chooseThread ? (
229 <Toolbar.Menu
230 identifier="pull-request-primary-action"
231 title={primaryLabel}
232 accessibilityLabel={primaryLabel}
233 variant="prominent"
234 icon={
235 verticalControls
236 ? pendingReview
237 ? "text.bubble"
238 : "bubble.left.and.bubble.right"
239 : undefined
240 }
241 tintColor={colors["--color-prominent-action"]}
242 disabled={primaryDisabled}
243 separateBackground
244 >
245 {renderThreadMenuItems(threadMenuItems, agent.select)}
246 </Toolbar.Menu>
247 ) : (
248 <Toolbar.Button
249 identifier="pull-request-primary-action"
250 label={primaryLabel}
251 accessibilityLabel={primaryLabel}
252 variant="prominent"
253 icon={
254 verticalControls
255 ? pendingReview
256 ? "text.bubble"
257 : "bubble.left.and.bubble.right"
258 : undefined
259 }
260 tintColor={colors["--color-prominent-action"]}
261 disabled={primaryDisabled}
262 onPress={primaryPress}
263 separateBackground
264 />
265 )
266 ) : null;
267 const agentControl = showAgentControls ? (
268 <Toolbar.Menu
269 identifier="pull-request-agent"
270 icon="ellipsis.bubble"
271 title={gt`AI helpers`}
272 accessibilityLabel={agentActionsLabel}
273 disabled={agent.disabled}
274 separateBackground
275 >
276 {agentActions.map((action) =>
277 chooseThread && !action.disabled ? (
278 <Toolbar.Menu key={action.task} title={action.title} icon={action.icon}>
279 {renderThreadMenuItems(agent.threadActions(action.task), agent.select)}
280 </Toolbar.Menu>
281 ) : (
282 <Toolbar.MenuAction
283 key={action.task}
284 icon={action.icon}
285 disabled={action.disabled}
286 onPress={() => agent.start(action.task)}
287 >
288 <Toolbar.Label>{action.title}</Toolbar.Label>
289 </Toolbar.MenuAction>
290 ),
291 )}
292 </Toolbar.Menu>
293 ) : null;
294 const filesControl = (
295 <Toolbar.Button
296 identifier="workspace-trailing-action"
297 icon={{ type: "image", source: require("../../../assets/icons/file-diff.png") }}
298 accessibilityLabel={gt`Changed files`}
299 onPress={openFiles}
300 separateBackground
301 />
302 );
303 // The vertical bar never overflows into a menu, and with a Live Activity it fits about three
304 // buttons above the workspace tabs: back, the primary action and one menu.
305 if (verticalControls)
306 return (
307 <Toolbar placement="bottom">
308 {primaryControl}
309 {createNativeHeaderMenu({
310 ...props.menu,
311 items: [
312 {
313 id: "changed-files",
314 title: gt`Changed files`,
315 icon: "doc.text.magnifyingglass",
316 onPress: openFiles,
317 },
318 ...(showAgentControls
319 ? [
320 {
321 id: "ai-helpers",
322 title: gt`AI helpers`,
323 icon: "ellipsis.bubble",
324 items: agentActions.map((action): AppBarMenuItem =>
325 chooseThread && !action.disabled
326 ? {
327 id: action.task,
328 title: action.title,
329 icon: action.icon,
330 items: appBarThreadMenuItems(
331 agent.threadActions(action.task),
332 agent.select,
333 ),
334 }
335 : {
336 id: action.task,
337 title: action.title,
338 icon: action.icon,
339 disabled: action.disabled || agent.disabled,
340 onPress: () => agent.start(action.task),
341 },
342 ),
343 },
344 ]
345 : []),
346 ...props.menu.items,
347 ],
348 })}
349 </Toolbar>
350 );
351 return (
352 <Toolbar placement="bottom">
353 {agentControl}
354 <Toolbar.Spacer flexible />
355 {primaryControl}
356 <Toolbar.Spacer flexible />
357 {filesControl}
358 </Toolbar>
359 );
360}
361
362function appBarThreadMenuItems(
363 items: MenuAction[],
364 select: (id: string) => void,
365): ReadonlyArray<AppBarMenuItem> {
366 return items.map((item) =>
367 item.subactions
368 ? {
369 id: item.id ?? item.title,
370 title: item.title,
371 icon: item.image,
372 items: appBarThreadMenuItems(item.subactions, select),
373 }
374 : {
375 id: item.id ?? item.title,
376 title: item.title,
377 icon: item.image,
378 subtitle: item.subtitle,
379 disabled: item.attributes?.disabled,
380 onPress: () => item.id && select(item.id),
381 },
382 );
383}
384
385function renderThreadMenuItems(items: MenuAction[], select: (id: string) => void): ReactNode {
386 return items.map((item) =>
387 item.subactions ? (
388 <Toolbar.Menu key={item.id} title={item.title} icon={item.image}>
389 {renderThreadMenuItems(item.subactions, select)}
390 </Toolbar.Menu>
391 ) : (
392 <Toolbar.MenuAction
393 key={item.id}
394 icon={item.image}
395 subtitle={item.subtitle}
396 disabled={item.attributes?.disabled}
397 onPress={() => item.id && select(item.id)}
398 >
399 <Toolbar.Label>{item.title}</Toolbar.Label>
400 </Toolbar.MenuAction>
401 ),
402 );
403}