selectors.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { createSelector } from 'reselect';
  2. import {
  3. RootState,
  4. TopicName,
  5. TopicsState,
  6. TopicConfigByName,
  7. } from 'redux/interfaces';
  8. import { createFetchingSelector } from 'redux/reducers/loader/selectors';
  9. import { Partition } from 'generated-sources';
  10. const topicsState = ({ topics }: RootState): TopicsState => topics;
  11. const getAllNames = (state: RootState) => topicsState(state).allNames;
  12. const getTopicMap = (state: RootState) => topicsState(state).byName;
  13. export const getTopicMessages = (state: RootState) =>
  14. topicsState(state).messages;
  15. export const getTopicListTotalPages = (state: RootState) =>
  16. topicsState(state).totalPages;
  17. const getTopicListFetchingStatus = createFetchingSelector('GET_TOPICS');
  18. const getTopicDetailsFetchingStatus =
  19. createFetchingSelector('GET_TOPIC_DETAILS');
  20. const getTopicMessagesFetchingStatus =
  21. createFetchingSelector('GET_TOPIC_MESSAGES');
  22. const getTopicConfigFetchingStatus = createFetchingSelector('GET_TOPIC_CONFIG');
  23. const getTopicCreationStatus = createFetchingSelector('POST_TOPIC');
  24. const getTopicUpdateStatus = createFetchingSelector('PATCH_TOPIC');
  25. export const getAreTopicsFetching = createSelector(
  26. getTopicListFetchingStatus,
  27. (status) => status === 'fetching' || status === 'notFetched'
  28. );
  29. export const getAreTopicsFetched = createSelector(
  30. getTopicListFetchingStatus,
  31. (status) => status === 'fetched'
  32. );
  33. export const getIsTopicDetailsFetching = createSelector(
  34. getTopicDetailsFetchingStatus,
  35. (status) => status === 'notFetched' || status === 'fetching'
  36. );
  37. export const getIsTopicDetailsFetched = createSelector(
  38. getTopicDetailsFetchingStatus,
  39. (status) => status === 'fetched'
  40. );
  41. export const getIsTopicMessagesFetched = createSelector(
  42. getTopicMessagesFetchingStatus,
  43. (status) => status === 'fetched'
  44. );
  45. export const getTopicConfigFetched = createSelector(
  46. getTopicConfigFetchingStatus,
  47. (status) => status === 'fetched'
  48. );
  49. export const getTopicCreated = createSelector(
  50. getTopicCreationStatus,
  51. (status) => status === 'fetched'
  52. );
  53. export const getTopicUpdated = createSelector(
  54. getTopicUpdateStatus,
  55. (status) => status === 'fetched'
  56. );
  57. export const getTopicList = createSelector(
  58. getAreTopicsFetched,
  59. getAllNames,
  60. getTopicMap,
  61. (isFetched, allNames, byName) => {
  62. if (!isFetched) {
  63. return [];
  64. }
  65. return allNames.map((name) => byName[name]);
  66. }
  67. );
  68. export const getExternalTopicList = createSelector(getTopicList, (topics) =>
  69. topics.filter(({ internal }) => !internal)
  70. );
  71. const getTopicName = (_: RootState, topicName: TopicName) => topicName;
  72. export const getTopicByName = createSelector(
  73. getTopicMap,
  74. getTopicName,
  75. (topics, topicName) => topics[topicName]
  76. );
  77. export const getPartitionsByTopicName = createSelector(
  78. getTopicMap,
  79. getTopicName,
  80. (topics, topicName) => topics[topicName].partitions as Partition[]
  81. );
  82. export const getFullTopic = createSelector(getTopicByName, (topic) =>
  83. topic && topic.config && !!topic.partitionCount ? topic : undefined
  84. );
  85. export const getTopicConfig = createSelector(
  86. getTopicByName,
  87. ({ config }) => config
  88. );
  89. export const getTopicConfigByParamName = createSelector(
  90. getTopicConfig,
  91. (config) => {
  92. const byParamName: TopicConfigByName = {
  93. byName: {},
  94. };
  95. if (config) {
  96. config.forEach((param) => {
  97. byParamName.byName[param.name] = param;
  98. });
  99. }
  100. return byParamName;
  101. }
  102. );
  103. export const getTopicsSearch = createSelector(
  104. topicsState,
  105. (state) => state.search
  106. );
  107. export const getTopicsOrderBy = createSelector(
  108. topicsState,
  109. (state) => state.orderBy
  110. );
  111. export const getIsTopicInternal = createSelector(
  112. getTopicByName,
  113. ({ internal }) => !!internal
  114. );
  115. export const getTopicConsumerGroups = createSelector(
  116. getTopicMap,
  117. getTopicName,
  118. (topics, topicName) => topics[topicName].consumerGroups?.consumers || []
  119. );