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 } 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 {term, select, mr {MR} cr {change request} other {PR}}", { term });
147 const primaryLabel = pendingReview
148 ? gt("Submit review · {count}", { count: draft.comments.length })
149 : threadLabel;
150 const agentActionsLabel = gt(
151 "{term, select, mr {MR} cr {change request} other {PR}} agent actions",
152 { term },
153 );
154 const chooseThread = agent.chooseThread;
155 const agentActions = agent.actions.filter((action) => action.task !== "ask");
156 const open = (mode: "review" | "comment") =>
157 navigation.navigate("PullRequestReview", {
158 ...props.params,
159 mode,
160 reviewedHeadSha: props.reviewedHeadSha ?? props.pr?.headSha,
161 });
162 const primaryPress = () => (pendingReview ? open("review") : agent.start("ask"));
163 const threadMenuItems = agent.threadActions("ask");
164 const openFiles =
165 props.onOpenFiles ?? (() => navigation.navigate("PullRequestFiles", props.params));
166 if (!props.pr)
167 return verticalControls ? (
168 <Toolbar placement="bottom">{createNativeHeaderMenu(props.menu)}</Toolbar>
169 ) : null;
170 const primaryDisabled = pendingReview ? false : agent.disabled;
171 const showPrimary = pendingReview || canOpenThread;
172 if (Platform.OS === "android") {
173 const primary = (
174 <Button
175 label={primaryLabel}
176 tone="primary"
177 disabled={primaryDisabled}
178 onPress={primaryPress}
179 />
180 );
181 return (
182 <View
183 pointerEvents="box-none"
184 className="absolute inset-x-0 bottom-0 z-10 items-center px-4 pt-2"
185 style={{ paddingBottom: insets.bottom + 16 }}
186 onLayout={(event) => props.onHeightChange?.(event.nativeEvent.layout.height)}
187 >
188 <Toolbar>
189 <View className="flex-row items-center gap-2">
190 {showAgentControls ? (
191 <Menu
192 title={gt("AI helpers")}
193 actions={agentActions.map((action) => agent.menuAction(action.task))}
194 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
195 >
196 <View pointerEvents="none">
197 <IconButton
198 icon="ellipsis.bubble"
199 accessibilityLabel={agentActionsLabel}
200 disabled={agent.disabled}
201 />
202 </View>
203 </Menu>
204 ) : null}
205 {showPrimary ? (
206 <View className="min-w-0 shrink">
207 {!pendingReview && chooseThread ? (
208 <Menu
209 title={gt("Open thread")}
210 actions={threadMenuItems}
211 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
212 >
213 <View pointerEvents="none">{primary}</View>
214 </Menu>
215 ) : (
216 primary
217 )}
218 </View>
219 ) : null}
220 <IconButton
221 icon="file.diff"
222 accessibilityLabel={gt("Changed files")}
223 onPress={openFiles}
224 />
225 </View>
226 </Toolbar>
227 </View>
228 );
229 }
230 const primaryControl = showPrimary ? (
231 !pendingReview && chooseThread ? (
232 <Toolbar.Menu
233 identifier="pull-request-primary-action"
234 title={primaryLabel}
235 accessibilityLabel={primaryLabel}
236 variant="prominent"
237 icon={
238 verticalControls
239 ? pendingReview
240 ? "text.bubble"
241 : "bubble.left.and.bubble.right"
242 : undefined
243 }
244 tintColor={colors["--color-prominent-action"]}
245 disabled={primaryDisabled}
246 separateBackground
247 >
248 {renderThreadMenuItems(threadMenuItems, agent.select)}
249 </Toolbar.Menu>
250 ) : (
251 <Toolbar.Button
252 identifier="pull-request-primary-action"
253 label={primaryLabel}
254 accessibilityLabel={primaryLabel}
255 variant="prominent"
256 icon={
257 verticalControls
258 ? pendingReview
259 ? "text.bubble"
260 : "bubble.left.and.bubble.right"
261 : undefined
262 }
263 tintColor={colors["--color-prominent-action"]}
264 disabled={primaryDisabled}
265 onPress={primaryPress}
266 separateBackground
267 />
268 )
269 ) : null;
270 const agentControl = showAgentControls ? (
271 <Toolbar.Menu
272 identifier="pull-request-agent"
273 icon="ellipsis.bubble"
274 title={gt("AI helpers")}
275 accessibilityLabel={agentActionsLabel}
276 disabled={agent.disabled}
277 separateBackground
278 >
279 {agentActions.map((action) =>
280 chooseThread && !action.disabled ? (
281 <Toolbar.Menu key={action.task} title={action.title} icon={action.icon}>
282 {renderThreadMenuItems(agent.threadActions(action.task), agent.select)}
283 </Toolbar.Menu>
284 ) : (
285 <Toolbar.MenuAction
286 key={action.task}
287 icon={action.icon}
288 disabled={action.disabled}
289 onPress={() => agent.start(action.task)}
290 >
291 <Toolbar.Label>{action.title}</Toolbar.Label>
292 </Toolbar.MenuAction>
293 ),
294 )}
295 </Toolbar.Menu>
296 ) : null;
297 const filesControl = (
298 <Toolbar.Button
299 identifier="workspace-trailing-action"
300 icon={{ type: "image", source: require("../../../assets/icons/file-diff.png") }}
301 accessibilityLabel={gt("Changed files")}
302 onPress={openFiles}
303 separateBackground
304 />
305 );
306 // The vertical bar never overflows into a menu, and with a Live Activity it fits about three
307 // buttons above the workspace tabs: back, the primary action and one menu.
308 if (verticalControls)
309 return (
310 <Toolbar placement="bottom">
311 {primaryControl}
312 {createNativeHeaderMenu({
313 ...props.menu,
314 items: [
315 {
316 id: "changed-files",
317 title: gt("Changed files"),
318 icon: "doc.text.magnifyingglass",
319 onPress: openFiles,
320 },
321 ...(showAgentControls
322 ? [
323 {
324 id: "ai-helpers",
325 title: gt("AI helpers"),
326 icon: "ellipsis.bubble",
327 items: agentActions.map((action): AppBarMenuItem =>
328 chooseThread && !action.disabled
329 ? {
330 id: action.task,
331 title: action.title,
332 icon: action.icon,
333 items: appBarThreadMenuItems(
334 agent.threadActions(action.task),
335 agent.select,
336 ),
337 }
338 : {
339 id: action.task,
340 title: action.title,
341 icon: action.icon,
342 disabled: action.disabled || agent.disabled,
343 onPress: () => agent.start(action.task),
344 },
345 ),
346 },
347 ]
348 : []),
349 ...props.menu.items,
350 ],
351 })}
352 </Toolbar>
353 );
354 return (
355 <Toolbar placement="bottom">
356 {agentControl}
357 <Toolbar.Spacer flexible />
358 {primaryControl}
359 <Toolbar.Spacer flexible />
360 {filesControl}
361 </Toolbar>
362 );
363}
364
365function appBarThreadMenuItems(
366 items: MenuAction[],
367 select: (id: string) => void,
368): ReadonlyArray<AppBarMenuItem> {
369 return items.map((item) =>
370 item.subactions
371 ? {
372 id: item.id ?? item.title,
373 title: item.title,
374 icon: item.image,
375 items: appBarThreadMenuItems(item.subactions, select),
376 }
377 : {
378 id: item.id ?? item.title,
379 title: item.title,
380 icon: item.image,
381 subtitle: item.subtitle,
382 disabled: item.attributes?.disabled,
383 onPress: () => item.id && select(item.id),
384 },
385 );
386}
387
388function renderThreadMenuItems(items: MenuAction[], select: (id: string) => void): ReactNode {
389 return items.map((item) =>
390 item.subactions ? (
391 <Toolbar.Menu key={item.id} title={item.title} icon={item.image}>
392 {renderThreadMenuItems(item.subactions, select)}
393 </Toolbar.Menu>
394 ) : (
395 <Toolbar.MenuAction
396 key={item.id}
397 icon={item.image}
398 subtitle={item.subtitle}
399 disabled={item.attributes?.disabled}
400 onPress={() => item.id && select(item.id)}
401 >
402 <Toolbar.Label>{item.title}</Toolbar.Label>
403 </Toolbar.MenuAction>
404 ),
405 );
406}