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