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

PullRequestListRow.tsx

206 lines8.8 KB

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