forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Navigation] Add recent items popup in top navigation (opensearch-pro…
…ject#7257) * feat: add recent items Signed-off-by: tygao <[email protected]> * test: update chrome tests Signed-off-by: tygao <[email protected]> * Changeset file for PR opensearch-project#7257 created/updated * add navGroupEnabled flag Signed-off-by: tygao <[email protected]> * use createRecentNavLink Signed-off-by: tygao <[email protected]> * update icon and add empty state Signed-off-by: tygao <[email protected]> * test: update snapshots Signed-off-by: tygao <[email protected]> * update typing Signed-off-by: tygao <[email protected]> * update name and style Signed-off-by: tygao <[email protected]> --------- Signed-off-by: tygao <[email protected]> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Co-authored-by: Yulong Ruan <[email protected]>
- Loading branch information
1 parent
c1dd5c1
commit 4f094a8
Showing
10 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feat: | ||
- Add recent items popup in top navigation ([#7257](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7257)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/core/public/chrome/ui/header/__snapshots__/header.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/core/public/chrome/ui/header/__snapshots__/recent_items.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { RecentItems, Props } from './recent_items'; | ||
import { applicationServiceMock, httpServiceMock } from '../../../mocks'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
jest.mock('./nav_link', () => ({ | ||
createRecentNavLink: jest.fn().mockImplementation(() => { | ||
return { | ||
href: '/recent_nav_link', | ||
}; | ||
}), | ||
})); | ||
|
||
const defaultMockProps = { | ||
navigateToUrl: applicationServiceMock.createStartContract().navigateToUrl, | ||
workspaceList$: new BehaviorSubject([]), | ||
recentlyAccessed$: new BehaviorSubject([]), | ||
navLinks$: new BehaviorSubject([]), | ||
basePath: httpServiceMock.createStartContract().basePath, | ||
}; | ||
const setup = (props: Props) => { | ||
return render(<RecentItems {...props} />); | ||
}; | ||
describe('Recent items', () => { | ||
it('should render base element normally', () => { | ||
const { baseElement } = setup(defaultMockProps); | ||
expect(baseElement).toMatchSnapshot(); | ||
}); | ||
|
||
it('should get workspace name through workspace id and render it with brackets wrapper', () => { | ||
const workspaceList$ = new BehaviorSubject([ | ||
{ | ||
id: 'workspace_id', | ||
name: 'workspace_name', | ||
}, | ||
]); | ||
const recentlyAccessed$ = new BehaviorSubject([ | ||
{ | ||
label: 'item_label', | ||
link: 'item_link', | ||
id: 'item_id', | ||
workspaceId: 'workspace_id', | ||
}, | ||
]); | ||
|
||
setup({ | ||
...defaultMockProps, | ||
workspaceList$, | ||
recentlyAccessed$, | ||
navigateToUrl: defaultMockProps.navigateToUrl, | ||
}); | ||
const button = screen.getByTestId('recentItemsSectionButton'); | ||
fireEvent.click(button); | ||
expect(screen.getByText('(workspace_name)')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call navigateToUrl with link generated from createRecentNavLink when clicking item', () => { | ||
const workspaceList$ = new BehaviorSubject([]); | ||
const recentlyAccessed$ = new BehaviorSubject([ | ||
{ | ||
label: 'item_label', | ||
link: 'item_link', | ||
id: 'item_id', | ||
}, | ||
]); | ||
const navigateToUrl = jest.fn(); | ||
setup({ | ||
...defaultMockProps, | ||
workspaceList$, | ||
recentlyAccessed$, | ||
navigateToUrl, | ||
}); | ||
const button = screen.getByTestId('recentItemsSectionButton'); | ||
fireEvent.click(button); | ||
const item = screen.getByText('item_label'); | ||
expect(navigateToUrl).not.toHaveBeenCalled(); | ||
fireEvent.click(item); | ||
expect(navigateToUrl).toHaveBeenCalledWith('/recent_nav_link'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import React, { useMemo, useState } from 'react'; | ||
import * as Rx from 'rxjs'; | ||
import { | ||
EuiPopover, | ||
EuiHeaderSectionItemButton, | ||
EuiTextColor, | ||
EuiListGroup, | ||
EuiListGroupItem, | ||
EuiTitle, | ||
EuiIcon, | ||
EuiText, | ||
} from '@elastic/eui'; | ||
import useObservable from 'react-use/lib/useObservable'; | ||
import { ChromeRecentlyAccessedHistoryItem } from '../..'; | ||
import { WorkspaceObject } from '../../../workspace'; | ||
import { createRecentNavLink } from './nav_link'; | ||
import { HttpStart } from '../../../http'; | ||
import { ChromeNavLink } from '../../../'; | ||
// TODO: replace this icon once added to OUI | ||
import recent_items from './assets/recent_items.svg'; | ||
|
||
export interface Props { | ||
recentlyAccessed$: Rx.Observable<ChromeRecentlyAccessedHistoryItem[]>; | ||
workspaceList$: Rx.Observable<WorkspaceObject[]>; | ||
navigateToUrl: (url: string) => Promise<void>; | ||
basePath: HttpStart['basePath']; | ||
navLinks$: Rx.Observable<ChromeNavLink[]>; | ||
} | ||
|
||
export const RecentItems = ({ | ||
recentlyAccessed$, | ||
workspaceList$, | ||
navigateToUrl, | ||
navLinks$, | ||
basePath, | ||
}: Props) => { | ||
const [isPopoverOpen, setIsPopoverOpen] = useState(false); | ||
|
||
const recentlyAccessedItems = useObservable(recentlyAccessed$, []); | ||
const workspaceList = useObservable(workspaceList$, []); | ||
const navLinks = useObservable(navLinks$, []).filter((link) => !link.hidden); | ||
|
||
const items = useMemo(() => { | ||
// Only display five most latest items | ||
return recentlyAccessedItems.slice(0, 5).map((item) => { | ||
return { | ||
link: createRecentNavLink(item, navLinks, basePath, navigateToUrl).href, | ||
label: item.label, | ||
workspaceId: item.workspaceId, | ||
workspaceName: | ||
workspaceList.find((workspace) => workspace.id === item.workspaceId)?.name ?? '', | ||
}; | ||
}); | ||
}, [recentlyAccessedItems, workspaceList, basePath, navLinks, navigateToUrl]); | ||
|
||
const handleItemClick = (link: string) => { | ||
navigateToUrl(link); | ||
setIsPopoverOpen(false); | ||
}; | ||
|
||
return ( | ||
<EuiPopover | ||
button={ | ||
<EuiHeaderSectionItemButton | ||
onClick={() => { | ||
setIsPopoverOpen((prev) => !prev); | ||
}} | ||
data-test-subj="recentItemsSectionButton" | ||
> | ||
<EuiIcon type={recent_items} size="m" /> | ||
</EuiHeaderSectionItemButton> | ||
} | ||
isOpen={isPopoverOpen} | ||
closePopover={() => { | ||
setIsPopoverOpen(false); | ||
}} | ||
anchorPosition="downCenter" | ||
repositionOnScroll | ||
initialFocus={false} | ||
> | ||
<EuiTitle size="xxs"> | ||
<h4>Recents</h4> | ||
</EuiTitle> | ||
{items.length > 0 ? ( | ||
<EuiListGroup> | ||
{items.map((item) => ( | ||
<EuiListGroupItem | ||
onClick={() => handleItemClick(item.link)} | ||
key={item.link} | ||
label={ | ||
<> | ||
{item.label} | ||
{item.workspaceName ? ( | ||
<EuiTextColor color="subdued">({item.workspaceName})</EuiTextColor> | ||
) : null} | ||
</> | ||
} | ||
color="text" | ||
/> | ||
))} | ||
</EuiListGroup> | ||
) : ( | ||
<EuiText color="subdued">No recently viewed items</EuiText> | ||
)} | ||
</EuiPopover> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters