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

BranchToolbar.logic.ts

107 lines3.1 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​
18export function resolveEffectiveEnvMode(input: {
19 activeWorktreePath: string | null;
20 hasServerThread: boolean;
21 draftThreadEnvMode: EnvMode | undefined;
22}): EnvMode {
23 const { activeWorktreePath, hasServerThread, draftThreadEnvMode } = input;
24 return activeWorktreePath || (!hasServerThread && draftThreadEnvMode === "worktree")
25 ? "worktree"
26 : "local";
28​
29export function resolveDraftEnvModeAfterBranchChange(input: {
30 nextWorktreePath: string | null;
31 currentWorktreePath: string | null;
32 effectiveEnvMode: EnvMode;
33}): EnvMode {
34 const { nextWorktreePath, currentWorktreePath, effectiveEnvMode } = input;
35 if (nextWorktreePath) {
36 return "worktree";
37 }
38 if (effectiveEnvMode === "worktree" && !currentWorktreePath) {
39 return "worktree";
40 }
41 return "local";
43​
44export function resolveBranchToolbarValue(input: {
45 envMode: EnvMode;
46 activeWorktreePath: string | null;
47 activeThreadBranch: string | null;
48 currentGitBranch: string | null;
49}): string | null {
50 const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
51 if (envMode === "worktree" && !activeWorktreePath) {
52 return activeThreadBranch ?? currentGitBranch;
53 }
54 return currentGitBranch ?? activeThreadBranch;
56​
57export function resolveBranchSelectionTarget(input: {
58 activeProjectCwd: string;
59 activeWorktreePath: string | null;
60 branch: Pick<GitBranch, "isDefault" | "worktreePath">;
61}): {
62 checkoutCwd: string;
63 nextWorktreePath: string | null;
64 reuseExistingWorktree: boolean;
65} {
66 const { activeProjectCwd, activeWorktreePath, branch } = input;
67​
68 if (branch.worktreePath) {
69 return {
70 checkoutCwd: branch.worktreePath,
71 nextWorktreePath: branch.worktreePath === activeProjectCwd ? null : branch.worktreePath,
72 reuseExistingWorktree: true,
73 };
74 }
75​
76 const nextWorktreePath =
77 activeWorktreePath !== null && branch.isDefault ? null : activeWorktreePath;
78​
79 return {
80 checkoutCwd: nextWorktreePath ?? activeProjectCwd,
81 nextWorktreePath,
82 reuseExistingWorktree: false,
83 };
85​
86export function shouldIncludeBranchPickerItem(input: {
87 itemValue: string;
88 normalizedQuery: string;
89 createBranchItemValue: string | null;
90 checkoutPullRequestItemValue: string | null;
91}): boolean {
92 const { itemValue, normalizedQuery, createBranchItemValue, checkoutPullRequestItemValue } = input;
93​
94 if (normalizedQuery.length === 0) {
95 return true;
96 }
97​
98 if (createBranchItemValue && itemValue === createBranchItemValue) {
99 return true;
100 }
101​
102 if (checkoutPullRequestItemValue && itemValue === checkoutPullRequestItemValue) {
103 return true;
104 }
105​
106 return itemValue.toLowerCase().includes(normalizedQuery);