Open History to see the last commit
1import { scopeProjectRef, scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime";
2import type { GitStatusResult } 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 { resolveThreadStatusPill, type ThreadStatusPill } from "./Sidebar.logic";
15import type { SidebarThreadSummary } from "../types";
16import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
17
18export interface PrStatusIndicator {
19 label: "PR open" | "PR closed" | "PR merged";
20 colorClass: string;
21 tooltip: string;
22 url: string;
23}
24
25export interface TerminalStatusIndicator {
26 label: "Terminal process running";
27 colorClass: string;
28 pulse: boolean;
29}
30
31export type ThreadPr = GitStatusResult["pr"];
32
33export function prStatusIndicator(pr: ThreadPr): PrStatusIndicator | null {
34 if (!pr) return null;
35
36 if (pr.state === "open") {
37 return {
38 label: "PR open",
39 colorClass: "text-emerald-600 dark:text-emerald-300/90",
40 tooltip: `#${pr.number} PR open: ${pr.title}`,
41 url: pr.url,
42 };
43 }
44 if (pr.state === "closed") {
45 return {
46 label: "PR closed",
47 colorClass: "text-zinc-500 dark:text-zinc-400/80",
48 tooltip: `#${pr.number} PR closed: ${pr.title}`,
49 url: pr.url,
50 };
51 }
52 if (pr.state === "merged") {
53 return {
54 label: "PR merged",
55 colorClass: "text-violet-600 dark:text-violet-300/90",
56 tooltip: `#${pr.number} PR merged: ${pr.title}`,
57 url: pr.url,
58 };
59 }
60 return null;
61}
62
63export function resolveThreadPr(
64 threadBranch: string | null,
65 gitStatus: GitStatusResult | null,
66): ThreadPr | null {
67 if (threadBranch === null || gitStatus === null || gitStatus.branch !== threadBranch) {
68 return null;
69 }
70
71 return gitStatus.pr ?? null;
72}
73
74export function terminalStatusFromRunningIds(
75 runningTerminalIds: string[],
76): TerminalStatusIndicator | null {
77 if (runningTerminalIds.length === 0) {
78 return null;
79 }
80 return {
81 label: "Terminal process running",
82 colorClass: "text-teal-600 dark:text-teal-300/90",
83 pulse: true,
84 };
85}
86
87export function ThreadStatusLabel({
88 status,
89 compact = false,
90}: {
91 status: ThreadStatusPill;
92 compact?: boolean;
93}) {
94 if (compact) {
95 return (
96 <span
97 title={status.label}
98 className={`inline-flex size-3.5 shrink-0 items-center justify-center ${status.colorClass}`}
99 >
100 <span
101 className={`size-[9px] rounded-full ${status.dotClass} ${
102 status.pulse ? "animate-pulse" : ""
103 }`}
104 />
105 <span className="sr-only">{status.label}</span>
106 </span>
107 );
108 }
109
110 return (
111 <span
112 title={status.label}
113 className={`inline-flex items-center gap-1 text-[10px] ${status.colorClass}`}
114 >
115 <span
116 className={`h-1.5 w-1.5 rounded-full ${status.dotClass} ${
117 status.pulse ? "animate-pulse" : ""
118 }`}
119 />
120 <span className="hidden md:inline">{status.label}</span>
121 </span>
122 );
123}
124
125/**
126 * Non-interactive leading status icons for a thread row in compact contexts
127 * like the command palette. Shows the PR state icon (if present) and the
128 * thread status dot, matching the sidebar's leading indicators.
129 */
130export function ThreadRowLeadingStatus({ thread }: { thread: SidebarThreadSummary }) {
131 const threadRef = scopeThreadRef(thread.environmentId, thread.id);
132 const lastVisitedAt = useUiStateStore(
133 (state) => state.threadLastVisitedAtById[scopedThreadKey(threadRef)],
134 );
135 const threadProjectCwd = useStore(
136 useMemo(
137 () => (state: AppState) =>
138 selectProjectByRef(state, scopeProjectRef(thread.environmentId, thread.projectId))?.cwd ??
139 null,
140 [thread.environmentId, thread.projectId],
141 ),
142 );
143 const gitCwd = thread.worktreePath ?? threadProjectCwd;
144 const gitStatus = useGitStatus({
145 environmentId: thread.environmentId,
146 cwd: thread.branch != null ? gitCwd : null,
147 });
148 const pr = resolveThreadPr(thread.branch, gitStatus.data);
149 const prStatus = prStatusIndicator(pr);
150 const threadStatus = resolveThreadStatusPill({
151 thread: {
152 ...thread,
153 lastVisitedAt,
154 },
155 });
156
157 if (!prStatus && !threadStatus) {
158 return null;
159 }
160
161 return (
162 <span className="inline-flex shrink-0 items-center gap-1.5">
163 {prStatus ? (
164 <Tooltip>
165 <TooltipTrigger
166 render={
167 <span
168 aria-label={prStatus.tooltip}
169 className={`inline-flex items-center justify-center ${prStatus.colorClass}`}
170 />
171 }
172 >
173 <GitPullRequestIcon className="size-3" />
174 </TooltipTrigger>
175 <TooltipPopup side="top">{prStatus.tooltip}</TooltipPopup>
176 </Tooltip>
177 ) : null}
178 {threadStatus ? <ThreadStatusLabel status={threadStatus} /> : null}
179 </span>
180 );
181}
182
183/**
184 * Non-interactive trailing status icons for a thread row in compact contexts
185 * like the command palette. Shows a terminal-running indicator and a remote
186 * environment indicator, matching the sidebar's trailing indicators.
187 */
188export function ThreadRowTrailingStatus({ thread }: { thread: SidebarThreadSummary }) {
189 const threadRef = scopeThreadRef(thread.environmentId, thread.id);
190 const runningTerminalIds = useTerminalStateStore(
191 (state) =>
192 selectThreadTerminalState(state.terminalStateByThreadKey, threadRef).runningTerminalIds,
193 );
194 const primaryEnvironmentId = usePrimaryEnvironmentId();
195 const isRemoteThread =
196 primaryEnvironmentId !== null && thread.environmentId !== primaryEnvironmentId;
197 const remoteEnvLabel = useSavedEnvironmentRuntimeStore(
198 (state) => state.byId[thread.environmentId]?.descriptor?.label ?? null,
199 );
200 const remoteEnvSavedLabel = useSavedEnvironmentRegistryStore(
201 (state) => state.byId[thread.environmentId]?.label ?? null,
202 );
203 const threadEnvironmentLabel = isRemoteThread
204 ? (remoteEnvLabel ?? remoteEnvSavedLabel ?? "Remote")
205 : null;
206 const terminalStatus = terminalStatusFromRunningIds(runningTerminalIds);
207
208 if (!terminalStatus && !isRemoteThread) {
209 return null;
210 }
211
212 return (
213 <span className="inline-flex shrink-0 items-center gap-1.5">
214 {terminalStatus ? (
215 <span
216 role="img"
217 aria-label={terminalStatus.label}
218 title={terminalStatus.label}
219 className={`inline-flex items-center justify-center ${terminalStatus.colorClass}`}
220 >
221 <TerminalIcon className={`size-3 ${terminalStatus.pulse ? "animate-pulse" : ""}`} />
222 </span>
223 ) : null}
224 {isRemoteThread ? (
225 <Tooltip>
226 <TooltipTrigger
227 render={
228 <span
229 aria-label={threadEnvironmentLabel ?? "Remote"}
230 className="inline-flex items-center justify-center"
231 />
232 }
233 >
234 <CloudIcon className="size-3 text-muted-foreground/60" />
235 </TooltipTrigger>
236 <TooltipPopup side="top">{threadEnvironmentLabel}</TooltipPopup>
237 </Tooltip>
238 ) : null}
239 </span>
240 );
241}