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

PullRequestStatusRows.tsx

339 lines12.1 KB

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