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

ThreadStatusIndicators.tsx

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