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

ThreadDetailsPrRows.tsx

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