Open History to see the last commit
1import { ThreadDetailsMoreButton } from "./ThreadDetailsControl";
2import type {
3 EnvironmentId,
4 ProjectId,
5 ScopedThreadRef,
6 ThreadPullRequestLink,
7 VcsStatusResult,
8} from "@t3tools/contracts";
9import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
10import {
11 pullRequestListLines,
12 resolveThreadPullRequestChains,
13 threadPullRequestKeyOf,
14} from "@t3tools/client-runtime/thread-pull-requests";
15import { useMessages } from "gt-react";
16import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
17
18import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
19import {
20 findProjectForChangeRequest,
21 findProjectOnChangeRequestHost,
22} from "@t3tools/client-runtime/change-request-links";
23
24import type { ComposerThreadTarget } from "~/composerDraftStore";
25import { useProjects, useQuietThreadShell } from "~/state/entities";
26import { threadEnvironment } from "~/state/threads";
27import { useAtomCommand } from "~/state/use-atom-command";
28
29import { useOpenPrLink } from "../../lib/openPullRequestLink";
30import {
31 useLinkedPullRequestStatus,
32 prStatusIndicator,
33 useThreadDisplayedPullRequest,
34} from "../ThreadStatusIndicators";
35
36import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
37
38function ThreadDetailsPrLinkRow({
39 environmentId,
40 projectId,
41 link,
42 onOpen,
43 onActed,
44 onStopWatching,
45 composerTarget,
46}: {
47 environmentId: EnvironmentId;
48 projectId: ProjectId | null;
49 link: ThreadPullRequestLink;
50 onOpen: (event: ReactMouseEvent<HTMLElement>) => void;
51 onActed?: (() => void) | undefined;
52 onStopWatching?: (() => void) | undefined;
53 composerTarget?: ComposerThreadTarget | undefined;
54}) {
55 const m = useMessages();
56 const projects = useProjects();
57 const parsed = sourceControlClients.parseChangeRequestUrl(link.url)?.link;
58 const project =
59 parsed === undefined
60 ? null
61 : (findProjectOnChangeRequestHost(
62 projects.filter((candidate) => candidate.environmentId === environmentId),
63 parsed,
64 ) ?? null);
65 const linked = useLinkedPullRequestStatus(environmentId, projectId, link);
66 const pr = linked?.pr ?? null;
67 return (
68 <ThreadDetailsPrRow
69 environmentId={environmentId}
70 pr={pr}
71 number={link.number}
72 reference={link}
73 status={prStatusIndicator(pr, linked?.sourceControlProvider, m)}
74 project={project}
75 label={`#${link.number}${pr?.title ? `: ${pr.title}` : ""}`}
76 onOpen={onOpen}
77 onStopWatching={onStopWatching}
78 composerTarget={composerTarget}
79 {...(onActed ? { onActed } : {})}
80 />
81 );
82}
83
84function ThreadDetailsPrRows({
85 threadRef,
86 projectId,
87 links,
88 currentLink,
89 onOpenLink,
90 ...row
91}: ComponentProps<typeof ThreadDetailsPrRow> & {
92 threadRef: ScopedThreadRef;
93 projectId: ProjectId | null;
94 links: ReadonlyArray<ThreadPullRequestLink>;
95 currentLink: ThreadPullRequestLink | null;
96 onOpenLink: (event: ReactMouseEvent<HTMLElement>, url: string) => void;
97}) {
98 const [expanded, setExpanded] = useState(false);
99 const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: true });
100 const stopWatching = (link: ThreadPullRequestLink | null) =>
101 link?.watch === undefined
102 ? undefined
103 : () =>
104 void watch({
105 environmentId: threadRef.environmentId,
106 input: {
107 threadId: threadRef.threadId,
108 host: link.host,
109 repository: link.repository,
110 number: link.number,
111 watching: false,
112 },
113 });
114 const currentRow = <ThreadDetailsPrRow {...row} onStopWatching={stopWatching(currentLink)} />;
115 const rest =
116 currentLink === null
117 ? []
118 : pullRequestListLines(resolveThreadPullRequestChains(links))
119 .map((line) => line.link)
120 .filter((link) => threadPullRequestKeyOf(link) !== threadPullRequestKeyOf(currentLink));
121 if (rest.length === 0) return currentRow;
122
123 return (
124 <>
125 {currentRow}
126 {expanded ? (
127 <div data-thread-details-pr-list className="flex flex-col">
128 {rest.map((link) => (
129 <ThreadDetailsPrLinkRow
130 key={threadPullRequestKeyOf(link)}
131 environmentId={row.environmentId}
132 projectId={projectId}
133 link={link}
134 onOpen={(event) => onOpenLink(event, link.url)}
135 onActed={row.onActed}
136 onStopWatching={stopWatching(link)}
137 composerTarget={row.composerTarget}
138 />
139 ))}
140 </div>
141 ) : null}
142 <ThreadDetailsMoreButton
143 expanded={expanded}
144 remaining={rest.length}
145 onToggle={() => setExpanded(!expanded)}
146 />
147 </>
148 );
149}
150
151/** Linked pull requests need no repository; only the branch's own pull request comes from git. */
152export function ThreadDetailsThreadPrRows({
153 threadRef,
154 project,
155 branchPr = null,
156 branchSourceControlProvider,
157 onActed,
158 composerTarget,
159}: {
160 threadRef: ScopedThreadRef;
161 project: EnvironmentProject | null;
162 branchPr?: VcsStatusResult["pr"];
163 branchSourceControlProvider?: VcsStatusResult["sourceControlProvider"];
164 onActed?: () => void;
165 composerTarget?: ComposerThreadTarget | undefined;
166}) {
167 const thread = useQuietThreadShell(threadRef);
168 const displayed = useThreadDisplayedPullRequest(
169 threadRef.environmentId,
170 thread,
171 branchPr,
172 branchSourceControlProvider,
173 );
174 const openPrLink = useOpenPrLink(threadRef);
175 const { currentLink, pr, number, url } = displayed;
176 const projects = useProjects();
177 const parsed =
178 currentLink === null
179 ? undefined
180 : sourceControlClients.parseChangeRequestUrl(currentLink.url)?.link;
181 // Merge settings belong to the project holding the linked repository, the thread's own first.
182 const rowProject =
183 parsed === undefined || (project !== null && findProjectForChangeRequest([project], parsed))
184 ? project
185 : (findProjectForChangeRequest(
186 projects.filter((candidate) => candidate.environmentId === threadRef.environmentId),
187 parsed,
188 ) ?? project);
189 if (number === undefined || url === undefined) return null;
190 return (
191 <ThreadDetailsPrRows
192 threadRef={threadRef}
193 projectId={thread?.projectId ?? null}
194 links={thread?.pullRequests ?? []}
195 currentLink={currentLink}
196 onOpenLink={openPrLink}
197 environmentId={threadRef.environmentId}
198 pr={pr}
199 number={number}
200 reference={currentLink}
201 status={displayed.status}
202 project={rowProject}
203 label={`#${number}${pr?.title.trim() ? `: ${pr.title}` : ""}`}
204 onOpen={(event) => openPrLink(event, url)}
205 composerTarget={composerTarget}
206 {...(onActed ? { onActed } : {})}
207 />
208 );
209}