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