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