Skip to content

Commit

Permalink
feat: remember state
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Sep 24, 2024
1 parent a12cb78 commit e58a9c0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/core/public/chrome/ui/header/header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,19 @@ describe('Header', () => {
expect(component.find('[data-test-subj="headerRightControl"]').exists()).toBeFalsy();
expect(component).toMatchSnapshot();
});

it('should remember the collapse state when new nav is enabled', () => {
const branding = {
useExpandedHeader: false,
};
const props = {
...mockProps(),
branding,
navGroupEnabled: true,
storage: new StubBrowserStorage(),
};
const component = mountWithIntl(<Header {...props} />);
component.find(EuiHeaderSectionItemButton).first().simulate('click');
expect(props.storage.getItem('core.leftNav.navGroupEnabled-true.isNavOpen')).toEqual('true');
});
});
19 changes: 17 additions & 2 deletions src/core/public/chrome/ui/header/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import classnames from 'classnames';
import React, { createRef, useMemo, useState } from 'react';
import React, { createRef, useCallback, useMemo, useState } from 'react';
import useObservable from 'react-use/lib/useObservable';
import { Observable } from 'rxjs';
import { LoadingIndicator } from '../';
Expand Down Expand Up @@ -122,6 +122,7 @@ export interface HeaderProps {
workspaceList$: Observable<WorkspaceObject[]>;
currentWorkspace$: WorkspacesStart['currentWorkspace$'];
useUpdatedHeader?: boolean;
storage?: Storage;
}

const hasValue = (value: any) => {
Expand All @@ -147,12 +148,16 @@ export function Header({
navGroupEnabled,
setCurrentNavGroup,
useUpdatedHeader,
storage = window.localStorage,
...observables
}: HeaderProps) {
const storageKey = `core.leftNav.navGroupEnabled-${!!navGroupEnabled}.isNavOpen`;
const isVisible = useObservable(observables.isVisible$, false);
const headerVariant = useObservable(observables.headerVariant$, HeaderVariant.PAGE);
const isLocked = useObservable(observables.isLocked$, false);
const [isNavOpen, setIsNavOpen] = useState(false);
const [isNavOpen, setIsNavOpenState] = useState(
localStorage.getItem(storageKey) === 'true' ? true : false
);
const sidecarConfig = useObservable(observables.sidecarConfig$, undefined);
const breadcrumbs = useObservable(observables.breadcrumbs$, []);
const currentWorkspace = useObservable(observables.currentWorkspace$, undefined);
Expand All @@ -161,6 +166,16 @@ export function Header({
return getOsdSidecarPaddingStyle(sidecarConfig);
}, [sidecarConfig]);

const setIsNavOpen = useCallback(
(value) => {
setIsNavOpenState(value);
if (navGroupEnabled) {
storage.setItem(storageKey, JSON.stringify(value));
}
},
[setIsNavOpenState, navGroupEnabled, storageKey, storage]
);

if (!isVisible) {
return <LoadingIndicator loadingCount$={observables.loadingCount$} showAsBar />;
}
Expand Down

0 comments on commit e58a9c0

Please sign in to comment.