The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 204 lines 6.56 KB
Raw
1 import { pipe } from "effect/Function";
2 import { ThreadDetailsMoreButton } from "./ThreadDetailsControl";
3 import type {
4 EnvironmentId,
5 ScopedThreadRef,
6 ThreadPullRequestLink,
7 VcsStatusResult,
8 } from "@t3tools/contracts";
9 import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
10 import {
11 pullRequestListLines,
12 resolveThreadPullRequestChains,
13 threadPullRequestKeyOf,
14 visibleThreadPullRequests,
15 } from "@t3tools/client-runtime/thread-pull-requests";
16 import { useMessages } from "gt-react";
17 import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
18
19 import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
20 import {
21 findProjectForChangeRequest,
22 findProjectOnChangeRequestHost,
23 } from "@t3tools/client-runtime/change-request-links";
24
25 import type { ComposerThreadTarget } from "~/composerDraftStore";
26 import { useProjects, useQuietThreadShell } from "~/state/entities";
27 import { threadEnvironment } from "~/state/threads";
28 import { useAtomCommand } from "~/state/use-atom-command";
29
30 import { useOpenPrLink } from "../../lib/openPullRequestLink";
31 import {
32 linkedPullRequestSnapshotStatus,
33 prStatusIndicator,
34 useThreadDisplayedPullRequest,
35 } from "../ThreadStatusIndicators";
36
37 import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
38
39 function 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 m = useMessages();
55 const projects = useProjects();
56 const parsed = sourceControlClients.parseChangeRequestUrl(link.url)?.link;
57 const project =
58 parsed === undefined
59 ? null
60 : (findProjectOnChangeRequestHost(
61 projects.filter((candidate) => candidate.environmentId === environmentId),
62 parsed,
63 ) ?? null);
64 const linked = linkedPullRequestSnapshotStatus(link);
65 const pr = linked?.pr ?? null;
66 return (
67 <ThreadDetailsPrRow
68 environmentId={environmentId}
69 pr={pr}
70 number={link.number}
71 reference={link}
72 status={prStatusIndicator(pr, linked?.sourceControlProvider, m)}
73 project={project}
74 label={`#${link.number}${link.snapshot === null ? "" : `: ${link.snapshot.title}`}`}
75 onOpen={onOpen}
76 onStopWatching={onStopWatching}
77 composerTarget={composerTarget}
78 {...(onActed ? { onActed } : {})}
79 />
80 );
81 }
82
83 function 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 <ThreadDetailsMoreButton
139 expanded={expanded}
140 remaining={rest.length}
141 onToggle={() => setExpanded(!expanded)}
142 />
143 </>
144 );
145 }
146
147 /** Linked pull requests need no repository; only the branch's own pull request comes from git. */
148 export function ThreadDetailsThreadPrRows({
149 threadRef,
150 project,
151 branchPr = null,
152 branchSourceControlProvider,
153 onActed,
154 composerTarget,
155 }: {
156 threadRef: ScopedThreadRef;
157 project: EnvironmentProject | null;
158 branchPr?: VcsStatusResult["pr"];
159 branchSourceControlProvider?: VcsStatusResult["sourceControlProvider"];
160 onActed?: () => void;
161 composerTarget?: ComposerThreadTarget | undefined;
162 }) {
163 const thread = useQuietThreadShell(threadRef);
164 const displayed = useThreadDisplayedPullRequest(
165 threadRef.environmentId,
166 thread,
167 branchPr,
168 branchSourceControlProvider,
169 );
170 const openPrLink = useOpenPrLink(threadRef);
171 const { currentLink, pr, number, url } = displayed;
172 const projects = useProjects();
173 const parsed =
174 currentLink === null
175 ? undefined
176 : sourceControlClients.parseChangeRequestUrl(currentLink.url)?.link;
177 // Merge settings belong to the project holding the linked repository, the thread's own first.
178 const rowProject =
179 parsed === undefined || (project !== null && findProjectForChangeRequest([project], parsed))
180 ? project
181 : (findProjectForChangeRequest(
182 projects.filter((candidate) => candidate.environmentId === threadRef.environmentId),
183 parsed,
184 ) ?? project);
185 if (number === undefined || url === undefined) return null;
186 return (
187 <ThreadDetailsPrRows
188 threadRef={threadRef}
189 links={thread?.pullRequests ?? []}
190 currentLink={currentLink}
191 onOpenLink={openPrLink}
192 environmentId={threadRef.environmentId}
193 pr={pr}
194 number={number}
195 reference={currentLink}
196 status={displayed.status}
197 project={rowProject}
198 label={`#${number}${pr?.title.trim() ? `: ${pr.title}` : ""}`}
199 onOpen={(event) => openPrLink(event, url)}
200 composerTarget={composerTarget}
201 {...(onActed ? { onActed } : {})}
202 />
203 );
204 }