Open History to see the last commit
1import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
2import type {
3 CommandId,
4 EnvironmentId,
5 ProjectMutation,
6 ProjectId,
7 SourceControlDiscoveryResult,
8 SourceControlProviderKind,
9 SourceControlRepositoryInfo,
10} from "@t3tools/contracts";
11import * as Arr from "effect/Array";
12import * as Option from "effect/Option";
13import * as Order from "effect/Order";
14
15import {
16 ensureBrowseDirectoryPath,
17 findProjectByPath,
18 inferProjectTitleFromPath,
19 isExplicitRelativeProjectPath,
20 isUnsupportedWindowsProjectPath,
21 resolveProjectPathForDispatch,
22} from "../state/projects.ts";
23import type { EnvironmentProject } from "../state/models.ts";
24
25export type AddProjectRemoteProviderKind = Extract<
26 SourceControlProviderKind,
27 "github" | "gitlab" | "bitbucket" | "azure-devops"
28>;
29export type AddProjectRemoteSource = AddProjectRemoteProviderKind | "url";
30
31export function canCreateProjectInEnvironment(
32 connectionPhase: EnvironmentConnectionPhase | null | undefined,
33): boolean {
34 return connectionPhase === "connected";
35}
36
37export type AddProjectRemoteSourceReadiness = Record<
38 AddProjectRemoteSource,
39 { readonly ready: boolean; readonly hint: string | null }
40>;
41
42export type AddProjectCloneFlow =
43 | {
44 readonly step: "repository";
45 readonly environmentId: EnvironmentId;
46 readonly source: AddProjectRemoteSource;
47 }
48 | {
49 readonly step: "confirm";
50 readonly environmentId: EnvironmentId;
51 readonly source: AddProjectRemoteSource;
52 readonly repositoryInput: string;
53 readonly repository: SourceControlRepositoryInfo | null;
54 readonly remoteUrl: string;
55 };
56
57const ADD_PROJECT_REMOTE_SOURCES: ReadonlyArray<AddProjectRemoteSource> = [
58 "url",
59 "github",
60 "gitlab",
61 "bitbucket",
62 "azure-devops",
63];
64
65const ADD_PROJECT_REMOTE_PROVIDER_SOURCES: ReadonlyArray<AddProjectRemoteProviderKind> = [
66 "github",
67 "gitlab",
68 "bitbucket",
69 "azure-devops",
70];
71
72export function addProjectRemoteSourceLabel(source: AddProjectRemoteSource): string {
73 switch (source) {
74 case "github":
75 return "GitHub";
76 case "gitlab":
77 return "GitLab";
78 case "bitbucket":
79 return "Bitbucket";
80 case "azure-devops":
81 return "Azure DevOps";
82 case "url":
83 return "Git URL";
84 }
85}
86
87export function addProjectRemoteSourcePathHint(source: AddProjectRemoteSource): string {
88 switch (source) {
89 case "github":
90 return "owner/repo";
91 case "gitlab":
92 return "group/project";
93 case "bitbucket":
94 return "workspace/repository";
95 case "azure-devops":
96 return "project/repository";
97 case "url":
98 return "URL";
99 }
100}
101
102export function addProjectRemoteSourceProvider(
103 source: AddProjectRemoteSource,
104): AddProjectRemoteProviderKind | null {
105 return source === "url" ? null : source;
106}
107
108export function sortAddProjectProviderSources(
109 readinessBySource: AddProjectRemoteSourceReadiness,
110): ReadonlyArray<AddProjectRemoteProviderKind> {
111 return Arr.sort(
112 ADD_PROJECT_REMOTE_PROVIDER_SOURCES,
113 Order.mapInput(
114 Order.Struct({
115 ready: Order.flip(Order.Boolean),
116 label: Order.String,
117 }),
118 (source: AddProjectRemoteProviderKind) => ({
119 ready: readinessBySource[source].ready,
120 label: addProjectRemoteSourceLabel(source),
121 }),
122 ),
123 );
124}
125
126export function buildAddProjectRemoteSourceReadiness(
127 discovery: SourceControlDiscoveryResult | null,
128): AddProjectRemoteSourceReadiness {
129 const unavailable = {
130 ready: false,
131 hint: "Provider status unavailable. Open Source Control settings and rescan.",
132 } as const;
133 const readiness: AddProjectRemoteSourceReadiness = {
134 url: { ready: true, hint: null },
135 github: unavailable,
136 gitlab: unavailable,
137 bitbucket: unavailable,
138 "azure-devops": unavailable,
139 };
140
141 if (!discovery) {
142 return readiness;
143 }
144
145 const providerByKind = new Map(
146 discovery.sourceControlProviders.map((provider) => [provider.kind, provider]),
147 );
148 for (const source of ADD_PROJECT_REMOTE_SOURCES) {
149 const kind = addProjectRemoteSourceProvider(source);
150 if (!kind) continue;
151 const provider = providerByKind.get(kind);
152 if (!provider) {
153 readiness[source] = unavailable;
154 continue;
155 }
156 if (provider.status !== "available") {
157 readiness[source] = { ready: false, hint: provider.installHint };
158 continue;
159 }
160 if (provider.auth.status === "unauthenticated") {
161 readiness[source] = {
162 ready: false,
163 hint:
164 Option.getOrNull(provider.auth.detail) ??
165 `${provider.label} is not authenticated. Open Source Control settings for setup guidance.`,
166 };
167 continue;
168 }
169 readiness[source] = { ready: true, hint: null };
170 }
171 return readiness;
172}
173
174export function getAddProjectInitialQuery(baseDirectory: string | null | undefined): string {
175 const trimmed = baseDirectory?.trim() ?? "";
176 return trimmed.length === 0 ? "~/" : ensureBrowseDirectoryPath(trimmed);
177}
178
179export function resolveAddProjectPath(input: {
180 readonly rawPath: string;
181 readonly currentProjectCwd?: string | null;
182 readonly platform: string;
183}): { readonly ok: true; readonly path: string } | { readonly ok: false; readonly error: string } {
184 const rawPath = input.rawPath.trim();
185 if (rawPath.length === 0) {
186 return { ok: false, error: "Enter a project path." };
187 }
188 if (isUnsupportedWindowsProjectPath(rawPath, input.platform)) {
189 return { ok: false, error: "Windows-style paths are only supported on Windows environments." };
190 }
191 if (isExplicitRelativeProjectPath(rawPath) && !input.currentProjectCwd) {
192 return { ok: false, error: "Relative paths require an active project in this environment." };
193 }
194 const path = resolveProjectPathForDispatch(rawPath, input.currentProjectCwd);
195 return path.length === 0 ? { ok: false, error: "Enter a project path." } : { ok: true, path };
196}
197
198export function findExistingAddProject(input: {
199 readonly projects: ReadonlyArray<EnvironmentProject>;
200 readonly environmentId: EnvironmentId;
201 readonly path: string;
202}): EnvironmentProject | null {
203 return (
204 findProjectByPath(
205 input.projects.filter((project) => project.environmentId === input.environmentId),
206 input.path,
207 ) ?? null
208 );
209}
210
211export function buildProjectCreateCommand(input: {
212 readonly commandId: CommandId;
213 readonly projectId: ProjectId;
214 readonly workspaceRoot: string;
215}): Extract<ProjectMutation, { type: "project.create" }> {
216 return {
217 type: "project.create",
218 commandId: input.commandId,
219 projectId: input.projectId,
220 title: inferProjectTitleFromPath(input.workspaceRoot),
221 workspaceRoot: input.workspaceRoot,
222 createWorkspaceRootIfMissing: true,
223 defaultModelSelection: null,
224 };
225}