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] Enable URL title param for initial filter on dashboard listing #4480

Merged
Changes from 1 commit
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,15 +3,16 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useCallback, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { i18n } from '@osd/i18n';
import { useMount } from 'react-use';
import { useLocation } from 'react-router-dom';
import {
useOpenSearchDashboards,
TableListView,
} from '../../../../opensearch_dashboards_react/public';
import { CreateButton } from '../listing/create_button';
import { DashboardConstants } from '../../dashboard_constants';
import { DashboardConstants, createDashboardEditUrl } from '../../dashboard_constants';
import { DashboardServices } from '../../types';
import { getTableColumns } from '../utils/get_table_columns';
import { getNoItemsMessage } from '../utils/get_no_items_message';
Expand All @@ -34,6 +35,46 @@ export const DashboardListing = () => {
},
} = useOpenSearchDashboards<DashboardServices>();

const location = useLocation();
const queryParameters = new URLSearchParams(location.search);
const initialFiltersFromURL = queryParameters.get('filter');
const [initialFilter, setInitialFilter] = useState<string | null>(initialFiltersFromURL);

useEffect(() => {
const getDashboardsBasedOnUrl = async () => {
const title = queryParameters.get('title');

try {
if (title) {
const results = await savedObjectsClient.find<any>({
search: `"${title}"`,
searchFields: ['title'],
type: 'dashboard',
});

const matchingDashboards = results.savedObjects.filter(
(dashboard) => dashboard.attributes.title.toLowerCase() === title.toLowerCase()
);

if (matchingDashboards.length === 1) {
history.replace(createDashboardEditUrl(matchingDashboards[0].id));
} else {
history.push(`${DashboardConstants.LANDING_PAGE_PATH}?filter="${title}"`);
setInitialFilter(title);
}
return new Promise(() => {});
}
} catch (e) {
notifications.toasts.addWarning(
i18n.translate('dashboard.listing. savedObjectWarning', {
defaultMessage: 'Uble to filter by title',
Copy link
Member

Choose a reason for hiding this comment

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

uble

Copy link
Member

Choose a reason for hiding this comment

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

unable?

})
);
}
};
getDashboardsBasedOnUrl();
}, [savedObjectsClient, history, notifications.toasts, queryParameters]);

const hideWriteControls = dashboardConfig.getHideWriteControls();

const tableColumns = useMemo(() => getTableColumns(application, history, uiSettings), [
Expand Down Expand Up @@ -143,7 +184,7 @@ export const DashboardListing = () => {
editItem={hideWriteControls ? undefined : editItem}
tableColumns={tableColumns}
listingLimit={listingLimit}
initialFilter={''}
initialFilter={initialFilter ?? ''}
initialPageSize={initialPageSize}
noItemsFragment={noItemsFragment}
entityName={i18n.translate('dashboard.listing.table.entityName', {
Expand Down