From 6720461ea1005f2d1226c48343f0055b5be6a351 Mon Sep 17 00:00:00 2001 From: Talisson Date: Wed, 22 Apr 2026 09:29:28 -0300 Subject: [PATCH] Revert "fix: prevent infinite API loop on invalid environment URLs (#7284)" This reverts commit de7d0a71bbb239b458bdd78587a72adffddf75bb. --- .../components/EnvironmentReadyChecker.tsx | 80 +++++++------------ .../navigation/EnvironmentAside.tsx | 14 +--- .../navigation/navbars/EnvironmentNavbar.tsx | 42 +++++----- 3 files changed, 50 insertions(+), 86 deletions(-) diff --git a/frontend/web/components/EnvironmentReadyChecker.tsx b/frontend/web/components/EnvironmentReadyChecker.tsx index 6f065fe66be1..9e32893f2bf9 100644 --- a/frontend/web/components/EnvironmentReadyChecker.tsx +++ b/frontend/web/components/EnvironmentReadyChecker.tsx @@ -1,9 +1,8 @@ -import { PropsWithChildren } from 'react' +import { PropsWithChildren, useEffect, useState } from 'react' import { useGetEnvironmentQuery } from 'common/services/useEnvironment' import { useRouteMatch } from 'react-router-dom' interface RouteParams { - projectId?: string environmentId?: string } @@ -11,61 +10,44 @@ type EnvironmentReadyCheckerType = { children: React.ReactNode } -const POLL_INTERVAL_MS = 1000 - -const LoadingState = () => ( -
- -
-) - -const CreatingState = () => ( -
-
- -

Preparing your environment

-

We are setting up your new environment...

-
-
-) - -const NotFoundState = () => ( -
-

Environment not found

-

- This environment may have been deleted, or you may not have permission to - access it. Check the URL and try again. -

-
-) - const EnvironmentReadyChecker = ({ children, }: PropsWithChildren) => { const match = useRouteMatch() - const environmentId = match?.params?.environmentId - const projectId = match?.params?.projectId - // 'create' is the new-env form route sentinel, not an env ID. - const hasEnvironmentId = !!environmentId && environmentId !== 'create' + const [environmentCreated, setEnvironmentCreated] = useState(false) - const { data, isError } = useGetEnvironmentQuery( - { id: environmentId || '' }, + const { data, isLoading } = useGetEnvironmentQuery( + { + id: match?.params?.environmentId || '', + }, { - pollingInterval: data?.is_creating ? POLL_INTERVAL_MS : 0, - skip: !hasEnvironmentId, + pollingInterval: 1000, + skip: !match?.params?.environmentId || environmentCreated, }, ) - - // Env-by-api_key endpoint isn't project-scoped — verify the match client-side. - const wrongProject = - !!data && !!projectId && data.project !== Number(projectId) - const environmentNotFound = hasEnvironmentId && (wrongProject || isError) - - if (!hasEnvironmentId) return children - if (environmentNotFound) return - if (!data) return - if (data.is_creating) return - return children + useEffect(() => { + if (!!data && !data?.is_creating) { + setEnvironmentCreated(true) + } + }, [data]) + if (!match?.params?.environmentId) { + return children + } + return isLoading ? ( +
+ +
+ ) : data?.is_creating ? ( +
+
+ +

Preparing your environment

+

We are setting up your new environment...

+
+
+ ) : ( + children + ) } export default EnvironmentReadyChecker diff --git a/frontend/web/components/navigation/EnvironmentAside.tsx b/frontend/web/components/navigation/EnvironmentAside.tsx index 45ec3eda32a1..07fa75f28111 100644 --- a/frontend/web/components/navigation/EnvironmentAside.tsx +++ b/frontend/web/components/navigation/EnvironmentAside.tsx @@ -16,12 +16,11 @@ import AppActions from 'common/dispatcher/app-actions' import EnvironmentSelect from 'components/EnvironmentSelect' import { components } from 'react-select' import BuildVersion from 'components/BuildVersion' -import { useGetEnvironmentsQuery } from 'common/services/useEnvironment' import { useGetHealthEventsQuery } from 'common/services/useHealthEvents' import Constants from 'common/constants' import EnvironmentNavbar from './navbars/EnvironmentNavbar' import OverflowNav from './OverflowNav' -import { ProjectPermission } from 'common/types/permissions.types' +import { ProjectPermission } from 'common/types/permissions.types'; type HomeAsideType = { environmentId: string @@ -103,8 +102,6 @@ const CustomSingleValue = ({ hasWarning, ...rest }: CustomSingleValueProps) => { const EnvironmentAside: FC = ({ environmentId, projectId }) => { const history = useHistory() - const { data: environments, isSuccess: environmentsLoaded } = - useGetEnvironmentsQuery({ projectId }, { skip: !projectId }) const { data: healthEvents } = useGetHealthEventsQuery( { projectId: projectId }, { skip: !projectId }, @@ -129,18 +126,9 @@ const EnvironmentAside: FC = ({ environmentId, projectId }) => { ? null : (ProjectStore.getEnvironment(environmentId) as any) - const environmentNotFound = - environmentId !== 'create' && - environmentsLoaded && - !environments?.results?.some((env) => env.api_key === environmentId) - const onProjectSave = () => { AppActions.refreshOrganisation() } - - if (environmentNotFound) { - return null - } return ( <> diff --git a/frontend/web/components/navigation/navbars/EnvironmentNavbar.tsx b/frontend/web/components/navigation/navbars/EnvironmentNavbar.tsx index 15ee18c8d47d..3dccf9503c7d 100644 --- a/frontend/web/components/navigation/navbars/EnvironmentNavbar.tsx +++ b/frontend/web/components/navigation/navbars/EnvironmentNavbar.tsx @@ -21,35 +21,29 @@ const EnvironmentNavbar: FC = ({ }) => { const date = useRef(moment().toISOString()) - const { data: environments, isSuccess: environmentsLoaded } = - useGetEnvironmentsQuery({ projectId }, { skip: !projectId }) - - const environment = environments?.results?.find( - (v) => v.api_key === environmentId, - ) - - const { data: scheduledData } = useGetChangeRequestsQuery( + const { data: environments } = useGetEnvironmentsQuery( { - committed: true, - environmentId, - live_from_after: date.current, - page_size: 1, + projectId: `${projectId}`, }, - { skip: !environment }, + { skip: !projectId }, ) - const { data: changeRequestsData } = useGetChangeRequestsQuery( - { - committed: false, - environmentId, - page_size: 1, - }, - { skip: !environment }, - ) + const { data: scheduledData } = useGetChangeRequestsQuery({ + committed: true, + environmentId, + live_from_after: date.current, + page_size: 1, + }) + + const { data: changeRequestsData } = useGetChangeRequestsQuery({ + committed: false, + environmentId, + page_size: 1, + }) - if (environmentsLoaded && !environment) { - return null - } + const environment = environments?.results?.find( + (v) => v.api_key === environmentId, + ) const changeRequests = (Utils.changeRequestsEnabled(