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

PullRequestStatusRows.tsx

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