Restore project grouping across hosts (#2565)
* fix(projects): restore cross-host grouping Random host-local project IDs replaced the remote-derived value that had also served as grouping identity. Persist a separate opaque group key and let normal boot reconciliation backfill older records without a migration. * fix(projects): keep ambiguous clones separate A shared remote can identify a project across hosts, but it cannot distinguish two independent clones on one host. Treat those keys as ambiguous so host-local project IDs remain separate. * fix(projects): preserve host-local project identity * fix(projects): strengthen grouping identity * test(app): update project host shape contract * fix(projects): preserve selected host identity * fix(projects): close grouped routing gaps * fix(projects): stabilize legacy subproject grouping * fix(projects): preserve selected project identity * fix(projects): isolate legacy host identity * fix(projects): preserve symlinked bootstrap roots * fix(projects): reconcile grouped custom names * fix(projects): stabilize settings and path identity * fix(projects): normalize default remote ports * fix(projects): harden host and remote identity * fix(projects): preserve host-scoped actions * fix(projects): preserve remote path semantics * fix(projects): model SCP remote semantics * fix(projects): keep settings on routed host * fix(projects): preserve SSH URL path semantics * fix(projects): separate identity from presentation * test(projects): use grouped identity in UI specs * fix(projects): harden grouped project routing * test(projects): expect framed settings route * fix(projects): preserve exact placement identity * fix(projects): harden grouping identity and routing * fix(projects): restore project key as grouping identity Random host-local project IDs had replaced the persisted cross-host value carried by projectKey. Keep projectId for routing while reconciliation owns and persists projectKey for grouping. * fix(projects): prevent project key collisions Preserve platform path semantics and generic remote transports in cross-host keys, and compare legacy checkout roots using filesystem equivalence before materializing ownership. * test(projects): assert host-scoped project keys * fix(projects): preserve project key compatibility Resolve legacy workspace project references at the daemon boundary while keeping projectKey as the cross-host identity. Complete generic SSH normalization without collapsing distinct repositories. * fix(projects): harden project reference identity * fix(projects): retain archived reference errors * fix(projects): scope legacy paths to hosts * test(app): model persisted project keys * fix(projects): retain absolute scp users * fix(projects): resolve legacy mutation aliases * fix(projects): preserve conservative identity boundaries * fix(projects): preserve legacy placement identity * test(projects): assert legacy placement identity * test(projects): expect canonical Windows keys * fix(projects): separate bootstrap identity fields * fix(projects): scope legacy paths by host * fix(projects): stabilize opaque project identity * fix(projects): preserve remaining opaque identities * fix(projects): classify forward-slash UNC roots * fix(projects): preserve opaque selection keys * fix(projects): frame host-local project keys * fix(projects): canonicalize framed identities * test(projects): isolate repeated remote fixtures * fix(projects): keep project key as UI identity * fix(projects): reconcile hydrated project references * fix(projects): preserve archived project identity * refactor(projects): group projects from project records, not workspaces Cross-host grouping had been built by hanging a projectKey off every workspace descriptor. That scoped the key to the wrong entity: the app then had to reconstruct project identity per workspace in six modules, which forced projectKey fallbacks into RPC arguments and a server-side resolver that turned grouping keys back into project ids. A workspace has a projectId. The key belongs to the project, and grouping is a display concern, so the daemon now publishes projects over project.list plus the existing project.update channel and the app groups them in one place. Projects are addressed by host-local projectId everywhere, including the settings route. Projects persisted without a key are backfilled by ordinary boot reconciliation, with no migration. Old daemons advertise no projectList capability and simply do not group. * test(projects): seed projects alongside workspaces in fixtures Sidebar fixtures seeded workspaces without their projects, which the daemon cannot produce now that projects arrive on their own channel, so nothing rendered. Adds a seeding helper that supplies both. Also drops the provisioning test for accepting a project key where a project id belongs; that resolver was removed deliberately. * fix(projects): keep derived project identity stable Four gaps left by shrinking the key derivation: Local keys used a bare path resolve, so one directory reached by a different spelling produced a different key and reset grouped identity. Windows CI caught this with the 8.3 and long-name forms of one temp path. Canonicalize through the shared realpath-aware normalization. Subproject display names lost their selected-path input, so every subproject of a monorepo rendered under the same remote-derived name. Reconciliation only ever backfilled a missing key, so a project whose remote changed stayed grouped with hosts on the old remote. Project settings kept a disconnected host selected and rendered a dead end with no way back to an editable host. * fix(projects): read scheme remotes as URLs, not scp form An `ssh://user@host:22/org/repo` remote matched the scp-form pattern first, because `[^@]+` happily eats `ssh://git`, so the port landed in the path and the project keyed as `remote:host/22/org/repo`. Two spellings of one remote therefore never grouped. Try scp form only when there is no scheme, and drop protocol-default ports so an explicitly written `:22` matches the same remote without it.
This commit is contained in:
@@ -4,14 +4,7 @@ import type {
|
||||
WorkspaceStructureProject,
|
||||
} from "@/projects/workspace-structure";
|
||||
|
||||
export interface HostProjectListItem {
|
||||
projectKey: string;
|
||||
projectName: string;
|
||||
projectKind: WorkspaceDescriptor["projectKind"];
|
||||
iconWorkingDir: string;
|
||||
hosts: WorkspaceStructureHostPlacement[];
|
||||
workspaceKeys: string[];
|
||||
}
|
||||
export type HostProjectListItem = WorkspaceStructureProject;
|
||||
|
||||
export interface HostProjectRouteContext {
|
||||
serverId: string;
|
||||
@@ -31,33 +24,21 @@ export function canCreateWorktreeForProjectKind(
|
||||
return projectKind === "git";
|
||||
}
|
||||
|
||||
export function buildHostProjectList(input: {
|
||||
projects: readonly WorkspaceStructureProject[];
|
||||
}): HostProjectListItem[] {
|
||||
return input.projects.map((project) => ({
|
||||
projectKey: project.projectKey,
|
||||
projectName: project.projectName,
|
||||
projectKind: project.projectKind,
|
||||
iconWorkingDir: project.iconWorkingDir,
|
||||
hosts: project.hosts,
|
||||
workspaceKeys: project.workspaceKeys,
|
||||
}));
|
||||
}
|
||||
|
||||
export function hostProjectFromRoute(route: HostProjectRouteContext): HostProjectListItem | null {
|
||||
const projectKey = trimOptional(route.projectId);
|
||||
const projectId = route.projectId?.trim() || undefined;
|
||||
const iconWorkingDir = trimOptional(route.sourceDirectory);
|
||||
if (!projectKey || !iconWorkingDir) {
|
||||
if (!projectId || !iconWorkingDir) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
projectKey,
|
||||
projectName: trimOptional(route.displayName) ?? projectKey,
|
||||
projectKey: projectId,
|
||||
projectName: trimOptional(route.displayName) || projectId,
|
||||
projectKind: "git",
|
||||
iconWorkingDir,
|
||||
hosts: [
|
||||
{
|
||||
serverId: route.serverId,
|
||||
projectId,
|
||||
iconWorkingDir,
|
||||
canCreateWorktree: true,
|
||||
},
|
||||
@@ -73,20 +54,24 @@ export function hostProjectFromWorkspace(input: {
|
||||
if (!input.workspace) {
|
||||
return null;
|
||||
}
|
||||
const projectKey = input.workspace.projectId.trim();
|
||||
const projectId = input.workspace.projectId.trim() || undefined;
|
||||
if (!projectId) {
|
||||
return null;
|
||||
}
|
||||
const iconWorkingDir = input.workspace.projectRootPath.trim();
|
||||
if (!projectKey || !iconWorkingDir) {
|
||||
if (!iconWorkingDir) {
|
||||
return null;
|
||||
}
|
||||
const canCreate = canCreateWorktreeForProjectKind(input.workspace.projectKind);
|
||||
return {
|
||||
projectKey,
|
||||
projectName: input.workspace.projectDisplayName || projectKey,
|
||||
projectKey: projectId,
|
||||
projectName: input.workspace.projectDisplayName || projectId,
|
||||
projectKind: input.workspace.projectKind,
|
||||
iconWorkingDir,
|
||||
hosts: [
|
||||
{
|
||||
serverId: input.serverId,
|
||||
projectId: input.workspace.projectId,
|
||||
iconWorkingDir,
|
||||
canCreateWorktree: canCreate,
|
||||
},
|
||||
@@ -99,11 +84,25 @@ function projectCanCreateWorktree(project: HostProjectListItem): boolean {
|
||||
return project.hosts.some((h) => h.canCreateWorktree);
|
||||
}
|
||||
|
||||
function getHostProjectPlacement(
|
||||
project: HostProjectListItem,
|
||||
serverId: string,
|
||||
): WorkspaceStructureHostPlacement | null {
|
||||
for (const host of project.hosts) {
|
||||
if (host.serverId === serverId) return host;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getHostProjectSourceDirectory(
|
||||
project: HostProjectListItem,
|
||||
serverId: string,
|
||||
): string | null {
|
||||
return project.hosts.find((host) => host.serverId === serverId)?.iconWorkingDir ?? null;
|
||||
return getHostProjectPlacement(project, serverId)?.iconWorkingDir ?? null;
|
||||
}
|
||||
|
||||
export function getHostProjectId(project: HostProjectListItem, serverId: string): string | null {
|
||||
return getHostProjectPlacement(project, serverId)?.projectId ?? null;
|
||||
}
|
||||
|
||||
export function canCreateWorkspaceForHostProject(input: {
|
||||
@@ -111,7 +110,7 @@ export function canCreateWorkspaceForHostProject(input: {
|
||||
serverId: string;
|
||||
allowAllProjects: boolean;
|
||||
}): boolean {
|
||||
const host = input.project.hosts.find((candidate) => candidate.serverId === input.serverId);
|
||||
const host = getHostProjectPlacement(input.project, input.serverId);
|
||||
if (!host) {
|
||||
return false;
|
||||
}
|
||||
@@ -144,8 +143,20 @@ export function resolveInitialWorkspaceProject(input: {
|
||||
if (!candidate) {
|
||||
continue;
|
||||
}
|
||||
const candidatePlacement = candidate.hosts.find(
|
||||
(host) => host.serverId === input.serverId && host.projectId,
|
||||
);
|
||||
const hydratedProject =
|
||||
input.projects.find((project) => project.projectKey === candidate.projectKey) ?? candidate;
|
||||
(candidatePlacement
|
||||
? input.projects.find((project) =>
|
||||
project.hosts.some(
|
||||
(host) =>
|
||||
host.serverId === input.serverId && host.projectId === candidatePlacement.projectId,
|
||||
),
|
||||
)
|
||||
: undefined) ??
|
||||
input.projects.find((project) => project.projectKey === candidate.projectKey) ??
|
||||
candidate;
|
||||
if (
|
||||
canCreateWorkspaceForHostProject({
|
||||
project: hydratedProject,
|
||||
|
||||
@@ -1,312 +1,64 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { WorkspaceDescriptor } from "@/stores/session-store";
|
||||
import type { WorkspaceStructureProject } from "@/projects/workspace-structure";
|
||||
import { describe, expect, test } from "vitest";
|
||||
import type { HostProjectListItem } from "./host-project-model";
|
||||
import {
|
||||
buildHostProjectList,
|
||||
canCreateWorkspaceForHostProject,
|
||||
canCreateWorktreeForProjectKind,
|
||||
filterWorkspaceProjectsForHost,
|
||||
getHostProjectId,
|
||||
getHostProjectSourceDirectory,
|
||||
hostProjectFromRoute,
|
||||
hostProjectFromWorkspace,
|
||||
resolveInitialWorkspaceProject,
|
||||
resolveInitialWorktreeProject,
|
||||
resolveSelectedHostProject,
|
||||
type HostProjectListItem,
|
||||
} from "./host-project-model";
|
||||
|
||||
function structureProject(input: Partial<WorkspaceStructureProject>): WorkspaceStructureProject {
|
||||
function project(): HostProjectListItem {
|
||||
return {
|
||||
projectKey: input.projectKey ?? "project-a",
|
||||
projectName: input.projectName ?? "Project A",
|
||||
projectKind: input.projectKind ?? "git",
|
||||
iconWorkingDir: input.iconWorkingDir ?? "/repo/a",
|
||||
hosts: input.hosts ?? [
|
||||
projectKey: "remote:github.com/acme/app",
|
||||
projectName: "acme/app",
|
||||
projectKind: "git",
|
||||
iconWorkingDir: "/repo/a",
|
||||
hosts: [
|
||||
{
|
||||
serverId: "host-a",
|
||||
iconWorkingDir: input.iconWorkingDir ?? "/repo/a",
|
||||
canCreateWorktree: input.projectKind !== "directory",
|
||||
projectId: "prj_a",
|
||||
iconWorkingDir: "/repo/a",
|
||||
canCreateWorktree: true,
|
||||
},
|
||||
],
|
||||
workspaceKeys: input.workspaceKeys ?? ["workspace-a"],
|
||||
};
|
||||
}
|
||||
|
||||
function hostProject(input: Partial<HostProjectListItem>): HostProjectListItem {
|
||||
return {
|
||||
projectKey: input.projectKey ?? "project-a",
|
||||
projectName: input.projectName ?? "Project A",
|
||||
projectKind: input.projectKind ?? "git",
|
||||
iconWorkingDir: input.iconWorkingDir ?? "/repo/a",
|
||||
hosts: input.hosts ?? [
|
||||
{
|
||||
serverId: "host-a",
|
||||
iconWorkingDir: input.iconWorkingDir ?? "/repo/a",
|
||||
serverId: "host-b",
|
||||
projectId: "prj_b",
|
||||
iconWorkingDir: "/repo/b",
|
||||
canCreateWorktree: true,
|
||||
},
|
||||
],
|
||||
workspaceKeys: input.workspaceKeys ?? ["workspace-a"],
|
||||
workspaceKeys: [],
|
||||
};
|
||||
}
|
||||
|
||||
function workspace(input: Partial<WorkspaceDescriptor>): WorkspaceDescriptor {
|
||||
return {
|
||||
id: input.id ?? "workspace-a",
|
||||
projectId: input.projectId ?? "project-a",
|
||||
projectDisplayName: input.projectDisplayName ?? "Project A",
|
||||
projectRootPath: input.projectRootPath ?? "/repo/a",
|
||||
workspaceDirectory: input.workspaceDirectory ?? "/repo/a",
|
||||
projectKind: input.projectKind ?? "git",
|
||||
workspaceKind: input.workspaceKind ?? "local_checkout",
|
||||
name: input.name ?? "main",
|
||||
status: input.status ?? "done",
|
||||
statusEnteredAt: input.statusEnteredAt ?? null,
|
||||
archivingAt: input.archivingAt ?? null,
|
||||
diffStat: input.diffStat ?? null,
|
||||
scripts: input.scripts ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
const routeProject = hostProject({
|
||||
projectKey: "route-project",
|
||||
projectName: "Route Project",
|
||||
iconWorkingDir: "/repo/route",
|
||||
});
|
||||
const lastActiveProject = hostProject({
|
||||
projectKey: "last-project",
|
||||
projectName: "Last Project",
|
||||
iconWorkingDir: "/repo/last",
|
||||
});
|
||||
const firstProject = hostProject({
|
||||
projectKey: "first-project",
|
||||
projectName: "First Project",
|
||||
iconWorkingDir: "/repo/first",
|
||||
});
|
||||
|
||||
describe("host project list", () => {
|
||||
it("preserves workspace-structure order and project metadata", () => {
|
||||
expect(
|
||||
buildHostProjectList({
|
||||
projects: [
|
||||
structureProject({
|
||||
projectKey: "project-b",
|
||||
projectName: "Project B",
|
||||
projectKind: "directory",
|
||||
iconWorkingDir: "/repo/b",
|
||||
workspaceKeys: ["workspace-b"],
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/b", canCreateWorktree: false }],
|
||||
}),
|
||||
structureProject({
|
||||
projectKey: "project-a",
|
||||
projectName: "Project A",
|
||||
projectKind: "git",
|
||||
iconWorkingDir: "/repo/a",
|
||||
workspaceKeys: ["workspace-a"],
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: true }],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
).toEqual([
|
||||
{
|
||||
projectKey: "project-b",
|
||||
projectName: "Project B",
|
||||
projectKind: "directory",
|
||||
iconWorkingDir: "/repo/b",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/b", canCreateWorktree: false }],
|
||||
workspaceKeys: ["workspace-b"],
|
||||
},
|
||||
{
|
||||
projectKey: "project-a",
|
||||
projectName: "Project A",
|
||||
projectKind: "git",
|
||||
iconWorkingDir: "/repo/a",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: true }],
|
||||
workspaceKeys: ["workspace-a"],
|
||||
},
|
||||
]);
|
||||
describe("host project lookups", () => {
|
||||
test("returns host-local ids and roots without falling back to the grouping key", () => {
|
||||
expect(getHostProjectId(project(), "host-b")).toBe("prj_b");
|
||||
expect(getHostProjectSourceDirectory(project(), "host-b")).toBe("/repo/b");
|
||||
expect(getHostProjectId(project(), "missing")).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps worktree capability separate from project listability", () => {
|
||||
expect(canCreateWorktreeForProjectKind("git")).toBe(true);
|
||||
expect(canCreateWorktreeForProjectKind("directory")).toBe(false);
|
||||
});
|
||||
|
||||
it("uses route project before last active project when it can create worktrees", () => {
|
||||
test("checks workspace creation against the selected host placement", () => {
|
||||
expect(
|
||||
resolveInitialWorktreeProject({
|
||||
routeProject,
|
||||
lastActiveProject,
|
||||
projects: [firstProject],
|
||||
}),
|
||||
).toEqual(routeProject);
|
||||
});
|
||||
|
||||
it("skips non-worktree route and last-active projects", () => {
|
||||
expect(
|
||||
resolveInitialWorktreeProject({
|
||||
routeProject: {
|
||||
...routeProject,
|
||||
projectKind: "directory",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/route", canCreateWorktree: false }],
|
||||
},
|
||||
lastActiveProject: {
|
||||
...lastActiveProject,
|
||||
projectKind: "directory",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/last", canCreateWorktree: false }],
|
||||
},
|
||||
projects: [
|
||||
{
|
||||
...firstProject,
|
||||
projectKind: "directory",
|
||||
hosts: [
|
||||
{ serverId: "host-a", iconWorkingDir: "/repo/first", canCreateWorktree: false },
|
||||
],
|
||||
},
|
||||
hostProject({ projectKey: "git-project", projectName: "Git Project" }),
|
||||
],
|
||||
}),
|
||||
).toMatchObject({ projectKey: "git-project" });
|
||||
});
|
||||
|
||||
it("leaves the project empty when no worktree-capable project is available", () => {
|
||||
expect(
|
||||
resolveInitialWorktreeProject({
|
||||
routeProject: null,
|
||||
lastActiveProject: null,
|
||||
projects: [
|
||||
{
|
||||
...firstProject,
|
||||
projectKind: "directory",
|
||||
hosts: [
|
||||
{ serverId: "host-a", iconWorkingDir: "/repo/first", canCreateWorktree: false },
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("filters new-workspace projects to the selected host", () => {
|
||||
const hostAOnly = hostProject({
|
||||
projectKey: "host-a-project",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: true }],
|
||||
});
|
||||
const hostBOnly = hostProject({
|
||||
projectKey: "host-b-project",
|
||||
hosts: [{ serverId: "host-b", iconWorkingDir: "/repo/b", canCreateWorktree: true }],
|
||||
});
|
||||
|
||||
expect(
|
||||
filterWorkspaceProjectsForHost({
|
||||
projects: [hostAOnly, hostBOnly],
|
||||
canCreateWorkspaceForHostProject({
|
||||
project: project(),
|
||||
serverId: "host-b",
|
||||
allowAllProjects: false,
|
||||
}).map((project) => project.projectKey),
|
||||
).toEqual(["host-b-project"]);
|
||||
});
|
||||
|
||||
it("allows directory projects only when workspace multiplicity is supported", () => {
|
||||
const directoryProject = hostProject({
|
||||
projectKey: "directory-project",
|
||||
projectKind: "directory",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/directory", canCreateWorktree: false }],
|
||||
});
|
||||
|
||||
expect(
|
||||
canCreateWorkspaceForHostProject({
|
||||
project: directoryProject,
|
||||
serverId: "host-a",
|
||||
allowAllProjects: false,
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
canCreateWorkspaceForHostProject({
|
||||
project: directoryProject,
|
||||
serverId: "host-a",
|
||||
allowAllProjects: true,
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("falls back when the route project is not available on the selected host", () => {
|
||||
const selectedHostProject = hostProject({
|
||||
projectKey: "selected-host-project",
|
||||
hosts: [
|
||||
{ serverId: "host-b", iconWorkingDir: "/repo/selected-host", canCreateWorktree: true },
|
||||
],
|
||||
});
|
||||
|
||||
expect(
|
||||
resolveInitialWorkspaceProject({
|
||||
routeProject,
|
||||
lastActiveProject: null,
|
||||
projects: [selectedHostProject],
|
||||
serverId: "host-b",
|
||||
allowAllProjects: false,
|
||||
}),
|
||||
).toEqual(selectedHostProject);
|
||||
});
|
||||
|
||||
it("resolves the selected host project source directory", () => {
|
||||
const project = hostProject({
|
||||
hosts: [
|
||||
{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: true },
|
||||
{ serverId: "host-b", iconWorkingDir: "/repo/b", canCreateWorktree: true },
|
||||
],
|
||||
});
|
||||
|
||||
expect(getHostProjectSourceDirectory(project, "host-b")).toBe("/repo/b");
|
||||
expect(getHostProjectSourceDirectory(project, "host-c")).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps a selected route project available before project hydration", () => {
|
||||
expect(
|
||||
resolveSelectedHostProject({
|
||||
selectedProjectKey: routeProject.projectKey,
|
||||
projects: [],
|
||||
routeProject,
|
||||
lastActiveProject: null,
|
||||
}),
|
||||
).toEqual(routeProject);
|
||||
});
|
||||
|
||||
it("converts route project only when it has a key and source directory", () => {
|
||||
test("builds an unhydrated route project around the routed project id", () => {
|
||||
expect(
|
||||
hostProjectFromRoute({
|
||||
serverId: "host-a",
|
||||
projectId: "project-a",
|
||||
displayName: "Project A",
|
||||
projectId: "prj_a",
|
||||
displayName: "App",
|
||||
sourceDirectory: "/repo/a",
|
||||
}),
|
||||
).toEqual({
|
||||
projectKey: "project-a",
|
||||
projectName: "Project A",
|
||||
projectKind: "git",
|
||||
iconWorkingDir: "/repo/a",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: true }],
|
||||
workspaceKeys: [],
|
||||
});
|
||||
expect(hostProjectFromRoute({ serverId: "host-a", projectId: "project-a" })).toBeNull();
|
||||
});
|
||||
|
||||
it("converts last active workspaces with matching worktree capability", () => {
|
||||
expect(hostProjectFromWorkspace({ serverId: "host-a", workspace: workspace({}) })).toEqual({
|
||||
projectKey: "project-a",
|
||||
projectName: "Project A",
|
||||
projectKind: "git",
|
||||
iconWorkingDir: "/repo/a",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: true }],
|
||||
workspaceKeys: ["host-a:workspace-a"],
|
||||
});
|
||||
|
||||
expect(
|
||||
hostProjectFromWorkspace({
|
||||
serverId: "host-a",
|
||||
workspace: workspace({ projectKind: "directory" }),
|
||||
}),
|
||||
).toMatchObject({
|
||||
projectKind: "directory",
|
||||
hosts: [{ serverId: "host-a", iconWorkingDir: "/repo/a", canCreateWorktree: false }],
|
||||
projectKey: "prj_a",
|
||||
hosts: [{ serverId: "host-a", projectId: "prj_a" }],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import { useMemo } from "react";
|
||||
import { useWorkspaceStructure } from "@/stores/session-store-hooks";
|
||||
import { buildHostProjectList, type HostProjectListItem } from "@/projects/host-project-model";
|
||||
import { type HostProjectListItem } from "@/projects/host-project-model";
|
||||
|
||||
export {
|
||||
buildHostProjectList,
|
||||
canCreateWorkspaceForHostProject,
|
||||
canCreateWorktreeForProjectKind,
|
||||
filterWorkspaceProjectsForHost,
|
||||
getHostProjectSourceDirectory,
|
||||
getHostProjectId,
|
||||
hostProjectFromRoute,
|
||||
hostProjectFromWorkspace,
|
||||
resolveInitialWorkspaceProject,
|
||||
@@ -19,10 +18,5 @@ export {
|
||||
|
||||
export function useHostProjects(serverIds: string[]): HostProjectListItem[] {
|
||||
const workspaceStructure = useWorkspaceStructure(serverIds);
|
||||
return useMemo(() => {
|
||||
if (workspaceStructure.projects.length === 0) {
|
||||
return [];
|
||||
}
|
||||
return buildHostProjectList({ projects: workspaceStructure.projects });
|
||||
}, [workspaceStructure.projects]);
|
||||
return workspaceStructure.projects;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ import {
|
||||
|
||||
const project: ProjectRemoveProject = {
|
||||
projectKey: "remote:github.com/acme/app",
|
||||
hosts: [{ serverId: "host-a" }, { serverId: "host-b" }],
|
||||
hosts: [
|
||||
{ serverId: "host-a", projectId: "prj_host_a" },
|
||||
{ serverId: "host-b", projectId: "prj_host_b" },
|
||||
],
|
||||
};
|
||||
|
||||
function createProjectRemoveClient() {
|
||||
@@ -46,12 +49,14 @@ describe("project remove policy", () => {
|
||||
|
||||
expect(readiness).toEqual({
|
||||
kind: "ready",
|
||||
targets: [{ serverId: "host-a" }, { serverId: "host-b" }],
|
||||
targets: [
|
||||
{ serverId: "host-a", projectId: "prj_host_a" },
|
||||
{ serverId: "host-b", projectId: "prj_host_b" },
|
||||
],
|
||||
});
|
||||
|
||||
const outcome = await removeProjectFromHosts({
|
||||
projectKey: project.projectKey,
|
||||
targets: [{ serverId: "host-a" }, { serverId: "host-b" }],
|
||||
targets: readiness.kind === "ready" ? readiness.targets : [],
|
||||
getClient: (serverId) => {
|
||||
if (serverId === "host-a") return hostA.client;
|
||||
if (serverId === "host-b") return hostB.client;
|
||||
@@ -60,16 +65,18 @@ describe("project remove policy", () => {
|
||||
});
|
||||
|
||||
expect(outcome).toEqual({ kind: "removed", serverIds: ["host-a", "host-b"] });
|
||||
expect(hostA.removedProjectKeys).toEqual([project.projectKey]);
|
||||
expect(hostB.removedProjectKeys).toEqual([project.projectKey]);
|
||||
expect(hostA.removedProjectKeys).toEqual(["prj_host_a"]);
|
||||
expect(hostB.removedProjectKeys).toEqual(["prj_host_b"]);
|
||||
});
|
||||
|
||||
it("reports disconnected hosts before sending any remove request", async () => {
|
||||
const hostA = createProjectRemoveClient();
|
||||
|
||||
const outcome = await removeProjectFromHosts({
|
||||
projectKey: project.projectKey,
|
||||
targets: [{ serverId: "host-a" }, { serverId: "host-b" }],
|
||||
targets: [
|
||||
{ serverId: "host-a", projectId: "prj_host_a" },
|
||||
{ serverId: "host-b", projectId: "prj_host_b" },
|
||||
],
|
||||
getClient: (serverId) => (serverId === "host-a" ? hostA.client : null),
|
||||
});
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import { selectHostFeature } from "@/runtime/host-features";
|
||||
|
||||
interface ProjectRemoveHost {
|
||||
serverId: string;
|
||||
projectId: string;
|
||||
}
|
||||
|
||||
export interface ProjectRemoveProject {
|
||||
@@ -13,6 +14,7 @@ export interface ProjectRemoveProject {
|
||||
|
||||
export interface ProjectRemoveTarget {
|
||||
serverId: string;
|
||||
projectId: string;
|
||||
}
|
||||
|
||||
export type ProjectRemoveReadiness =
|
||||
@@ -38,7 +40,10 @@ export function getProjectRemoveReadiness(input: {
|
||||
unsupportedServerIds.push(host.serverId);
|
||||
continue;
|
||||
}
|
||||
targets.push({ serverId: host.serverId });
|
||||
targets.push({
|
||||
serverId: host.serverId,
|
||||
projectId: host.projectId,
|
||||
});
|
||||
}
|
||||
|
||||
if (unsupportedServerIds.length > 0) {
|
||||
@@ -59,11 +64,10 @@ export function getCurrentProjectRemoveReadiness(
|
||||
}
|
||||
|
||||
export async function removeProjectFromHosts(input: {
|
||||
projectKey: string;
|
||||
targets: readonly ProjectRemoveTarget[];
|
||||
getClient: (serverId: string) => ProjectRemoveClient | null;
|
||||
}): Promise<ProjectRemoveOutcome> {
|
||||
const clients: Array<{ serverId: string; client: ProjectRemoveClient }> = [];
|
||||
const clients: Array<{ serverId: string; projectId: string; client: ProjectRemoveClient }> = [];
|
||||
const disconnectedServerIds: string[] = [];
|
||||
|
||||
for (const target of input.targets) {
|
||||
@@ -72,7 +76,7 @@ export async function removeProjectFromHosts(input: {
|
||||
disconnectedServerIds.push(target.serverId);
|
||||
continue;
|
||||
}
|
||||
clients.push({ serverId: target.serverId, client });
|
||||
clients.push({ serverId: target.serverId, projectId: target.projectId, client });
|
||||
}
|
||||
|
||||
if (disconnectedServerIds.length > 0) {
|
||||
@@ -80,8 +84,8 @@ export async function removeProjectFromHosts(input: {
|
||||
}
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
clients.map(async ({ client }) => {
|
||||
await client.removeProject(input.projectKey);
|
||||
clients.map(async ({ client, projectId }) => {
|
||||
await client.removeProject(projectId);
|
||||
}),
|
||||
);
|
||||
const failedServerIds: string[] = [];
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
|
||||
import {
|
||||
type EmptyProjectDescriptor,
|
||||
normalizeEmptyProjectDescriptor,
|
||||
normalizeWorkspaceDescriptor,
|
||||
type WorkspaceDescriptor,
|
||||
} from "@/stores/session-store";
|
||||
|
||||
export type FetchWorkspacesClient = Pick<DaemonClient, "fetchWorkspaces">;
|
||||
export type FetchWorkspacesSort = NonNullable<
|
||||
Parameters<DaemonClient["fetchWorkspaces"]>[0]
|
||||
>["sort"];
|
||||
|
||||
export interface FetchWorkspaceDescriptorsResult {
|
||||
workspaces: WorkspaceDescriptor[];
|
||||
/**
|
||||
* Project parents with no active workspaces. The daemon only rides these on
|
||||
* the first page of `fetchWorkspaces`, so a freshly-added project that has no
|
||||
* workspace yet shows up here and nowhere else — drop it and the project is
|
||||
* invisible to anything that derives projects from workspaces alone.
|
||||
*/
|
||||
emptyProjects: EmptyProjectDescriptor[];
|
||||
}
|
||||
|
||||
export async function fetchAllWorkspaceDescriptors(input: {
|
||||
client: FetchWorkspacesClient;
|
||||
sort: FetchWorkspacesSort;
|
||||
}): Promise<FetchWorkspaceDescriptorsResult> {
|
||||
const workspaces: WorkspaceDescriptor[] = [];
|
||||
const emptyProjects = new Map<string, EmptyProjectDescriptor>();
|
||||
let cursor: string | null = null;
|
||||
|
||||
while (true) {
|
||||
const payload = await input.client.fetchWorkspaces({
|
||||
sort: input.sort,
|
||||
page: cursor ? { limit: 200, cursor } : { limit: 200 },
|
||||
});
|
||||
workspaces.push(...payload.entries.map((entry) => normalizeWorkspaceDescriptor(entry)));
|
||||
for (const project of payload.emptyProjects ?? []) {
|
||||
const descriptor = normalizeEmptyProjectDescriptor(project);
|
||||
emptyProjects.set(descriptor.projectId, descriptor);
|
||||
}
|
||||
if (!payload.pageInfo.hasMore || !payload.pageInfo.nextCursor) {
|
||||
break;
|
||||
}
|
||||
cursor = payload.pageInfo.nextCursor;
|
||||
}
|
||||
|
||||
return { workspaces, emptyProjects: Array.from(emptyProjects.values()) };
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
import type { ProjectDescriptor, WorkspaceDescriptor } from "@/stores/session-store";
|
||||
import { buildWorkspaceStructureProjects } from "./workspace-structure";
|
||||
|
||||
function project(input: {
|
||||
id: string;
|
||||
key: string | null;
|
||||
root: string;
|
||||
name?: string;
|
||||
}): ProjectDescriptor {
|
||||
return {
|
||||
projectId: input.id,
|
||||
projectKey: input.key,
|
||||
projectDisplayName: input.name ?? "acme/app",
|
||||
projectCustomName: null,
|
||||
projectRootPath: input.root,
|
||||
projectKind: "git",
|
||||
};
|
||||
}
|
||||
|
||||
function workspace(id: string, projectId: string, root: string): WorkspaceDescriptor {
|
||||
return {
|
||||
id,
|
||||
projectId,
|
||||
projectDisplayName: "acme/app",
|
||||
projectCustomName: null,
|
||||
projectRootPath: root,
|
||||
workspaceDirectory: root,
|
||||
projectKind: "git",
|
||||
workspaceKind: "local_checkout",
|
||||
name: "main",
|
||||
status: "done",
|
||||
statusEnteredAt: null,
|
||||
archivingAt: null,
|
||||
diffStat: null,
|
||||
scripts: [],
|
||||
};
|
||||
}
|
||||
|
||||
describe("buildWorkspaceStructureProjects", () => {
|
||||
test("groups the same project key across hosts and keeps host-local ids", () => {
|
||||
const key = "remote:github.com/acme/app";
|
||||
const result = buildWorkspaceStructureProjects({
|
||||
sessions: [
|
||||
{
|
||||
serverId: "host-a",
|
||||
projects: [project({ id: "prj_a", key, root: "/a/app" })],
|
||||
workspaces: [workspace("ws-a", "prj_a", "/a/app")],
|
||||
},
|
||||
{
|
||||
serverId: "host-b",
|
||||
projects: [project({ id: "prj_b", key, root: "/b/app" })],
|
||||
workspaces: [workspace("ws-b", "prj_b", "/b/app")],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toMatchObject({
|
||||
projectKey: key,
|
||||
hosts: [
|
||||
{ serverId: "host-a", projectId: "prj_a" },
|
||||
{ serverId: "host-b", projectId: "prj_b" },
|
||||
],
|
||||
workspaceKeys: ["host-a:ws-a", "host-b:ws-b"],
|
||||
});
|
||||
});
|
||||
|
||||
test("keeps two clones with the same key on one host separate", () => {
|
||||
const key = "remote:github.com/acme/app";
|
||||
const result = buildWorkspaceStructureProjects({
|
||||
sessions: [
|
||||
{
|
||||
serverId: "host-a",
|
||||
projects: [
|
||||
project({ id: "prj_one", key, root: "/repos/one" }),
|
||||
project({ id: "prj_two", key, root: "/repos/two" }),
|
||||
],
|
||||
workspaces: [
|
||||
workspace("ws-one", "prj_one", "/repos/one"),
|
||||
workspace("ws-two", "prj_two", "/repos/two"),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result.map((item) => item.hosts[0]?.projectId).sort()).toEqual(["prj_one", "prj_two"]);
|
||||
expect(result.map((item) => item.workspaceKeys[0]).sort()).toEqual([
|
||||
"host-a:ws-one",
|
||||
"host-a:ws-two",
|
||||
]);
|
||||
});
|
||||
|
||||
test("does not let project order choose which same-host clone groups with another host", () => {
|
||||
const key = "remote:github.com/acme/app";
|
||||
const hostAProjects = [
|
||||
project({ id: "prj_one", key, root: "/repos/one" }),
|
||||
project({ id: "prj_two", key, root: "/repos/two" }),
|
||||
];
|
||||
const build = (projects: ProjectDescriptor[]) =>
|
||||
buildWorkspaceStructureProjects({
|
||||
sessions: [
|
||||
{ serverId: "host-a", projects, workspaces: [] },
|
||||
{
|
||||
serverId: "host-b",
|
||||
projects: [project({ id: "prj_remote", key, root: "/repos/remote" })],
|
||||
workspaces: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
const identitiesByProjectId = (projects: ReturnType<typeof build>) => {
|
||||
const identities: Record<string, string> = {};
|
||||
for (const group of projects) {
|
||||
for (const host of group.hosts) identities[host.projectId] = group.projectKey;
|
||||
}
|
||||
return identities;
|
||||
};
|
||||
|
||||
const forward = build(hostAProjects);
|
||||
const reversed = build(hostAProjects.toReversed());
|
||||
|
||||
expect(forward).toHaveLength(3);
|
||||
expect(identitiesByProjectId(forward)).toEqual(identitiesByProjectId(reversed));
|
||||
expect(new Set(Object.values(identitiesByProjectId(forward))).size).toBe(3);
|
||||
});
|
||||
|
||||
test("keeps projects without persisted keys scoped to their host", () => {
|
||||
const result = buildWorkspaceStructureProjects({
|
||||
sessions: [
|
||||
{
|
||||
serverId: "host-a",
|
||||
projects: [project({ id: "/workspace/app", key: null, root: "/workspace/app" })],
|
||||
workspaces: [],
|
||||
},
|
||||
{
|
||||
serverId: "host-b",
|
||||
projects: [project({ id: "/workspace/app", key: null, root: "/workspace/app" })],
|
||||
workspaces: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(new Set(result.map((item) => item.projectKey)).size).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,9 @@
|
||||
import type { EmptyProjectDescriptor, WorkspaceDescriptor } from "@/stores/session-store";
|
||||
import type { ProjectDescriptor, WorkspaceDescriptor } from "@/stores/session-store";
|
||||
import { projectDisplayNameFromProjectId } from "@/utils/project-display-name";
|
||||
|
||||
export interface WorkspaceStructureHostPlacement {
|
||||
serverId: string;
|
||||
projectId: string;
|
||||
iconWorkingDir: string;
|
||||
canCreateWorktree: boolean;
|
||||
}
|
||||
@@ -20,126 +21,55 @@ export interface WorkspaceStructure {
|
||||
projects: WorkspaceStructureProject[];
|
||||
}
|
||||
|
||||
function compareWorkspaceStructureItems(
|
||||
left: { workspaceId: string; workspaceName: string },
|
||||
right: { workspaceId: string; workspaceName: string },
|
||||
): number {
|
||||
const nameDelta = left.workspaceName.localeCompare(right.workspaceName, undefined, {
|
||||
numeric: true,
|
||||
sensitivity: "base",
|
||||
});
|
||||
if (nameDelta !== 0) {
|
||||
return nameDelta;
|
||||
}
|
||||
|
||||
return left.workspaceId.localeCompare(right.workspaceId, undefined, {
|
||||
sensitivity: "base",
|
||||
});
|
||||
}
|
||||
|
||||
function compareWorkspaceStructureProjects(
|
||||
left: WorkspaceStructureProject,
|
||||
right: WorkspaceStructureProject,
|
||||
): number {
|
||||
return left.projectName.localeCompare(right.projectName, undefined, {
|
||||
numeric: true,
|
||||
sensitivity: "base",
|
||||
});
|
||||
}
|
||||
|
||||
function canCreateWorktreeForProjectKind(projectKind: WorkspaceDescriptor["projectKind"]): boolean {
|
||||
return projectKind === "git";
|
||||
}
|
||||
|
||||
interface WorkspaceStructureSession {
|
||||
serverId: string;
|
||||
projects: Iterable<ProjectDescriptor>;
|
||||
workspaces: Iterable<WorkspaceDescriptor>;
|
||||
emptyProjects?: Iterable<EmptyProjectDescriptor>;
|
||||
}
|
||||
|
||||
interface ProjectDraft {
|
||||
projectKey: string;
|
||||
projectName: string;
|
||||
hasCustomName: boolean;
|
||||
projectKind: WorkspaceDescriptor["projectKind"];
|
||||
iconWorkingDir: string;
|
||||
hosts: Map<string, WorkspaceStructureHostPlacement>;
|
||||
workspaces: Array<{ workspaceId: string; workspaceName: string; workspaceKey: string }>;
|
||||
}
|
||||
|
||||
/** The single app boundary that turns host-local projects into grouped display projects. */
|
||||
export function buildWorkspaceStructureProjects(input: {
|
||||
sessions: WorkspaceStructureSession[];
|
||||
}): WorkspaceStructureProject[] {
|
||||
const byProject = new Map<
|
||||
string,
|
||||
{
|
||||
projectKey: string;
|
||||
projectName: string;
|
||||
projectKind: WorkspaceDescriptor["projectKind"];
|
||||
iconWorkingDir: string;
|
||||
hosts: Map<string, WorkspaceStructureHostPlacement>;
|
||||
workspaces: Array<{ workspaceId: string; workspaceName: string; workspaceKey: string }>;
|
||||
}
|
||||
>();
|
||||
const byProject = new Map<string, ProjectDraft>();
|
||||
const projectEntries: Array<{ serverId: string; project: ProjectDescriptor }> = [];
|
||||
const keyCountsByServer = new Map<string, Map<string, number>>();
|
||||
const viewKeyByServerProjectId = new Map<string, Map<string, string>>();
|
||||
|
||||
for (const session of input.sessions) {
|
||||
for (const emptyProject of session.emptyProjects ?? []) {
|
||||
const projectKey = emptyProject.projectId;
|
||||
const placement = {
|
||||
serverId: session.serverId,
|
||||
iconWorkingDir: emptyProject.projectRootPath,
|
||||
canCreateWorktree: canCreateWorktreeForProjectKind(emptyProject.projectKind),
|
||||
};
|
||||
const existing = byProject.get(projectKey);
|
||||
|
||||
if (!existing) {
|
||||
byProject.set(projectKey, {
|
||||
projectKey,
|
||||
projectName:
|
||||
emptyProject.projectCustomName ??
|
||||
emptyProject.projectDisplayName ??
|
||||
projectDisplayNameFromProjectId(projectKey),
|
||||
projectKind: emptyProject.projectKind,
|
||||
iconWorkingDir: emptyProject.projectRootPath,
|
||||
hosts: new Map([[session.serverId, placement]]),
|
||||
workspaces: [],
|
||||
});
|
||||
continue;
|
||||
for (const project of session.projects) {
|
||||
projectEntries.push({ serverId: session.serverId, project });
|
||||
const sharedKey = project.projectKey ?? null;
|
||||
if (sharedKey) {
|
||||
const counts = getOrCreate(keyCountsByServer, session.serverId, () => new Map());
|
||||
counts.set(sharedKey, (counts.get(sharedKey) ?? 0) + 1);
|
||||
}
|
||||
|
||||
existing.hosts.set(session.serverId, placement);
|
||||
}
|
||||
}
|
||||
|
||||
for (const { serverId, project } of projectEntries) {
|
||||
const projectKey = addProjectToView({ byProject, keyCountsByServer, serverId, project });
|
||||
getOrCreate(viewKeyByServerProjectId, serverId, () => new Map()).set(
|
||||
project.projectId,
|
||||
projectKey,
|
||||
);
|
||||
}
|
||||
|
||||
for (const session of input.sessions) {
|
||||
for (const workspace of session.workspaces) {
|
||||
const projectKey = workspace.project?.projectKey ?? workspace.projectId;
|
||||
const existing = byProject.get(projectKey);
|
||||
|
||||
if (!existing) {
|
||||
byProject.set(projectKey, {
|
||||
projectKey,
|
||||
projectName:
|
||||
workspace.projectCustomName ??
|
||||
workspace.projectDisplayName ??
|
||||
projectDisplayNameFromProjectId(projectKey),
|
||||
projectKind: workspace.projectKind,
|
||||
iconWorkingDir: workspace.projectRootPath,
|
||||
hosts: new Map([
|
||||
[
|
||||
session.serverId,
|
||||
{
|
||||
serverId: session.serverId,
|
||||
iconWorkingDir: workspace.projectRootPath,
|
||||
canCreateWorktree: canCreateWorktreeForProjectKind(workspace.projectKind),
|
||||
},
|
||||
],
|
||||
]),
|
||||
workspaces: [
|
||||
{
|
||||
workspaceId: workspace.id,
|
||||
workspaceName: workspace.name,
|
||||
workspaceKey: `${session.serverId}:${workspace.id}`,
|
||||
},
|
||||
],
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
existing.hosts.set(session.serverId, {
|
||||
serverId: session.serverId,
|
||||
iconWorkingDir: workspace.projectRootPath,
|
||||
canCreateWorktree: canCreateWorktreeForProjectKind(workspace.projectKind),
|
||||
});
|
||||
existing.workspaces.push({
|
||||
const projectKey = viewKeyByServerProjectId.get(session.serverId)?.get(workspace.projectId);
|
||||
if (!projectKey) continue;
|
||||
byProject.get(projectKey)?.workspaces.push({
|
||||
workspaceId: workspace.id,
|
||||
workspaceName: workspace.name,
|
||||
workspaceKey: `${session.serverId}:${workspace.id}`,
|
||||
@@ -147,19 +77,82 @@ export function buildWorkspaceStructureProjects(input: {
|
||||
}
|
||||
}
|
||||
|
||||
const projects: WorkspaceStructureProject[] = [];
|
||||
for (const raw of byProject.values()) {
|
||||
const sortedWorkspaces = [...raw.workspaces].sort(compareWorkspaceStructureItems);
|
||||
projects.push({
|
||||
projectKey: raw.projectKey,
|
||||
projectName: raw.projectName,
|
||||
projectKind: raw.projectKind,
|
||||
iconWorkingDir: raw.iconWorkingDir,
|
||||
hosts: Array.from(raw.hosts.values()),
|
||||
workspaceKeys: sortedWorkspaces.map((w) => w.workspaceKey),
|
||||
return Array.from(byProject.values())
|
||||
.map((draft) => ({
|
||||
projectKey: draft.projectKey,
|
||||
projectName: draft.projectName,
|
||||
projectKind: draft.projectKind,
|
||||
iconWorkingDir: draft.iconWorkingDir,
|
||||
hosts: Array.from(draft.hosts.values()),
|
||||
workspaceKeys: draft.workspaces
|
||||
.sort(compareWorkspaceStructureItems)
|
||||
.map((workspace) => workspace.workspaceKey),
|
||||
}))
|
||||
.sort((left, right) =>
|
||||
left.projectName.localeCompare(right.projectName, undefined, {
|
||||
numeric: true,
|
||||
sensitivity: "base",
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function addProjectToView(input: {
|
||||
byProject: Map<string, ProjectDraft>;
|
||||
keyCountsByServer: Map<string, Map<string, number>>;
|
||||
serverId: string;
|
||||
project: ProjectDescriptor;
|
||||
}): string {
|
||||
const { byProject, keyCountsByServer, serverId, project } = input;
|
||||
const sharedKey = project.projectKey ?? null;
|
||||
const canUseSharedKey =
|
||||
sharedKey !== null && keyCountsByServer.get(serverId)?.get(sharedKey) === 1;
|
||||
const projectKey = canUseSharedKey ? sharedKey : JSON.stringify([serverId, project.projectId]);
|
||||
const placement: WorkspaceStructureHostPlacement = {
|
||||
serverId,
|
||||
projectId: project.projectId,
|
||||
iconWorkingDir: project.projectRootPath,
|
||||
canCreateWorktree: project.projectKind === "git",
|
||||
};
|
||||
const draft = byProject.get(projectKey);
|
||||
if (!draft) {
|
||||
byProject.set(projectKey, {
|
||||
projectKey,
|
||||
projectName:
|
||||
project.projectCustomName ??
|
||||
project.projectDisplayName ??
|
||||
projectDisplayNameFromProjectId(project.projectId),
|
||||
hasCustomName: Boolean(project.projectCustomName),
|
||||
projectKind: project.projectKind,
|
||||
iconWorkingDir: project.projectRootPath,
|
||||
hosts: new Map([[serverId, placement]]),
|
||||
workspaces: [],
|
||||
});
|
||||
} else {
|
||||
if (project.projectCustomName && !draft.hasCustomName) {
|
||||
draft.projectName = project.projectCustomName;
|
||||
draft.hasCustomName = true;
|
||||
}
|
||||
draft.hosts.set(serverId, placement);
|
||||
}
|
||||
return projectKey;
|
||||
}
|
||||
|
||||
function getOrCreate<K, V>(map: Map<K, V>, key: K, create: () => V): V {
|
||||
const existing = map.get(key);
|
||||
if (existing !== undefined) return existing;
|
||||
const value = create();
|
||||
map.set(key, value);
|
||||
return value;
|
||||
}
|
||||
|
||||
projects.sort(compareWorkspaceStructureProjects);
|
||||
return projects;
|
||||
function compareWorkspaceStructureItems(
|
||||
left: { workspaceId: string; workspaceName: string },
|
||||
right: { workspaceId: string; workspaceName: string },
|
||||
): number {
|
||||
return (
|
||||
left.workspaceName.localeCompare(right.workspaceName, undefined, {
|
||||
numeric: true,
|
||||
sensitivity: "base",
|
||||
}) || left.workspaceId.localeCompare(right.workspaceId, undefined, { sensitivity: "base" })
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user