forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
workspace dropdown list #9
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c065ef2
draft
zhichao-aws 9c80eed
fix: validation & query
SuZhou-Joe 36da2f2
Merge remote-tracking branch 'suzhou/fix/validate' into view_list
zhichao-aws e3e61f3
Update mappings
SuZhou-Joe 370a15f
Update name type to keyword
SuZhou-Joe 3b3d032
Merge remote-tracking branch 'suzhou/fix/validate' into view_list
zhichao-aws 8706319
fix file path, change to front-end filter
zhichao-aws ebd54a2
merge branch
zhichao-aws 1756286
feat: use BehaviorObject and optimize code
SuZhou-Joe a46e7ef
Merge remote-tracking branch 'suzhou/feature/comment-opt' into view_list
zhichao-aws a75405d
useObservable workspaceList
zhichao-aws 1b48b65
current workspace
zhichao-aws e3f0a6e
fix search
zhichao-aws f42430a
update to useMemo
zhichao-aws 3a95df9
merge main
zhichao-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/plugins/workspace/public/containers/workspace_dropdown_list/index.ts
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,8 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { WorkspaceDropdownList } from './workspace_dropdown_list'; | ||
|
||
export { WorkspaceDropdownList }; |
83 changes: 83 additions & 0 deletions
83
src/plugins/workspace/public/containers/workspace_dropdown_list/workspace_dropdown_list.tsx
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,83 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState, useCallback, useMemo, useEffect } from 'react'; | ||
|
||
import { EuiButton, EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui'; | ||
import useObservable from 'react-use/lib/useObservable'; | ||
import { CoreStart, WorkspaceAttribute } from '../../../../../core/public'; | ||
|
||
type WorkspaceOption = EuiComboBoxOptionOption<WorkspaceAttribute>; | ||
|
||
interface WorkspaceDropdownListProps { | ||
coreStart: CoreStart; | ||
onCreateWorkspace: () => void; | ||
onSwitchWorkspace: (workspaceId: string) => Promise<void>; | ||
} | ||
|
||
function workspaceToOption(workspace: WorkspaceAttribute): WorkspaceOption { | ||
return { label: workspace.name, key: workspace.id, value: workspace }; | ||
} | ||
|
||
export function WorkspaceDropdownList(props: WorkspaceDropdownListProps) { | ||
const { coreStart, onCreateWorkspace, onSwitchWorkspace } = props; | ||
const workspaceList = useObservable(coreStart.workspaces.client.workspaceList$, []); | ||
const currentWorkspaceId = useObservable(coreStart.workspaces.client.currentWorkspaceId$, ''); | ||
|
||
const [loading, setLoading] = useState(false); | ||
const [workspaceOptions, setWorkspaceOptions] = useState([] as WorkspaceOption[]); | ||
|
||
const currentWorkspaceOption = useMemo(() => { | ||
const workspace = workspaceList.find((item) => item.id === currentWorkspaceId); | ||
if (!workspace) { | ||
coreStart.notifications.toasts.addDanger( | ||
`can not get current workspace of id [${currentWorkspaceId}]` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to wrap wordings by i18n. |
||
); | ||
return [workspaceToOption({ id: currentWorkspaceId, name: '' })]; | ||
} | ||
return [workspaceToOption(workspace)]; | ||
}, [workspaceList, currentWorkspaceId, coreStart]); | ||
const allWorkspaceOptions = useMemo(() => { | ||
return workspaceList.map(workspaceToOption); | ||
}, [workspaceList]); | ||
|
||
const onSearchChange = useCallback( | ||
(searchValue: string) => { | ||
setWorkspaceOptions(allWorkspaceOptions.filter((item) => item.label.includes(searchValue))); | ||
}, | ||
[allWorkspaceOptions] | ||
); | ||
|
||
const onChange = (workspaceOption: WorkspaceOption[]) => { | ||
/** switch the workspace */ | ||
setLoading(true); | ||
onSwitchWorkspace(workspaceOption[0].key!) | ||
.catch((err) => | ||
coreStart.notifications.toasts.addDanger('some error happens in workspace service') | ||
) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
onSearchChange(''); | ||
}, [onSearchChange]); | ||
|
||
return ( | ||
<> | ||
<EuiComboBox | ||
async | ||
options={workspaceOptions} | ||
isLoading={loading} | ||
onChange={onChange} | ||
selectedOptions={currentWorkspaceOption} | ||
singleSelection={{ asPlainText: true }} | ||
onSearchChange={onSearchChange} | ||
append={<EuiButton onClick={onCreateWorkspace}>Create workspace</EuiButton>} | ||
/> | ||
</> | ||
); | ||
} |
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,32 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { CoreStart } from '../../../core/public'; | ||
import { WorkspaceDropdownList } from './containers/workspace_dropdown_list'; | ||
|
||
export const mountDropdownList = (core: CoreStart) => { | ||
core.chrome.navControls.registerLeft({ | ||
order: 0, | ||
mount: (element) => { | ||
ReactDOM.render( | ||
<WorkspaceDropdownList | ||
coreStart={core} | ||
onCreateWorkspace={() => alert('create')} | ||
onSwitchWorkspace={async (id: string) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
alert(`switch to workspace ${id}`); | ||
}} | ||
// onSwitchWorkspace={(id: string) => alert(`switch to workspace ${id}`)} | ||
/>, | ||
element | ||
); | ||
return () => { | ||
ReactDOM.unmountComponentAtNode(element); | ||
}; | ||
}, | ||
}); | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a currentWorkspace$ that can be used to get currentWorkspace in core.workspaces.client, recommend to use that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will create another PR to change this