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

PullRequestListRow.tsx

210 lines9.0 KB

1import {
2 capitalizeTerm,
3 sourceControlClients,
4} from "@t3tools/client-runtime/source-control-clients";
5import { memo } from "react";
6import { Pressable, View } from "react-native";
7import { AppText as Text } from "../../components/ui/AppText";
8import { SymbolView } from "../../components/ui/AppSymbol";
9import { Menu } from "../../components/ui/Menu";
10import type { MenuProps } from "../../components/ui/Menu.types";
11import { cn } from "../../lib/cn";
12import { RelativeTime } from "../../components/RelativeTime";
13import {
14 selectedThreadRowColors,
15 THREAD_LIST_V2_SIDEBAR_ROW_STYLE,
16 THREAD_LIST_V2_SIDEBAR_ROW_CONTAINER_STYLE,
17} from "../threads/thread-list-v2-row-appearance";
18import {
19 PullRequestAvatar,
20 PullRequestConflictBadge,
21 PullRequestDiffStats,
22} from "./PullRequestPresentation";
23import {
24 pullRequestLabelColor,
25 pullRequestListReviewStatus,
26 pullRequestListRowsAreEqual,
27 type EnvironmentPullRequestListEntry,
28} from "./pullRequestList";
29import {
30 pullRequestChecksStatus,
31 pullRequestConflict,
32 pullRequestStatus,
33} from "./pullRequestStatus";
34import { PullRequestGlyph } from "./pullRequestGlyphs";
35​
36export const PullRequestListRow = memo(
37 function PullRequestListRow(props: {
38 readonly item: EnvironmentPullRequestListEntry;
39 readonly onSelect: (item: EnvironmentPullRequestListEntry) => void;
40 readonly selected: boolean;
41 readonly sidebar: boolean;
42 readonly busy: boolean;
43 readonly getActions: (item: EnvironmentPullRequestListEntry) => MenuProps["actions"];
44 readonly onAction: (item: EnvironmentPullRequestListEntry, action: string) => void;
45 }) {
46 const { item } = props;
47 const foreground = props.selected
48 ? selectedThreadRowColors.foregroundClassName
49 : props.sidebar
50 ? "text-drawer-foreground"
51 : "text-foreground";
52 const mutedForeground = props.selected
53 ? selectedThreadRowColors.mutedForegroundClassName
54 : props.sidebar
55 ? "text-drawer-foreground-muted"
56 : "text-foreground-muted";
57 const tertiaryForeground = props.selected
58 ? selectedThreadRowColors.mutedForegroundClassName
59 : props.sidebar
60 ? "text-drawer-foreground-muted"
61 : "text-foreground-tertiary";
62 const status = pullRequestStatus(item);
63 const conflict = pullRequestConflict(item);
64 const review = pullRequestListReviewStatus(item);
65 const checks = item.checksState ? pullRequestChecksStatus(item.checksState) : null;
66 return (
67 <View style={props.sidebar ? THREAD_LIST_V2_SIDEBAR_ROW_CONTAINER_STYLE : undefined}>
68 <Menu
69 shouldOpenOnLongPress
70 title={`#${item.number} · ${item.repository}`}
71 actions={props.getActions(item)}
72 onPressAction={({ nativeEvent }) => props.onAction(item, nativeEvent.event)}
73 >
74 <Pressable
75 accessibilityRole="button"
76 accessibilityState={{ selected: props.selected, busy: props.busy }}
77 accessibilityLabel={`${capitalizeTerm(sourceControlClients.get(item.provider).changeRequest.singular)} ${item.number}, ${item.title}, ${status.label}${conflict ? `, ${conflict.label.toLowerCase()}` : ""}, by ${item.author?.login ?? "unknown author"}${review ? `, ${review.accessibleLabel}` : ""}${item.checksState ? `, checks ${item.checksState}` : ""}${item.stack ? `, stack ${item.stack.position} of ${item.stack.size}` : ""}${item.labels.length ? `, ${item.labels.map((label) => label.name).join(", ")}` : ""}, ${item.additions} additions, ${item.deletions} deletions`}
78 accessibilityHint="Double tap to open. Touch and hold for quick actions."
79 onPress={() => props.onSelect(item)}
80 disabled={props.busy}
81 style={props.sidebar ? THREAD_LIST_V2_SIDEBAR_ROW_STYLE : undefined}
82 className={cn(
83 "flex-row gap-3 overflow-hidden",
84 !props.sidebar && "px-4 active:bg-row-hover",
85 props.sidebar && "bg-drawer",
86 props.sidebar && !props.selected && "active:bg-thread-hover",
87 props.selected && "bg-thread-selected",
88 props.busy && "opacity-50",
89 )}
90 >
91 <View className={cn("self-start", props.sidebar ? "pt-0.5" : "pt-2.5")}>
92 <SymbolView name={status.icon} size={20} tintColorClassName={status.tint} />
93 {conflict ? (
94 <PullRequestConflictBadge
95 surfaceClassName={
96 props.selected
97 ? "text-thread-selected"
98 : props.sidebar
99 ? "text-drawer"
100 : "text-screen"
101 }
102 />
103 ) : null}
104 </View>
105 <View
106 className={cn(
107 "min-w-0 flex-1 gap-1",
108 !props.sidebar && "border-b border-border-subtle py-2.5",
109 )}
110 >
111 <View className="flex-row items-center gap-1.5">
112 <PullRequestAvatar actor={item.author} size={16} />
113 <Text className={cn("min-w-0 flex-1 text-xs", mutedForeground)} numberOfLines={1}>
114 {item.repository} <Text className={tertiaryForeground}>#{item.number}</Text>
115 </Text>
116 {item.stack ? (
117 <View className="flex-row items-center gap-1">
118 <SymbolView
119 name={PullRequestGlyph.stack}
120 size={12}
121 tintColorClassName={
122 props.selected
123 ? selectedThreadRowColors.mutedIconTintClassName
124 : props.sidebar
125 ? "accent-drawer-foreground-muted"
126 : "accent-foreground-muted"
127 }
128 />
129 <Text className={cn("text-2xs tabular-nums", mutedForeground)}>
130 {item.stack.position}/{item.stack.size}
131 </Text>
132 </View>
133 ) : null}
134 <Text className={cn("text-2xs tabular-nums", tertiaryForeground)}>
135 <RelativeTime timestamp={item.updatedAt} />
136 </Text>
137 </View>
138 <Text
139 className={cn("text-base leading-5 font-t3-medium", foreground)}
140 numberOfLines={2}
141 ellipsizeMode="tail"
142 >
143 {item.title}
144 </Text>
145 <View className="flex-row items-center gap-1.5">
146 <View className="min-w-0 flex-1 flex-row items-center gap-1">
147 {item.labels.slice(0, 2).map((label) => {
148 const color = pullRequestLabelColor(label.color);
149 return (
150 <View
151 key={label.name}
152 className="min-w-0 shrink flex-row items-center gap-1 rounded-md bg-subtle px-1.5 py-0.5"
153 style={{
154 maxWidth: 160,
155 ...(color ? { backgroundColor: `${color}20` } : {}),
156 }}
157 >
158 {color ? (
159 <View
160 style={{ width: 5, height: 5, borderRadius: 3, backgroundColor: color }}
161 />
162 ) : null}
163 <Text
164 className={cn("shrink text-2xs font-t3-medium", mutedForeground)}
165 numberOfLines={1}
166 >
167 {label.name}
168 </Text>
169 </View>
170 );
171 })}
172 {item.labels.length > 2 ? (
173 <Text className={cn("shrink-0 text-2xs", tertiaryForeground)}>
174 +{item.labels.length - 2}
175 </Text>
176 ) : null}
177 </View>
178 {checks ? (
179 <View className="shrink-0" accessibilityLabel={`Checks ${item.checksState}`}>
180 <SymbolView name={checks.icon} size={13} tintColorClassName={checks.tint} />
181 </View>
182 ) : null}
183 {review ? (
184 <View className="shrink-0" accessibilityLabel={review.accessibleLabel}>
185 <SymbolView name={review.icon} size={13} tintColorClassName={review.tint} />
186 </View>
187 ) : null}
188 {item.additions || item.deletions ? (
189 <PullRequestDiffStats
190 additions={item.additions}
191 deletions={item.deletions}
192 compact
193 />
194 ) : null}
195 </View>
196 </View>
197 </Pressable>
198 </Menu>
199 </View>
200 );
201 },
202 (previous, next) =>
203 pullRequestListRowsAreEqual(previous.item, next.item) &&
204 previous.selected === next.selected &&
205 previous.sidebar === next.sidebar &&
206 previous.busy === next.busy &&
207 previous.onSelect === next.onSelect &&
208 previous.onAction === next.onAction &&
209 previous.getActions === next.getActions,
210);