Open History to see the last commit
1import { ThreadDetailsControl } from "./ThreadDetailsControl";
2import type { EnvironmentId, ScopedThreadRef, ThreadPullRequestLink } from "@t3tools/contracts";
3import {
4 pullRequestListLines,
5 resolveThreadPullRequestChains,
6 threadPullRequestKeyOf,
7 visibleThreadPullRequests,
8} from "@t3tools/client-runtime/thread-pull-requests";
9import { ChevronDown, ChevronUp, Minus, Plus } from "lucide";
10import { useState, type ComponentProps, type MouseEvent as ReactMouseEvent } from "react";
11
12import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
13import { findProjectOnChangeRequestHost } from "@t3tools/client-runtime/change-request-links";
14
15import { useProjects } from "~/state/entities";
16import { threadEnvironment } from "~/state/threads";
17import { useAtomCommand } from "~/state/use-atom-command";
18
19import { MorphIcon } from "~/components/MorphIcon";
20import { useClientSettings } from "~/hooks/useSettings";
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 // The plus already means "add" in this card (Add project script).
80 const redesign = useClientSettings((s) => s.redesignEnabled);
81 const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: true });
82 const stopWatching = (link: ThreadPullRequestLink | null) =>
83 link?.watch === undefined
84 ? undefined
85 : () =>
86 void watch({
87 environmentId: threadRef.environmentId,
88 input: {
89 threadId: threadRef.threadId,
90 host: link.host,
91 repository: link.repository,
92 number: link.number,
93 watching: false,
94 },
95 });
96 const currentRow = <ThreadDetailsPrRow {...row} onStopWatching={stopWatching(currentLink)} />;
97 const rest =
98 currentLink === null
99 ? []
100 : pullRequestListLines(resolveThreadPullRequestChains(visibleThreadPullRequests(links)))
101 .map((line) => line.link)
102 .filter((link) => threadPullRequestKeyOf(link) !== threadPullRequestKeyOf(currentLink));
103 if (rest.length === 0) return currentRow;
104
105 return (
106 <>
107 {currentRow}
108 {expanded ? (
109 <div data-thread-details-pr-list className="flex flex-col">
110 {rest.map((link) => (
111 <ThreadDetailsPrLinkRow
112 key={threadPullRequestKeyOf(link)}
113 environmentId={row.environmentId}
114 link={link}
115 onOpen={(event) => onOpenLink(event, link.url)}
116 onActed={row.onActed}
117 onStopWatching={stopWatching(link)}
118 />
119 ))}
120 </div>
121 ) : null}
122 <ThreadDetailsControl
123 variant="ghost"
124 size="sm"
125 onClick={() => setExpanded(!expanded)}
126 part="row"
127 tone="muted"
128 className="w-full active:scale-100"
129 >
130 <MorphIcon
131 aria-hidden
132 className="size-4 shrink-0"
133 icon={expanded ? (redesign ? ChevronUp : Minus) : redesign ? ChevronDown : Plus}
134 />
135 {expanded ? "Show less" : `Show ${rest.length} more`}
136 </ThreadDetailsControl>
137 </>
138 );
139}