Open History to see the last commit
1import { ThreadDetailsControl } from "./ThreadDetailsControl";
2import type { EnvironmentId, ScopedThreadRef, ThreadPullRequestLink } from "@t3tools/contracts";
3import {
4 pullRequestListLines,
5 resolveThreadPullRequestChains,
6 threadPullRequestKeyOf,
7 visibleThreadPullRequests,
8} from "@t3tools/shared/threadPullRequests";
9import { Minus, Plus } from "lucide";
10import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
11
12import { findProjectOnChangeRequestHost, parseChangeRequestUrl } from "~/lib/openPullRequestLink";
13
14import { useProjects } from "~/state/entities";
15import { threadEnvironment } from "~/state/threads";
16import { useAtomCommand } from "~/state/use-atom-command";
17
18import { MorphIcon } from "~/components/MorphIcon";
19import { linkedPullRequestSnapshotStatus, prStatusIndicator } from "../ThreadStatusIndicators";
20
21import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
22
23function ThreadDetailsPrLinkRow({
24 environmentId,
25 link,
26 onOpen,
27 onActed,
28 onStopWatching,
29}: {
30 environmentId: EnvironmentId;
31 link: ThreadPullRequestLink;
32 onOpen: (event: ReactMouseEvent<HTMLElement>) => void;
33 onActed?: (() => void) | undefined;
34 onStopWatching?: (() => void) | undefined;
35}) {
36 const projects = useProjects();
37 const parsed = parseChangeRequestUrl(link.url);
38 const project =
39 parsed === null
40 ? null
41 : (findProjectOnChangeRequestHost(
42 projects.filter((candidate) => candidate.environmentId === environmentId),
43 parsed,
44 ) ?? null);
45 const linked = linkedPullRequestSnapshotStatus(link);
46 const pr = linked?.pr ?? null;
47 return (
48 <ThreadDetailsPrRow
49 environmentId={environmentId}
50 pr={pr}
51 number={link.number}
52 reference={link}
53 status={prStatusIndicator(pr, linked?.sourceControlProvider)}
54 project={project}
55 label={`#${link.number}${link.snapshot === null ? "" : `: ${link.snapshot.title}`}`}
56 openAriaLabel={link.url}
57 onOpen={onOpen}
58 onStopWatching={onStopWatching}
59 {...(onActed ? { onActed } : {})}
60 />
61 );
62}
63
64export function ThreadDetailsPrRows({
65 threadRef,
66 links,
67 currentLink,
68 onOpenLink,
69 ...row
70}: ComponentProps<typeof ThreadDetailsPrRow> & {
71 threadRef: ScopedThreadRef;
72 links: ReadonlyArray<ThreadPullRequestLink>;
73 currentLink: ThreadPullRequestLink | null;
74 onOpenLink: (event: ReactMouseEvent<HTMLElement>, url: string) => void;
75}) {
76 const [expanded, setExpanded] = useState(false);
77 const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: true });
78 const stopWatching = (link: ThreadPullRequestLink | null) =>
79 link?.watch === undefined
80 ? undefined
81 : () =>
82 void watch({
83 environmentId: threadRef.environmentId,
84 input: {
85 threadId: threadRef.threadId,
86 host: link.host,
87 repository: link.repository,
88 number: link.number,
89 watching: false,
90 },
91 });
92 const currentRow = <ThreadDetailsPrRow {...row} onStopWatching={stopWatching(currentLink)} />;
93 const rest =
94 currentLink === null
95 ? []
96 : pullRequestListLines(resolveThreadPullRequestChains(visibleThreadPullRequests(links)))
97 .map((line) => line.link)
98 .filter((link) => threadPullRequestKeyOf(link) !== threadPullRequestKeyOf(currentLink));
99 if (rest.length === 0) return currentRow;
100
101 return (
102 <>
103 {currentRow}
104 {expanded
105 ? rest.map((link) => (
106 <ThreadDetailsPrLinkRow
107 key={threadPullRequestKeyOf(link)}
108 environmentId={row.environmentId}
109 link={link}
110 onOpen={(event) => onOpenLink(event, link.url)}
111 onActed={row.onActed}
112 onStopWatching={stopWatching(link)}
113 />
114 ))
115 : null}
116 <ThreadDetailsControl
117 variant="ghost"
118 size="sm"
119 onClick={() => setExpanded(!expanded)}
120 part="row"
121 tone="muted"
122 className="w-full active:scale-100"
123 >
124 <MorphIcon aria-hidden className="size-4 shrink-0" icon={expanded ? Minus : Plus} />
125 {expanded ? "Show less" : `Show ${rest.length} more`}
126 </ThreadDetailsControl>
127 </>
128 );
129}