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

new-task-context-presentation.ts

217 lines7.5 KB

1import { sanitizeNewRefName } from "@t3tools/shared/git";
2import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
3import { changeRequestTermKind } from "../../lib/changeRequestTerm";
4import { resolveSendEnvMode } from "@t3tools/shared/projectSettings";
5import { msg, select, part } from "gt-react-native";
6​
7import { m, type MessageResolver } from "../../i18n/messages";
8import type { ComposerDraftWorkspaceSelection } from "../../state/use-composer-drafts";
9​
10type WorkspaceMode = "local" | "worktree";
11​
12export function resolveNewTaskWorkspaceSelection(input: {
13 readonly selection: ComposerDraftWorkspaceSelection | undefined;
14 readonly fallback: ComposerDraftWorkspaceSelection;
15 readonly isGitRepo: boolean;
16 /** A project folder that is no repository can still take a worktree of one it tracks. */
17 readonly tracksGitRepositories?: boolean | undefined;
18}): ComposerDraftWorkspaceSelection {
19 const selection = input.selection ?? input.fallback;
20 if (input.isGitRepo) return selection;
21 const mode = resolveSendEnvMode({
22 requestedEnvMode: selection.mode,
23 isGitRepo: false,
24 tracksGitRepositories: input.tracksGitRepositories === true,
25 });
26 return {
27 mode,
28 branch: null,
29 worktreePath: null,
30 ...(selection.repositories !== undefined ? { repositories: selection.repositories } : {}),
31 };
33​
34export function resolveNewTaskWorkspaceLabel(
35 input: {
36 readonly workspaceMode: WorkspaceMode;
37 readonly worktreePath: string | null;
38 },
39 resolve: MessageResolver = m,
40): string {
41 if (input.workspaceMode === "worktree") {
42 return resolve(msg("New worktree"));
43 }
44 return input.worktreePath ? resolve(msg("Current worktree")) : resolve(msg("Current checkout"));
46​
47export function resolveNewTaskBranchWorktreePath(input: {
48 readonly workspaceMode: WorkspaceMode;
49 readonly projectCwd: string;
50 readonly branchWorktreePath: string | null | undefined;
51}): string | null {
52 if (
53 input.workspaceMode === "worktree" ||
54 !input.branchWorktreePath ||
55 input.branchWorktreePath === input.projectCwd
56 ) {
57 return null;
58 }
59 return input.branchWorktreePath;
61​
62export function resolveNewTaskLocalWorkspaceSelection(input: {
63 readonly branches: ReadonlyArray<{
64 readonly name: string;
65 readonly current: boolean;
66 readonly worktreePath?: string | null;
67 }>;
68 readonly projectCwd: string;
69}): {
70 readonly branch: string | null;
71 readonly worktreePath: string | null;
72 readonly awaitsCurrentBranch: boolean;
73} {
74 const currentBranch = input.branches.find((branch) => branch.current) ?? null;
75 if (!currentBranch) {
76 return {
77 branch: null,
78 worktreePath: null,
79 awaitsCurrentBranch: true,
80 };
81 }
82​
83 return {
84 branch: currentBranch.name,
85 worktreePath: resolveNewTaskBranchWorktreePath({
86 workspaceMode: "local",
87 projectCwd: input.projectCwd,
88 branchWorktreePath: currentBranch.worktreePath,
89 }),
90 awaitsCurrentBranch: false,
91 };
93​
94export function resolveNewTaskBranchLabel(
95 input: {
96 readonly branchName: string | null;
97 readonly startFromOrigin: boolean;
98 readonly workspaceMode: WorkspaceMode;
99 readonly pullRequestReference?: string;
100 },
101 resolve: MessageResolver = m,
102): string {
103 if (input.workspaceMode === "worktree" && input.pullRequestReference) {
104 const parsed = sourceControlClients.parseChangeRequestUrl(input.pullRequestReference);
105 if (parsed)
106 return resolve(
107 msg`${select(
108 { term: changeRequestTermKind(parsed.definition.changeRequest) },
109 {
110 mr: part`From MR #${{ number: parsed.link.number }}`,
111 cr: part`From change request #${{ number: parsed.link.number }}`,
112 other: part`From PR #${{ number: parsed.link.number }}`,
113 },
114 )}`,
115 );
116 }
117 if (!input.branchName) {
118 return resolve(msg("Choose branch"));
119 }
120​
121 if (input.workspaceMode === "local") {
122 return input.branchName;
123 }
124​
125 // Only the server can determine a local branch's configured remote.
126 return input.startFromOrigin
127 ? resolve(msg`From ${{ branchName: input.branchName }} (remote)`)
128 : resolve(msg`From ${{ branchName: input.branchName }}`);
130​
131export function shouldCheckoutNewTaskBranch(input: {
132 readonly branchIsCurrent: boolean;
133 readonly branchWorktreePath: string | null | undefined;
134 readonly workspaceMode: WorkspaceMode;
135}): boolean {
136 return input.workspaceMode === "local" && !input.branchIsCurrent && !input.branchWorktreePath;
138​
139export function filterNewTaskBranches<T extends { readonly name: string }>(
140 branches: ReadonlyArray<T>,
141 rawQuery: string,
142): ReadonlyArray<T> {
143 const query = sanitizeNewRefName(rawQuery).toLowerCase();
144 return query.length === 0
145 ? branches
146 : branches.filter((branch) => branch.name.toLowerCase().includes(query));
148​
149/**
150 * A worktree with no chosen base starts from the default branch, and a local
151 * selection the user just switched to follows the current checkout until refs
152 * load. Both are derived, never written back, so a late ref list or t3.json
153 * default still wins.
154 */
155export function fillNewTaskWorkspaceBranch(input: {
156 readonly selection: ComposerDraftWorkspaceSelection;
157 readonly branches: ReadonlyArray<{
158 readonly name: string;
159 readonly current: boolean;
160 readonly isDefault: boolean;
161 readonly isRemote?: boolean;
162 readonly worktreePath?: string | null;
163 }>;
164 readonly projectCwd: string | null;
165 readonly worktreeDefaultSettled: boolean;
166}): ComposerDraftWorkspaceSelection {
167 const { selection } = input;
168 if (selection.branch !== null) return selection;
169 if (selection.mode === "worktree") {
170 // The default may only exist as origin/<default>, so search remote refs too.
171 const base =
172 input.branches.find((branch) => branch.isDefault) ??
173 input.branches.find((branch) => !branch.isRemote && branch.current) ??
174 null;
175 return input.worktreeDefaultSettled && base
176 ? { ...selection, branch: base.name, worktreePath: null }
177 : selection;
178 }
179 if (!selection.followsCurrentCheckout || input.projectCwd === null) return selection;
180 const local = resolveNewTaskLocalWorkspaceSelection({
181 branches: input.branches.filter((branch) => !branch.isRemote),
182 projectCwd: input.projectCwd,
183 });
184 return local.awaitsCurrentBranch
185 ? selection
186 : { ...selection, branch: local.branch, worktreePath: local.worktreePath };
188​
189/**
190 * The workspace a new-task draft starts in: its own selection, else the
191 * project's default mode, with the branch filled in as above. The new-task
192 * flow, its send path and the home pending-drafts list all resolve through
193 * this, so what each shows is what is sent.
194 */
195export function resolveNewTaskDraftWorkspace(input: {
196 readonly selection: ComposerDraftWorkspaceSelection | undefined;
197 readonly canChooseWorkspace: boolean;
198 readonly defaultMode: WorkspaceMode;
199 readonly defaultModeSettled: boolean;
200 readonly isGitRepo: boolean;
201 readonly tracksGitRepositories?: boolean | undefined;
202 readonly branches: Parameters<typeof fillNewTaskWorkspaceBranch>[0]["branches"];
203 readonly projectCwd: string | null;
204}): ComposerDraftWorkspaceSelection {
205 const selection = input.canChooseWorkspace ? input.selection : undefined;
206 return fillNewTaskWorkspaceBranch({
207 selection: resolveNewTaskWorkspaceSelection({
208 selection,
209 fallback: { mode: input.defaultMode, branch: null, worktreePath: null },
210 isGitRepo: input.isGitRepo,
211 tracksGitRepositories: input.tracksGitRepositories,
212 }),
213 branches: input.branches,
214 projectCwd: input.projectCwd,
215 worktreeDefaultSettled: selection?.mode !== undefined || input.defaultModeSettled,
216 });