|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { useState, useCallback, useMemo, useEffect } from 'react'; |
| 7 | + |
| 8 | +import { EuiButton, EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui'; |
| 9 | +import useObservable from 'react-use/lib/useObservable'; |
| 10 | +import { CoreStart, WorkspaceAttribute } from '../../../../../core/public'; |
| 11 | + |
| 12 | +type WorkspaceOption = EuiComboBoxOptionOption<WorkspaceAttribute>; |
| 13 | + |
| 14 | +interface WorkspaceDropdownListProps { |
| 15 | + coreStart: CoreStart; |
| 16 | + onCreateWorkspace: () => void; |
| 17 | + onSwitchWorkspace: (workspaceId: string) => Promise<void>; |
| 18 | +} |
| 19 | + |
| 20 | +function workspaceToOption(workspace: WorkspaceAttribute): WorkspaceOption { |
| 21 | + return { label: workspace.name, key: workspace.id, value: workspace }; |
| 22 | +} |
| 23 | + |
| 24 | +export function WorkspaceDropdownList(props: WorkspaceDropdownListProps) { |
| 25 | + const { coreStart, onCreateWorkspace, onSwitchWorkspace } = props; |
| 26 | + const workspaceList = useObservable(coreStart.workspaces.client.workspaceList$, []); |
| 27 | + const currentWorkspaceId = useObservable(coreStart.workspaces.client.currentWorkspaceId$, ''); |
| 28 | + |
| 29 | + const [loading, setLoading] = useState(false); |
| 30 | + const [workspaceOptions, setWorkspaceOptions] = useState([] as WorkspaceOption[]); |
| 31 | + |
| 32 | + const currentWorkspaceOption = useMemo(() => { |
| 33 | + const workspace = workspaceList.find((item) => item.id === currentWorkspaceId); |
| 34 | + if (!workspace) { |
| 35 | + coreStart.notifications.toasts.addDanger( |
| 36 | + `can not get current workspace of id [${currentWorkspaceId}]` |
| 37 | + ); |
| 38 | + return [workspaceToOption({ id: currentWorkspaceId, name: '' })]; |
| 39 | + } |
| 40 | + return [workspaceToOption(workspace)]; |
| 41 | + }, [workspaceList, currentWorkspaceId, coreStart]); |
| 42 | + const allWorkspaceOptions = useMemo(() => { |
| 43 | + return workspaceList.map(workspaceToOption); |
| 44 | + }, [workspaceList]); |
| 45 | + |
| 46 | + const onSearchChange = useCallback( |
| 47 | + (searchValue: string) => { |
| 48 | + setWorkspaceOptions(allWorkspaceOptions.filter((item) => item.label.includes(searchValue))); |
| 49 | + }, |
| 50 | + [allWorkspaceOptions] |
| 51 | + ); |
| 52 | + |
| 53 | + const onChange = (workspaceOption: WorkspaceOption[]) => { |
| 54 | + /** switch the workspace */ |
| 55 | + setLoading(true); |
| 56 | + onSwitchWorkspace(workspaceOption[0].key!) |
| 57 | + .catch((err) => |
| 58 | + coreStart.notifications.toasts.addDanger('some error happens in workspace service') |
| 59 | + ) |
| 60 | + .finally(() => { |
| 61 | + setLoading(false); |
| 62 | + }); |
| 63 | + }; |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + onSearchChange(''); |
| 67 | + }, [onSearchChange]); |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <EuiComboBox |
| 72 | + async |
| 73 | + options={workspaceOptions} |
| 74 | + isLoading={loading} |
| 75 | + onChange={onChange} |
| 76 | + selectedOptions={currentWorkspaceOption} |
| 77 | + singleSelection={{ asPlainText: true }} |
| 78 | + onSearchChange={onSearchChange} |
| 79 | + append={<EuiButton onClick={onCreateWorkspace}>Create workspace</EuiButton>} |
| 80 | + /> |
| 81 | + </> |
| 82 | + ); |
| 83 | +} |
0 commit comments