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

ThreadPullRequestsSheet.tsx

132 lines4.5 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 { useSafeAreaInsets } from "react-native-safe-area-context";
6​
7import { AndroidSheetHeader } from "../../components/ui/AndroidScreenHeader";
8import { AppText as Text } from "../../components/ui/AppText";
9import { ScreenContent } from "../../components/ui/ScreenContent";
10import { SheetHeader } from "../../components/ui/SheetHeader";
11import { Toolbar } from "../../components/ui/Toolbar";
12import { NativeStackScreenOptions } from "../../native/StackHeader";
13import { useOpenPullRequest } from "../../native/useOpenPullRequest";
14import { useThreadSelection } from "../../state/use-thread-selection";
15import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
16import {
17 LinkedPullRequestRow,
18 linkedPullRequestKey,
19 useLinkedPullRequestLines,
20} from "./LinkedPullRequestList";
21import { useLinkedPullRequestActions } from "./useLinkedPullRequestActions";
22​
23const TITLE = "Linked pull requests";
24​
25export function ThreadPullRequestsSheet(
26 _props: StaticScreenProps<{ readonly environmentId: string; readonly threadId: string }>,
27) {
28 const { layout } = useAdaptiveWorkspaceLayout();
29 const navigation = useNavigation();
30 const insets = useSafeAreaInsets();
31 const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } =
32 useThreadSelection();
33 const lines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime);
34 const openCount = useMemo(
35 () =>
36 lines.filter((line) => line.link.snapshot === null || line.link.snapshot.state === "open")
37 .length,
38 [lines],
39 );
40 const openPullRequest = useOpenPullRequest({
41 replace: true,
42 context: selectedThread ?? undefined,
43 });
44 const actions = useLinkedPullRequestActions({
45 thread: selectedThread,
46 project: selectedThreadProject,
47 runtime: selectedEnvironmentRuntime,
48 });
49 const rowInputs = useMemo(() => ({ lines, actions }), [lines, actions]);
50​
51 const content = (
52 <View className="flex-1 android:bg-sheet-solid ios:bg-screen">
53 <LegendList
54 data={lines}
55 keyExtractor={linkedPullRequestKey}
56 extraData={rowInputs}
57 renderItem={({ item, index }) => (
58 <LinkedPullRequestRow
59 line={item}
60 separator={index < lines.length - 1}
61 surfaceClassName={Platform.OS === "ios" ? "text-screen" : "text-sheet-solid"}
62 openPullRequest={openPullRequest}
63 actions={actions}
64 />
65 )}
66 estimatedItemSize={88}
67 recycleItems
68 alwaysBounceVertical
69 style={{ flex: 1 }}
70 contentInsetAdjustmentBehavior={Platform.OS === "ios" ? "automatic" : "never"}
71 showsVerticalScrollIndicator={false}
72 contentContainerStyle={{
73 paddingBottom: Math.max(insets.bottom, 18) + 18,
74 paddingHorizontal: 8,
75 }}
76 ListHeaderComponent={
77 lines.length > 0 ? (
78 <Text className="px-4 pt-2 pb-1 text-xs text-foreground-muted">
79 {openCount} open · {lines.length} linked
80 </Text>
81 ) : null
82 }
83 ListEmptyComponent={
84 <Text className="pt-6 text-center text-sm text-foreground-muted">
85 No linked pull requests.
86 </Text>
87 }
88 />
89 </View>
90 );
91​
92 if (Platform.OS === "ios") {
93 return (
94 <View collapsable={false} className="flex-1 bg-sheet">
95 <SheetHeader presentation="native" title={TITLE} onClose={() => navigation.goBack()} />
96 {actions.canLink ? (
97 <Toolbar placement="right">
98 <Toolbar.Button
99 icon="plus"
100 accessibilityLabel="Link pull request"
101 onPress={actions.promptLink}
102 />
103 </Toolbar>
104 ) : null}
105 {content}
106 </View>
107 );
108 }
109​
110 return (
111 <View collapsable={false} className="flex-1 bg-header">
112 <NativeStackScreenOptions options={{ sheetCornerRadius: 28 }} />
113 <AndroidSheetHeader
114 title={TITLE}
115 hideBottomBorder
116 onBack={() => navigation.goBack()}
117 actions={
118 actions.canLink
119 ? [
120 {
121 accessibilityLabel: "Link pull request",
122 icon: "plus",
123 onPress: actions.promptLink,
124 },
125 ]
126 : undefined
127 }
128 />
129 <ScreenContent insetHorizontal={layout.usesSplitView}>{content}</ScreenContent>
130 </View>
131 );