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

ThreadDetailsPrRows.tsx

109 lines3.5 KB

1import type { EnvironmentId, ThreadPullRequestLink } from "@t3tools/contracts";
2import {
3 resolveThreadPullRequestChains,
4 threadPullRequestKeyOf,
5 visibleThreadPullRequests,
6} from "@t3tools/shared/threadPullRequests";
7import { MinusIcon, PlusIcon } from "lucide-react";
8import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
9​
10import { findProjectOnChangeRequestHost, parseChangeRequestUrl } from "~/lib/openPullRequestLink";
11import { cn } from "~/lib/utils";
12import { useProjects } from "~/state/entities";
13​
14import { pullRequestListLines } from "../pullRequest/pullRequestListLines";
15import { linkedPullRequestSnapshotStatus, prStatusIndicator } from "../ThreadStatusIndicators";
16import { Button } from "../ui/button";
17import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
18import { THREAD_DETAILS_PANEL_ROW_CLASS } from "./threadDetailsPanelStyles";
19​
20function ThreadDetailsPrLinkRow({
21 environmentId,
22 link,
23 onOpen,
24 onActed,
25}: {
26 environmentId: EnvironmentId;
27 link: ThreadPullRequestLink;
28 onOpen: (event: ReactMouseEvent<HTMLElement>) => void;
29 onActed?: (() => void) | undefined;
30}) {
31 const projects = useProjects();
32 const parsed = parseChangeRequestUrl(link.url);
33 const project =
34 parsed === null
35 ? null
36 : (findProjectOnChangeRequestHost(
37 projects.filter((candidate) => candidate.environmentId === environmentId),
38 parsed,
39 ) ?? null);
40 const linked = linkedPullRequestSnapshotStatus(link);
41 const pr = linked?.pr ?? null;
42 return (
43 <ThreadDetailsPrRow
44 environmentId={environmentId}
45 pr={pr}
46 number={link.number}
47 reference={link}
48 status={prStatusIndicator(pr, linked?.sourceControlProvider)}
49 project={project}
50 label={`#${link.number}${link.snapshot === null ? "" : `: ${link.snapshot.title}`}`}
51 openAriaLabel={link.url}
52 onOpen={onOpen}
53 {...(onActed ? { onActed } : {})}
54 />
55 );
57​
58export function ThreadDetailsPrRows({
59 links,
60 currentLink,
61 onOpenLink,
62 ...row
63}: ComponentProps<typeof ThreadDetailsPrRow> & {
64 links: ReadonlyArray<ThreadPullRequestLink>;
65 currentLink: ThreadPullRequestLink | null;
66 onOpenLink: (event: ReactMouseEvent<HTMLElement>, url: string) => void;
67}) {
68 const [expanded, setExpanded] = useState(false);
69 const rest =
70 currentLink === null
71 ? []
72 : pullRequestListLines(resolveThreadPullRequestChains(visibleThreadPullRequests(links)))
73 .map((line) => line.link)
74 .filter((link) => threadPullRequestKeyOf(link) !== threadPullRequestKeyOf(currentLink));
75 if (rest.length === 0) return <ThreadDetailsPrRow {...row} />;
76​
77 return (
78 <>
79 <ThreadDetailsPrRow {...row} />
80 {expanded
81 ? rest.map((link) => (
82 <ThreadDetailsPrLinkRow
83 key={threadPullRequestKeyOf(link)}
84 environmentId={row.environmentId}
85 link={link}
86 onOpen={(event) => onOpenLink(event, link.url)}
87 onActed={row.onActed}
88 />
89 ))
90 : null}
91 <Button
92 variant="ghost"
93 size="sm"
94 onClick={() => setExpanded(!expanded)}
95 className={cn(
96 THREAD_DETAILS_PANEL_ROW_CLASS,
97 "w-full text-muted-foreground/70 hover:text-foreground/80 active:scale-100",
98 )}
99 >
100 {expanded ? (
101 <MinusIcon aria-hidden className="-mx-0.5 size-4 shrink-0" />
102 ) : (
103 <PlusIcon aria-hidden className="-mx-0.5 size-4 shrink-0" />
104 )}
105 {expanded ? "Show less" : `Show ${rest.length} more`}
106 </Button>
107 </>
108 );