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

PullRequestControls.tsx

310 lines11.0 KB

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