Skip to content

Commit

Permalink
[navigation]feat: remember state when expand / collapse left nav (#8286)
Browse files Browse the repository at this point in the history
* feat: remember state

Signed-off-by: SuZhou-Joe <[email protected]>

* Changeset file for PR #8286 created/updated

* feat: optimize code

Signed-off-by: SuZhou-Joe <[email protected]>

* feat: update

Signed-off-by: SuZhou-Joe <[email protected]>

---------

Signed-off-by: SuZhou-Joe <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 83d5fba commit a663a84
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8286.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [navigation] remember state when expand / collapse left nav ([#8286](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8286))
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe('<CollapsibleNavGroupEnabled />', () => {
appId$: new BehaviorSubject('test'),
basePath: mockBasePath,
id: 'collapsibe-nav',
isLocked: false,
isNavOpen: false,
currentWorkspace$: new BehaviorSubject<WorkspaceObject | null>({ id: 'test', name: 'test' }),
navLinks$: new BehaviorSubject([
Expand All @@ -94,7 +93,6 @@ describe('<CollapsibleNavGroupEnabled />', () => {
...(props?.navLinks || []),
]),
storage: new StubBrowserStorage(),
onIsLockedUpdate: () => {},
closeNav: () => {},
navigateToApp: () => Promise.resolve(),
navigateToUrl: () => Promise.resolve(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { ChromeNavControl, ChromeNavLink } from '../..';
import { AppCategory, NavGroupType } from '../../../../types';
import { InternalApplicationStart } from '../../../application/types';
import { HttpStart } from '../../../http';
import { OnIsLockedUpdate } from './';
import { createEuiListItem } from './nav_link';
import type { Logos } from '../../../../common/types';
import {
Expand All @@ -42,11 +41,9 @@ export interface CollapsibleNavGroupEnabledProps {
collapsibleNavHeaderRender?: () => JSX.Element | null;
basePath: HttpStart['basePath'];
id: string;
isLocked: boolean;
isNavOpen: boolean;
navLinks$: Rx.Observable<ChromeNavLink[]>;
storage?: Storage;
onIsLockedUpdate: OnIsLockedUpdate;
closeNav: () => void;
navigateToApp: InternalApplicationStart['navigateToApp'];
navigateToUrl: InternalApplicationStart['navigateToUrl'];
Expand Down Expand Up @@ -80,10 +77,8 @@ enum NavWidth {
export function CollapsibleNavGroupEnabled({
basePath,
id,
isLocked,
isNavOpen,
storage = window.localStorage,
onIsLockedUpdate,
closeNav,
navigateToApp,
navigateToUrl,
Expand Down
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,
useUpdatedHeader: true,
onIsLockedUpdate: jest.fn(),
};
const component = mountWithIntl(<Header {...props} />);
component.find(EuiHeaderSectionItemButton).first().simulate('click');
expect(props.onIsLockedUpdate).toBeCalledWith(true);
});
});
22 changes: 18 additions & 4 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 @@ -153,7 +153,7 @@ export function Header({
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 [isNavOpenState, setIsNavOpenState] = useState(false);
const sidecarConfig = useObservable(observables.sidecarConfig$, undefined);
const breadcrumbs = useObservable(observables.breadcrumbs$, []);

Expand All @@ -177,6 +177,22 @@ export function Header({
return getOsdSidecarPaddingStyle(sidecarConfig);
}, [sidecarConfig]);

const isNavOpen = useUpdatedHeader ? isLocked : isNavOpenState;

const setIsNavOpen = useCallback(
(value) => {
/**
* When use updated header, we will regard the lock state as source of truth
*/
if (useUpdatedHeader) {
onIsLockedUpdate(value);
} else {
setIsNavOpenState(value);
}
},
[setIsNavOpenState, onIsLockedUpdate, useUpdatedHeader]
);

if (!isVisible) {
return <LoadingIndicator loadingCount$={observables.loadingCount$} showAsBar />;
}
Expand Down Expand Up @@ -616,13 +632,11 @@ export function Header({
appId$={application.currentAppId$}
collapsibleNavHeaderRender={collapsibleNavHeaderRender}
id={navId}
isLocked={isLocked}
navLinks$={observables.navLinks$}
isNavOpen={isNavOpen}
basePath={basePath}
navigateToApp={application.navigateToApp}
navigateToUrl={application.navigateToUrl}
onIsLockedUpdate={onIsLockedUpdate}
closeNav={() => {
setIsNavOpen(false);
if (toggleCollapsibleNavRef.current) {
Expand Down

0 comments on commit a663a84

Please sign in to comment.