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

LinkedPullRequestList.tsx

174 lines7.3 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import {
3 pullRequestListLines,
4 resolveThreadPullRequestChains,
5 threadPullRequestKeyOf,
6 type PullRequestListLine,
7} from "@t3tools/shared/threadPullRequests";
8import { memo, useMemo } from "react";
9import { Pressable, useWindowDimensions, View } from "react-native";
10​
11import { SymbolView } from "../../components/ui/AppSymbol";
12import { AppText as Text } from "../../components/ui/AppText";
13import * as Dialog from "../../components/ui/Dialog";
14import { Menu } from "../../components/ui/Menu";
15import { cn } from "../../lib/cn";
16import { relativeTime } from "../../lib/time";
17import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types";
18import { useScaledTextRole } from "../settings/appearance/useScaledTextRole";
19import { PullRequestGlyph } from "../pull-requests/pullRequestGlyphs";
20import type { LinkedPullRequestActions } from "./useLinkedPullRequestActions";
21import { pullRequestListReviewStatus } from "../pull-requests/pullRequestList";
22import {
23 PullRequestAvatar,
24 PullRequestConflictBadge,
25 PullRequestDiffStats,
26} from "../pull-requests/PullRequestPresentation";
27import {
28 pullRequestChecksStatus,
29 pullRequestConflict,
30 pullRequestStatus,
31} from "../pull-requests/pullRequestStatus";
32​
33export function useLinkedPullRequestLines(
34 thread: EnvironmentThreadShell | null,
35 runtime: EnvironmentRuntimeState | null,
36) {
37 const supported = runtime?.serverConfig?.environment.capabilities.threadPullRequests === true;
38 const pullRequests = thread?.pullRequests;
39 return useMemo(() => {
40 const chains = resolveThreadPullRequestChains(supported ? (pullRequests ?? []) : []);
41 const open = chains.filter((chain) => chain.layers.some(isOpenLink));
42 const done = chains.filter((chain) => !chain.layers.some(isOpenLink));
43 return [...pullRequestListLines(open), ...pullRequestListLines(done)];
44 }, [pullRequests, supported]);
46​
47function isOpenLink(link: PullRequestListLine["link"]) {
48 return link.snapshot === null || link.snapshot.state === "open";
50​
51export function linkedPullRequestKey(line: PullRequestListLine) {
52 return threadPullRequestKeyOf(line.link);
54​
55export const LinkedPullRequestRow = memo(function LinkedPullRequestRow(props: {
56 readonly line: PullRequestListLine;
57 readonly separator: boolean;
58 readonly surfaceClassName: string;
59 readonly openPullRequest: (url: string) => Promise<boolean>;
60 readonly actions: LinkedPullRequestActions | null;
61}) {
62 const { link, depth, stack } = props.line;
63 const snapshot = link.snapshot;
64 const status = snapshot === null ? null : pullRequestStatus(snapshot);
65 const conflict = snapshot === null ? null : pullRequestConflict(snapshot);
66 const open = snapshot === null || snapshot.state === "open";
67 const checks =
68 open && snapshot?.checksState ? pullRequestChecksStatus(snapshot.checksState) : null;
69 const review =
70 open && snapshot?.reviewDecision
71 ? pullRequestListReviewStatus({
72 reviewDecision: snapshot.reviewDecision,
73 viewerReviewRequested: false,
74 })
75 : null;
76 const watching = open && link.watch !== undefined;
77 const additions = snapshot?.additions ?? 0;
78 const deletions = snapshot?.deletions ?? 0;
79 const metaLineHeight = useScaledTextRole("label").lineHeight * useWindowDimensions().fontScale;
80 const row = (
81 <Pressable
82 accessibilityRole="button"
83 accessibilityLabel={`Pull request ${link.number}, ${snapshot?.title ?? "waiting for host state"}${status ? `, ${status.label}` : ""}${conflict ? `, ${conflict.label.toLowerCase()}` : ""}, ${link.repository}${stack ? `, stack of ${stack.size}` : ""}${watching ? ", watching" : ""}${checks ? `, checks ${snapshot?.checksState}` : ""}${review ? `, ${review.accessibleLabel}` : ""}`}
84 accessibilityHint={props.actions ? "Touch and hold for actions." : undefined}
85 onPress={() => {
86 void props.openPullRequest(link.url).then((opened) => {
87 if (!opened) Dialog.alert("Unable to open PR", "The pull request could not be opened.");
88 });
89 }}
90 // Steps in per layer like the web list, capped so deep stacks keep their width.
91 style={{ paddingLeft: 16 + Math.min(depth, 3) * 16 }}
92 className="flex-row gap-3 rounded-xl pr-4 active:bg-row-hover"
93 >
94 {depth > 0 ? <View className="-ml-2 w-px self-stretch bg-border" /> : null}
95 <View className="self-start pt-2.5">
96 <SymbolView
97 name={status?.icon ?? PullRequestGlyph.pullRequest}
98 size={20}
99 tintColorClassName={status?.tint ?? "accent-icon-muted"}
100 />
101 {conflict ? <PullRequestConflictBadge surfaceClassName={props.surfaceClassName} /> : null}
102 </View>
103 <View
104 className={cn(
105 "min-w-0 flex-1 gap-1 py-2.5",
106 props.separator && "border-b border-border-subtle",
107 )}
108 >
109 <View className="flex-row items-center gap-1.5">
110 {snapshot?.author ? <PullRequestAvatar actor={snapshot.author} size={16} /> : null}
111 <Text className="min-w-0 shrink text-xs text-foreground-muted" numberOfLines={1}>
112 {link.repository}
113 </Text>
114 <Text className="shrink-0 text-xs text-foreground-tertiary">#{link.number}</Text>
115 <View className="flex-1" />
116 {stack ? (
117 <View className="flex-row items-center gap-1">
118 <SymbolView
119 name={PullRequestGlyph.stack}
120 size={12}
121 tintColorClassName="accent-foreground-muted"
122 />
123 <Text className="text-2xs tabular-nums text-foreground-muted">{stack.size}</Text>
124 </View>
125 ) : null}
126 {watching ? (
127 <SymbolView name="eye" size={13} tintColorClassName="accent-foreground-muted" />
128 ) : null}
129 {checks ? (
130 <SymbolView name={checks.icon} size={13} tintColorClassName={checks.tint} />
131 ) : null}
132 {review ? (
133 <SymbolView name={review.icon} size={13} tintColorClassName={review.tint} />
134 ) : null}
135 {additions + deletions > 0 || snapshot?.updatedAt ? (
136 <View
137 className="-ml-1.5 flex-row flex-wrap items-center gap-x-1.5 overflow-hidden"
138 style={{ height: metaLineHeight, flexShrink: 100 }}
139 >
140 <View className="w-0" />
141 {additions + deletions > 0 ? (
142 <PullRequestDiffStats additions={additions} deletions={deletions} compact />
143 ) : null}
144 {snapshot?.updatedAt ? (
145 <Text className="text-2xs tabular-nums text-foreground-tertiary">
146 {relativeTime(snapshot.updatedAt)}
147 </Text>
148 ) : null}
149 </View>
150 ) : null}
151 </View>
152 <Text
153 className="text-base leading-5 font-t3-medium text-foreground"
154 numberOfLines={2}
155 ellipsizeMode="tail"
156 >
157 {snapshot?.title ?? "Waiting for host state"}
158 </Text>
159 </View>
160 </Pressable>
161 );
162 const actions = props.actions;
163 if (actions === null) return row;
164 return (
165 <Menu
166 shouldOpenOnLongPress
167 title={`#${link.number} · ${link.repository}`}
168 actions={actions.menuActions(link)}
169 onPressAction={({ nativeEvent }) => actions.onMenuAction(link, nativeEvent.event)}
170 >
171 {row}
172 </Menu>
173 );
174});