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

[Dashboard De-Angular] Render dashboard listing page #4015

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,156 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import React, { useCallback, useMemo } from 'react';
import { i18n } from '@osd/i18n';
import { useMount } from 'react-use';
import {
useOpenSearchDashboards,
TableListView,
} from '../../../../opensearch_dashboards_react/public';
import { CreateButton } from '../listing/create_button';
import { DashboardConstants } from '../../dashboard_constants';
import { DashboardServices } from '../../types';
import { getTableColumns } from '../utils/get_table_columns';
import { getNoItemsMessage } from '../utils/get_no_items_message';

export const EMPTY_FILTER = '';

export const DashboardListing = () => {
return <div>Dashboard Listing</div>;
const {
services: {
application,
chrome,
savedObjectsPublic,
savedObjectsClient,
dashboardConfig,
history,
uiSettings,
notifications,
savedDashboards,
dashboardProviders,
addBasePath,
},
} = useOpenSearchDashboards<DashboardServices>();

const hideWriteControls = dashboardConfig.getHideWriteControls();

const tableColumns = useMemo(() => getTableColumns(application, history, uiSettings), [
application,
history,
uiSettings,
]);

const createItem = useCallback(() => {
history.push(DashboardConstants.CREATE_NEW_DASHBOARD_URL);
}, [history]);

const noItemsFragment = useMemo(
() => getNoItemsMessage(hideWriteControls, createItem, application),
[hideWriteControls, createItem, application]
);

const dashboardProvidersForListing = dashboardProviders() || {};

const dashboardListTypes = Object.keys(dashboardProvidersForListing);
const initialPageSize = savedObjectsPublic.settings.getPerPage();
const listingLimit = savedObjectsPublic.settings.getListingLimit();

const mapListAttributesToDashboardProvider = (obj: any) => {
const provider = dashboardProvidersForListing[obj.type];
return {
id: obj.id,
appId: provider.appId,
type: provider.savedObjectsName,
...obj.attributes,
updated_at: obj.updated_at,
viewUrl: provider.viewUrlPathFn(obj),
editUrl: provider.editUrlPathFn(obj),
};
};

const find = async (search: any) => {
const res = await savedObjectsClient.find({
type: dashboardListTypes,
search: search ? `${search}*` : undefined,
fields: ['title', 'type', 'description', 'updated_at'],
perPage: listingLimit,
page: 1,
searchFields: ['title^3', 'type', 'description'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does title^3 mean it's more heavily weighted than the other fields?

defaultSearchOperator: 'AND',
});
const list = res.savedObjects?.map(mapListAttributesToDashboardProvider) || [];

return {
total: list.length,
hits: list,
};
};

const editItem = useCallback(
({ editUrl }: any) => {
if (addBasePath) {
history.push(addBasePath(editUrl));
}
},
[history, addBasePath]
);

const viewItem = useCallback(
({ viewUrl }: any) => {
if (addBasePath) {
history.push(addBasePath(viewUrl));
}
},
[history, addBasePath]
);

const deleteItems = useCallback(
(dashboards: object[]) => {
return savedDashboards.delete(dashboards.map((d: any) => d.id));
},
[savedDashboards]
);

useMount(() => {
chrome.setBreadcrumbs([
{
text: i18n.translate('dashboard.dashboardBreadcrumbsTitle', {
defaultMessage: 'Dashboards',
}),
},
]);

chrome.docTitle.change(
i18n.translate('dashboard.dashboardPageTitle', { defaultMessage: 'Dashboards' })
);
});

return (
<TableListView
headingId="dashboardListingHeading"
createItem={hideWriteControls ? undefined : createItem}
createButton={
hideWriteControls ? undefined : <CreateButton dashboardProviders={dashboardProviders()} />
}
findItems={find}
deleteItems={hideWriteControls ? undefined : deleteItems}
editItem={hideWriteControls ? undefined : editItem}
tableColumns={tableColumns}
listingLimit={listingLimit}
initialFilter={''}
initialPageSize={initialPageSize}
noItemsFragment={noItemsFragment}
entityName={i18n.translate('dashboard.listing.table.entityName', {
defaultMessage: 'dashboard',
})}
entityNamePlural={i18n.translate('dashboard.listing.table.entityNamePlural', {
defaultMessage: 'dashboards',
})}
tableListTitle={i18n.translate('dashboard.listing.dashboardsTitle', {
defaultMessage: 'Dashboards',
})}
toastNotifications={notifications.toasts}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import type { DashboardProvider } from '../../types';

interface CreateButtonProps {
dashboardProviders?: DashboardProvider[];
dashboardProviders?: { [key: string]: DashboardProvider };
}

const CreateButton = (props: CreateButtonProps) => {
Expand Down
Loading