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