@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 389 lines 14.2 KB
Raw
1 import { parsePullRequestAnchor } from "@t3tools/shared/pullRequestAnchor";
2 import { usePullRequestConnected } from "./usePullRequestConnected";
3 import { useAtomValue } from "@effect/atom-react";
4 import { useNavigation } from "@react-navigation/native";
5 import type { PullRequestAction, PullRequestDetail } from "@t3tools/contracts";
6 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
7 import { changeRequestTermKind } from "../../lib/changeRequestTerm";
8 import Animated from "react-native-reanimated";
9 import { PR_ROW_LAYOUT, PR_DISCLOSURE_ENTER, PR_ROW_EXIT } from "./pullRequestAnimations";
10 import { T, useGT, useMessages } from "gt-react-native";
11 import { useState } from "react";
12 import { Pressable, View } from "react-native";
13 import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
14 import { AppText as Text } from "../../components/ui/AppText";
15 import { SymbolView } from "../../components/ui/AppSymbol";
16 import { Menu } from "../../components/ui/Menu";
17 import { PullRequestAvatar } from "./PullRequestPresentation";
18 import { PullRequestCardRow } from "./PullRequestControls";
19 import { PullRequestAgentRow } from "./PullRequestAgentRow";
20 import { PullRequestGlyph } from "./pullRequestGlyphs";
21 import { composerPullRequests } from "../../state/pull-requests";
22 import { useEnvironmentQuery } from "../../state/query";
23 import {
24 allowedUpdateMethods,
25 PR_ACTION_ICONS,
26 pullRequestActionLabel,
27 } from "./pullRequestActions";
28 import {
29 checkStatus,
30 isFailingCheck,
31 pullRequestBranchSummary,
32 pullRequestChecksSummary,
33 pullRequestReviewSummary,
34 pullRequestStatus,
35 } from "./pullRequestStatus";
36 import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
37 import { usePullRequestAction } from "./usePullRequestAction";
38 import { usePullRequestAgentActions } from "./usePullRequestAgentActions";
39
40 type RowProps = { readonly pr: PullRequestDetail; readonly params: PullRequestParams };
41 const STATUS_ACTIONS: readonly PullRequestAction[] = [
42 "ready",
43 "draft",
44 "close",
45 "reopen",
46 "revert",
47 ];
48 const 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
58 export 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 );
115 }
116
117 export 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 );
210 }
211
212 export 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 );
278 }
279
280 export 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 ? gt("Approved", { $context: "review state" })
325 : review.reviewState === "CHANGES_REQUESTED"
326 ? gt("Changes requested")
327 : gt("Commented", { $context: "review state" })
328 }`}
329 className="min-h-14 flex-row items-center gap-3 px-4 py-2"
330 >
331 <SymbolView
332 name={
333 review.reviewState === "APPROVED"
334 ? "checkmark"
335 : review.reviewState === "CHANGES_REQUESTED"
336 ? "plusminus"
337 : "bubble.left"
338 }
339 size={18}
340 tintColorClassName={
341 review.reviewState === "APPROVED"
342 ? "accent-adaptive-emerald-600-400"
343 : review.reviewState === "CHANGES_REQUESTED"
344 ? "accent-danger-foreground"
345 : "accent-icon-muted"
346 }
347 />
348 <PullRequestAvatar actor={review.author} size={32} />
349 <Text className="min-w-0 flex-1 text-sm font-t3-medium" numberOfLines={1}>
350 {review.author?.login ?? gt("Unknown reviewer")}
351 </Text>
352 </View>
353 ))}
354 {props.pr.reviewers
355 .filter(
356 (reviewer) =>
357 !summary.reviews.some((review) => review.author?.login === reviewer.login),
358 )
359 .map((reviewer) => (
360 <View
361 key={reviewer.login}
362 accessibilityLabel={gt("{login}, Review requested", { login: reviewer.login })}
363 className="min-h-14 flex-row items-center gap-3 px-4 py-2"
364 >
365 <SymbolView name="clock" size={18} tintColorClassName="accent-icon-muted" />
366 <PullRequestAvatar actor={reviewer} size={32} />
367 <Text className="min-w-0 flex-1 text-sm font-t3-medium" numberOfLines={1}>
368 {reviewer.login}
369 </Text>
370 </View>
371 ))}
372 {canRequest ? (
373 <PullRequestCardRow
374 title={gt("Request review")}
375 icon="person.badge.plus"
376 onPress={() => navigation.navigate("PullRequestReviewers", props.params)}
377 />
378 ) : (
379 <PullRequestCardRow
380 title={gt("View reviewers on provider")}
381 icon="arrow.up.right"
382 onPress={() => void tryOpenExternalUrl(props.pr.url, "pull-request")}
383 />
384 )}
385 </Animated.View>
386 ) : null}
387 </Animated.View>
388 );
389 }