Open History to see the last commit
1import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { pullRequestDetailToVcsStatus } from "@t3tools/client-runtime/state/pull-requests";
3import {
4 type EnvironmentId,
5 resolveEnvironmentMachineKind,
6 type ThreadLinkedPullRequest,
7 type VcsStatusResult,
8} from "@t3tools/contracts";
9import { FolderGit2Icon, TerminalIcon } from "lucide-react";
10import { useMemo } from "react";
11import { useEnvironment, usePrimaryEnvironmentId } from "../state/environments";
12import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
13import { useEnvironmentQuery } from "../state/query";
14import { linkedPullRequestDetailAtom, useSharedPullRequestSummary } from "../state/pullRequests";
15import { useThreadRunningTerminalIds } from "../state/terminalSessions";
16import { useUiStateStore } from "../uiStateStore";
17import { resolveChangeRequestPresentation } from "../sourceControlPresentation";
18import { resolveThreadStatusPill, type ThreadStatusPill } from "./Sidebar.logic";
19import { resolvePullRequestState } from "./pullRequest/pullRequestPresentation";
20import type { SidebarThreadSummary } from "../types";
21import { formatWorktreePathForDisplay } from "../worktreeCleanup";
22import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
23
24export interface PrStatusIndicator {
25 label: string;
26 colorClass: string;
27 tooltip: string;
28 tooltipLead: string;
29 tooltipTitle: string;
30 url: string;
31}
32
33export interface TerminalStatusIndicator {
34 label: "Terminal process running";
35 colorClass: string;
36 pulse: boolean;
37}
38
39export type ThreadPr = VcsStatusResult["pr"];
40
41export interface LinkedThreadPullRequestStatus {
42 readonly pr: NonNullable<ThreadPr>;
43 readonly sourceControlProvider: NonNullable<VcsStatusResult["sourceControlProvider"]>;
44}
45
46/** Keep cached summaries visible when an offscreen row stops live queries. */
47export function useLinkedThreadPullRequest(
48 environmentId: EnvironmentId | null,
49 linkedPullRequest: ThreadLinkedPullRequest | null | undefined,
50 enabled = true,
51): LinkedThreadPullRequestStatus | null {
52 const queried = useEnvironmentQuery(
53 !enabled || environmentId === null || linkedPullRequest == null
54 ? null
55 : linkedPullRequestDetailAtom({
56 environmentId,
57 input: {
58 projectId: linkedPullRequest.projectId,
59 repository: linkedPullRequest.repository,
60 number: linkedPullRequest.number,
61 },
62 }),
63 ).data;
64 const detail = useSharedPullRequestSummary(environmentId, linkedPullRequest ?? null, queried);
65
66 return useMemo(
67 () =>
68 detail === null
69 ? null
70 : {
71 pr: pullRequestDetailToVcsStatus(detail),
72 sourceControlProvider: {
73 kind: detail.provider,
74 name: detail.provider,
75 baseUrl: "",
76 },
77 },
78 [detail],
79 );
80}
81
82export function settledPrHoverColorClass(
83 state: NonNullable<ThreadPr>["state"],
84 isDraft = false,
85): string {
86 switch (state) {
87 case "open":
88 if (isDraft) {
89 return "group-hover/v2-row:text-zinc-500 dark:group-hover/v2-row:text-zinc-400/80";
90 }
91 return "group-hover/v2-row:text-emerald-600 dark:group-hover/v2-row:text-emerald-300/90";
92 case "merged":
93 return "group-hover/v2-row:text-violet-600 dark:group-hover/v2-row:text-violet-300/90";
94 case "closed":
95 return "group-hover/v2-row:text-red-600 dark:group-hover/v2-row:text-red-300/90";
96 }
97}
98
99export function prStatusIndicator(
100 pr: ThreadPr,
101 provider: VcsStatusResult["sourceControlProvider"] | null | undefined,
102): PrStatusIndicator | null {
103 function formatPrState(pr: NonNullable<ThreadPr>): string {
104 if (pr.state === "open" && pr.isDraft === true) return "Draft";
105 return pr.state.charAt(0).toUpperCase() + pr.state.slice(1);
106 }
107
108 function formatPrStatusLead(pr: NonNullable<ThreadPr>, changeRequestShortName: string): string {
109 return `${changeRequestShortName} #${pr.number} - ${formatPrState(pr)}`;
110 }
111 if (!pr) return null;
112 const presentation = resolveChangeRequestPresentation(provider);
113
114 const tooltipLead = formatPrStatusLead(pr, presentation.shortName);
115 const tooltip = `${tooltipLead}: ${pr.title}`;
116
117 if (pr.state === "open") {
118 const isDraft = pr.isDraft === true;
119 return {
120 label: `${presentation.shortName} ${isDraft ? "draft" : "open"}`,
121 colorClass: isDraft
122 ? "text-zinc-500 dark:text-zinc-400/80"
123 : "text-emerald-600 dark:text-emerald-300/90",
124 tooltip,
125 tooltipLead,
126 tooltipTitle: pr.title,
127 url: pr.url,
128 };
129 }
130 if (pr.state === "closed") {
131 return {
132 label: `${presentation.shortName} closed`,
133 colorClass: "text-red-600 dark:text-red-300/90",
134 tooltip,
135 tooltipLead,
136 tooltipTitle: pr.title,
137 url: pr.url,
138 };
139 }
140 if (pr.state === "merged") {
141 return {
142 label: `${presentation.shortName} merged`,
143 colorClass: "text-violet-600 dark:text-violet-300/90",
144 tooltip,
145 tooltipLead,
146 tooltipTitle: pr.title,
147 url: pr.url,
148 };
149 }
150 return null;
151}
152
153export function ChangeRequestStatusIcon({
154 state,
155 isDraft = false,
156 className,
157}: Pick<NonNullable<ThreadPr>, "state"> & {
158 readonly isDraft?: boolean | undefined;
159 readonly className?: string | undefined;
160}) {
161 const presentation = resolvePullRequestState({ state, isDraft });
162 return <presentation.Icon className={className} />;
163}
164
165export function PrStatusTooltipContent({ status }: { status: PrStatusIndicator }) {
166 return (
167 <span className="flex max-w-[min(34rem,calc(100vw-2rem))] items-stretch overflow-hidden whitespace-nowrap">
168 <span className="shrink-0 pr-2 font-medium">{status.tooltipLead}</span>
169 <span className="min-h-4 shrink-0 border-border/70 border-l" aria-hidden="true" />
170 <span className="min-w-0 truncate pl-2">{status.tooltipTitle}</span>
171 </span>
172 );
173}
174
175export function terminalStatusFromRunningIds(
176 runningTerminalIds: ReadonlyArray<string>,
177): TerminalStatusIndicator | null {
178 if (runningTerminalIds.length === 0) {
179 return null;
180 }
181 return {
182 label: "Terminal process running",
183 colorClass: "text-teal-600 dark:text-teal-300/90",
184 pulse: true,
185 };
186}
187
188export function ThreadWorktreeIndicator({
189 thread,
190}: {
191 thread: Pick<SidebarThreadSummary, "id" | "branch" | "worktreePath">;
192}) {
193 const worktreePath = thread.worktreePath?.trim();
194 if (!worktreePath) {
195 return null;
196 }
197
198 const displayPath = formatWorktreePathForDisplay(worktreePath);
199 const tooltip = thread.branch
200 ? `Worktree: ${displayPath} (${thread.branch})`
201 : `Worktree: ${displayPath}`;
202
203 return (
204 <Tooltip>
205 <TooltipTrigger
206 render={
207 <span
208 role="img"
209 aria-label={tooltip}
210 data-testid={`thread-worktree-${thread.id}`}
211 className="inline-flex items-center justify-center"
212 />
213 }
214 >
215 <FolderGit2Icon className="size-3 text-muted-foreground/40" />
216 </TooltipTrigger>
217 <TooltipPopup side="top">{tooltip}</TooltipPopup>
218 </Tooltip>
219 );
220}
221
222export function ThreadStatusLabel({
223 status,
224 compact = false,
225}: {
226 status: ThreadStatusPill;
227 compact?: boolean;
228}) {
229 if (compact) {
230 return (
231 <Tooltip>
232 <TooltipTrigger
233 render={
234 <span
235 aria-label={status.label}
236 className={`inline-flex size-3.5 shrink-0 items-center justify-center ${status.colorClass}`}
237 />
238 }
239 >
240 <span
241 className={`size-[9px] rounded-full ${status.dotClass} ${
242 status.pulse ? "animate-status-pulse" : ""
243 }`}
244 />
245 </TooltipTrigger>
246 <TooltipPopup side="top">{status.label}</TooltipPopup>
247 </Tooltip>
248 );
249 }
250
251 return (
252 <Tooltip>
253 <TooltipTrigger
254 render={
255 <span
256 aria-label={status.label}
257 className={`inline-flex items-center gap-1 text-[10px] ${status.colorClass}`}
258 />
259 }
260 >
261 <span
262 className={`h-1.5 w-1.5 rounded-full ${status.dotClass} ${
263 status.pulse ? "animate-status-pulse" : ""
264 }`}
265 />
266 <span className="hidden md:inline">{status.label}</span>
267 </TooltipTrigger>
268 <TooltipPopup side="top">{status.label}</TooltipPopup>
269 </Tooltip>
270 );
271}
272
273/**
274 * Non-interactive leading status icons for a thread row in compact contexts
275 * like the command palette. Shows the change request state icon (if present) and the
276 * thread status dot, matching the sidebar's leading indicators.
277 */
278export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummary }) {
279 const threadRef = scopeThreadRef(thread.environmentId, thread.id);
280 const lastVisitedAt = useUiStateStore(
281 (state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)],
282 );
283 const pullRequest = useLinkedThreadPullRequest(
284 thread.environmentId,
285 thread.linkedPullRequest ?? thread.branchPullRequest,
286 );
287 const pr = pullRequest?.pr ?? null;
288 const prStatus = prStatusIndicator(pr, pullRequest?.sourceControlProvider);
289 const threadStatus = resolveThreadStatusPill({
290 thread: {
291 ...thread,
292 lastVisitedAt,
293 },
294 });
295
296 if (!prStatus && !threadStatus) {
297 return null;
298 }
299
300 return (
301 <span className="inline-flex shrink-0 items-center gap-1.5">
302 {prStatus && pr ? (
303 <Tooltip>
304 <TooltipTrigger
305 render={
306 <span
307 aria-label={prStatus.tooltip}
308 className={`inline-flex items-center justify-center ${prStatus.colorClass}`}
309 />
310 }
311 >
312 <ChangeRequestStatusIcon state={pr.state} isDraft={pr.isDraft} className="size-3" />
313 </TooltipTrigger>
314 <TooltipPopup side="top">
315 <PrStatusTooltipContent status={prStatus} />
316 </TooltipPopup>
317 </Tooltip>
318 ) : null}
319 {threadStatus ? <ThreadStatusLabel status={threadStatus} /> : null}
320 </span>
321 );
322}
323
324/**
325 * Non-interactive trailing status icons for a thread row in compact contexts
326 * like the command palette. Shows a terminal-running indicator and a remote
327 * environment indicator, matching the sidebar's trailing indicators.
328 */
329export function ThreadRowTrailingStatus({ thread }: { thread: SidebarThreadSummary }) {
330 const runningTerminalIds = useThreadRunningTerminalIds({
331 environmentId: thread.environmentId,
332 threadId: thread.id,
333 });
334 const environment = useEnvironment(thread.environmentId);
335 const primaryEnvironmentId = usePrimaryEnvironmentId();
336 // No primary (the hosted app) means every thread is remote, and the machine
337 // glyph is what tells the environments apart.
338 const isRemoteThread = thread.environmentId !== primaryEnvironmentId;
339 const remoteEnvLabel = environment?.label ?? null;
340 const threadEnvironmentLabel = isRemoteThread ? (remoteEnvLabel ?? "Remote") : null;
341 const remoteMachine = resolveEnvironmentMachineKind(environment?.serverConfig ?? null);
342 const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
343
344 if (!terminalStatus && !isRemoteThread) {
345 return null;
346 }
347
348 return (
349 <span className="inline-flex shrink-0 items-center gap-1.5">
350 {terminalStatus ? (
351 <Tooltip>
352 <TooltipTrigger
353 render={
354 <span
355 role="img"
356 aria-label={terminalStatus.label}
357 className={`inline-flex items-center justify-center ${terminalStatus.colorClass}`}
358 />
359 }
360 >
361 <TerminalIcon
362 className={`size-3 ${terminalStatus.pulse ? "animate-status-pulse" : ""}`}
363 />
364 </TooltipTrigger>
365 <TooltipPopup side="top">{terminalStatus.label}</TooltipPopup>
366 </Tooltip>
367 ) : null}
368 {isRemoteThread ? (
369 <Tooltip>
370 <TooltipTrigger
371 render={
372 <span
373 aria-label={threadEnvironmentLabel ?? "Remote"}
374 className="inline-flex items-center justify-center"
375 />
376 }
377 >
378 <EnvironmentMachineIcon
379 kind={remoteMachine}
380 className="size-3 text-muted-foreground/60"
381 />
382 </TooltipTrigger>
383 <TooltipPopup side="top">{threadEnvironmentLabel}</TooltipPopup>
384 </Tooltip>
385 ) : null}
386 </span>
387 );
388}