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

ThreadPullRequestsSheet.tsx

132 lines4.4 KB

1import { useNavigation, type StaticScreenProps } from "@react-navigation/native";
2import { LegendList } from "@legendapp/list/react-native";
3import { useGT, select } from "gt-react-native";
4import { changeRequestTermKind } from "../../lib/changeRequestTerm";
5import { useMemo } from "react";
6import { View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8​
9import { AppText as Text } from "../../components/ui/AppText";
10import { ScreenContent } from "../../components/ui/ScreenContent";
11import { SheetHeader } from "../../components/ui/SheetHeader";
12import { useOpenPullRequest } from "../../native/useOpenPullRequest";
13import { useThreadSelection } from "../../state/use-thread-selection";
14import {
15 LinkedPullRequestRow,
16 linkedPullRequestKey,
17 useLinkedPullRequestLines,
18} from "./LinkedPullRequestList";
19import { useLinkedPullRequestActions } from "./useLinkedPullRequestActions";
20​
21export function ThreadPullRequestsSheet(
22 _props: StaticScreenProps<{ readonly environmentId: string; readonly threadId: string }>,
23) {
24 const navigation = useNavigation();
25 const gt = useGT();
26 const insets = useSafeAreaInsets();
27 const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } =
28 useThreadSelection();
29 const lines = useLinkedPullRequestLines(selectedThread, selectedEnvironmentRuntime);
30 const openCount = useMemo(
31 () =>
32 lines.filter((line) => line.link.snapshot === null || line.link.snapshot.state === "open")
33 .length,
34 [lines],
35 );
36 const openPullRequest = useOpenPullRequest({
37 replace: true,
38 context: selectedThread ?? undefined,
39 });
40 const actions = useLinkedPullRequestActions({
41 thread: selectedThread,
42 project: selectedThreadProject,
43 runtime: selectedEnvironmentRuntime,
44 });
45 const rowInputs = { lines, actions };
46 const term = changeRequestTermKind(actions.terminology);
47 const title = gt`${select(
48 { term },
49 {
50 mr: "Linked merge requests",
51 cr: "Linked change requests",
52 other: "Linked pull requests",
53 },
54 )}`;
55​
56 const content = (
57 <View className="flex-1 bg-screen">
58 {/* oxlint-disable-next-line t3code/require-registered-list-scroll -- Sheet content; sheets sit outside workspace columns and keep their own header handling. */}
59 <LegendList
60 data={lines}
61 keyExtractor={linkedPullRequestKey}
62 extraData={rowInputs}
63 renderItem={({ item, index }) => (
64 <LinkedPullRequestRow
65 line={item}
66 separator={index < lines.length - 1}
67 surfaceClassName="text-screen"
68 openPullRequest={openPullRequest}
69 actions={actions}
70 />
71 )}
72 estimatedItemSize={88}
73 recycleItems
74 alwaysBounceVertical
75 style={{ flex: 1 }}
76 contentInsetAdjustmentBehavior="automatic"
77 showsVerticalScrollIndicator={false}
78 contentContainerStyle={{
79 paddingBottom: Math.max(insets.bottom, 18) + 18,
80 paddingHorizontal: 8,
81 }}
82 ListHeaderComponent={
83 lines.length > 0 ? (
84 <Text className="px-4 pt-2 pb-1 text-xs text-foreground-muted">
85 {gt`${{ open: openCount }} open · ${{ linked: lines.length }} linked`}
86 </Text>
87 ) : null
88 }
89 ListEmptyComponent={
90 <Text className="pt-6 text-center text-sm text-foreground-muted">
91 {gt`${select(
92 { term },
93 {
94 mr: "No linked merge requests.",
95 cr: "No linked change requests.",
96 other: "No linked pull requests.",
97 },
98 )}`}
99 </Text>
100 }
101 />
102 </View>
103 );
104​
105 return (
106 <View collapsable={false} className="flex-1 bg-sheet">
107 <SheetHeader
108 title={title}
109 onClose={() => navigation.goBack()}
110 actions={
111 actions.canLink
112 ? [
113 {
114 accessibilityLabel: gt`${select(
115 { term },
116 {
117 mr: "Link merge request",
118 cr: "Link change request",
119 other: "Link pull request",
120 },
121 )}`,
122 icon: "plus",
123 onPress: actions.promptLink,
124 },
125 ]
126 : undefined
127 }
128 />
129 <ScreenContent>{content}</ScreenContent>
130 </View>
131 );