From ea60740d48786280b8d387bda3382fef7413991b Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Thu, 28 Jul 2022 19:24:25 +0200 Subject: [PATCH] fix(sidebar): save sidebar state to local storage (#7207) --- app/react/sidebar/EnvironmentSidebar.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/react/sidebar/EnvironmentSidebar.tsx b/app/react/sidebar/EnvironmentSidebar.tsx index 2ad4bf5fb..7f894a017 100644 --- a/app/react/sidebar/EnvironmentSidebar.tsx +++ b/app/react/sidebar/EnvironmentSidebar.tsx @@ -1,5 +1,5 @@ import { useCurrentStateAndParams, useRouter } from '@uirouter/react'; -import { useEffect, useState } from 'react'; +import { useEffect } from 'react'; import { X, Slash } from 'react-feather'; import { @@ -9,6 +9,7 @@ import { } from '@/portainer/environments/types'; import { getPlatformType } from '@/portainer/environments/utils'; import { useEnvironment } from '@/portainer/environments/queries/useEnvironment'; +import { useLocalStorage } from '@/portainer/hooks/useLocalStorage'; import { getPlatformIcon } from '../portainer/environments/utils/get-platform-icon'; @@ -86,14 +87,16 @@ function Content({ environment, onClear }: ContentProps) { function useCurrentEnvironment() { const { params } = useCurrentStateAndParams(); const router = useRouter(); - const [environmentId, setEnvironmentId] = useState(); + const [environmentId, setEnvironmentId] = useLocalStorage< + EnvironmentId | undefined + >('environmentId', undefined, sessionStorage); useEffect(() => { const environmentId = parseInt(params.endpointId, 10); if (params.endpointId && !Number.isNaN(environmentId)) { setEnvironmentId(environmentId); } - }, [params.endpointId]); + }, [params.endpointId, setEnvironmentId]); return { query: useEnvironment(environmentId), clearEnvironment };