From f69b512082b1ba40feb83fa336aa2d635a894cbd Mon Sep 17 00:00:00 2001 From: Niladri Adhikary <91966855+niladrix719@users.noreply.github.com> Date: Thu, 15 Jan 2026 19:52:45 +0530 Subject: [PATCH] fix: persist environment tab data when switching between tabs (#9138) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: persist environment tab data when switching between tabs Signed-off-by: “niladrix719” * Update packages/insomnia/src/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId.environment.tsx Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: use memo wrap dependence --------- Signed-off-by: “niladrix719” Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Curry Yang <163384738+CurryYangxx@users.noreply.github.com> Co-authored-by: Curry Yang <1019yanglu@gmail.com> --- ...projectId.workspace.$workspaceId.environment.tsx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/insomnia/src/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId.environment.tsx b/packages/insomnia/src/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId.environment.tsx index 17510292ea..14ba79c814 100644 --- a/packages/insomnia/src/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId.environment.tsx +++ b/packages/insomnia/src/routes/organization.$organizationId.project.$projectId.workspace.$workspaceId.environment.tsx @@ -1,5 +1,5 @@ import type { IconName, IconProp } from '@fortawesome/fontawesome-svg-core'; -import React, { Fragment, useEffect, useRef, useState } from 'react'; +import React, { Fragment, useEffect, useMemo, useRef, useState } from 'react'; import { Breadcrumb, Breadcrumbs, @@ -88,7 +88,16 @@ const Component = ({ loaderData, params }: Route.ComponentProps) => { const isUsingInsomniaCloudSync = Boolean(isRemoteProject(activeProject) && !activeWorkspaceMeta?.gitRepositoryId); const isUsingGitSync = Boolean(features.gitSync.enabled && activeWorkspaceMeta?.gitRepositoryId); - const allEnvironment = [baseEnvironment, ...subEnvironments]; + const allEnvironment = useMemo(() => { + return [baseEnvironment, ...subEnvironments]; + }, [baseEnvironment, subEnvironments]); + + // Keep selectedEnvironmentId in sync when navigating between different environment workspaces/tabs. + useEffect(() => { + if (!allEnvironment.find(env => env._id === selectedEnvironmentId)) { + setSelectedEnvironmentId(activeEnvironment._id); + } + }, [selectedEnvironmentId, activeEnvironment._id, allEnvironment]); const selectedEnvironment = allEnvironment.find(env => env._id === selectedEnvironmentId); // Do not allowed to switch to json environment if contains secret item const allowSwitchEnvironment = !selectedEnvironment?.kvPairData?.some(