selectors.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const getTopicMessageSchemaFetchingStatus =
  26. createFetchingSelector('GET_TOPIC_SCHEMA');
  27. const getTopicMessageSendingStatus =
  28. createFetchingSelector('SEND_TOPIC_MESSAGE');
  29. const getPartitionsCountIncreaseStatus =
  30. createFetchingSelector('UPDATE_PARTITIONS');
  31. const getReplicationFactorUpdateStatus = createFetchingSelector(
  32. 'UPDATE_REPLICATION_FACTOR'
  33. );
  34. export const getAreTopicsFetching = createSelector(
  35. getTopicListFetchingStatus,
  36. (status) => status === 'fetching' || status === 'notFetched'
  37. );
  38. export const getAreTopicsFetched = createSelector(
  39. getTopicListFetchingStatus,
  40. (status) => status === 'fetched'
  41. );
  42. export const getIsTopicDetailsFetching = createSelector(
  43. getTopicDetailsFetchingStatus,
  44. (status) => status === 'notFetched' || status === 'fetching'
  45. );
  46. export const getIsTopicDetailsFetched = createSelector(
  47. getTopicDetailsFetchingStatus,
  48. (status) => status === 'fetched'
  49. );
  50. export const getIsTopicMessagesFetched = createSelector(
  51. getTopicMessagesFetchingStatus,
  52. (status) => status === 'fetched'
  53. );
  54. export const getTopicConfigFetched = createSelector(
  55. getTopicConfigFetchingStatus,
  56. (status) => status === 'fetched'
  57. );
  58. export const getTopicCreated = createSelector(
  59. getTopicCreationStatus,
  60. (status) => status === 'fetched'
  61. );
  62. export const getTopicUpdated = createSelector(
  63. getTopicUpdateStatus,
  64. (status) => status === 'fetched'
  65. );
  66. export const getTopicMessageSchemaFetched = createSelector(
  67. getTopicMessageSchemaFetchingStatus,
  68. (status) => status === 'fetched'
  69. );
  70. export const getTopicMessageSent = createSelector(
  71. getTopicMessageSendingStatus,
  72. (status) => status === 'fetched'
  73. );
  74. export const getTopicMessageSending = createSelector(
  75. getTopicMessageSendingStatus,
  76. (status) => status === 'fetching'
  77. );
  78. export const getTopicPartitionsCountIncreased = createSelector(
  79. getPartitionsCountIncreaseStatus,
  80. (status) => status === 'fetched'
  81. );
  82. export const getTopicReplicationFactorUpdated = createSelector(
  83. getReplicationFactorUpdateStatus,
  84. (status) => status === 'fetched'
  85. );
  86. export const getTopicList = createSelector(
  87. getAreTopicsFetched,
  88. getAllNames,
  89. getTopicMap,
  90. (isFetched, allNames, byName) => {
  91. if (!isFetched) {
  92. return [];
  93. }
  94. return allNames.map((name) => byName[name]);
  95. }
  96. );
  97. const getTopicName = (_: RootState, topicName: TopicName) => topicName;
  98. export const getTopicByName = createSelector(
  99. getTopicMap,
  100. getTopicName,
  101. (topics, topicName) => topics[topicName]
  102. );
  103. export const getPartitionsByTopicName = createSelector(
  104. getTopicMap,
  105. getTopicName,
  106. (topics, topicName) => topics[topicName].partitions as Partition[]
  107. );
  108. export const getFullTopic = createSelector(getTopicByName, (topic) =>
  109. topic && topic.config && !!topic.partitionCount ? topic : undefined
  110. );
  111. export const getTopicConfig = createSelector(
  112. getTopicByName,
  113. ({ config }) => config
  114. );
  115. export const getTopicConfigByParamName = createSelector(
  116. getTopicConfig,
  117. (config) => {
  118. const byParamName: TopicConfigByName = {
  119. byName: {},
  120. };
  121. if (config) {
  122. config.forEach((param) => {
  123. byParamName.byName[param.name] = param;
  124. });
  125. }
  126. return byParamName;
  127. }
  128. );
  129. export const getTopicsSearch = createSelector(
  130. topicsState,
  131. (state) => state.search
  132. );
  133. export const getTopicsOrderBy = createSelector(
  134. topicsState,
  135. (state) => state.orderBy
  136. );
  137. export const getIsTopicInternal = createSelector(
  138. getTopicByName,
  139. ({ internal }) => !!internal
  140. );
  141. export const getTopicConsumerGroups = createSelector(
  142. getTopicMap,
  143. getTopicName,
  144. (topics, topicName) => topics[topicName].consumerGroups || []
  145. );
  146. export const getMessageSchemaByTopicName = createSelector(
  147. getTopicByName,
  148. (topic) => topic.messageSchema
  149. );