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