Open History to see the last commit
1import { scopedProjectKey, scopeProjectRef } from "@t3tools/client-runtime";
2import type { ScopedProjectRef, SidebarProjectGroupingMode } from "@t3tools/contracts";
3import { normalizeProjectPathForComparison } from "./lib/projectPaths";
4import type { Project } from "./types";
5
6export interface ProjectGroupingSettings {
7 sidebarProjectGroupingMode: SidebarProjectGroupingMode;
8 sidebarProjectGroupingOverrides: Record<string, SidebarProjectGroupingMode>;
9}
10
11export type ProjectGroupingMode = SidebarProjectGroupingMode;
12
13function uniqueNonEmptyValues(values: ReadonlyArray<string | null | undefined>): string[] {
14 const seen = new Set<string>();
15 const unique: string[] = [];
16 for (const value of values) {
17 const trimmed = value?.trim();
18 if (!trimmed || seen.has(trimmed)) {
19 continue;
20 }
21 seen.add(trimmed);
22 unique.push(trimmed);
23 }
24 return unique;
25}
26
27function deriveRepositoryRelativeProjectPath(
28 project: Pick<Project, "cwd" | "repositoryIdentity">,
29): string | null {
30 const rootPath = project.repositoryIdentity?.rootPath?.trim();
31 if (!rootPath) {
32 return null;
33 }
34
35 const normalizedProjectPath = normalizeProjectPathForComparison(project.cwd);
36 const normalizedRootPath = normalizeProjectPathForComparison(rootPath);
37 if (normalizedProjectPath.length === 0 || normalizedRootPath.length === 0) {
38 return null;
39 }
40
41 if (normalizedProjectPath === normalizedRootPath) {
42 return "";
43 }
44
45 const separator = normalizedRootPath.includes("\\") ? "\\" : "/";
46 const rootPrefix = `${normalizedRootPath}${separator}`;
47 if (!normalizedProjectPath.startsWith(rootPrefix)) {
48 return null;
49 }
50
51 return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/");
52}
53
54export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string {
55 return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`;
56}
57
58export function derivePhysicalProjectKey(project: Pick<Project, "environmentId" | "cwd">): string {
59 return derivePhysicalProjectKeyFromPath(project.environmentId, project.cwd);
60}
61
62export function deriveProjectGroupingOverrideKey(
63 project: Pick<Project, "environmentId" | "cwd">,
64): string {
65 return derivePhysicalProjectKey(project);
66}
67
68// Key under which a project's manual sort order (projectOrder) is stored.
69// Must stay aligned with the writer side in `uiStateStore.syncProjects` and
70// the drag handlers in `Sidebar` so readers and writers agree.
71export function getProjectOrderKey(project: Pick<Project, "environmentId" | "cwd">): string {
72 return derivePhysicalProjectKey(project);
73}
74
75export function resolveProjectGroupingMode(
76 project: Pick<Project, "environmentId" | "cwd">,
77 settings: ProjectGroupingSettings,
78): SidebarProjectGroupingMode {
79 return (
80 settings.sidebarProjectGroupingOverrides?.[deriveProjectGroupingOverrideKey(project)] ??
81 settings.sidebarProjectGroupingMode
82 );
83}
84
85function deriveRepositoryScopedKey(
86 project: Pick<Project, "cwd" | "repositoryIdentity">,
87 groupingMode: SidebarProjectGroupingMode,
88): string | null {
89 const canonicalKey = project.repositoryIdentity?.canonicalKey;
90 if (!canonicalKey) {
91 return null;
92 }
93
94 if (groupingMode === "repository") {
95 return canonicalKey;
96 }
97
98 const relativeProjectPath = deriveRepositoryRelativeProjectPath(project);
99 if (relativeProjectPath === null) {
100 return canonicalKey;
101 }
102
103 return relativeProjectPath.length === 0
104 ? canonicalKey
105 : `${canonicalKey}::${relativeProjectPath}`;
106}
107
108export function deriveLogicalProjectKey(
109 project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">,
110 options?: {
111 groupingMode?: SidebarProjectGroupingMode;
112 },
113): string {
114 const groupingMode = options?.groupingMode ?? "repository";
115 if (groupingMode === "separate") {
116 return derivePhysicalProjectKey(project);
117 }
118
119 return (
120 deriveRepositoryScopedKey(project, groupingMode) ??
121 derivePhysicalProjectKey(project) ??
122 scopedProjectKey(scopeProjectRef(project.environmentId, project.id))
123 );
124}
125
126export function deriveLogicalProjectKeyFromSettings(
127 project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">,
128 settings: ProjectGroupingSettings,
129): string {
130 return deriveLogicalProjectKey(project, {
131 groupingMode: resolveProjectGroupingMode(project, settings),
132 });
133}
134
135export function deriveLogicalProjectKeyFromRef(
136 projectRef: ScopedProjectRef,
137 project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity"> | null | undefined,
138 options?: {
139 groupingMode?: SidebarProjectGroupingMode;
140 },
141): string {
142 return project ? deriveLogicalProjectKey(project, options) : scopedProjectKey(projectRef);
143}
144
145export function deriveProjectGroupLabel(input: {
146 representative: Pick<Project, "name" | "repositoryIdentity">;
147 members: ReadonlyArray<Pick<Project, "name" | "repositoryIdentity">>;
148}): string {
149 const sharedDisplayNames = uniqueNonEmptyValues(
150 input.members.map((member) => member.repositoryIdentity?.displayName),
151 );
152 if (sharedDisplayNames.length === 1) {
153 return sharedDisplayNames[0]!;
154 }
155
156 const sharedRepositoryNames = uniqueNonEmptyValues(
157 input.members.map((member) => member.repositoryIdentity?.name),
158 );
159 if (sharedRepositoryNames.length === 1) {
160 return sharedRepositoryNames[0]!;
161 }
162
163 return input.representative.name;
164}