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

ThreadDetailsPrRows.tsx

222 lines7.1 KB

1import { pipe } from "effect/Function";
2import { ThreadDetailsControl } from "./ThreadDetailsControl";
3import type {
4 EnvironmentId,
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 visibleThreadPullRequests,
15} from "@t3tools/client-runtime/thread-pull-requests";
16import { T, useMessages, Var } from "gt-react";
17import { ChevronDownIcon, ChevronUpIcon } from "lucide-react";
18import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
19​
20import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
21import {
22 findProjectForChangeRequest,
23 findProjectOnChangeRequestHost,
24} from "@t3tools/client-runtime/change-request-links";
25​
26import type { ComposerThreadTarget } from "~/composerDraftStore";
27import { useProjects, useQuietThreadShell } from "~/state/entities";
28import { threadEnvironment } from "~/state/threads";
29import { useAtomCommand } from "~/state/use-atom-command";
30​
31import { useOpenPrLink } from "../../lib/openPullRequestLink";
32import {
33 linkedPullRequestSnapshotStatus,
34 prStatusIndicator,
35 useThreadDisplayedPullRequest,
36} from "../ThreadStatusIndicators";
37​
38import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
39​
40function ThreadDetailsPrLinkRow({
41 environmentId,
42 link,
43 onOpen,
44 onActed,
45 onStopWatching,
46 composerTarget,
47}: {
48 environmentId: EnvironmentId;
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 = linkedPullRequestSnapshotStatus(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}${link.snapshot === null ? "" : `: ${link.snapshot.title}`}`}
76 onOpen={onOpen}
77 onStopWatching={onStopWatching}
78 composerTarget={composerTarget}
79 {...(onActed ? { onActed } : {})}
80 />
81 );
83​
84function ThreadDetailsPrRows({
85 threadRef,
86 links,
87 currentLink,
88 onOpenLink,
89 ...row
90}: ComponentProps<typeof ThreadDetailsPrRow> & {
91 threadRef: ScopedThreadRef;
92 links: ReadonlyArray<ThreadPullRequestLink>;
93 currentLink: ThreadPullRequestLink | null;
94 onOpenLink: (event: ReactMouseEvent<HTMLElement>, url: string) => void;
95}) {
96 const [expanded, setExpanded] = useState(false);
97 const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: true });
98 const stopWatching = (link: ThreadPullRequestLink | null) =>
99 link?.watch === undefined
100 ? undefined
101 : () =>
102 void watch({
103 environmentId: threadRef.environmentId,
104 input: {
105 threadId: threadRef.threadId,
106 host: link.host,
107 repository: link.repository,
108 number: link.number,
109 watching: false,
110 },
111 });
112 const currentRow = <ThreadDetailsPrRow {...row} onStopWatching={stopWatching(currentLink)} />;
113 const rest =
114 currentLink === null
115 ? []
116 : pipe(visibleThreadPullRequests(links), resolveThreadPullRequestChains, pullRequestListLines)
117 .map((line) => line.link)
118 .filter((link) => threadPullRequestKeyOf(link) !== threadPullRequestKeyOf(currentLink));
119 if (rest.length === 0) return currentRow;
120​
121 return (
122 <>
123 {currentRow}
124 {expanded ? (
125 <div data-thread-details-pr-list className="flex flex-col">
126 {rest.map((link) => (
127 <ThreadDetailsPrLinkRow
128 key={threadPullRequestKeyOf(link)}
129 environmentId={row.environmentId}
130 link={link}
131 onOpen={(event) => onOpenLink(event, link.url)}
132 onActed={row.onActed}
133 onStopWatching={stopWatching(link)}
134 composerTarget={row.composerTarget}
135 />
136 ))}
137 </div>
138 ) : null}
139 <ThreadDetailsControl
140 variant="ghost"
141 size="sm"
142 onClick={() => setExpanded(!expanded)}
143 part="row"
144 tone="muted"
145 className="w-full active:scale-100"
146 >
147 {/* Chevrons, not plus and minus: the plus already means "add" in this card. */}
148 {expanded ? (
149 <ChevronUpIcon aria-hidden className="size-4 shrink-0" />
150 ) : (
151 <ChevronDownIcon aria-hidden className="size-4 shrink-0" />
152 )}
153 {expanded ? (
154 <T>Show less</T>
155 ) : (
156 <T>
157 Show <Var>{rest.length}</Var> more
158 </T>
159 )}
160 </ThreadDetailsControl>
161 </>
162 );
164​
165/** Linked pull requests need no repository; only the branch's own pull request comes from git. */
166export function ThreadDetailsThreadPrRows({
167 threadRef,
168 project,
169 branchPr = null,
170 branchSourceControlProvider,
171 onActed,
172 composerTarget,
173}: {
174 threadRef: ScopedThreadRef;
175 project: EnvironmentProject | null;
176 branchPr?: VcsStatusResult["pr"];
177 branchSourceControlProvider?: VcsStatusResult["sourceControlProvider"];
178 onActed?: () => void;
179 composerTarget?: ComposerThreadTarget | undefined;
180}) {
181 const thread = useQuietThreadShell(threadRef);
182 const displayed = useThreadDisplayedPullRequest(
183 threadRef.environmentId,
184 thread,
185 branchPr,
186 branchSourceControlProvider,
187 );
188 const openPrLink = useOpenPrLink(threadRef);
189 const { currentLink, pr, number, url } = displayed;
190 const projects = useProjects();
191 const parsed =
192 currentLink === null
193 ? undefined
194 : sourceControlClients.parseChangeRequestUrl(currentLink.url)?.link;
195 // Merge settings belong to the project holding the linked repository, the thread's own first.
196 const rowProject =
197 parsed === undefined || (project !== null && findProjectForChangeRequest([project], parsed))
198 ? project
199 : (findProjectForChangeRequest(
200 projects.filter((candidate) => candidate.environmentId === threadRef.environmentId),
201 parsed,
202 ) ?? project);
203 if (number === undefined || url === undefined) return null;
204 return (
205 <ThreadDetailsPrRows
206 threadRef={threadRef}
207 links={thread?.pullRequests ?? []}
208 currentLink={currentLink}
209 onOpenLink={openPrLink}
210 environmentId={threadRef.environmentId}
211 pr={pr}
212 number={number}
213 reference={currentLink}
214 status={displayed.status}
215 project={rowProject}
216 label={`#${number}${pr?.title.trim() ? `: ${pr.title}` : ""}`}
217 onOpen={(event) => openPrLink(event, url)}
218 composerTarget={composerTarget}
219 {...(onActed ? { onActed } : {})}
220 />
221 );