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