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

PullRequestCommentBody.tsx

70 lines2.2 KB

1import { useGT } from "gt-react";
2import { useId, useRef, useState, type ComponentProps } from "react";
3​
4import { cn } from "~/lib/utils";
5import { Button } from "../ui/button";
6import { PullRequestMarkdown } from "./PullRequestMarkdown";
7import { observeResize } from "~/lib/observeResize";
8import { LRUCache } from "~/lib/lruCache";
9​
10// Right-panel tabs unmount when switched away, so expansion outlives the component.
11const expandedComments = new LRUCache<boolean>(256, 64 * 1024);
12​
13export function PullRequestCommentBody({
14 className,
15 expandedKey,
16 ...props
17}: ComponentProps<typeof PullRequestMarkdown> & { expandedKey: string }) {
18 const gt = useGT();
19 const [expanded, setExpandedState] = useState(() => expandedComments.get(expandedKey) ?? false);
20 const setExpanded = (next: boolean) => {
21 expandedComments.set(expandedKey, next, 8 + expandedKey.length * 2);
22 setExpandedState(next);
23 };
24 const [overflowing, setOverflowing] = useState(false);
25 const clip = useRef<HTMLDivElement>(null);
26 const id = useId();
27​
28 return (
29 <div className={cn("min-w-0", className)}>
30 <div
31 ref={clip}
32 id={id}
33 className={cn("relative", !expanded && "max-h-[240px] overflow-hidden")}
34 onFocusCapture={() => setExpanded(true)}
35 >
36 <div
37 ref={(element) => {
38 if (element === null) return;
39 return observeResize(element, ([entry]) => {
40 if (entry) setOverflowing(entry.contentRect.height > 240);
41 });
42 }}
43 >
44 <PullRequestMarkdown {...props} />
45 </div>
46 {overflowing && !expanded ? (
47 <div
48 aria-hidden
49 className="pointer-events-none absolute inset-x-0 bottom-0 h-10 bg-gradient-to-t from-background to-transparent"
50 />
51 ) : null}
52 </div>
53 {overflowing ? (
54 <Button
55 size="xs"
56 variant="ghost-muted"
57 className="mt-2"
58 aria-expanded={expanded}
59 aria-controls={id}
60 onClick={() => {
61 if (expanded) clip.current?.scrollIntoView({ block: "nearest" });
62 setExpanded(!expanded);
63 }}
64 >
65 {expanded ? gt`Show less` : gt`Show full comment`}
66 </Button>
67 ) : null}
68 </div>
69 );