The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { scopedProjectKey, scopeProjectRef } from "../environment/scoped.ts"; |
| 2 | import { |
| 3 | repositoryGroupingDisplayNameOf, |
| 4 | repositoryGroupingKeyOf, |
| 5 | type EnvironmentId, |
| 6 | type ScopedProjectRef, |
| 7 | type SidebarProjectGroupingMode, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import type { ClientSettings } from "@t3tools/contracts/settings"; |
| 10 | |
| 11 | import type { EnvironmentProject } from "./models.ts"; |
| 12 | import { normalizeProjectPathForComparison } from "./projects.ts"; |
| 13 | import { mFallback, msg } from "gt-i18n"; |
| 14 | import type { MFunctionType } from "gt-i18n/types"; |
| 15 | |
| 16 | const SCRATCH_PROJECT_SCOPE_KEY = "\0no-project"; |
| 17 | |
| 18 | export interface ProjectGroupingSettings { |
| 19 | readonly sidebarProjectGroupingMode: SidebarProjectGroupingMode; |
| 20 | readonly sidebarProjectGroupingOverrides: Record<string, SidebarProjectGroupingMode>; |
| 21 | } |
| 22 | |
| 23 | export type ProjectGroupingMode = SidebarProjectGroupingMode; |
| 24 | |
| 25 | export function selectProjectGroupingSettings(settings: ClientSettings): ProjectGroupingSettings { |
| 26 | return { |
| 27 | sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode, |
| 28 | sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | function uniqueNonEmptyValues(values: ReadonlyArray<string | null | undefined>): string[] { |
| 33 | const seen = new Set<string>(); |
| 34 | const unique: string[] = []; |
| 35 | for (const value of values) { |
| 36 | const trimmed = value?.trim(); |
| 37 | if (!trimmed || seen.has(trimmed)) { |
| 38 | continue; |
| 39 | } |
| 40 | seen.add(trimmed); |
| 41 | unique.push(trimmed); |
| 42 | } |
| 43 | return unique; |
| 44 | } |
| 45 | |
| 46 | function deriveRepositoryRelativeProjectPath( |
| 47 | project: Pick<EnvironmentProject, "workspaceRoot" | "repositoryIdentity">, |
| 48 | ): string | null { |
| 49 | const rootPath = project.repositoryIdentity?.rootPath?.trim(); |
| 50 | if (!rootPath) { |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | const normalizedProjectPath = normalizeProjectPathForComparison(project.workspaceRoot); |
| 55 | const normalizedRootPath = normalizeProjectPathForComparison(rootPath); |
| 56 | if (normalizedProjectPath.length === 0 || normalizedRootPath.length === 0) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | if (normalizedProjectPath === normalizedRootPath) { |
| 61 | return ""; |
| 62 | } |
| 63 | |
| 64 | const separator = normalizedRootPath.includes("\\") ? "\\" : "/"; |
| 65 | const rootPrefix = `${normalizedRootPath}${separator}`; |
| 66 | if (!normalizedProjectPath.startsWith(rootPrefix)) { |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/"); |
| 71 | } |
| 72 | |
| 73 | export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string { |
| 74 | return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`; |
| 75 | } |
| 76 | |
| 77 | export function derivePhysicalProjectKey( |
| 78 | project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">, |
| 79 | ): string { |
| 80 | return derivePhysicalProjectKeyFromPath(project.environmentId, project.workspaceRoot); |
| 81 | } |
| 82 | |
| 83 | function deriveProjectGroupingOverrideKey( |
| 84 | project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">, |
| 85 | ): string { |
| 86 | return derivePhysicalProjectKey(project); |
| 87 | } |
| 88 | |
| 89 | export function getProjectOrderKey( |
| 90 | project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">, |
| 91 | ): string { |
| 92 | return derivePhysicalProjectKey(project); |
| 93 | } |
| 94 | |
| 95 | function resolveProjectGroupingMode( |
| 96 | project: Pick<EnvironmentProject, "environmentId" | "workspaceRoot">, |
| 97 | settings: ProjectGroupingSettings, |
| 98 | ): SidebarProjectGroupingMode { |
| 99 | return ( |
| 100 | settings.sidebarProjectGroupingOverrides?.[deriveProjectGroupingOverrideKey(project)] ?? |
| 101 | settings.sidebarProjectGroupingMode |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | function deriveRepositoryScopedKey( |
| 106 | project: Pick<EnvironmentProject, "workspaceRoot" | "repositoryIdentity">, |
| 107 | groupingMode: SidebarProjectGroupingMode, |
| 108 | ): string | null { |
| 109 | const canonicalKey = project.repositoryIdentity |
| 110 | ? repositoryGroupingKeyOf(project.repositoryIdentity) |
| 111 | : null; |
| 112 | // A NUL reserves the scratch key namespace outside valid repository identities. |
| 113 | if (!canonicalKey || canonicalKey.includes("\0")) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | if (groupingMode === "repository") { |
| 118 | return canonicalKey; |
| 119 | } |
| 120 | |
| 121 | const relativeProjectPath = deriveRepositoryRelativeProjectPath(project); |
| 122 | if (relativeProjectPath === null) { |
| 123 | return canonicalKey; |
| 124 | } |
| 125 | |
| 126 | return relativeProjectPath.length === 0 |
| 127 | ? canonicalKey |
| 128 | : `${canonicalKey}::${relativeProjectPath}`; |
| 129 | } |
| 130 | |
| 131 | function deriveLogicalProjectKey( |
| 132 | project: Pick< |
| 133 | EnvironmentProject, |
| 134 | "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity" | "isScratch" |
| 135 | >, |
| 136 | options?: { |
| 137 | readonly groupingMode?: SidebarProjectGroupingMode; |
| 138 | readonly groupScratchProjects?: boolean; |
| 139 | }, |
| 140 | ): string { |
| 141 | if (options?.groupScratchProjects && project.isScratch) return SCRATCH_PROJECT_SCOPE_KEY; |
| 142 | const groupingMode = options?.groupingMode ?? "repository"; |
| 143 | if (groupingMode === "separate") { |
| 144 | return derivePhysicalProjectKey(project); |
| 145 | } |
| 146 | |
| 147 | return ( |
| 148 | deriveRepositoryScopedKey(project, groupingMode) ?? |
| 149 | derivePhysicalProjectKey(project) ?? |
| 150 | scopedProjectKey(scopeProjectRef(project.environmentId, project.id)) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | export function deriveLogicalProjectKeyFromSettings( |
| 155 | project: Pick< |
| 156 | EnvironmentProject, |
| 157 | "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity" | "isScratch" |
| 158 | >, |
| 159 | settings: ProjectGroupingSettings, |
| 160 | options?: { readonly groupScratchProjects?: boolean }, |
| 161 | ): string { |
| 162 | return deriveLogicalProjectKey(project, { |
| 163 | groupingMode: resolveProjectGroupingMode(project, settings), |
| 164 | groupScratchProjects: options?.groupScratchProjects ?? false, |
| 165 | }); |
| 166 | } |
| 167 | |
| 168 | export function resolveScratchProjectScopeKey( |
| 169 | key: string | null, |
| 170 | projects: ReadonlyArray< |
| 171 | Pick< |
| 172 | EnvironmentProject, |
| 173 | "environmentId" | "id" | "workspaceRoot" | "repositoryIdentity" | "isScratch" |
| 174 | > |
| 175 | >, |
| 176 | settings: ProjectGroupingSettings = { |
| 177 | sidebarProjectGroupingMode: "repository", |
| 178 | sidebarProjectGroupingOverrides: {}, |
| 179 | }, |
| 180 | ): string | null { |
| 181 | if (key === null || key === SCRATCH_PROJECT_SCOPE_KEY) return key; |
| 182 | if ( |
| 183 | projects.some( |
| 184 | (project) => |
| 185 | deriveLogicalProjectKeyFromSettings(project, settings, { groupScratchProjects: true }) === |
| 186 | key, |
| 187 | ) |
| 188 | ) { |
| 189 | return key; |
| 190 | } |
| 191 | return projects.some((project) => { |
| 192 | if (!project.isScratch) return false; |
| 193 | const mode = resolveProjectGroupingMode(project, settings); |
| 194 | return ( |
| 195 | derivePhysicalProjectKey(project) === key || |
| 196 | (mode !== "separate" && deriveRepositoryScopedKey(project, mode) === key) |
| 197 | ); |
| 198 | }) |
| 199 | ? SCRATCH_PROJECT_SCOPE_KEY |
| 200 | : key; |
| 201 | } |
| 202 | |
| 203 | export function deriveProjectGroupLabel(input: { |
| 204 | readonly groupKey: string; |
| 205 | readonly representative: Pick<EnvironmentProject, "title" | "repositoryIdentity">; |
| 206 | readonly members: ReadonlyArray< |
| 207 | Pick<EnvironmentProject, "title" | "hasCustomTitle" | "repositoryIdentity"> |
| 208 | >; |
| 209 | }): string { |
| 210 | const sharedTitles = uniqueNonEmptyValues(input.members.map((member) => member.title)); |
| 211 | const sharedDisplayNames = uniqueNonEmptyValues( |
| 212 | input.members.map((member) => |
| 213 | member.repositoryIdentity ? repositoryGroupingDisplayNameOf(member.repositoryIdentity) : null, |
| 214 | ), |
| 215 | ); |
| 216 | const sharedRepositoryNames = uniqueNonEmptyValues( |
| 217 | input.members.map((member) => member.repositoryIdentity?.name), |
| 218 | ); |
| 219 | const sharedTitle = sharedTitles[0]; |
| 220 | if ( |
| 221 | sharedTitles.length === 1 && |
| 222 | sharedTitle !== undefined && |
| 223 | (input.members.some((member) => member.hasCustomTitle === true) || |
| 224 | (!sharedDisplayNames.includes(sharedTitle) && !sharedRepositoryNames.includes(sharedTitle))) |
| 225 | ) { |
| 226 | return sharedTitle; |
| 227 | } |
| 228 | if (sharedDisplayNames.length === 1) { |
| 229 | return sharedDisplayNames[0]!; |
| 230 | } |
| 231 | |
| 232 | if (sharedRepositoryNames.length === 1) { |
| 233 | return sharedRepositoryNames[0]!; |
| 234 | } |
| 235 | |
| 236 | // A whole-repository group is named for the repository, not one member's folder: |
| 237 | // clones can disagree on its casing. A subfolder group keeps its own title. |
| 238 | const representativeIdentity = input.representative.repositoryIdentity; |
| 239 | if ( |
| 240 | representativeIdentity && |
| 241 | input.groupKey === repositoryGroupingKeyOf(representativeIdentity) |
| 242 | ) { |
| 243 | return ( |
| 244 | repositoryGroupingDisplayNameOf(representativeIdentity) ?? |
| 245 | representativeIdentity.name ?? |
| 246 | input.representative.title |
| 247 | ); |
| 248 | } |
| 249 | return input.representative.title; |
| 250 | } |
| 251 | |
| 252 | export interface ProjectGroupMember<TProject extends EnvironmentProject = EnvironmentProject> { |
| 253 | readonly physicalProjectKey: string; |
| 254 | readonly project: TProject; |
| 255 | } |
| 256 | |
| 257 | export interface ProjectGroup<TProject extends EnvironmentProject = EnvironmentProject> { |
| 258 | readonly key: string; |
| 259 | readonly label: string; |
| 260 | readonly representative: TProject; |
| 261 | readonly members: ReadonlyArray<ProjectGroupMember<TProject>>; |
| 262 | readonly memberProjectRefs: ReadonlyArray<ScopedProjectRef>; |
| 263 | /** Clients write every member together. When members still disagree, the project stays visible. */ |
| 264 | readonly pinned: boolean; |
| 265 | readonly archived: boolean; |
| 266 | } |
| 267 | |
| 268 | /** The groups a project picker offers: archived groups left out, pinned ones first. */ |
| 269 | export function pickableProjectGroups< |
| 270 | TGroup extends { readonly pinned: boolean; readonly archived: boolean }, |
| 271 | >(groups: ReadonlyArray<TGroup>): TGroup[] { |
| 272 | const open = groups.filter((group) => !group.archived); |
| 273 | return [...open.filter((group) => group.pinned), ...open.filter((group) => !group.pinned)]; |
| 274 | } |
| 275 | |
| 276 | export function isPickableProjectMember(member: Pick<EnvironmentProject, "archivedAt">): boolean { |
| 277 | return member.archivedAt == null; |
| 278 | } |
| 279 | |
| 280 | function projectFreshnessTime(project: EnvironmentProject): number { |
| 281 | const updatedAtTime = Date.parse(project.updatedAt); |
| 282 | if (Number.isFinite(updatedAtTime)) { |
| 283 | return updatedAtTime; |
| 284 | } |
| 285 | const createdAtTime = Date.parse(project.createdAt); |
| 286 | return Number.isFinite(createdAtTime) ? createdAtTime : 0; |
| 287 | } |
| 288 | |
| 289 | function shouldReplacePhysicalProjectWinner<TProject extends EnvironmentProject>( |
| 290 | existing: TProject, |
| 291 | candidate: TProject, |
| 292 | ): boolean { |
| 293 | const freshnessDelta = projectFreshnessTime(candidate) - projectFreshnessTime(existing); |
| 294 | return freshnessDelta > 0 || (freshnessDelta === 0 && candidate.id > existing.id); |
| 295 | } |
| 296 | |
| 297 | function selectProjectIdentitySource<TProject extends EnvironmentProject>( |
| 298 | projects: ReadonlyArray<TProject>, |
| 299 | winner: TProject, |
| 300 | ): TProject { |
| 301 | if (winner.repositoryIdentity !== null) { |
| 302 | return winner; |
| 303 | } |
| 304 | |
| 305 | let freshestIdentifiedProject: TProject | null = null; |
| 306 | for (const project of projects) { |
| 307 | if (project.repositoryIdentity === null) { |
| 308 | continue; |
| 309 | } |
| 310 | if ( |
| 311 | freshestIdentifiedProject === null || |
| 312 | shouldReplacePhysicalProjectWinner(freshestIdentifiedProject, project) |
| 313 | ) { |
| 314 | freshestIdentifiedProject = project; |
| 315 | } |
| 316 | } |
| 317 | return freshestIdentifiedProject ?? winner; |
| 318 | } |
| 319 | |
| 320 | export function buildProjectGroups<TProject extends EnvironmentProject>( |
| 321 | input: { |
| 322 | readonly projects: ReadonlyArray<TProject>; |
| 323 | readonly settings: ProjectGroupingSettings; |
| 324 | readonly preferredEnvironmentId?: EnvironmentId | null; |
| 325 | readonly groupScratchProjects?: boolean; |
| 326 | }, |
| 327 | resolve: MFunctionType = mFallback, |
| 328 | ): ReadonlyArray<ProjectGroup<TProject>> { |
| 329 | const projectsByPhysicalKey = new Map<string, TProject[]>(); |
| 330 | for (const project of input.projects) { |
| 331 | const physicalProjectKey = derivePhysicalProjectKey(project); |
| 332 | const existing = projectsByPhysicalKey.get(physicalProjectKey); |
| 333 | if (existing) { |
| 334 | existing.push(project); |
| 335 | } else { |
| 336 | projectsByPhysicalKey.set(physicalProjectKey, [project]); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | const logicalKeyByPhysicalKey = new Map<string, string>(); |
| 341 | const groupedMembers = new Map<string, ProjectGroupMember<TProject>[]>(); |
| 342 | for (const [physicalProjectKey, physicalProjects] of projectsByPhysicalKey) { |
| 343 | const winner = physicalProjects.reduce((current, candidate) => |
| 344 | shouldReplacePhysicalProjectWinner(current, candidate) ? candidate : current, |
| 345 | ); |
| 346 | const identitySource = selectProjectIdentitySource(physicalProjects, winner); |
| 347 | const logicalKey = deriveLogicalProjectKey(identitySource, { |
| 348 | groupingMode: resolveProjectGroupingMode(winner, input.settings), |
| 349 | groupScratchProjects: input.groupScratchProjects ?? false, |
| 350 | }); |
| 351 | logicalKeyByPhysicalKey.set(physicalProjectKey, logicalKey); |
| 352 | const member = { physicalProjectKey, project: winner }; |
| 353 | const existing = groupedMembers.get(logicalKey); |
| 354 | if (existing) { |
| 355 | existing.push(member); |
| 356 | } else { |
| 357 | groupedMembers.set(logicalKey, [member]); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | const projectRefsByLogicalKey = new Map<string, ScopedProjectRef[]>(); |
| 362 | const seenProjectRefs = new Set<string>(); |
| 363 | for (const project of input.projects) { |
| 364 | const physicalProjectKey = derivePhysicalProjectKey(project); |
| 365 | const logicalKey = |
| 366 | logicalKeyByPhysicalKey.get(physicalProjectKey) ?? |
| 367 | deriveLogicalProjectKeyFromSettings(project, input.settings, { |
| 368 | groupScratchProjects: input.groupScratchProjects ?? false, |
| 369 | }); |
| 370 | const projectRefKey = scopedProjectKey(scopeProjectRef(project.environmentId, project.id)); |
| 371 | if (seenProjectRefs.has(projectRefKey)) continue; |
| 372 | seenProjectRefs.add(projectRefKey); |
| 373 | const projectRef = scopeProjectRef(project.environmentId, project.id); |
| 374 | const existing = projectRefsByLogicalKey.get(logicalKey); |
| 375 | if (existing) { |
| 376 | existing.push(projectRef); |
| 377 | } else { |
| 378 | projectRefsByLogicalKey.set(logicalKey, [projectRef]); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | const preferredEnvironmentId = input.preferredEnvironmentId ?? null; |
| 383 | return Array.from(groupedMembers, ([key, members]) => { |
| 384 | const representative = |
| 385 | (preferredEnvironmentId |
| 386 | ? members.find((member) => member.project.environmentId === preferredEnvironmentId)?.project |
| 387 | : null) ?? members[0]!.project; |
| 388 | return { |
| 389 | key, |
| 390 | label: |
| 391 | input.groupScratchProjects && representative.isScratch |
| 392 | ? resolve(msg("No project")) |
| 393 | : members.length > 1 |
| 394 | ? deriveProjectGroupLabel({ |
| 395 | groupKey: key, |
| 396 | representative, |
| 397 | members: members.map((member) => member.project), |
| 398 | }) |
| 399 | : representative.title, |
| 400 | representative, |
| 401 | members, |
| 402 | memberProjectRefs: projectRefsByLogicalKey.get(key) ?? [], |
| 403 | pinned: members.some((member) => member.project.pinnedAt != null), |
| 404 | archived: !members.some(({ project }) => isPickableProjectMember(project)), |
| 405 | }; |
| 406 | }); |
| 407 | } |
| 408 | |
| 409 | export function sortProjectsByGroupOrder< |
| 410 | TProject extends { readonly id: string; readonly environmentId: string }, |
| 411 | >( |
| 412 | projects: ReadonlyArray<TProject>, |
| 413 | groups: ReadonlyArray<{ |
| 414 | readonly memberProjectRefs: ReadonlyArray<ScopedProjectRef>; |
| 415 | readonly pinned: boolean; |
| 416 | readonly archived: boolean; |
| 417 | }>, |
| 418 | ): TProject[] { |
| 419 | const rankByProject = new Map<string, number>(); |
| 420 | pickableProjectGroups(groups).forEach((group, rank) => { |
| 421 | for (const ref of group.memberProjectRefs) { |
| 422 | const key = `${ref.environmentId}\0${ref.projectId}`; |
| 423 | if (!rankByProject.has(key)) rankByProject.set(key, rank); |
| 424 | } |
| 425 | }); |
| 426 | const rankOf = (project: TProject) => |
| 427 | rankByProject.get(`${project.environmentId}\0${project.id}`) ?? groups.length; |
| 428 | return [...projects].sort((left, right) => rankOf(left) - rankOf(right)); |
| 429 | } |