@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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