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