selectors.ts 3.6 KB

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