Open History to see the last commit
1import Animated from "react-native-reanimated";
2import { PR_ROW_LAYOUT } from "./pullRequestAnimations";
3import { useNavigation } from "@react-navigation/native";
4import type { PullRequestDetail } from "@t3tools/contracts";
5import type { ReactNode } from "react";
6import { Platform, Pressable, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8import { AppText as Text } from "../../components/ui/AppText";
9import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
10import { Button } from "../../components/ui/Button";
11import { IconButton } from "../../components/ui/IconButton";
12import { Toolbar } from "../../components/ui/Toolbar";
13import { Menu } from "../../components/ui/Menu";
14
15import { useNativeLayoutMetrics } from "../../native/native-layout-metrics";
16import { type PullRequestParams } from "./usePullRequestDiff";
17import { usePullRequestPermissions } from "./usePullRequestPermissions";
18import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
19import { usePullRequestReviewDraft } from "./usePullRequestReviewDraft";
20import { usePullRequestAgentActions } from "./usePullRequestAgentActions";
21
22export function PullRequestCardGroup(props: { readonly children: ReactNode }) {
23 return (
24 <Animated.View
25 layout={PR_ROW_LAYOUT}
26 className="overflow-hidden rounded-[22px] border border-composer-border border-continuous bg-grouped-card"
27 >
28 {props.children}
29 </Animated.View>
30 );
31}
32
33export function PullRequestCardRow(props: {
34 readonly title: string;
35 readonly icon: AppSymbolName;
36 readonly tint?: string;
37 readonly value?: string;
38 readonly detail?: string;
39 readonly onPress?: () => void;
40 readonly accessibilityLabel?: string;
41 readonly destructive?: boolean;
42 readonly last?: boolean;
43 readonly compact?: boolean;
44 readonly menu?: boolean;
45 readonly expanded?: boolean;
46 readonly children?: ReactNode;
47}) {
48 return (
49 <Pressable
50 accessibilityRole={props.onPress || props.menu ? "button" : undefined}
51 accessibilityLabel={props.accessibilityLabel}
52 accessibilityState={props.expanded === undefined ? undefined : { expanded: props.expanded }}
53 onPress={props.onPress}
54 disabled={!props.onPress && !props.menu}
55 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" : ""}`}
56 >
57 <SymbolView
58 name={props.icon}
59 size={18}
60 tintColorClassName={
61 props.destructive ? "accent-danger-foreground" : (props.tint ?? "accent-icon-muted")
62 }
63 />
64 <View className="min-w-0 flex-1 gap-0.5">
65 <Text
66 className={
67 props.destructive
68 ? "text-sm font-t3-medium text-danger-foreground"
69 : "text-sm font-t3-medium"
70 }
71 >
72 {props.title}
73 </Text>
74 {props.detail ? (
75 <Text className="text-2xs text-foreground-muted" numberOfLines={2}>
76 {props.detail}
77 </Text>
78 ) : null}
79 </View>
80 {props.children ??
81 (props.value ? (
82 <Text
83 className={`${props.compact ? "max-w-[38%] text-2xs" : "max-w-[60%] text-xs"} shrink text-right text-foreground-muted`}
84 numberOfLines={2}
85 >
86 {props.value}
87 </Text>
88 ) : null)}
89 {props.onPress || props.menu ? (
90 <SymbolView
91 name={
92 props.expanded === undefined
93 ? "chevron.right"
94 : props.expanded
95 ? "chevron.up"
96 : "chevron.down"
97 }
98 size={11}
99 tintColorClassName="accent-icon-muted"
100 />
101 ) : null}
102 </Pressable>
103 );
104}
105
106export function PullRequestActionToolbar(props: {
107 readonly pr: PullRequestDetail | null;
108 readonly params: PullRequestParams;
109 readonly filePath?: string;
110 readonly onOpenFiles?: () => void;
111 readonly onHeightChange?: (height: number) => void;
112}) {
113 const navigation = useNavigation();
114 const { draft } = usePullRequestReviewDraft(props.params);
115 const { themeVariables: colors } = useAppearancePreferences();
116 const insets = useSafeAreaInsets();
117 const nativeMetrics = useNativeLayoutMetrics();
118 const verticalControls = nativeMetrics !== null && nativeMetrics.verticalBarEdge !== "none";
119 const permissions = usePullRequestPermissions(props.params, props.pr);
120 const agent = usePullRequestAgentActions(props.params, props.pr, props.filePath);
121 const canReview = permissions.verdicts.length > 0;
122 const pendingReview = canReview && draft.comments.length > 0;
123 const showAgentControls = agent.canAsk || !permissions.connected;
124 const canOpenThread = agent.threads.length > 0 || showAgentControls;
125 const threadLabel = agent.threads.length ? "Continue thread" : "Work on PR";
126 const primaryLabel = pendingReview ? `Submit review · ${draft.comments.length}` : threadLabel;
127 const chooseThread = agent.threads.length > 1;
128 const agentActions = agent.actions.filter((action) => action.task !== "ask");
129 const open = (mode: "review" | "comment") =>
130 navigation.navigate("PullRequestReview", { ...props.params, mode });
131 const primaryPress = () => (pendingReview ? open("review") : agent.start("ask"));
132 const threadMenuItems = agent.threadActions("ask");
133 const openFiles =
134 props.onOpenFiles ?? (() => navigation.navigate("PullRequestFiles", props.params));
135 if (!props.pr) return null;
136 const primaryDisabled = pendingReview ? false : agent.disabled;
137 const showPrimary = pendingReview || canOpenThread;
138 if (Platform.OS === "android") {
139 const primary = (
140 <Button
141 label={primaryLabel}
142 tone="primary"
143 disabled={primaryDisabled}
144 onPress={primaryPress}
145 />
146 );
147 return (
148 <View
149 pointerEvents="box-none"
150 className="absolute inset-x-0 bottom-0 z-10 items-center px-4 pt-2"
151 style={{ paddingBottom: insets.bottom + 16 }}
152 onLayout={(event) => props.onHeightChange?.(event.nativeEvent.layout.height)}
153 >
154 <Toolbar>
155 <View className="flex-row items-center gap-2">
156 {showAgentControls ? (
157 <Menu
158 title="AI helpers"
159 actions={agentActions.map((action) => agent.menuAction(action.task))}
160 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
161 >
162 <View pointerEvents="none">
163 <IconButton
164 icon="sparkles"
165 accessibilityLabel="PR agent actions"
166 disabled={agent.disabled}
167 />
168 </View>
169 </Menu>
170 ) : null}
171 {showPrimary ? (
172 <View className="min-w-0 shrink">
173 {!pendingReview && chooseThread ? (
174 <Menu
175 title="Open thread"
176 actions={threadMenuItems}
177 onPressAction={({ nativeEvent }) => agent.select(nativeEvent.event)}
178 >
179 <View pointerEvents="none">{primary}</View>
180 </Menu>
181 ) : (
182 primary
183 )}
184 </View>
185 ) : null}
186 <IconButton icon="file.diff" accessibilityLabel="Changed files" onPress={openFiles} />
187 </View>
188 </Toolbar>
189 </View>
190 );
191 }
192 const nativeThreadItems = threadMenuItems.map((item) => (
193 <Toolbar.MenuAction
194 key={item.id}
195 icon={item.image}
196 subtitle={"subtitle" in item ? item.subtitle : undefined}
197 disabled={"attributes" in item && item.attributes.disabled}
198 onPress={() => agent.select(item.id)}
199 >
200 <Toolbar.Label>{item.title}</Toolbar.Label>
201 </Toolbar.MenuAction>
202 ));
203 const primaryControl = showPrimary ? (
204 !pendingReview && chooseThread ? (
205 <Toolbar.Menu
206 identifier="pull-request-primary-action"
207 title={primaryLabel}
208 accessibilityLabel={primaryLabel}
209 variant="prominent"
210 icon={
211 verticalControls
212 ? pendingReview
213 ? "text.bubble"
214 : "bubble.left.and.bubble.right"
215 : undefined
216 }
217 tintColor={colors["--color-prominent-action"]}
218 disabled={primaryDisabled}
219 separateBackground
220 >
221 {nativeThreadItems}
222 </Toolbar.Menu>
223 ) : (
224 <Toolbar.Button
225 identifier="pull-request-primary-action"
226 label={primaryLabel}
227 accessibilityLabel={primaryLabel}
228 variant="prominent"
229 icon={
230 verticalControls
231 ? pendingReview
232 ? "text.bubble"
233 : "bubble.left.and.bubble.right"
234 : undefined
235 }
236 tintColor={colors["--color-prominent-action"]}
237 disabled={primaryDisabled}
238 onPress={primaryPress}
239 separateBackground
240 />
241 )
242 ) : null;
243 const agentControl = showAgentControls ? (
244 <Toolbar.Menu
245 identifier="pull-request-agent"
246 icon="sparkles"
247 title="AI helpers"
248 accessibilityLabel="PR agent actions"
249 disabled={agent.disabled}
250 separateBackground
251 >
252 {agentActions.map((action) =>
253 chooseThread && !action.disabled ? (
254 <Toolbar.Menu key={action.task} title={action.title} icon={action.icon}>
255 {agent.threadActions(action.task).map((item) => (
256 <Toolbar.MenuAction
257 key={item.id}
258 icon={item.image}
259 subtitle={"subtitle" in item ? item.subtitle : undefined}
260 disabled={"attributes" in item && item.attributes.disabled}
261 onPress={() => agent.select(item.id)}
262 >
263 <Toolbar.Label>{item.title}</Toolbar.Label>
264 </Toolbar.MenuAction>
265 ))}
266 </Toolbar.Menu>
267 ) : (
268 <Toolbar.MenuAction
269 key={action.task}
270 icon={action.icon}
271 disabled={action.disabled}
272 onPress={() => agent.start(action.task)}
273 >
274 <Toolbar.Label>{action.title}</Toolbar.Label>
275 </Toolbar.MenuAction>
276 ),
277 )}
278 </Toolbar.Menu>
279 ) : null;
280 const filesControl = (
281 <Toolbar.Button
282 identifier="workspace-trailing-action"
283 icon={{ type: "image", source: require("../../../assets/icons/file-diff.png") }}
284 accessibilityLabel="Changed files"
285 onPress={openFiles}
286 separateBackground
287 />
288 );
289 if (verticalControls)
290 return (
291 <Toolbar placement="bottom">
292 {filesControl}
293 {agentControl}
294 {primaryControl}
295 </Toolbar>
296 );
297 return (
298 <Toolbar placement="bottom">
299 {agentControl}
300 <Toolbar.Spacer flexible />
301 {primaryControl}
302 <Toolbar.Spacer flexible />
303 {filesControl}
304 </Toolbar>
305 );
306}