Skip to content
TypeScript

fix(web): keep the selected environment when changing projects (#9102)

Theo Brownec0995d2eaf8e
diff --git a/apps/web/src/environmentGrouping.test.ts b/apps/web/src/environmentGrouping.test.ts
index 9029f1204d3..9bd7a3e9242 100644
--- a/apps/web/src/environmentGrouping.test.ts
+++ b/apps/web/src/environmentGrouping.test.ts
@@ -323,6 +323,74 @@ describe("environment grouping", () => {
expect(entries[1]?.group.displayName).toBe("separate");
});
+ it("keeps the current environment when available and falls back otherwise", () => {
+ const currentPrimary = makeProject({ repositoryIdentity });
+ const currentRemote = makeProject({
+ id: ProjectId.make("current-remote"),
+ environmentId: remoteEnvironmentId,
+ repositoryIdentity,
+ });
+ const destinationRepositoryIdentity = {
+ canonicalKey: "github.com/example/destination",
+ locator: {
+ source: "git-remote" as const,
+ remoteName: "origin",
+ remoteUrl: "https://github.com/example/destination.git",
+ },
+ };
+ const destinationPrimary = makeProject({
+ id: ProjectId.make("destination-primary"),
+ title: "destination",
+ workspaceRoot: "/tmp/destination",
+ repositoryIdentity: destinationRepositoryIdentity,
+ });
+ const destinationRemote = makeProject({
+ id: ProjectId.make("destination-remote"),
+ environmentId: remoteEnvironmentId,
+ title: "destination",
+ workspaceRoot: "/remote/destination",
+ repositoryIdentity: destinationRepositoryIdentity,
+ });
+ const fallbackPrimary = makeProject({
+ id: ProjectId.make("fallback-primary"),
+ title: "fallback",
+ workspaceRoot: "/tmp/fallback",
+ });
+ const groups = buildSidebarProjectSnapshots({
+ projects: [
+ currentPrimary,
+ currentRemote,
+ destinationPrimary,
+ destinationRemote,
+ fallbackPrimary,
+ ],
+ settings: defaultGroupingSettings,
+ primaryEnvironmentId,
+ resolveEnvironmentLabel: () => null,
+ });
+
+ const entries = buildSidebarProjectPickerEntries({
+ groups,
+ preferredProjectRef: {
+ environmentId: remoteEnvironmentId,
+ projectId: currentRemote.id,
+ },
+ });
+ const destination = entries.find(
+ (entry) => entry.group.projectKey === destinationRepositoryIdentity.canonicalKey,
+ );
+ const fallback = entries.find((entry) => entry.group.displayName === "fallback");
+
+ expect(destination?.targetProject).toMatchObject({
+ environmentId: remoteEnvironmentId,
+ id: destinationRemote.id,
+ });
+ expect(fallback?.targetProject).toMatchObject({
+ environmentId: primaryEnvironmentId,
+ id: fallbackPrimary.id,
+ });
+ });
+
it("keeps manual project order when building grouped sidebar entries", () => {
const primary = makeProject({ repositoryIdentity });
const remote = makeProject({
diff --git a/apps/web/src/sidebarProjectGrouping.ts b/apps/web/src/sidebarProjectGrouping.ts
index be92fcaee84..8cf3c5665ac 100644
--- a/apps/web/src/sidebarProjectGrouping.ts
+++ b/apps/web/src/sidebarProjectGrouping.ts
@@ -118,22 +118,23 @@ export function buildSidebarProjectPickerEntries(input: {
groups: ReadonlyArray<SidebarProjectSnapshot>;
preferredProjectRef: ScopedProjectRef | null;
}) {
+ const preferredProjectRef = input.preferredProjectRef;
const entries = input.groups.flatMap((group): SidebarProjectPickerEntry[] => {
- const isPreferred = input.preferredProjectRef
+ const isPreferred = preferredProjectRef
? group.memberProjectRefs.some(
(projectRef) =>
- projectRef.environmentId === input.preferredProjectRef?.environmentId &&
- projectRef.projectId === input.preferredProjectRef.projectId,
+ projectRef.environmentId === preferredProjectRef.environmentId &&
+ projectRef.projectId === preferredProjectRef.projectId,
)
: false;
- const preferredProject = isPreferred
+ const preferredProject = preferredProjectRef
? (group.memberProjects.find(
(project) =>
- project.environmentId === input.preferredProjectRef?.environmentId &&
- project.id === input.preferredProjectRef?.projectId,
+ project.environmentId === preferredProjectRef.environmentId &&
+ project.id === preferredProjectRef.projectId,
) ??
group.memberProjects.find(
- (project) => project.environmentId === input.preferredProjectRef?.environmentId,
+ (project) => project.environmentId === preferredProjectRef.environmentId,
))
: null;
const targetProject =
diff --git a/docs/user/composer.md b/docs/user/composer.md
index 86525f7e186..97f1f5cb14f 100644
--- a/docs/user/composer.md
+++ b/docs/user/composer.md
@@ -52,6 +52,11 @@ On mobile, the model picker shows each OpenCode model's upstream provider, such
GitHub Copilot, or OpenCode Zen, beneath its name. Search by that provider name to narrow the list
when starting a thread or changing an existing thread's model.
+## Changing projects
+
+On web and desktop, changing the project from a new thread keeps the current environment when that
+project exists there. If it does not, T3 Code selects another environment that has the project.
+
## Notices above the composer
On web and desktop, loading and syncing statuses fill the available banner width beside the