Skip to content

Commit 4cf46da

Browse files
committed
fix(web): keep the selected environment when changing projects
1 parent 163d508 commit 4cf46da

3 files changed

Lines changed: 81 additions & 7 deletions

File tree

‎apps/web/src/environmentGrouping.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,74 @@ describe("environment grouping", () => {
323323
expect(entries[1]?.group.displayName).toBe("separate");
324324
});
325325

326+
it("keeps the current environment when available and falls back otherwise", () => {
327+
const currentPrimary = makeProject({ repositoryIdentity });
328+
const currentRemote = makeProject({
329+
id: ProjectId.make("current-remote"),
330+
environmentId: remoteEnvironmentId,
331+
repositoryIdentity,
332+
});
333+
const destinationRepositoryIdentity = {
334+
canonicalKey: "github.com/example/destination",
335+
locator: {
336+
source: "git-remote" as const,
337+
remoteName: "origin",
338+
remoteUrl: "https://github.com/example/destination.git",
339+
},
340+
};
341+
const destinationPrimary = makeProject({
342+
id: ProjectId.make("destination-primary"),
343+
title: "destination",
344+
workspaceRoot: "/tmp/destination",
345+
repositoryIdentity: destinationRepositoryIdentity,
346+
});
347+
const destinationRemote = makeProject({
348+
id: ProjectId.make("destination-remote"),
349+
environmentId: remoteEnvironmentId,
350+
title: "destination",
351+
workspaceRoot: "/remote/destination",
352+
repositoryIdentity: destinationRepositoryIdentity,
353+
});
354+
const fallbackPrimary = makeProject({
355+
id: ProjectId.make("fallback-primary"),
356+
title: "fallback",
357+
workspaceRoot: "/tmp/fallback",
358+
});
359+
const groups = buildSidebarProjectSnapshots({
360+
projects: [
361+
currentPrimary,
362+
currentRemote,
363+
destinationPrimary,
364+
destinationRemote,
365+
fallbackPrimary,
366+
],
367+
settings: defaultGroupingSettings,
368+
primaryEnvironmentId,
369+
resolveEnvironmentLabel: () => null,
370+
});
371+
372+
const entries = buildSidebarProjectPickerEntries({
373+
groups,
374+
preferredProjectRef: {
375+
environmentId: remoteEnvironmentId,
376+
projectId: currentRemote.id,
377+
},
378+
});
379+
const destination = entries.find(
380+
(entry) => entry.group.projectKey === destinationRepositoryIdentity.canonicalKey,
381+
);
382+
const fallback = entries.find((entry) => entry.group.displayName === "fallback");
383+
384+
expect(destination?.targetProject).toMatchObject({
385+
environmentId: remoteEnvironmentId,
386+
id: destinationRemote.id,
387+
});
388+
expect(fallback?.targetProject).toMatchObject({
389+
environmentId: primaryEnvironmentId,
390+
id: fallbackPrimary.id,
391+
});
392+
});
393+
326394
it("keeps manual project order when building grouped sidebar entries", () => {
327395
const primary = makeProject({ repositoryIdentity });
328396
const remote = makeProject({

‎apps/web/src/sidebarProjectGrouping.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,23 @@ export function buildSidebarProjectPickerEntries(input: {
118118
groups: ReadonlyArray<SidebarProjectSnapshot>;
119119
preferredProjectRef: ScopedProjectRef | null;
120120
}) {
121+
const preferredProjectRef = input.preferredProjectRef;
121122
const entries = input.groups.flatMap((group): SidebarProjectPickerEntry[] => {
122-
const isPreferred = input.preferredProjectRef
123+
const isPreferred = preferredProjectRef
123124
? group.memberProjectRefs.some(
124125
(projectRef) =>
125-
projectRef.environmentId === input.preferredProjectRef?.environmentId &&
126-
projectRef.projectId === input.preferredProjectRef.projectId,
126+
projectRef.environmentId === preferredProjectRef.environmentId &&
127+
projectRef.projectId === preferredProjectRef.projectId,
127128
)
128129
: false;
129-
const preferredProject = isPreferred
130+
const preferredProject = preferredProjectRef
130131
? (group.memberProjects.find(
131132
(project) =>
132-
project.environmentId === input.preferredProjectRef?.environmentId &&
133-
project.id === input.preferredProjectRef?.projectId,
133+
project.environmentId === preferredProjectRef.environmentId &&
134+
project.id === preferredProjectRef.projectId,
134135
) ??
135136
group.memberProjects.find(
136-
(project) => project.environmentId === input.preferredProjectRef?.environmentId,
137+
(project) => project.environmentId === preferredProjectRef.environmentId,
137138
))
138139
: null;
139140
const targetProject =

‎docs/user/composer.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ On mobile, the model picker shows each OpenCode model's upstream provider, such
5252
GitHub Copilot, or OpenCode Zen, beneath its name. Search by that provider name to narrow the list
5353
when starting a thread or changing an existing thread's model.
5454

55+
## Changing projects
56+
57+
On web and desktop, changing the project from a new thread keeps the current environment when that
58+
project exists there. If it does not, T3 Code selects another environment that has the project.
59+
5560
## Notices above the composer
5661

5762
On web and desktop, loading and syncing statuses fill the available banner width beside the

0 commit comments

Comments
 (0)