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

ThreadStatusIndicators.tsx

352 lines10.7 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 { 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;
29​
30export interface TerminalStatusIndicator {
31 label: "Terminal process running";
32 colorClass: string;
33 pulse: boolean;
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 }
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;
98​
99export function ChangeRequestStatusIcon({ className }: { className?: string }) {
100 return <GitPullRequestIcon className={className} />;
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 );
112​
113export function resolveThreadPr(input: {
114 threadBranch: string | null;
115 gitStatus: VcsStatusResult | null;
116}): ThreadPr | null {
117 const { threadBranch, gitStatus } = input;
118 if (gitStatus === null) {
119 return null;
120 }
121​
122 if (threadBranch === null || gitStatus.refName !== threadBranch) {
123 return null;
124 }
125​
126 return gitStatus.pr ?? null;
128​
129export function terminalStatusFromRunningIds(
130 runningTerminalIds: ReadonlyArray<string>,
131): TerminalStatusIndicator | null {
132 if (runningTerminalIds.length === 0) {
133 return null;
134 }
135 return {
136 label: "Terminal process running",
137 colorClass: "text-teal-600 dark:text-teal-300/90",
138 pulse: true,
139 };
141​
142export function ThreadWorktreeIndicator({
143 thread,
144}: {
145 thread: Pick<SidebarThreadSummary, "id" | "branch" | "worktreePath">;
146}) {
147 const worktreePath = thread.worktreePath?.trim();
148 if (!worktreePath) {
149 return null;
150 }
151​
152 const displayPath = formatWorktreePathForDisplay(worktreePath);
153 const tooltip = thread.branch
154 ? `Worktree: ${displayPath} (${thread.branch})`
155 : `Worktree: ${displayPath}`;
156​
157 return (
158 <Tooltip>
159 <TooltipTrigger
160 render={
161 <span
162 role="img"
163 aria-label={tooltip}
164 data-testid={`thread-worktree-${thread.id}`}
165 className="inline-flex items-center justify-center"
166 />
167 }
168 >
169 <FolderGit2Icon className="size-3 text-muted-foreground/40" />
170 </TooltipTrigger>
171 <TooltipPopup side="top">{tooltip}</TooltipPopup>
172 </Tooltip>
173 );
175​
176export function ThreadStatusLabel({
177 status,
178 compact = false,
179}: {
180 status: ThreadStatusPill;
181 compact?: boolean;
182}) {
183 if (compact) {
184 return (
185 <Tooltip>
186 <TooltipTrigger
187 render={
188 <span
189 aria-label={status.label}
190 className={`inline-flex size-3.5 shrink-0 items-center justify-center ${status.colorClass}`}
191 />
192 }
193 >
194 <span
195 className={`size-[9px] rounded-full ${status.dotClass} ${
196 status.pulse ? "animate-status-pulse" : ""
197 }`}
198 />
199 </TooltipTrigger>
200 <TooltipPopup side="top">{status.label}</TooltipPopup>
201 </Tooltip>
202 );
203 }
204​
205 return (
206 <Tooltip>
207 <TooltipTrigger
208 render={
209 <span
210 aria-label={status.label}
211 className={`inline-flex items-center gap-1 text-[10px] ${status.colorClass}`}
212 />
213 }
214 >
215 <span
216 className={`h-1.5 w-1.5 rounded-full ${status.dotClass} ${
217 status.pulse ? "animate-status-pulse" : ""
218 }`}
219 />
220 <span className="hidden md:inline">{status.label}</span>
221 </TooltipTrigger>
222 <TooltipPopup side="top">{status.label}</TooltipPopup>
223 </Tooltip>
224 );
226​
227/**
228 * Non-interactive leading status icons for a thread row in compact contexts
229 * like the command palette. Shows the change request state icon (if present) and the
230 * thread status dot, matching the sidebar's leading indicators.
231 */
232export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummary }) {
233 const threadRef = scopeThreadRef(thread.environmentId, thread.id);
234 const lastVisitedAt = useUiStateStore(
235 (state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)],
236 );
237 const threadProject = useProject(
238 useMemo(
239 () => scopeProjectRef(thread.environmentId, thread.projectId),
240 [thread.environmentId, thread.projectId],
241 ),
242 );
243 const threadProjectCwd = threadProject?.workspaceRoot ?? null;
244 const gitCwd = thread.worktreePath ?? threadProjectCwd;
245 const gitStatus = useEnvironmentQuery(
246 (thread.branch != null || thread.worktreePath !== null) && gitCwd !== null
247 ? vcsEnvironment.status({
248 environmentId: thread.environmentId,
249 input: { cwd: gitCwd },
250 })
251 : null,
252 );
253 const pr = resolveThreadPr({
254 threadBranch: thread.branch,
255 gitStatus: gitStatus.data,
256 });
257 const prStatus = prStatusIndicator(pr, gitStatus.data?.sourceControlProvider);
258 const threadStatus = resolveThreadStatusPill({
259 thread: {
260 ...thread,
261 lastVisitedAt,
262 },
263 });
264​
265 if (!prStatus && !threadStatus) {
266 return null;
267 }
268​
269 return (
270 <span className="inline-flex shrink-0 items-center gap-1.5">
271 {prStatus ? (
272 <Tooltip>
273 <TooltipTrigger
274 render={
275 <span
276 aria-label={prStatus.tooltip}
277 className={`inline-flex items-center justify-center ${prStatus.colorClass}`}
278 />
279 }
280 >
281 <ChangeRequestStatusIcon className="size-3" />
282 </TooltipTrigger>
283 <TooltipPopup side="top">
284 <PrStatusTooltipContent status={prStatus} />
285 </TooltipPopup>
286 </Tooltip>
287 ) : null}
288 {threadStatus ? <ThreadStatusLabel status={threadStatus} /> : null}
289 </span>
290 );
292​
293/**
294 * Non-interactive trailing status icons for a thread row in compact contexts
295 * like the command palette. Shows a terminal-running indicator and a remote
296 * environment indicator, matching the sidebar's trailing indicators.
297 */
298export function ThreadRowTrailingStatus({ thread }: { thread: SidebarThreadSummary }) {
299 const runningTerminalIds = useThreadRunningTerminalIds({
300 environmentId: thread.environmentId,
301 threadId: thread.id,
302 });
303 const environment = useEnvironment(thread.environmentId);
304 const primaryEnvironmentId = usePrimaryEnvironmentId();
305 const isRemoteThread =
306 primaryEnvironmentId !== null && thread.environmentId !== primaryEnvironmentId;
307 const remoteEnvLabel = environment?.label ?? null;
308 const threadEnvironmentLabel = isRemoteThread ? (remoteEnvLabel ?? "Remote") : null;
309 const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
310​
311 if (!terminalStatus && !isRemoteThread) {
312 return null;
313 }
314​
315 return (
316 <span className="inline-flex shrink-0 items-center gap-1.5">
317 {terminalStatus ? (
318 <Tooltip>
319 <TooltipTrigger
320 render={
321 <span
322 role="img"
323 aria-label={terminalStatus.label}
324 className={`inline-flex items-center justify-center ${terminalStatus.colorClass}`}
325 />
326 }
327 >
328 <TerminalIcon
329 className={`size-3 ${terminalStatus.pulse ? "animate-status-pulse" : ""}`}
330 />
331 </TooltipTrigger>
332 <TooltipPopup side="top">{terminalStatus.label}</TooltipPopup>
333 </Tooltip>
334 ) : null}
335 {isRemoteThread ? (
336 <Tooltip>
337 <TooltipTrigger
338 render={
339 <span
340 aria-label={threadEnvironmentLabel ?? "Remote"}
341 className="inline-flex items-center justify-center"
342 />
343 }
344 >
345 <CloudIcon className="size-3 text-muted-foreground/60" />
346 </TooltipTrigger>
347 <TooltipPopup side="top">{threadEnvironmentLabel}</TooltipPopup>
348 </Tooltip>
349 ) : null}
350 </span>
351 );