Open History to see the last commit
1import { useEffect, useId, useRef, useState, type ComponentProps } from "react";
2
3import { cn } from "~/lib/utils";
4import { Button } from "../ui/button";
5import { PullRequestMarkdown } from "./PullRequestMarkdown";
6
7/** Keep the complete markdown intact while limiting long reports to a readable preview. */
8export function PullRequestCommentBody({
9 className,
10 ...props
11}: ComponentProps<typeof PullRequestMarkdown>) {
12 const [expanded, setExpanded] = useState(false);
13 const [overflowing, setOverflowing] = useState(false);
14 const content = useRef<HTMLDivElement>(null);
15 const id = useId();
16
17 useEffect(() => {
18 const element = content.current;
19 if (!element) return;
20 const measure = () => setOverflowing(element.getBoundingClientRect().height > 240);
21 measure();
22 const observer = new ResizeObserver(measure);
23 observer.observe(element);
24 return () => observer.disconnect();
25 }, []);
26
27 return (
28 <div className={cn("min-w-0", className)}>
29 <div
30 id={id}
31 className={cn("relative", !expanded && "max-h-[240px] overflow-hidden")}
32 onFocusCapture={() => setExpanded(true)}
33 >
34 <div ref={content}>
35 <PullRequestMarkdown {...props} />
36 </div>
37 {overflowing && !expanded ? (
38 <div
39 aria-hidden
40 className="pointer-events-none absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-background to-transparent"
41 />
42 ) : null}
43 </div>
44 {overflowing ? (
45 <Button
46 size="xs"
47 variant="ghost-muted"
48 className="mt-2"
49 aria-expanded={expanded}
50 aria-controls={id}
51 onClick={() => {
52 if (expanded) content.current?.parentElement?.scrollIntoView({ block: "nearest" });
53 setExpanded(!expanded);
54 }}
55 >
56 {expanded ? "Show less" : "Show full comment"}
57 </Button>
58 ) : null}
59 </div>
60 );
61}