import { splitPullRequestBody } from "@t3tools/client-runtime/pull-request-markdown";
import {
PullRequestImageAttachment,
PullRequestMedia,
PullRequestMediaContext,
} from "./PullRequestMedia";
import { PullRequestSectionHeading } from "./PullRequestSectionHeading";
import { useContext, useMemo, useState, type ComponentProps, type ReactNode } from "react";
import { Platform, Pressable, ScrollView, StyleSheet, View } from "react-native";
import Svg, { Defs, LinearGradient, Rect, Stop } from "react-native-svg";
import { pullRequestImageSource } from "./pullRequestMediaSource";
import { parseMarkdownDocument } from "../../lib/markdownDocument";
import { SymbolView } from "../../components/ui/AppSymbol";
import { AppText as Text } from "../../components/ui/AppText";
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
import type { FilePreviewItem } from "../../components/FilePreviewModal";
import { isBotBadgeImage } from "./botBadgeImages";
import { PullRequestMarkdown } from "./PullRequestMarkdown";
import { pullRequestDescriptionContent } from "./pullRequestDescriptionContent";
const COLLAPSED_HEIGHT = 240;
export function PullRequestDescription(props: {
readonly markdown: string;
readonly onCollapse: () => void;
readonly expanded: boolean;
readonly onExpandedChange: (expanded: boolean) => void;
readonly footer?: ReactNode;
readonly onTaskListChange?: ComponentProps<typeof PullRequestMarkdown>["onTaskListChange"];
}) {
const { expanded, onExpandedChange } = props;
const [bodyHeight, setBodyHeight] = useState(0);
const mediaContext = useContext(PullRequestMediaContext);
const { themeVariables: colors } = useAppearancePreferences();
const fadeColor = colors[Platform.OS === "android" ? "--color-sheet-solid" : "--color-screen"];
const content = useMemo(() => {
const document = parseMarkdownDocument(props.markdown);
const content = pullRequestDescriptionContent(document, splitPullRequestBody(props.markdown));
return {
...content,
attachments: content.attachments.filter((attachment) => !isBotBadgeImage(attachment.uri)),
};
}, [props.markdown]);
const gallery: FilePreviewItem[] = content.attachments
.filter((attachment) => attachment.kind === "image")
.map((attachment) => {
return {
...pullRequestImageSource(attachment.uri, mediaContext),
name: new URL(attachment.uri).pathname.split("/").pop() || "Image",
};
});
const canCollapse = bodyHeight > COLLAPSED_HEIGHT;
const collapsed = canCollapse && !expanded;
return (
<View className="gap-5">
<View className="gap-3">
<PullRequestSectionHeading>Description</PullRequestSectionHeading>
{props.markdown.trim() ? (
<View className="overflow-hidden">
<View
style={{
height: expanded ? bodyHeight : Math.min(bodyHeight, COLLAPSED_HEIGHT),
overflow: "hidden",
}}
>
<View
style={{ position: "absolute", top: 0, left: 0, right: 0 }}
onLayout={(event) => setBodyHeight(event.nativeEvent.layout.height)}
>
<PullRequestMarkdown
markdown={props.markdown}
onTaskListChange={props.onTaskListChange}
/>
</View>
</View>
{collapsed ? (
<View
pointerEvents="none"
style={[StyleSheet.absoluteFill, { top: undefined, height: 80, zIndex: 1 }]}
>
<Svg width="100%" height={80}>
<Defs>
<LinearGradient
id="description-fade"
x1="0"
y1="0"
x2="0"
y2="80"
gradientUnits="userSpaceOnUse"
>
<Stop offset="0" stopColor={fadeColor} stopOpacity={0} />
<Stop offset="0.5" stopColor={fadeColor} stopOpacity={0.94} />
<Stop offset="0.72" stopColor={fadeColor} stopOpacity={1} />
<Stop offset="1" stopColor={fadeColor} stopOpacity={1} />
</LinearGradient>
</Defs>
<Rect width="100%" height="100%" fill="url(#description-fade)" />
</Svg>
<View
style={{
position: "absolute",
bottom: 0,
left: 0,
right: 0,
height: 44,
backgroundColor: fadeColor,
}}
/>
</View>
) : null}
{canCollapse ? (
<Pressable
accessibilityRole="button"
accessibilityState={{ expanded }}
accessibilityLabel={expanded ? "Collapse description" : "Expand description"}
onPress={() => {
if (expanded) props.onCollapse();
onExpandedChange(!expanded);
}}
style={
collapsed
? { position: "absolute", bottom: 2, left: 0, right: 0, height: 44, zIndex: 2 }
: undefined
}
className="min-h-11 flex-row items-center justify-center gap-1 py-2 active:opacity-70"
>
<Text className="text-xs font-t3-medium text-foreground">
{expanded ? "Show less" : "Show more"}
</Text>
<SymbolView
name={expanded ? "chevron.up" : "chevron.down"}
size={10}
tintColorClassName="accent-foreground"
/>
</Pressable>
) : null}
</View>
) : (
<Text className="text-sm text-foreground-muted">No description provided.</Text>
)}
{props.footer}
</View>
{content.attachments.length ? (
<View className="gap-3">
<PullRequestSectionHeading>Attachments</PullRequestSectionHeading>
<ScrollView
horizontal
keyboardShouldPersistTaps="always"
scrollsToTop={false}
showsHorizontalScrollIndicator={false}
contentContainerStyle={{ gap: 12 }}
>
{content.attachments.map((attachment) =>
attachment.kind !== "image" ? (
<PullRequestMedia
key={attachment.uri}
url={attachment.uri}
kind={attachment.kind}
caption={attachment.caption}
compact
/>
) : (
<PullRequestImageAttachment
key={attachment.uri}
url={attachment.uri}
caption={attachment.caption}
source={
gallery[
content.attachments
.filter((item) => item.kind === "image")
.indexOf(attachment)
]!
}
gallery={gallery}
/>
),
)}
</ScrollView>
</View>
) : null}
</View>
);
}