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

BranchToolbar.logic.ts

146 lines4.3 KB

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