Open History to see the last commit
1import { ThreadDetailsControl } from "./ThreadDetailsControl";
2import type { EnvironmentId, ThreadPullRequestLink } from "@t3tools/contracts";
3import {
4 resolveThreadPullRequestChains,
5 threadPullRequestKeyOf,
6 visibleThreadPullRequests,
7} from "@t3tools/shared/threadPullRequests";
8import { MinusIcon, PlusIcon } from "lucide-react";
9import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
10
11import { findProjectOnChangeRequestHost, parseChangeRequestUrl } from "~/lib/openPullRequestLink";
12
13import { useProjects } from "~/state/entities";
14
15import { pullRequestListLines } from "../pullRequest/pullRequestListLines";
16import { linkedPullRequestSnapshotStatus, prStatusIndicator } from "../ThreadStatusIndicators";
17
18import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
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 );
56}
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 <ThreadDetailsControl
92 variant="ghost"
93 size="sm"
94 onClick={() => setExpanded(!expanded)}
95 part="row"
96 tone="muted"
97 className="w-full active:scale-100"
98 >
99 {expanded ? (
100 <MinusIcon aria-hidden className="size-4 shrink-0" />
101 ) : (
102 <PlusIcon aria-hidden className="size-4 shrink-0" />
103 )}
104 {expanded ? "Show less" : `Show ${rest.length} more`}
105 </ThreadDetailsControl>
106 </>
107 );
108}