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

ThreadStatusIndicators.tsx

249 lines7.8 KB

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;
25​
26export interface TerminalStatusIndicator {
27 label: "Terminal process running";
28 colorClass: string;
29 pulse: boolean;
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;
67​
68export function ChangeRequestStatusIcon({ className }: { className?: string }) {
69 return <GitPullRequestIcon className={className} />;
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;
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 };
95​
96export function ThreadStatusLabel({
97 status,
98 compact = false,
99}: {
100 status: ThreadStatusPill;
101 compact?: boolean;
102}) {
103 if (compact) {
104 return (
105 <span
106 title={status.label}
107 className={`inline-flex size-3.5 shrink-0 items-center justify-center ${status.colorClass}`}
108 >
109 <span
110 className={`size-[9px] rounded-full ${status.dotClass} ${
111 status.pulse ? "animate-pulse" : ""
112 }`}
113 />
114 <span className="sr-only">{status.label}</span>
115 </span>
116 );
117 }
118​
119 return (
120 <span
121 title={status.label}
122 className={`inline-flex items-center gap-1 text-[10px] ${status.colorClass}`}
123 >
124 <span
125 className={`h-1.5 w-1.5 rounded-full ${status.dotClass} ${
126 status.pulse ? "animate-pulse" : ""
127 }`}
128 />
129 <span className="hidden md:inline">{status.label}</span>
130 </span>
131 );
133​
134/**
135 * Non-interactive leading status icons for a thread row in compact contexts
136 * like the command palette. Shows the change request state icon (if present) and the
137 * thread status dot, matching the sidebar's leading indicators.
138 */
139export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummary }) {
140 const threadRef = scopeThreadRef(thread.environmentId, thread.id);
141 const lastVisitedAt = useUiStateStore(
142 (state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)],
143 );
144 const threadProjectCwd = useStore(
145 useMemo(
146 () => (state: AppState) =>
147 selectProjectByRef(state, scopeProjectRef(thread.environmentId, thread.projectId))?.cwd ??
148 null,
149 [thread.environmentId, thread.projectId],
150 ),
151 );
152 const gitCwd = thread.worktreePath ?? threadProjectCwd;
153 const gitStatus = useVcsStatus({
154 environmentId: thread.environmentId,
155 cwd: thread.branch != null ? gitCwd : null,
156 });
157 const pr = resolveThreadPr(thread.branch, gitStatus.data);
158 const prStatus = prStatusIndicator(pr, gitStatus.data?.sourceControlProvider);
159 const threadStatus = resolveThreadStatusPill({
160 thread: {
161 ...thread,
162 lastVisitedAt,
163 },
164 });
165​
166 if (!prStatus && !threadStatus) {
167 return null;
168 }
169​
170 return (
171 <span className="inline-flex shrink-0 items-center gap-1.5">
172 {prStatus ? (
173 <Tooltip>
174 <TooltipTrigger
175 render={
176 <span
177 aria-label={prStatus.tooltip}
178 className={`inline-flex items-center justify-center ${prStatus.colorClass}`}
179 />
180 }
181 >
182 <ChangeRequestStatusIcon className="size-3" />
183 </TooltipTrigger>
184 <TooltipPopup side="top">{prStatus.tooltip}</TooltipPopup>
185 </Tooltip>
186 ) : null}
187 {threadStatus ? <ThreadStatusLabel status={threadStatus} /> : null}
188 </span>
189 );
191​
192/**
193 * Non-interactive trailing status icons for a thread row in compact contexts
194 * like the command palette. Shows a terminal-running indicator and a remote
195 * environment indicator, matching the sidebar's trailing indicators.
196 */
197export function ThreadRowTrailingStatus({ thread }: { thread: SidebarThreadSummary }) {
198 const runningTerminalIds = useThreadRunningTerminalIds({
199 environmentId: thread.environmentId,
200 threadId: thread.id,
201 });
202 const primaryEnvironmentId = usePrimaryEnvironmentId();
203 const isRemoteThread =
204 primaryEnvironmentId !== null && thread.environmentId !== primaryEnvironmentId;
205 const remoteEnvLabel = useSavedEnvironmentRuntimeStore(
206 (state) => state.byId[thread.environmentId]?.descriptor?.label ?? null,
207 );
208 const remoteEnvSavedLabel = useSavedEnvironmentRegistryStore(
209 (state) => state.byId[thread.environmentId]?.label ?? null,
210 );
211 const threadEnvironmentLabel = isRemoteThread
212 ? (remoteEnvLabel ?? remoteEnvSavedLabel ?? "Remote")
213 : null;
214 const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
215​
216 if (!terminalStatus && !isRemoteThread) {
217 return null;
218 }
219​
220 return (
221 <span className="inline-flex shrink-0 items-center gap-1.5">
222 {terminalStatus ? (
223 <span
224 role="img"
225 aria-label={terminalStatus.label}
226 title={terminalStatus.label}
227 className={`inline-flex items-center justify-center ${terminalStatus.colorClass}`}
228 >
229 <TerminalIcon className={`size-3 ${terminalStatus.pulse ? "animate-pulse" : ""}`} />
230 </span>
231 ) : null}
232 {isRemoteThread ? (
233 <Tooltip>
234 <TooltipTrigger
235 render={
236 <span
237 aria-label={threadEnvironmentLabel ?? "Remote"}
238 className="inline-flex items-center justify-center"
239 />
240 }
241 >
242 <CloudIcon className="size-3 text-muted-foreground/60" />
243 </TooltipTrigger>
244 <TooltipPopup side="top">{threadEnvironmentLabel}</TooltipPopup>
245 </Tooltip>
246 ) : null}
247 </span>
248 );