fix(sidebar): save sidebar state to local storage (#7207)

pull/7316/head
Chaim Lev-Ari 2 years ago committed by GitHub
parent 762c664948
commit ea60740d48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,5 @@
import { useCurrentStateAndParams, useRouter } from '@uirouter/react'; import { useCurrentStateAndParams, useRouter } from '@uirouter/react';
import { useEffect, useState } from 'react'; import { useEffect } from 'react';
import { X, Slash } from 'react-feather'; import { X, Slash } from 'react-feather';
import { import {
@ -9,6 +9,7 @@ import {
} from '@/portainer/environments/types'; } from '@/portainer/environments/types';
import { getPlatformType } from '@/portainer/environments/utils'; import { getPlatformType } from '@/portainer/environments/utils';
import { useEnvironment } from '@/portainer/environments/queries/useEnvironment'; import { useEnvironment } from '@/portainer/environments/queries/useEnvironment';
import { useLocalStorage } from '@/portainer/hooks/useLocalStorage';
import { getPlatformIcon } from '../portainer/environments/utils/get-platform-icon'; import { getPlatformIcon } from '../portainer/environments/utils/get-platform-icon';
@ -86,14 +87,16 @@ function Content({ environment, onClear }: ContentProps) {
function useCurrentEnvironment() { function useCurrentEnvironment() {
const { params } = useCurrentStateAndParams(); const { params } = useCurrentStateAndParams();
const router = useRouter(); const router = useRouter();
const [environmentId, setEnvironmentId] = useState<EnvironmentId>(); const [environmentId, setEnvironmentId] = useLocalStorage<
EnvironmentId | undefined
>('environmentId', undefined, sessionStorage);
useEffect(() => { useEffect(() => {
const environmentId = parseInt(params.endpointId, 10); const environmentId = parseInt(params.endpointId, 10);
if (params.endpointId && !Number.isNaN(environmentId)) { if (params.endpointId && !Number.isNaN(environmentId)) {
setEnvironmentId(environmentId); setEnvironmentId(environmentId);
} }
}, [params.endpointId]); }, [params.endpointId, setEnvironmentId]);
return { query: useEnvironment(environmentId), clearEnvironment }; return { query: useEnvironment(environmentId), clearEnvironment };

Loading…
Cancel
Save