Skip to content
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] Refactor workspace detail page #7598

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7598.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- [Workspace] Refactor workspace detail page ([#7598](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7598))
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,95 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import React, { useState } from 'react';
import {
EuiPage,
EuiFlexItem,
EuiPageBody,
EuiFlexGroup,
EuiPageHeader,
EuiFlexItem,
EuiSmallButton,
EuiConfirmModal,
EuiTabbedContent,
EuiFlexGroup,
EuiPanel,
} from '@elastic/eui';

import { useObservable } from 'react-use';
import { i18n } from '@osd/i18n';
import { CoreStart } from 'opensearch-dashboards/public';
import { BehaviorSubject } from 'rxjs';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { useObservable } from 'react-use';
import { BehaviorSubject, of } from 'rxjs';
import { WorkspaceUseCase } from '../../types';
import { WorkspaceDetailContent } from './workspace_detail_content';
import { WorkspaceUpdater } from './workspace_updater';
import { DetailTab } from '../workspace_form/constants';
import { WorkspaceDetailPanel } from './workspace_detail_panel';
import { DeleteWorkspaceModal } from '../delete_workspace_modal';
import { WORKSPACE_LIST_APP_ID } from '../../../common/constants';
import { cleanWorkspaceId } from '../../../../../core/public/utils';
import { DetailTab, DetailTabTitles } from '../workspace_form/constants';
import { CoreStart, WorkspaceAttribute } from '../../../../../core/public';
import { getFirstUseCaseOfFeatureConfigs, getUseCaseUrl } from '../../utils';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { DataSourceManagementPluginSetup } from '../../../../../plugins/data_source_management/public';

const workspaceDelete = i18n.translate('workspace.detail.delete', {
defaultMessage: 'delete',
});

export interface WorkspaceDetailProps {
registeredUseCases$: BehaviorSubject<WorkspaceUseCase[]>;
}

export const WorkspaceDetail = (props: WorkspaceDetailProps) => {
const {
services: { workspaces, application },
} = useOpenSearchDashboards<CoreStart>();
services: { workspaces, application, http, dataSourceManagement },
} = useOpenSearchDashboards<{
CoreStart: CoreStart;
dataSourceManagement?: DataSourceManagementPluginSetup;
}>();

const currentWorkspace = useObservable(workspaces.currentWorkspace$);
const [deletedWorkspace, setDeletedWorkspace] = useState<WorkspaceAttribute | null>(null);
const [selectedTabId, setSelectedTabId] = useState<string>(DetailTab.Details);
const [numberOfChanges, setNumberOfChanges] = useState(0);
const [modalVisible, setModalVisible] = useState(false);
const [tabId, setTabId] = useState<string>(DetailTab.Details);
const [resetFunction, setResetFunction] = useState(() => () => {});

const availableUseCases = useObservable(props.registeredUseCases$, []);
const isDashboardAdmin = application?.capabilities?.dashboards?.isDashboardAdmin;
const currentWorkspace = useObservable(workspaces ? workspaces.currentWorkspace$ : of(null));
const isPermissionEnabled = application?.capabilities.workspaces.permissionEnabled;
const currentUseCase = availableUseCases.find(
(useCase) => useCase.id === getFirstUseCaseOfFeatureConfigs(currentWorkspace?.features ?? [])
);

if (!currentWorkspace) {
if (!currentWorkspace || !application || !http) {
return null;
}

const useCaseUrl = getUseCaseUrl(currentUseCase, currentWorkspace, application, http);

const getResetFunction = (func: any) => {
setResetFunction(() => func);

Check warning on line 70 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L70

Added line #L70 was not covered by tests
};

const getNumberOfChanges = (data: number) => {
setNumberOfChanges(data);

Check warning on line 74 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L74

Added line #L74 was not covered by tests
};

const handleTabClick = (tab: any) => {
if (numberOfChanges > 0) {
setTabId(tab.id);
setModalVisible(true);
return;

Check warning on line 81 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L79-L81

Added lines #L79 - L81 were not covered by tests
}
setSelectedTabId(tab.id);

Check warning on line 83 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L83

Added line #L83 was not covered by tests
};

const handleBadgeClick = () => {
if (selectedTabId !== DetailTab.TeamMembers && numberOfChanges > 0) {
setTabId(DetailTab.TeamMembers);
setModalVisible(true);
return;

Check warning on line 90 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L88-L90

Added lines #L88 - L90 were not covered by tests
}
setSelectedTabId(DetailTab.TeamMembers);

Check warning on line 92 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L92

Added line #L92 was not covered by tests
};

const pageTitle = (
<EuiFlexGroup gutterSize="none" alignItems="baseline" justifyContent="flexStart">
<EuiFlexItem grow={false}>{currentWorkspace?.name}</EuiFlexItem>
Expand All @@ -48,57 +100,133 @@

const detailTabs = [
{
id: DetailTab.Overview,
name: i18n.translate('workspace.overview.tabTitle', {
defaultMessage: 'Overview',
}),
content: <WorkspaceDetailContent />,
},
{
id: DetailTab.Settings,
name: i18n.translate('workspace.overview.setting.tabTitle', {
defaultMessage: 'Settings',
}),
id: DetailTab.Details,
name: DetailTabTitles.details,
content: (
<WorkspaceUpdater
detailTab={DetailTab.Settings}
detailTab={DetailTab.Details}
registeredUseCases$={props.registeredUseCases$}
detailTitle={DetailTabTitles.details}
getNumberOfChanges={getNumberOfChanges}
getResetFunction={getResetFunction}
/>
),
},
...(isDashboardAdmin && dataSourceManagement
? [
{
id: DetailTab.DataSources,
name: DetailTabTitles.dataSources,
content: (
<WorkspaceUpdater
detailTab={DetailTab.DataSources}
registeredUseCases$={props.registeredUseCases$}
detailTitle={DetailTabTitles.dataSources}
getNumberOfChanges={getNumberOfChanges}
getResetFunction={getResetFunction}
/>
),
},
]
: []),
...(isPermissionEnabled
? [
{
id: DetailTab.Collaborators,
name: i18n.translate('workspace.overview.collaborators.tabTitle', {
defaultMessage: 'Collaborators',
}),
id: DetailTab.TeamMembers,
name: DetailTabTitles.teamMembers,
content: (
<WorkspaceUpdater
detailTab={DetailTab.Collaborators}
detailTab={DetailTab.TeamMembers}
registeredUseCases$={props.registeredUseCases$}
detailTitle={DetailTabTitles.teamMembers}
getNumberOfChanges={getNumberOfChanges}
getResetFunction={getResetFunction}
/>
),
},
]
: []),
];

const button = (
<EuiSmallButton
color="danger"
iconType="trash"
onClick={() => setDeletedWorkspace(currentWorkspace)}

Check warning on line 155 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L155

Added line #L155 was not covered by tests
>
{workspaceDelete}
</EuiSmallButton>
);

return (
<>
<EuiPanel paddingSize="l" borderRadius="none" hasShadow={false} hasBorder={false}>
<EuiPageHeader pageTitle={pageTitle} />
</EuiPanel>
<EuiPage paddingSize="l">
<EuiPageHeader pageTitle={pageTitle} rightSideItems={[button]} alignItems="center" />
</EuiPage>
<EuiPage paddingSize="l">
<WorkspaceDetailPanel
useCaseUrl={useCaseUrl}
handleBadgeClick={handleBadgeClick}
currentUseCase={currentUseCase}
currentWorkspace={currentWorkspace}
/>
</EuiPage>

<EuiPage paddingSize="l">
<EuiPageBody>
<EuiTabbedContent
data-test-subj="workspaceTabs"
tabs={detailTabs}
initialSelectedTab={detailTabs[0]}
autoFocus="selected"
// initialSelectedTab={detailTabs[0]}
selectedTab={detailTabs[detailTabs.findIndex((tab) => tab.id === selectedTabId)]}
// autoFocus="selected"
onTabClick={handleTabClick}
/>
</EuiPageBody>
</EuiPage>
{deletedWorkspace && (
<DeleteWorkspaceModal
selectedWorkspace={deletedWorkspace}
onClose={() => setDeletedWorkspace(null)}

Check warning on line 190 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L190

Added line #L190 was not covered by tests
onDeleteSuccess={() => {
window.setTimeout(() => {
window.location.assign(

Check warning on line 193 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L192-L193

Added lines #L192 - L193 were not covered by tests
cleanWorkspaceId(
application.getUrlForApp(WORKSPACE_LIST_APP_ID, {
absolute: false,
})
)
);
}, 1000);
}}
/>
)}
{modalVisible && (
<EuiConfirmModal
data-test-subj="workspaceForm-cancelModal"
title={i18n.translate('workspace.form.cancelModal.title', {
defaultMessage: 'Navigate away',
})}
onCancel={() => setModalVisible(false)}

Check warning on line 210 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L210

Added line #L210 was not covered by tests
onConfirm={() => {
resetFunction();
setModalVisible(false);
setSelectedTabId(tabId);

Check warning on line 214 in src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_detail/workspace_detail.tsx#L212-L214

Added lines #L212 - L214 were not covered by tests
}}
cancelButtonText={i18n.translate('workspace.form.cancelButtonText', {
defaultMessage: 'Cancel',
})}
confirmButtonText={i18n.translate('workspace.form.confirmButtonText', {
defaultMessage: 'navigate away',
})}
buttonColor="danger"
defaultFocusedButton="confirm"
>
{i18n.translate('workspace.form.cancelModal.body', {
defaultMessage: 'Any unsaved changes will be lost.',
})}
</EuiConfirmModal>
)}
</>
);
};

This file was deleted.

Loading
Loading