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

BranchToolbar.logic.ts

150 lines4.5 KB

1import type { EnvironmentId, VcsRef, ProjectId } from "@t3tools/contracts";
2import { Schema } from "effect";
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 resolveEffectiveEnvMode(input: {
58 activeWorktreePath: string | null;
59 hasServerThread: boolean;
60 draftThreadEnvMode: EnvMode | undefined;
61}): EnvMode {
62 const { activeWorktreePath, hasServerThread, draftThreadEnvMode } = input;
63 if (!hasServerThread) {
64 if (activeWorktreePath) {
65 return "local";
66 }
67 return draftThreadEnvMode === "worktree" ? "worktree" : "local";
68 }
69 return activeWorktreePath ? "worktree" : "local";
71​
72export function resolveDraftEnvModeAfterBranchChange(input: {
73 nextWorktreePath: string | null;
74 currentWorktreePath: string | null;
75 effectiveEnvMode: EnvMode;
76}): EnvMode {
77 const { nextWorktreePath, currentWorktreePath, effectiveEnvMode } = input;
78 if (nextWorktreePath) {
79 return "worktree";
80 }
81 if (effectiveEnvMode === "worktree" && !currentWorktreePath) {
82 return "worktree";
83 }
84 return "local";
86​
87export function resolveBranchToolbarValue(input: {
88 envMode: EnvMode;
89 activeWorktreePath: string | null;
90 activeThreadBranch: string | null;
91 currentGitBranch: string | null;
92}): string | null {
93 const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
94 if (envMode === "worktree" && !activeWorktreePath) {
95 return activeThreadBranch ?? currentGitBranch;
96 }
97 return currentGitBranch ?? activeThreadBranch;
99​
100export function resolveBranchSelectionTarget(input: {
101 activeProjectCwd: string;
102 activeWorktreePath: string | null;
103 refName: Pick<VcsRef, "isDefault" | "worktreePath">;
104}): {
105 checkoutCwd: string;
106 nextWorktreePath: string | null;
107 reuseExistingWorktree: boolean;
108} {
109 const { activeProjectCwd, activeWorktreePath, refName } = input;
110​
111 if (refName.worktreePath) {
112 return {
113 checkoutCwd: refName.worktreePath,
114 nextWorktreePath: refName.worktreePath === activeProjectCwd ? null : refName.worktreePath,
115 reuseExistingWorktree: true,
116 };
117 }
118​
119 const nextWorktreePath =
120 activeWorktreePath !== null && refName.isDefault ? null : activeWorktreePath;
121​
122 return {
123 checkoutCwd: nextWorktreePath ?? activeProjectCwd,
124 nextWorktreePath,
125 reuseExistingWorktree: false,
126 };
128​
129export function shouldIncludeBranchPickerItem(input: {
130 itemValue: string;
131 normalizedQuery: string;
132 createBranchItemValue: string | null;
133 checkoutPullRequestItemValue: string | null;
134}): boolean {
135 const { itemValue, normalizedQuery, createBranchItemValue, checkoutPullRequestItemValue } = input;
136​
137 if (normalizedQuery.length === 0) {
138 return true;
139 }
140​
141 if (createBranchItemValue && itemValue === createBranchItemValue) {
142 return true;
143 }
144​
145 if (checkoutPullRequestItemValue && itemValue === checkoutPullRequestItemValue) {
146 return true;
147 }
148​
149 return itemValue.toLowerCase().includes(normalizedQuery);