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