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