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

BranchToolbar.logic.ts

157 lines4.8 KB

1import type { EnvironmentId, VcsRef, ProjectId } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3export {
4 dedupeRemoteBranchesWithLocalMatches,
5 deriveLocalBranchNameFromRemoteRef,
6} from "@t3tools/shared/git";
7​
8export interface EnvironmentOption {
9 environmentId: EnvironmentId;
10 projectId: ProjectId;
11 label: string;
12 isPrimary: boolean;
14​
15export const EnvMode = Schema.Literals(["local", "worktree"]);
16export type EnvMode = typeof EnvMode.Type;
17​
18const GENERIC_LOCAL_ENVIRONMENT_LABELS = new Set(["local", "local environment"]);
19​
20function normalizeDisplayLabel(value: string | null | undefined): string | null {
21 const trimmed = value?.trim();
22 return trimmed && trimmed.length > 0 ? trimmed : null;
24​
25export function resolveEnvironmentOptionLabel(input: {
26 isPrimary: boolean;
27 environmentId: EnvironmentId;
28 runtimeLabel?: string | null;
29 savedLabel?: string | null;
30}): string {
31 const runtimeLabel = normalizeDisplayLabel(input.runtimeLabel);
32 const savedLabel = normalizeDisplayLabel(input.savedLabel);
33​
34 if (input.isPrimary) {
35 const preferredLocalLabel = [runtimeLabel, savedLabel].find((label) => {
36 if (!label) return false;
37 return !GENERIC_LOCAL_ENVIRONMENT_LABELS.has(label.toLowerCase());
38 });
39 return preferredLocalLabel ?? "This device";
40 }
41​
42 return runtimeLabel ?? savedLabel ?? input.environmentId;
44​
45export function resolveEnvModeLabel(mode: EnvMode): string {
46 return mode === "worktree" ? "New worktree" : "Current checkout";
48​
49export function resolveCurrentWorkspaceLabel(activeWorktreePath: string | null): string {
50 return activeWorktreePath ? "Current worktree" : resolveEnvModeLabel("local");
52​
53export function resolveLockedWorkspaceLabel(activeWorktreePath: string | null): string {
54 return activeWorktreePath ? "Worktree" : "Local checkout";
56​
57export function resolveWorkspaceDisplayName(path: string | null): string | null {
58 if (!path) return null;
59 const normalizedPath = path.replace(/[\\/]+$/, "");
60 if (normalizedPath.length === 0) return path;
61 return normalizedPath.split(/[\\/]/).at(-1) ?? normalizedPath;
63​
64export function resolveEffectiveEnvMode(input: {
65 activeWorktreePath: string | null;
66 hasServerThread: boolean;
67 draftThreadEnvMode: EnvMode | undefined;
68}): EnvMode {
69 const { activeWorktreePath, hasServerThread, draftThreadEnvMode } = input;
70 if (!hasServerThread) {
71 if (activeWorktreePath) {
72 return "local";
73 }
74 return draftThreadEnvMode === "worktree" ? "worktree" : "local";
75 }
76 return activeWorktreePath ? "worktree" : "local";
78​
79export function resolveDraftEnvModeAfterBranchChange(input: {
80 nextWorktreePath: string | null;
81 currentWorktreePath: string | null;
82 effectiveEnvMode: EnvMode;
83}): EnvMode {
84 const { nextWorktreePath, currentWorktreePath, effectiveEnvMode } = input;
85 if (nextWorktreePath) {
86 return "worktree";
87 }
88 if (effectiveEnvMode === "worktree" && !currentWorktreePath) {
89 return "worktree";
90 }
91 return "local";
93​
94export function resolveBranchToolbarValue(input: {
95 envMode: EnvMode;
96 activeWorktreePath: string | null;
97 activeThreadBranch: string | null;
98 currentGitBranch: string | null;
99}): string | null {
100 const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
101 if (envMode === "worktree" && !activeWorktreePath) {
102 return activeThreadBranch ?? currentGitBranch;
103 }
104 return currentGitBranch ?? activeThreadBranch;
106​
107export function resolveBranchSelectionTarget(input: {
108 activeProjectCwd: string;
109 activeWorktreePath: string | null;
110 refName: Pick<VcsRef, "isDefault" | "worktreePath">;
111}): {
112 checkoutCwd: string;
113 nextWorktreePath: string | null;
114 reuseExistingWorktree: boolean;
115} {
116 const { activeProjectCwd, activeWorktreePath, refName } = input;
117​
118 if (refName.worktreePath) {
119 return {
120 checkoutCwd: refName.worktreePath,
121 nextWorktreePath: refName.worktreePath === activeProjectCwd ? null : refName.worktreePath,
122 reuseExistingWorktree: true,
123 };
124 }
125​
126 const nextWorktreePath =
127 activeWorktreePath !== null && refName.isDefault ? null : activeWorktreePath;
128​
129 return {
130 checkoutCwd: nextWorktreePath ?? activeProjectCwd,
131 nextWorktreePath,
132 reuseExistingWorktree: false,
133 };
135​
136export function shouldIncludeBranchPickerItem(input: {
137 itemValue: string;
138 normalizedQuery: string;
139 createBranchItemValue: string | null;
140 checkoutPullRequestItemValue: string | null;
141}): boolean {
142 const { itemValue, normalizedQuery, createBranchItemValue, checkoutPullRequestItemValue } = input;
143​
144 if (normalizedQuery.length === 0) {
145 return true;
146 }
147​
148 if (createBranchItemValue && itemValue === createBranchItemValue) {
149 return true;
150 }
151​
152 if (checkoutPullRequestItemValue && itemValue === checkoutPullRequestItemValue) {
153 return true;
154 }
155​
156 return itemValue.toLowerCase().includes(normalizedQuery);