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 { useGitStatus } from "../lib/gitStatusState";
11import { type AppState, selectProjectByRef, useStore } from "../store";
12import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore";
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: 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 <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 );
132}
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 = useGitStatus({
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 );
190}
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 threadRef = scopeThreadRef(thread.environmentId, thread.id);
199 const runningTerminalIds = useTerminalStateStore(
200 (state) =>
201 selectThreadTerminalState(state.terminalStateByThreadKey, threadRef).runningTerminalIds,
202 );
203 const primaryEnvironmentId = usePrimaryEnvironmentId();
204 const isRemoteThread =
205 primaryEnvironmentId !== null && thread.environmentId !== primaryEnvironmentId;
206 const remoteEnvLabel = useSavedEnvironmentRuntimeStore(
207 (state) => state.byId[thread.environmentId]?.descriptor?.label ?? null,
208 );
209 const remoteEnvSavedLabel = useSavedEnvironmentRegistryStore(
210 (state) => state.byId[thread.environmentId]?.label ?? null,
211 );
212 const threadEnvironmentLabel = isRemoteThread
213 ? (remoteEnvLabel ?? remoteEnvSavedLabel ?? "Remote")
214 : null;
215 const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
216
217 if (!terminalStatus && !isRemoteThread) {
218 return null;
219 }
220
221 return (
222 <span className="inline-flex shrink-0 items-center gap-1.5">
223 {terminalStatus ? (
224 <span
225 role="img"
226 aria-label={terminalStatus.label}
227 title={terminalStatus.label}
228 className={`inline-flex items-center justify-center ${terminalStatus.colorClass}`}
229 >
230 <TerminalIcon className={`size-3 ${terminalStatus.pulse ? "animate-pulse" : ""}`} />
231 </span>
232 ) : null}
233 {isRemoteThread ? (
234 <Tooltip>
235 <TooltipTrigger
236 render={
237 <span
238 aria-label={threadEnvironmentLabel ?? "Remote"}
239 className="inline-flex items-center justify-center"
240 />
241 }
242 >
243 <CloudIcon className="size-3 text-muted-foreground/60" />
244 </TooltipTrigger>
245 <TooltipPopup side="top">{threadEnvironmentLabel}</TooltipPopup>
246 </Tooltip>
247 ) : null}
248 </span>
249 );
250}