Open History to see the last commit
1import type { EnvironmentId, VcsRef, ProjectId } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3import { toSortableTimestamp } from "../lib/threadSort";
4export {
5 dedupeRemoteBranchesWithLocalMatches,
6 deriveLocalBranchNameFromRemoteRef,
7} from "@t3tools/shared/git";
8
9export interface EnvironmentOption {
10 environmentId: EnvironmentId;
11 projectId: ProjectId;
12 label: string;
13 isPrimary: boolean;
14}
15
16export const EnvMode = Schema.Literals(["local", "worktree"]);
17export type EnvMode = typeof EnvMode.Type;
18
19const GENERIC_LOCAL_ENVIRONMENT_LABELS = new Set(["local", "local environment"]);
20
21function normalizeDisplayLabel(value: string | null | undefined): string | null {
22 const trimmed = value?.trim();
23 return trimmed && trimmed.length > 0 ? trimmed : null;
24}
25
26export function resolveEnvironmentOptionLabel(input: {
27 isPrimary: boolean;
28 environmentId: EnvironmentId;
29 runtimeLabel?: string | null;
30 savedLabel?: string | null;
31}): string {
32 const runtimeLabel = normalizeDisplayLabel(input.runtimeLabel);
33 const savedLabel = normalizeDisplayLabel(input.savedLabel);
34
35 if (input.isPrimary) {
36 const preferredLocalLabel = [runtimeLabel, savedLabel].find((label) => {
37 if (!label) return false;
38 return !GENERIC_LOCAL_ENVIRONMENT_LABELS.has(label.toLowerCase());
39 });
40 return preferredLocalLabel ?? "This device";
41 }
42
43 return runtimeLabel ?? savedLabel ?? input.environmentId;
44}
45
46// A remote (non-primary) environment is always surfaced, even when it is the
47// only environment available: with a single connected machine there is nothing
48// to pick, but the user still needs to see where the project runs.
49export function shouldShowEnvironmentIndicator(input: {
50 activeEnvironment: Pick<EnvironmentOption, "isPrimary"> | null;
51 canPickEnvironment: boolean;
52}): boolean {
53 if (input.canPickEnvironment) return true;
54 return input.activeEnvironment !== null && !input.activeEnvironment.isPrimary;
55}
56
57export function resolveEnvModeLabel(mode: EnvMode): string {
58 return mode === "worktree" ? "New worktree" : "Current checkout";
59}
60
61export function resolveCurrentWorkspaceLabel(activeWorktreePath: string | null): string {
62 return activeWorktreePath ? "Current worktree" : resolveEnvModeLabel("local");
63}
64
65export function resolveLockedWorkspaceLabel(activeWorktreePath: string | null): string {
66 return activeWorktreePath ? "Worktree" : "Local checkout";
67}
68
69export interface PreviousWorktreeSeed {
70 branch: string | null;
71 worktreePath: string;
72}
73
74// The most recently touched worktree in the project that the composer isn't
75// already pointing at. Backs the "Previous worktree" entry in the workspace
76// selector so a follow-up thread can hop back into the worktree you just
77// worked in without hunting for its branch. Archived threads don't compete —
78// the rest of the UI hides them, so their worktrees shouldn't resurface here.
79export function resolvePreviousWorktreeSeed(input: {
80 threads: ReadonlyArray<{
81 branch: string | null;
82 worktreePath: string | null;
83 updatedAt: string;
84 archivedAt?: string | null;
85 }>;
86 currentWorktreePath: string | null;
87}): PreviousWorktreeSeed | null {
88 let latest: { branch: string | null; worktreePath: string; updatedAt: number } | null = null;
89 for (const thread of input.threads) {
90 if (
91 !thread.worktreePath ||
92 thread.worktreePath === input.currentWorktreePath ||
93 (thread.archivedAt ?? null) !== null
94 ) {
95 continue;
96 }
97 const updatedAt = toSortableTimestamp(thread.updatedAt);
98 if (updatedAt === null) {
99 continue;
100 }
101 if (latest === null || updatedAt > latest.updatedAt) {
102 latest = {
103 branch: thread.branch,
104 worktreePath: thread.worktreePath,
105 updatedAt,
106 };
107 }
108 }
109 return latest === null ? null : { branch: latest.branch, worktreePath: latest.worktreePath };
110}
111
112export function resolvePreviousWorktreeLabel(seed: PreviousWorktreeSeed): string {
113 return seed.branch ? `Previous worktree (${seed.branch})` : "Previous worktree";
114}
115
116export function resolveEffectiveEnvMode(input: {
117 activeWorktreePath: string | null;
118 hasServerThread: boolean;
119 draftThreadEnvMode: EnvMode | undefined;
120}): EnvMode {
121 const { activeWorktreePath, hasServerThread, draftThreadEnvMode } = input;
122 if (!hasServerThread) {
123 if (activeWorktreePath) {
124 return "local";
125 }
126 return draftThreadEnvMode === "worktree" ? "worktree" : "local";
127 }
128 return activeWorktreePath ? "worktree" : "local";
129}
130
131export function resolveDraftEnvModeAfterBranchChange(input: {
132 nextWorktreePath: string | null;
133 currentWorktreePath: string | null;
134 effectiveEnvMode: EnvMode;
135}): EnvMode {
136 const { nextWorktreePath, currentWorktreePath, effectiveEnvMode } = input;
137 if (nextWorktreePath) {
138 return "worktree";
139 }
140 if (effectiveEnvMode === "worktree" && !currentWorktreePath) {
141 return "worktree";
142 }
143 return "local";
144}
145
146export function resolveBranchToolbarValue(input: {
147 envMode: EnvMode;
148 activeWorktreePath: string | null;
149 activeThreadBranch: string | null;
150 currentGitBranch: string | null;
151}): string | null {
152 const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
153 if (envMode === "worktree" && !activeWorktreePath) {
154 return activeThreadBranch ?? currentGitBranch;
155 }
156 return currentGitBranch ?? activeThreadBranch;
157}
158
159export function resolveBranchTriggerLabel(input: {
160 activeWorktreePath: string | null;
161 effectiveEnvMode: EnvMode;
162 resolvedActiveBranch: string | null;
163 resolvedActiveBranchIsRemote: boolean | null;
164 startFromOrigin: boolean;
165}): string {
166 const {
167 activeWorktreePath,
168 effectiveEnvMode,
169 resolvedActiveBranch,
170 resolvedActiveBranchIsRemote,
171 startFromOrigin,
172 } = input;
173 if (!resolvedActiveBranch) {
174 return "Select ref";
175 }
176 if (effectiveEnvMode === "worktree" && !activeWorktreePath) {
177 const baseRef =
178 startFromOrigin && resolvedActiveBranchIsRemote === false
179 ? `origin/${resolvedActiveBranch}`
180 : resolvedActiveBranch;
181 return `From ${baseRef}`;
182 }
183 return resolvedActiveBranch;
184}
185
186export function resolveBranchToolbarPrBranch(input: {
187 activeThreadBranch: string | null;
188 resolvedActiveBranch: string | null;
189}): string | null {
190 return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null;
191}
192
193export function resolveLocalCheckoutBranchMismatch(input: {
194 effectiveEnvMode: EnvMode;
195 activeWorktreePath: string | null;
196 activeThreadBranch: string | null;
197 currentGitBranch: string | null;
198}): { threadBranch: string; currentBranch: string } | null {
199 const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
200 if (effectiveEnvMode !== "local" || activeWorktreePath !== null) {
201 return null;
202 }
203 if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) {
204 return null;
205 }
206 return { threadBranch: activeThreadBranch, currentBranch: currentGitBranch };
207}
208
209export function resolveBranchSelectionTarget(input: {
210 activeProjectCwd: string;
211 activeWorktreePath: string | null;
212 refName: Pick<VcsRef, "isDefault" | "worktreePath">;
213}): {
214 checkoutCwd: string;
215 nextWorktreePath: string | null;
216 reuseExistingWorktree: boolean;
217} {
218 const { activeProjectCwd, activeWorktreePath, refName } = input;
219
220 if (refName.worktreePath) {
221 return {
222 checkoutCwd: refName.worktreePath,
223 nextWorktreePath: refName.worktreePath === activeProjectCwd ? null : refName.worktreePath,
224 reuseExistingWorktree: true,
225 };
226 }
227
228 const nextWorktreePath =
229 activeWorktreePath !== null && refName.isDefault ? null : activeWorktreePath;
230
231 return {
232 checkoutCwd: nextWorktreePath ?? activeProjectCwd,
233 nextWorktreePath,
234 reuseExistingWorktree: false,
235 };
236}
237
238export function shouldIncludeBranchPickerItem(input: {
239 itemValue: string;
240 normalizedQuery: string;
241 createBranchItemValue: string | null;
242 checkoutPullRequestItemValue: string | null;
243}): boolean {
244 const { itemValue, normalizedQuery, createBranchItemValue, checkoutPullRequestItemValue } = input;
245
246 if (normalizedQuery.length === 0) {
247 return true;
248 }
249
250 if (createBranchItemValue && itemValue === createBranchItemValue) {
251 return true;
252 }
253
254 if (checkoutPullRequestItemValue && itemValue === checkoutPullRequestItemValue) {
255 return true;
256 }
257
258 return itemValue.toLowerCase().includes(normalizedQuery);
259}