Skip to content

Commit ea5f614

Browse files
opensearch-trigger-bot[bot]github-actions[bot]opensearch-changeset-bot[bot]
authored
[navigation]feat: remember state when expand / collapse left nav (#8286) (#8389)
* feat: remember state * Changeset file for PR #8286 created/updated * feat: optimize code * feat: update --------- (cherry picked from commit a663a84) Signed-off-by: SuZhou-Joe <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
1 parent 3d1b3bb commit ea5f614

File tree

5 files changed

+35
-11
lines changed

5 files changed

+35
-11
lines changed

changelogs/fragments/8286.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
feat:
2+
- [navigation] remember state when expand / collapse left nav ([#8286](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8286))

src/core/public/chrome/ui/header/collapsible_nav_group_enabled.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe('<CollapsibleNavGroupEnabled />', () => {
6969
appId$: new BehaviorSubject('test'),
7070
basePath: mockBasePath,
7171
id: 'collapsibe-nav',
72-
isLocked: false,
7372
isNavOpen: false,
7473
currentWorkspace$: new BehaviorSubject<WorkspaceObject | null>({ id: 'test', name: 'test' }),
7574
navLinks$: new BehaviorSubject([
@@ -94,7 +93,6 @@ describe('<CollapsibleNavGroupEnabled />', () => {
9493
...(props?.navLinks || []),
9594
]),
9695
storage: new StubBrowserStorage(),
97-
onIsLockedUpdate: () => {},
9896
closeNav: () => {},
9997
navigateToApp: () => Promise.resolve(),
10098
navigateToUrl: () => Promise.resolve(),

src/core/public/chrome/ui/header/collapsible_nav_group_enabled.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { ChromeNavControl, ChromeNavLink } from '../..';
2323
import { AppCategory, NavGroupType } from '../../../../types';
2424
import { InternalApplicationStart } from '../../../application/types';
2525
import { HttpStart } from '../../../http';
26-
import { OnIsLockedUpdate } from './';
2726
import { createEuiListItem } from './nav_link';
2827
import type { Logos } from '../../../../common/types';
2928
import {
@@ -42,11 +41,9 @@ export interface CollapsibleNavGroupEnabledProps {
4241
collapsibleNavHeaderRender?: () => JSX.Element | null;
4342
basePath: HttpStart['basePath'];
4443
id: string;
45-
isLocked: boolean;
4644
isNavOpen: boolean;
4745
navLinks$: Rx.Observable<ChromeNavLink[]>;
4846
storage?: Storage;
49-
onIsLockedUpdate: OnIsLockedUpdate;
5047
closeNav: () => void;
5148
navigateToApp: InternalApplicationStart['navigateToApp'];
5249
navigateToUrl: InternalApplicationStart['navigateToUrl'];
@@ -80,10 +77,8 @@ enum NavWidth {
8077
export function CollapsibleNavGroupEnabled({
8178
basePath,
8279
id,
83-
isLocked,
8480
isNavOpen,
8581
storage = window.localStorage,
86-
onIsLockedUpdate,
8782
closeNav,
8883
navigateToApp,
8984
navigateToUrl,

src/core/public/chrome/ui/header/header.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,19 @@ describe('Header', () => {
257257
expect(component.find('[data-test-subj="headerRightControl"]').exists()).toBeFalsy();
258258
expect(component).toMatchSnapshot();
259259
});
260+
261+
it('should remember the collapse state when new nav is enabled', () => {
262+
const branding = {
263+
useExpandedHeader: false,
264+
};
265+
const props = {
266+
...mockProps(),
267+
branding,
268+
useUpdatedHeader: true,
269+
onIsLockedUpdate: jest.fn(),
270+
};
271+
const component = mountWithIntl(<Header {...props} />);
272+
component.find(EuiHeaderSectionItemButton).first().simulate('click');
273+
expect(props.onIsLockedUpdate).toBeCalledWith(true);
274+
});
260275
});

src/core/public/chrome/ui/header/header.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
} from '@elastic/eui';
4646
import { i18n } from '@osd/i18n';
4747
import classnames from 'classnames';
48-
import React, { createRef, useMemo, useState } from 'react';
48+
import React, { createRef, useCallback, useMemo, useState } from 'react';
4949
import useObservable from 'react-use/lib/useObservable';
5050
import { Observable } from 'rxjs';
5151
import { LoadingIndicator } from '../';
@@ -153,7 +153,7 @@ export function Header({
153153
const isVisible = useObservable(observables.isVisible$, false);
154154
const headerVariant = useObservable(observables.headerVariant$, HeaderVariant.PAGE);
155155
const isLocked = useObservable(observables.isLocked$, false);
156-
const [isNavOpen, setIsNavOpen] = useState(false);
156+
const [isNavOpenState, setIsNavOpenState] = useState(false);
157157
const sidecarConfig = useObservable(observables.sidecarConfig$, undefined);
158158
const breadcrumbs = useObservable(observables.breadcrumbs$, []);
159159

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

180+
const isNavOpen = useUpdatedHeader ? isLocked : isNavOpenState;
181+
182+
const setIsNavOpen = useCallback(
183+
(value) => {
184+
/**
185+
* When use updated header, we will regard the lock state as source of truth
186+
*/
187+
if (useUpdatedHeader) {
188+
onIsLockedUpdate(value);
189+
} else {
190+
setIsNavOpenState(value);
191+
}
192+
},
193+
[setIsNavOpenState, onIsLockedUpdate, useUpdatedHeader]
194+
);
195+
180196
if (!isVisible) {
181197
return <LoadingIndicator loadingCount$={observables.loadingCount$} showAsBar />;
182198
}
@@ -616,13 +632,11 @@ export function Header({
616632
appId$={application.currentAppId$}
617633
collapsibleNavHeaderRender={collapsibleNavHeaderRender}
618634
id={navId}
619-
isLocked={isLocked}
620635
navLinks$={observables.navLinks$}
621636
isNavOpen={isNavOpen}
622637
basePath={basePath}
623638
navigateToApp={application.navigateToApp}
624639
navigateToUrl={application.navigateToUrl}
625-
onIsLockedUpdate={onIsLockedUpdate}
626640
closeNav={() => {
627641
setIsNavOpen(false);
628642
if (toggleCollapsibleNavRef.current) {

0 commit comments

Comments
 (0)