selectors.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { createSelector } from '@reduxjs/toolkit';
  2. import { PluginError, PluginErrorCode } from '@grafana/data';
  3. import { RequestStatus, PluginCatalogStoreState } from '../types';
  4. import { pluginsAdapter } from './reducer';
  5. export const selectRoot = (state: PluginCatalogStoreState) => state.plugins;
  6. export const selectItems = createSelector(selectRoot, ({ items }) => items);
  7. export const selectDisplayMode = createSelector(selectRoot, ({ settings }) => settings.displayMode);
  8. export const { selectAll, selectById } = pluginsAdapter.getSelectors(selectItems);
  9. const selectInstalled = (filterBy: string) =>
  10. createSelector(selectAll, (plugins) =>
  11. plugins.filter((plugin) => (filterBy === 'installed' ? plugin.isInstalled : !plugin.isCore))
  12. );
  13. const findByInstallAndType = (filterBy: string, filterByType: string) =>
  14. createSelector(selectInstalled(filterBy), (plugins) =>
  15. plugins.filter((plugin) => filterByType === 'all' || plugin.type === filterByType)
  16. );
  17. const findByKeyword = (searchBy: string) =>
  18. createSelector(selectAll, (plugins) => {
  19. if (searchBy === '') {
  20. return [];
  21. }
  22. return plugins.filter((plugin) => {
  23. const fields: String[] = [];
  24. if (plugin.name) {
  25. fields.push(plugin.name.toLowerCase());
  26. }
  27. if (plugin.orgName) {
  28. fields.push(plugin.orgName.toLowerCase());
  29. }
  30. return fields.some((f) => f.includes(searchBy.toLowerCase()));
  31. });
  32. });
  33. export const find = (searchBy: string, filterBy: string, filterByType: string) =>
  34. createSelector(
  35. findByInstallAndType(filterBy, filterByType),
  36. findByKeyword(searchBy),
  37. (filteredPlugins, searchedPlugins) => {
  38. return searchBy === '' ? filteredPlugins : searchedPlugins;
  39. }
  40. );
  41. export const selectPluginErrors = createSelector(selectAll, (plugins) =>
  42. plugins
  43. ? plugins
  44. .filter((p) => Boolean(p.error))
  45. .map(
  46. (p): PluginError => ({
  47. pluginId: p.id,
  48. errorCode: p!.error as PluginErrorCode,
  49. })
  50. )
  51. : []
  52. );
  53. // The following selectors are used to get information about the outstanding or completed plugins-related network requests.
  54. export const selectRequest = (actionType: string) =>
  55. createSelector(selectRoot, ({ requests = {} }) => requests[actionType]);
  56. export const selectIsRequestPending = (actionType: string) =>
  57. createSelector(selectRequest(actionType), (request) => request?.status === RequestStatus.Pending);
  58. export const selectRequestError = (actionType: string) =>
  59. createSelector(selectRequest(actionType), (request) =>
  60. request?.status === RequestStatus.Rejected ? request?.error : null
  61. );
  62. export const selectIsRequestNotFetched = (actionType: string) =>
  63. createSelector(selectRequest(actionType), (request) => request === undefined);