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