selectors.ts 4.2 KB

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