2024-10-25 00:03:17 +08:00
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
|
import { useCallback, useMemo } from 'react';
|
2022-07-07 17:15:34 +08:00
|
|
|
|
|
2022-11-30 16:41:01 +08:00
|
|
|
|
import { SelectableValue } from '@grafana/data';
|
2022-07-07 17:15:34 +08:00
|
|
|
|
import PageActionBar from 'app/core/components/PageActionBar/PageActionBar';
|
2023-04-14 16:43:11 +08:00
|
|
|
|
import { StoreState, useSelector, useDispatch } from 'app/types';
|
2022-07-07 17:15:34 +08:00
|
|
|
|
|
2023-04-14 16:43:11 +08:00
|
|
|
|
import { getDataSourcesSearchQuery, getDataSourcesSort, setDataSourcesSearchQuery, setIsSortAscending } from '../state';
|
2024-10-25 00:03:17 +08:00
|
|
|
|
import { trackDsSearched } from '../tracking';
|
2022-11-30 16:41:01 +08:00
|
|
|
|
|
|
|
|
|
|
const ascendingSortValue = 'alpha-asc';
|
|
|
|
|
|
const descendingSortValue = 'alpha-desc';
|
|
|
|
|
|
|
|
|
|
|
|
const sortOptions = [
|
|
|
|
|
|
// We use this unicode 'en dash' character (U+2013), because it looks nicer
|
|
|
|
|
|
// than simple dash in this context. This is also used in the response of
|
|
|
|
|
|
// the `sorting` endpoint, which is used in the search dashboard page.
|
|
|
|
|
|
{ label: 'Sort by A–Z', value: ascendingSortValue },
|
|
|
|
|
|
{ label: 'Sort by Z–A', value: descendingSortValue },
|
|
|
|
|
|
];
|
2022-07-07 17:15:34 +08:00
|
|
|
|
|
2022-07-20 15:25:09 +08:00
|
|
|
|
export function DataSourcesListHeader() {
|
2022-07-07 17:15:34 +08:00
|
|
|
|
const dispatch = useDispatch();
|
2024-10-25 00:03:17 +08:00
|
|
|
|
const debouncedTrackSearch = useMemo(
|
|
|
|
|
|
() =>
|
|
|
|
|
|
debounce((q) => {
|
|
|
|
|
|
trackDsSearched({ query: q });
|
|
|
|
|
|
}, 300),
|
|
|
|
|
|
[]
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const setSearchQuery = useCallback(
|
|
|
|
|
|
(q: string) => {
|
|
|
|
|
|
dispatch(setDataSourcesSearchQuery(q));
|
|
|
|
|
|
if (q) {
|
|
|
|
|
|
debouncedTrackSearch(q);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
[dispatch, debouncedTrackSearch]
|
|
|
|
|
|
);
|
2022-07-07 17:15:34 +08:00
|
|
|
|
const searchQuery = useSelector(({ dataSources }: StoreState) => getDataSourcesSearchQuery(dataSources));
|
|
|
|
|
|
|
2022-11-30 16:41:01 +08:00
|
|
|
|
const setSort = useCallback(
|
|
|
|
|
|
(sort: SelectableValue) => dispatch(setIsSortAscending(sort.value === ascendingSortValue)),
|
|
|
|
|
|
[dispatch]
|
2022-07-20 15:25:09 +08:00
|
|
|
|
);
|
2022-11-30 16:41:01 +08:00
|
|
|
|
const isSortAscending = useSelector(({ dataSources }: StoreState) => getDataSourcesSort(dataSources));
|
2022-07-20 15:25:09 +08:00
|
|
|
|
|
2022-11-30 16:41:01 +08:00
|
|
|
|
const sortPicker = {
|
|
|
|
|
|
onChange: setSort,
|
|
|
|
|
|
value: isSortAscending ? ascendingSortValue : descendingSortValue,
|
|
|
|
|
|
getSortOptions: () => Promise.resolve(sortOptions),
|
2022-07-07 17:15:34 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2023-04-14 16:43:11 +08:00
|
|
|
|
<PageActionBar searchQuery={searchQuery} setSearchQuery={setSearchQuery} key="action-bar" sortPicker={sortPicker} />
|
2022-07-07 17:15:34 +08:00
|
|
|
|
);
|
2022-07-20 15:25:09 +08:00
|
|
|
|
}
|