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

ThreadPullRequestsSheet.tsx

162 lines5.8 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { LegendList } from "@legendapp/list/react-native";
3import { useMemo } from "react";
4import { Platform, View } from "react-native";
5import { ScreenStack } from "react-native-screens";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7​
8import { AndroidSheetHeader } from "../../components/ui/AndroidScreenHeader";
9import { AppText as Text } from "../../components/ui/AppText";
10import { ScreenContent } from "../../components/ui/ScreenContent";
11import { NativeSheetContent } from "../../native/NativeSheetContent";
12import { NativeStackScreenOptions, nativeHeaderScrollEdgeEffects } from "../../native/StackHeader";
13import { NativeHeader, NativeScreen } from "../../native/ThemedScreenStack";
14import { useOpenPullRequest } from "../../native/useOpenPullRequest";
15import { useThreadSelection } from "../../state/use-thread-selection";
16import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
17import {
18 LinkedPullRequestRow,
19 linkedPullRequestKey,
20 useLinkedPullRequestLines,
21} from "./LinkedPullRequestList";
22import { useLinkedPullRequestActions } from "./useLinkedPullRequestActions";
23​
24const HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version);
25const TITLE = "Linked pull requests";
26​
27export function ThreadPullRequestsSheet(
28 _props: StaticScreenProps<{ readonly environmentId: string; readonly threadId: string }>,
29) {
30 const { layout } = useAdaptiveWorkspaceLayout();
31 const navigation = useNavigation();
32 const insets = useSafeAreaInsets();
33 const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } =
34 useThreadSelection();
35 const lines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime);
36 const openCount = useMemo(
37 () =>
38 lines.filter((line) => line.link.snapshot === null || line.link.snapshot.state === "open")
39 .length,
40 [lines],
41 );
42 const openPullRequest = useOpenPullRequest({
43 replace: true,
44 context: selectedThread ?? undefined,
45 });
46 const actions = useLinkedPullRequestActions({
47 thread: selectedThread,
48 project: selectedThreadProject,
49 runtime: selectedEnvironmentRuntime,
50 });
51 const rowInputs = useMemo(() => ({ lines, actions }), [lines, actions]);
52​
53 const content = (
54 <View className="flex-1 android:bg-sheet-solid ios:bg-screen">
55 <LegendList
56 data={lines}
57 keyExtractor={linkedPullRequestKey}
58 extraData={rowInputs}
59 renderItem={({ item, index }) => (
60 <LinkedPullRequestRow
61 line={item}
62 separator={index < lines.length - 1}
63 surfaceClassName={Platform.OS === "ios" ? "text-screen" : "text-sheet-solid"}
64 openPullRequest={openPullRequest}
65 actions={actions}
66 />
67 )}
68 estimatedItemSize={88}
69 recycleItems
70 alwaysBounceVertical
71 style={{ flex: 1 }}
72 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
73 showsVerticalScrollIndicator={false}
74 contentContainerStyle={{
75 paddingBottom: Math.max(insets.bottom, 18) + 18,
76 paddingHorizontal: 8,
77 }}
78 ListHeaderComponent={
79 lines.length > 0 ? (
80 <Text className="px-4 pt-2 pb-1 text-xs text-foreground-muted">
81 {openCount} open · {lines.length} linked
82 </Text>
83 ) : null
84 }
85 ListEmptyComponent={
86 <Text className="pt-6 text-center text-sm text-foreground-muted">
87 No linked pull requests.
88 </Text>
89 }
90 />
91 </View>
92 );
93​
94 if (Platform.OS === "ios") {
95 // A plain formSheet screen never renders a stack header, so it comes from
96 // a nested native stack inside the sheet (same shape as the git sheet).
97 return (
98 <View collapsable={false} className="flex-1 bg-sheet">
99 <ScreenStack style={{ flex: 1 }}>
100 <NativeScreen
101 activityState={2}
102 enabled
103 isNativeStack
104 screenId="thread-pull-requests-sheet-native"
105 scrollEdgeEffects={HEADER_SCROLL_EDGE_EFFECTS}
106 className="flex-1 bg-sheet"
107 >
108 <NativeSheetContent>{content}</NativeSheetContent>
109 <NativeHeader
110 backgroundColor="rgba(0,0,0,0)"
111 tintColorClassName="accent-foreground"
112 hideBackButton
113 hideShadow={false}
114 navigationItemStyle="editor"
115 title={TITLE}
116 titleColorClassName="accent-foreground"
117 titleFontSize={18}
118 titleFontWeight="800"
119 translucent
120 headerRightBarButtonItems={
121 actions.canLink
122 ? [
123 {
124 type: "button",
125 accessibilityLabel: "Link pull request",
126 icon: { name: "plus", type: "sfSymbol" },
127 identifier: "thread-pull-requests-link",
128 onPress: actions.promptLink,
129 },
130 ]
131 : undefined
132 }
133 />
134 </NativeScreen>
135 </ScreenStack>
136 </View>
137 );
138 }
139​
140 return (
141 <View collapsable={false} className="flex-1 bg-header">
142 <NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} />
143 <AndroidSheetHeader
144 title={TITLE}
145 hideBottomBorder
146 onBack={() => navigation.goBack()}
147 actions={
148 actions.canLink
149 ? [
150 {
151 accessibilityLabel: "Link pull request",
152 icon: "plus",
153 onPress: actions.promptLink,
154 },
155 ]
156 : undefined
157 }
158 />
159 <ScreenContent insetHorizontal={layout.usesSplitView}>{content}</ScreenContent>
160 </View>
161 );