Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThreadStatusIndicators.tsx

357 lines10.8 KB

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