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

PullRequestStatusRows.tsx

387 lines14.0 KB

1import { parsePullRequestAnchor } from "@t3tools/shared/pullRequestAnchor";
2import { usePullRequestConnected } from "./usePullRequestConnected";
3import { useAtomValue } from "@effect/atom-react";
4import { useNavigation } from "@react-navigation/native";
5import type { PullRequestAction, PullRequestDetail } from "@t3tools/contracts";
6import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
7import { changeRequestTermKind } from "../../lib/changeRequestTerm";
8import Animated from "react-native-reanimated";
9import { PR_ROW_LAYOUT, PR_DISCLOSURE_ENTER, PR_ROW_EXIT } from "./pullRequestAnimations";
10import { T, useGT, useMessages } from "gt-react-native";
11import { useState } from "react";
12import { Linking, Pressable, View } from "react-native";
13import { AppText as Text } from "../../components/ui/AppText";
14import { SymbolView } from "../../components/ui/AppSymbol";
15import { Menu } from "../../components/ui/Menu";
16import { PullRequestAvatar } from "./PullRequestPresentation";
17import { PullRequestCardRow } from "./PullRequestControls";
18import { PullRequestAgentRow } from "./PullRequestAgentRow";
19import { PullRequestGlyph } from "./pullRequestGlyphs";
20import { composerPullRequests } from "../../state/pull-requests";
21import { useEnvironmentQuery } from "../../state/query";
22import {
23 allowedUpdateMethods,
24 PR_ACTION_ICONS,
25 pullRequestActionLabel,
26} from "./pullRequestActions";
27import {
28 checkStatus,
29 isFailingCheck,
30 pullRequestBranchSummary,
31 pullRequestChecksSummary,
32 pullRequestReviewSummary,
33 pullRequestStatus,
34} from "./pullRequestStatus";
35import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
36import { usePullRequestAction } from "./usePullRequestAction";
37import { usePullRequestAgentActions } from "./usePullRequestAgentActions";
38​
39type RowProps = { readonly pr: PullRequestDetail; readonly params: PullRequestParams };
40const STATUS_ACTIONS: readonly PullRequestAction[] = [
41 "ready",
42 "draft",
43 "close",
44 "reopen",
45 "revert",
46];
47const CHECK_PRIORITY = {
48 failure: 0,
49 "action-required": 1,
50 pending: 2,
51 cancelled: 3,
52 neutral: 4,
53 skipped: 5,
54 success: 6,
55};
56​
57export function PullRequestStatusMenu(props: RowProps) {
58 const action = usePullRequestAction(props.params, props.pr);
59 const actions = STATUS_ACTIONS.filter(action.available);
60 const gt = useGT();
61 const m = useMessages();
62 const status = pullRequestStatus(props.pr, m);
63 const terminology = sourceControlClients.get(props.pr.provider).changeRequest;
64 const term = changeRequestTermKind(terminology);
65 const trigger = (
66 <Pressable
67 accessibilityRole={actions.length ? "button" : undefined}
68 accessibilityLabel={gt(
69 "{term, select, mr {Merge request} cr {Change request} other {Pull request}} status, {status}",
70 { term, status: status.label },
71 )}
72 disabled={!actions.length}
73 className="min-h-8 flex-row items-center gap-1.5 active:opacity-70"
74 >
75 <SymbolView name={status.icon} size={14} tintColorClassName={status.tint} />
76 <Text className={`text-xs font-t3-medium ${status.text}`}>{status.label}</Text>
77 {actions.length ? (
78 <SymbolView name="chevron.down" size={9} tintColorClassName="accent-icon-muted" />
79 ) : null}
80 </Pressable>
81 );
82 if (!actions.length) return trigger;
83 return (
84 <Menu
85 title={gt(
86 "{term, select, mr {Merge request} cr {Change request} other {Pull request}} status",
87 { term },
88 )}
89 actions={actions.map((id) => ({
90 id,
91 title: pullRequestActionLabel(id, terminology, m),
92 image: PR_ACTION_ICONS[id],
93 attributes: { destructive: id === "close" },
94 }))}
95 onPressAction={({ nativeEvent }) => {
96 const selected = actions.find((id) => id === nativeEvent.event);
97 if (!selected) return;
98 if (selected === "close") action.confirmClose();
99 else if (selected === "revert")
100 action.confirm(
101 selected,
102 pullRequestActionLabel("revert", terminology, m),
103 gt(
104 "Open a new {term, select, mr {merge request} cr {change request} other {pull request}} that reverses these changes?",
105 { term },
106 ),
107 );
108 else void action.perform(selected);
109 }}
110 >
111 {trigger}
112 </Menu>
113 );
115​
116export function PullRequestChecksRow(props: RowProps) {
117 const linked = parsePullRequestAnchor(props.params.navigationUrl)?.kind === "checks";
118 const [expanded, setExpanded] = useState(linked);
119 const [seenNavigation, setSeenNavigation] = useState(props.params.navigationId);
120 if (props.params.navigationId !== seenNavigation) {
121 setSeenNavigation(props.params.navigationId);
122 if (linked) setExpanded(true);
123 }
124 const gt = useGT();
125 const m = useMessages();
126 const checks = pullRequestChecksSummary(props.pr, m);
127 const action = usePullRequestAction(props.params, props.pr);
128 const definition = sourceControlClients.get(props.pr.provider);
129 return (
130 <Animated.View layout={PR_ROW_LAYOUT}>
131 <PullRequestCardRow
132 title={checks.title}
133 icon={checks.icon}
134 tint={checks.tint}
135 value={checks.count}
136 compact
137 accessibilityLabel={gt("Checks, {summary}", { summary: checks.label })}
138 expanded={expanded}
139 onPress={() => setExpanded(!expanded)}
140 />
141 {expanded ? (
142 <Animated.View
143 layout={PR_ROW_LAYOUT}
144 entering={PR_DISCLOSURE_ENTER}
145 exiting={PR_ROW_EXIT}
146 className="pl-8"
147 >
148 {props.pr.state === "open" && props.pr.checks.some(isFailingCheck) ? (
149 <PullRequestAgentRow {...props} task="feedback" title={gt("Fix failing checks")} />
150 ) : null}
151 {action.available("approve-workflows") ? (
152 <PullRequestCardRow
153 title={gt("Approve workflows")}
154 icon="checkmark"
155 detail={gt(
156 "{count, plural, one {{count} waiting for approval} other {{count} waiting for approval}}",
157 {
158 count: props.pr.workflowApprovalsRequired ?? 0,
159 },
160 )}
161 onPress={() =>
162 action.confirm(
163 "approve-workflows",
164 gt("Approve workflows"),
165 gt(
166 "Allow this {term, select, mr {merge request} cr {change request} other {pull request}}''s workflows to run? Review the workflow changes first.",
167 { term: changeRequestTermKind(definition.changeRequest) },
168 ),
169 )
170 }
171 />
172 ) : null}
173 {[...props.pr.checks]
174 .sort((a, b) => CHECK_PRIORITY[a.status] - CHECK_PRIORITY[b.status])
175 .map((check) => {
176 const status = checkStatus(check, m);
177 const url = check.url;
178 return (
179 <PullRequestCardRow
180 key={`${check.name}:${url}`}
181 title={check.name}
182 icon={status.icon}
183 tint={status.tint}
184 value={status.label}
185 detail={check.description ?? undefined}
186 onPress={url ? () => void Linking.openURL(url) : undefined}
187 />
188 );
189 })}
190 {!props.pr.checks.length ? (
191 <Text className="px-4 py-3 text-xs text-foreground-muted">
192 <T>No checks reported.</T>
193 </Text>
194 ) : null}
195 <PullRequestCardRow
196 title={gt("Open checks on provider")}
197 icon="arrow.up.right"
198 onPress={() =>
199 void Linking.openURL(
200 definition.changeRequestChecksUrl?.(props.pr.url) ?? props.pr.url,
201 )
202 }
203 />
204 </Animated.View>
205 ) : null}
206 </Animated.View>
207 );
209​
210export function PullRequestBranchRow(props: RowProps) {
211 const action = usePullRequestAction(props.params, props.pr);
212 const agent = usePullRequestAgentActions(props.params, props.pr);
213 const gt = useGT();
214 const m = useMessages();
215 const methods = action.available("update-branch") ? allowedUpdateMethods(props.pr) : [];
216 const canResolve =
217 agent.canAsk && props.pr.state === "open" && props.pr.mergeability === "conflicting";
218 const summary = action.busy ? gt("Updating…") : pullRequestBranchSummary(props.pr, m);
219 const clean = props.pr.mergeability === "mergeable";
220 const conflicting = props.pr.mergeability === "conflicting";
221 const row = (
222 <PullRequestCardRow
223 title={
224 props.pr.mergeability === "conflicting"
225 ? gt("Conflicts with {branch}", { branch: props.pr.baseBranch })
226 : clean
227 ? gt("No conflicts with {branch}", { branch: props.pr.baseBranch })
228 : gt("Branch", { $context: "git branch" })
229 }
230 icon={
231 conflicting ? PullRequestGlyph.conflicting : clean ? "checkmark.circle" : "arrow.branch"
232 }
233 tint={
234 conflicting
235 ? "accent-danger-foreground"
236 : clean
237 ? "accent-adaptive-emerald-600-400"
238 : undefined
239 }
240 value={
241 conflicting || (clean && props.pr.baseComparison !== "behind" && !action.busy)
242 ? undefined
243 : summary
244 }
245 compact
246 menu={methods.length > 0 || canResolve}
247 accessibilityLabel={gt("Branch, {summary}", { summary })}
248 />
249 );
250 if (!methods.length && !canResolve) return row;
251 return (
252 <Menu
253 title={gt("Branch", { $context: "git branch" })}
254 actions={[
255 ...(canResolve ? [agent.menuAction("conflicts")] : []),
256 ...methods.map((id) => ({
257 id,
258 title: gt(
259 "Update with {method, select, merge {merge} rebase {rebase} other {{method}}}",
260 {
261 method: id,
262 },
263 ),
264 image: "arrow.up.circle",
265 })),
266 ]}
267 onPressAction={({ nativeEvent }) => {
268 const method = methods.find((id) => id === nativeEvent.event);
269 if (method) void action.perform("update-branch", method);
270 else agent.select(nativeEvent.event);
271 }}
272 >
273 {row}
274 </Menu>
275 );
277​
278export function PullRequestReviewersRow(props: RowProps) {
279 const [expanded, setExpanded] = useState(false);
280 const navigation = useNavigation();
281 const gt = useGT();
282 const m = useMessages();
283 const activity = useEnvironmentQuery(
284 composerPullRequests.activity(pullRequestTarget(props.params)),
285 );
286 const summary = pullRequestReviewSummary(props.pr, activity.data, m);
287 const canWrite = useAtomValue(
288 composerPullRequests.requestReviewers.permissionAtom(
289 pullRequestTarget(props.params).environmentId,
290 ),
291 );
292 const connected = usePullRequestConnected(props.params.environmentId);
293 const canRequest =
294 connected &&
295 canWrite &&
296 props.pr.state === "open" &&
297 props.pr.capabilities.reviewers.request &&
298 props.pr.viewerPermissions.requestReviewers;
299 return (
300 <Animated.View layout={PR_ROW_LAYOUT}>
301 <PullRequestCardRow
302 title={summary.title}
303 icon={summary.icon}
304 tint={summary.tint}
305 value={summary.count}
306 compact
307 expanded={expanded}
308 onPress={() => setExpanded(!expanded)}
309 />
310 {expanded ? (
311 <Animated.View
312 layout={PR_ROW_LAYOUT}
313 entering={PR_DISCLOSURE_ENTER}
314 exiting={PR_ROW_EXIT}
315 className="pl-8"
316 >
317 {summary.reviews.map((review) => (
318 <View
319 key={review.id}
320 accessibilityLabel={`${review.author?.login ?? gt("Unknown reviewer")}, ${
321 review.reviewState === "APPROVED"
322 ? gt("Approved", { $context: "review state" })
323 : review.reviewState === "CHANGES_REQUESTED"
324 ? gt("Changes requested")
325 : gt("Commented", { $context: "review state" })
326 }`}
327 className="min-h-14 flex-row items-center gap-3 px-4 py-2"
328 >
329 <SymbolView
330 name={
331 review.reviewState === "APPROVED"
332 ? "checkmark"
333 : review.reviewState === "CHANGES_REQUESTED"
334 ? "plusminus"
335 : "bubble.left"
336 }
337 size={18}
338 tintColorClassName={
339 review.reviewState === "APPROVED"
340 ? "accent-adaptive-emerald-600-400"
341 : review.reviewState === "CHANGES_REQUESTED"
342 ? "accent-danger-foreground"
343 : "accent-icon-muted"
344 }
345 />
346 <PullRequestAvatar actor={review.author} size={32} />
347 <Text className="min-w-0 flex-1 text-sm font-t3-medium" numberOfLines={1}>
348 {review.author?.login ?? gt("Unknown reviewer")}
349 </Text>
350 </View>
351 ))}
352 {props.pr.reviewers
353 .filter(
354 (reviewer) =>
355 !summary.reviews.some((review) => review.author?.login === reviewer.login),
356 )
357 .map((reviewer) => (
358 <View
359 key={reviewer.login}
360 accessibilityLabel={gt("{login}, Review requested", { login: reviewer.login })}
361 className="min-h-14 flex-row items-center gap-3 px-4 py-2"
362 >
363 <SymbolView name="clock" size={18} tintColorClassName="accent-icon-muted" />
364 <PullRequestAvatar actor={reviewer} size={32} />
365 <Text className="min-w-0 flex-1 text-sm font-t3-medium" numberOfLines={1}>
366 {reviewer.login}
367 </Text>
368 </View>
369 ))}
370 {canRequest ? (
371 <PullRequestCardRow
372 title={gt("Request review")}
373 icon="person.badge.plus"
374 onPress={() => navigation.navigate("PullRequestReviewers", props.params)}
375 />
376 ) : (
377 <PullRequestCardRow
378 title={gt("View reviewers on provider")}
379 icon="arrow.up.right"
380 onPress={() => void Linking.openURL(props.pr.url)}
381 />
382 )}
383 </Animated.View>
384 ) : null}
385 </Animated.View>
386 );