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

PullRequestDescription.tsx

201 lines7.9 KB

1import { mediaReferenceFileName } from "@t3tools/client-runtime/media-reference";
2import { splitPullRequestBody } from "@t3tools/client-runtime/pull-request-markdown";
3import {
4 PullRequestImageAttachment,
5 PullRequestMedia,
6 PullRequestMediaContext,
7} from "./PullRequestMedia";
8import { PullRequestSectionHeading } from "./PullRequestSectionHeading";
9import { T, useGT, useMessages } from "gt-react-native";
10import { useContext, useMemo, useState, type ComponentProps, type ReactNode } from "react";
11import { Platform, Pressable, ScrollView, StyleSheet, View } from "react-native";
12import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
13import { pullRequestImageSource } from "./pullRequestMediaSource";
14import { markdownReferenceDefinitions, parseMarkdownDocument } from "../../lib/markdownDocument";
15import { SymbolView } from "../../components/ui/AppSymbol";
16import { AppText as Text } from "../../components/ui/AppText";
17import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
18import type { FilePreviewItem } from "../../components/ui/FilePreviewModal";
19import { isBotBadgeImage } from "./botBadgeImages";
20import { PullRequestMarkdown } from "./PullRequestMarkdown";
21import { pullRequestDescriptionContent } from "./pullRequestDescriptionContent";
22​
23const COLLAPSED_HEIGHT = 240;
24​
25export function PullRequestDescription(props: {
26 readonly markdown: string;
27 /** Clips a long description behind "Show more"; absent, it is always shown in full. */
28 readonly collapse?: {
29 readonly expanded: boolean;
30 readonly onExpandedChange: (expanded: boolean) => void;
31 readonly onCollapse: () => void;
32 };
33 readonly footer?: ReactNode;
34 readonly onTaskListChange?: ComponentProps<typeof PullRequestMarkdown>["onTaskListChange"];
35}) {
36 const { collapse } = props;
37 const expanded = collapse?.expanded ?? true;
38 const gt = useGT();
39 const messages = useMessages();
40 const [bodyHeight, setBodyHeight] = useState(0);
41 const mediaContext = useContext(PullRequestMediaContext);
42 const { themeVariables: colors } = useAppearancePreferences();
43 const fadeColor = colors[Platform.OS === "android" ? "--color-sheet-solid" : "--color-screen"];
44 const content = useMemo(() => {
45 const document = parseMarkdownDocument(props.markdown);
46 const content = pullRequestDescriptionContent(
47 document,
48 splitPullRequestBody(props.markdown, markdownReferenceDefinitions),
49 messages,
50 );
51 return {
52 ...content,
53 attachments: content.attachments.filter((attachment) => !isBotBadgeImage(attachment.uri)),
54 };
55 }, [props.markdown, messages]);
56 const gallery: FilePreviewItem[] = content.attachments
57 .filter((attachment) => attachment.kind === "image")
58 .map((attachment) => {
59 return {
60 ...pullRequestImageSource(attachment.uri, mediaContext),
61 name: mediaReferenceFileName({ kind: "url", url: attachment.uri }) || gt`Image`,
62 };
63 });
64 const canCollapse = collapse !== undefined && bodyHeight > COLLAPSED_HEIGHT;
65 const collapsed = canCollapse && !expanded;
66 return (
67 <View className="gap-5">
68 <View className="gap-3">
69 <PullRequestSectionHeading>
70 <T>Description</T>
71 </PullRequestSectionHeading>
72 {props.markdown.trim() ? (
73 <View className="overflow-hidden">
74 <View
75 style={{
76 height: expanded ? bodyHeight : Math.min(bodyHeight, COLLAPSED_HEIGHT),
77 overflow: "hidden",
78 }}
79 >
80 <View
81 style={{ position: "absolute", top: 0, left: 0, right: 0 }}
82 onLayout={(event) => setBodyHeight(event.nativeEvent.layout.height)}
83 >
84 <PullRequestMarkdown
85 markdown={props.markdown}
86 onTaskListChange={props.onTaskListChange}
87 />
88 </View>
89 </View>
90 {collapsed ? (
91 <View
92 pointerEvents="none"
93 style={[StyleSheet.absoluteFill, { top: undefined, height: 80, zIndex: 1 }]}
94 >
95 <Svg width="100%" height={80}>
96 <Defs>
97 <LinearGradient
98 id="description-fade"
99 x1="0"
100 y1="0"
101 x2="0"
102 y2="80"
103 gradientUnits="userSpaceOnUse"
104 >
105 <Stop offset="0" stopColor={fadeColor} stopOpacity={0} />
106 <Stop offset="0.5" stopColor={fadeColor} stopOpacity={0.94} />
107 <Stop offset="0.72" stopColor={fadeColor} stopOpacity={1} />
108 <Stop offset="1" stopColor={fadeColor} stopOpacity={1} />
109 </LinearGradient>
110 </Defs>
111 <Rect width="100%" height="100%" fill="url(#description-fade)" />
112 </Svg>
113 <View
114 style={{
115 position: "absolute",
116 bottom: 0,
117 left: 0,
118 right: 0,
119 height: 44,
120 backgroundColor: fadeColor,
121 }}
122 />
123 </View>
124 ) : null}
125 {collapse !== undefined && canCollapse ? (
126 <Pressable
127 accessibilityRole="button"
128 accessibilityState={{ expanded }}
129 accessibilityLabel={expanded ? gt`Collapse description` : gt`Expand description`}
130 onPress={() => {
131 if (expanded) collapse.onCollapse();
132 collapse.onExpandedChange(!expanded);
133 }}
134 style={
135 collapsed
136 ? { position: "absolute", bottom: 2, left: 0, right: 0, height: 44, zIndex: 2 }
137 : undefined
138 }
139 className="min-h-11 flex-row items-center justify-center gap-1 py-2 active:opacity-70"
140 >
141 <Text className="text-xs font-t3-medium text-foreground">
142 {expanded ? gt`Show less` : gt`Show more`}
143 </Text>
144 <SymbolView
145 name={expanded ? "chevron.up" : "chevron.down"}
146 size={10}
147 tintColorClassName="accent-foreground"
148 />
149 </Pressable>
150 ) : null}
151 </View>
152 ) : (
153 <Text className="text-sm text-foreground-muted">
154 <T>No description provided.</T>
155 </Text>
156 )}
157 {props.footer}
158 </View>
159 {content.attachments.length ? (
160 <View className="gap-3">
161 <PullRequestSectionHeading>
162 <T>Attachments</T>
163 </PullRequestSectionHeading>
164 <ScrollView
165 horizontal
166 keyboardShouldPersistTaps="always"
167 scrollsToTop={false}
168 showsHorizontalScrollIndicator={false}
169 contentContainerStyle={{ gap: 12 }}
170 >
171 {content.attachments.map((attachment) =>
172 attachment.kind !== "image" ? (
173 <PullRequestMedia
174 key={attachment.uri}
175 url={attachment.uri}
176 kind={attachment.kind}
177 caption={attachment.caption}
178 compact
179 />
180 ) : (
181 <PullRequestImageAttachment
182 key={attachment.uri}
183 url={attachment.uri}
184 caption={attachment.caption}
185 source={
186 gallery[
187 content.attachments
188 .filter((item) => item.kind === "image")
189 .indexOf(attachment)
190 ]!
191 }
192 gallery={gallery}
193 />
194 ),
195 )}
196 </ScrollView>
197 </View>
198 ) : null}
199 </View>
200 );