Skip to content

Commit

Permalink
add workspace switch (opensearch-project#17)
Browse files Browse the repository at this point in the history
* feat: update workspace switch

Signed-off-by: tygao <tygao@amazon.com>

* fix: fix switch error

Signed-off-by: tygao <tygao@amazon.com>

* fix: fix prettier after merge

Signed-off-by: tygao <tygao@amazon.com>

* chore: remove extra code after merge

Signed-off-by: tygao <tygao@amazon.com>

---------

Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao authored and ruanyl committed Sep 15, 2023
1 parent ec02b38 commit 6aa12fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import React, { useState, useCallback, useMemo, useEffect } from 'react';

import { EuiButton, EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import useObservable from 'react-use/lib/useObservable';
import { i18n } from '@osd/i18n';
import { CoreStart, WorkspaceAttribute } from '../../../../../core/public';
import { WORKSPACE_APP_ID, PATHS } from '../../../common/constants';

type WorkspaceOption = EuiComboBoxOptionOption<WorkspaceAttribute>;

interface WorkspaceDropdownListProps {
coreStart: CoreStart;
onSwitchWorkspace: (workspaceId: string) => Promise<void>;
}

function workspaceToOption(workspace: WorkspaceAttribute): WorkspaceOption {
Expand All @@ -28,7 +26,8 @@ export function getErrorMessage(err: any) {
}

export function WorkspaceDropdownList(props: WorkspaceDropdownListProps) {
const { coreStart, onSwitchWorkspace } = props;
const { coreStart } = props;

const workspaceList = useObservable(coreStart.workspaces.client.workspaceList$, []);
const currentWorkspace = useObservable(coreStart.workspaces.client.currentWorkspace$, null);

Expand All @@ -53,22 +52,19 @@ export function WorkspaceDropdownList(props: WorkspaceDropdownListProps) {
[allWorkspaceOptions]
);

const onChange = (workspaceOption: WorkspaceOption[]) => {
/** switch the workspace */
setLoading(true);
onSwitchWorkspace(workspaceOption[0].key!)
.catch((err) =>
coreStart.notifications.toasts.addDanger({
title: i18n.translate('workspace.dropdownList.switchWorkspaceErrorTitle', {
defaultMessage: 'some error happens when switching workspace',
}),
text: getErrorMessage(err),
})
)
.finally(() => {
setLoading(false);
});
};
const onChange = useCallback(
(workspaceOption: WorkspaceOption[]) => {
/** switch the workspace */
setLoading(true);
const id = workspaceOption[0].key!;
const newUrl = coreStart.workspaces?.formatUrlWithWorkspaceId(window.location.href, id);
if (newUrl) {
window.location.href = newUrl;
}
setLoading(false);
},
[coreStart.workspaces]
);

const onCreateWorkspaceClick = () => {
coreStart.application.navigateToApp(WORKSPACE_APP_ID, { path: PATHS.create });
Expand Down
12 changes: 1 addition & 11 deletions src/plugins/workspace/public/mount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ export const mountDropdownList = (core: CoreStart) => {
core.chrome.navControls.registerLeft({
order: 0,
mount: (element) => {
ReactDOM.render(
<WorkspaceDropdownList
coreStart={core}
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
);
ReactDOM.render(<WorkspaceDropdownList coreStart={core} />, element);
return () => {
ReactDOM.unmountComponentAtNode(element);
};
Expand Down

0 comments on commit 6aa12fa

Please sign in to comment.