Topic.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import React, { Suspense } from 'react';
  2. import { NavLink, Route, Routes, useNavigate } from 'react-router-dom';
  3. import {
  4. clusterTopicConsumerGroupsRelativePath,
  5. clusterTopicEditRelativePath,
  6. clusterTopicMessagesRelativePath,
  7. clusterTopicSettingsRelativePath,
  8. clusterTopicsPath,
  9. clusterTopicStatisticsRelativePath,
  10. RouteParamsClusterTopic,
  11. } from 'lib/paths';
  12. import ClusterContext from 'components/contexts/ClusterContext';
  13. import PageHeading from 'components/common/PageHeading/PageHeading';
  14. import {
  15. ActionButton,
  16. ActionNavLink,
  17. ActionDropdownItem,
  18. } from 'components/common/ActionComponent';
  19. import Navbar from 'components/common/Navigation/Navbar.styled';
  20. import { useAppDispatch } from 'lib/hooks/redux';
  21. import useAppParams from 'lib/hooks/useAppParams';
  22. import { Dropdown, DropdownItemHint } from 'components/common/Dropdown';
  23. import {
  24. useClearTopicMessages,
  25. useDeleteTopic,
  26. useRecreateTopic,
  27. useTopicDetails,
  28. } from 'lib/hooks/api/topics';
  29. import { resetTopicMessages } from 'redux/reducers/topicMessages/topicMessagesSlice';
  30. import { Action, CleanUpPolicy, ResourceType } from 'generated-sources';
  31. import PageLoader from 'components/common/PageLoader/PageLoader';
  32. import SlidingSidebar from 'components/common/SlidingSidebar';
  33. import useBoolean from 'lib/hooks/useBoolean';
  34. import Messages from './Messages/Messages';
  35. import Overview from './Overview/Overview';
  36. import Settings from './Settings/Settings';
  37. import TopicConsumerGroups from './ConsumerGroups/TopicConsumerGroups';
  38. import Statistics from './Statistics/Statistics';
  39. import Edit from './Edit/Edit';
  40. import SendMessage from './SendMessage/SendMessage';
  41. const Topic: React.FC = () => {
  42. const dispatch = useAppDispatch();
  43. const {
  44. value: isSidebarOpen,
  45. setFalse: closeSidebar,
  46. setTrue: openSidebar,
  47. } = useBoolean(false);
  48. const { clusterName, topicName } = useAppParams<RouteParamsClusterTopic>();
  49. const navigate = useNavigate();
  50. const deleteTopic = useDeleteTopic(clusterName);
  51. const recreateTopic = useRecreateTopic({ clusterName, topicName });
  52. const { data } = useTopicDetails({ clusterName, topicName });
  53. const { isReadOnly, isTopicDeletionAllowed } =
  54. React.useContext(ClusterContext);
  55. const deleteTopicHandler = async () => {
  56. await deleteTopic.mutateAsync(topicName);
  57. navigate(clusterTopicsPath(clusterName));
  58. };
  59. React.useEffect(() => {
  60. return () => {
  61. dispatch(resetTopicMessages());
  62. };
  63. }, []);
  64. const clearMessages = useClearTopicMessages(clusterName);
  65. const clearTopicMessagesHandler = async () => {
  66. await clearMessages.mutateAsync(topicName);
  67. };
  68. const canCleanup = data?.cleanUpPolicy === CleanUpPolicy.DELETE;
  69. return (
  70. <>
  71. <PageHeading
  72. text={topicName}
  73. backText="Topics"
  74. backTo={clusterTopicsPath(clusterName)}
  75. >
  76. <ActionButton
  77. buttonSize="M"
  78. buttonType="primary"
  79. onClick={openSidebar}
  80. disabled={isReadOnly}
  81. permission={{
  82. resource: ResourceType.TOPIC,
  83. action: Action.MESSAGES_PRODUCE,
  84. value: topicName,
  85. }}
  86. >
  87. Produce Message
  88. </ActionButton>
  89. <Dropdown disabled={isReadOnly || data?.internal}>
  90. <ActionDropdownItem
  91. onClick={() => navigate(clusterTopicEditRelativePath)}
  92. permission={{
  93. resource: ResourceType.TOPIC,
  94. action: Action.EDIT,
  95. value: topicName,
  96. }}
  97. >
  98. Edit settings
  99. <DropdownItemHint>
  100. Pay attention! This operation has
  101. <br />
  102. especially important consequences.
  103. </DropdownItemHint>
  104. </ActionDropdownItem>
  105. <ActionDropdownItem
  106. onClick={clearTopicMessagesHandler}
  107. confirm="Are you sure want to clear topic messages?"
  108. disabled={!canCleanup}
  109. danger
  110. permission={{
  111. resource: ResourceType.TOPIC,
  112. action: Action.MESSAGES_DELETE,
  113. value: topicName,
  114. }}
  115. >
  116. Clear messages
  117. <DropdownItemHint>
  118. Clearing messages is only allowed for topics
  119. <br />
  120. with DELETE policy
  121. </DropdownItemHint>
  122. </ActionDropdownItem>
  123. <ActionDropdownItem
  124. onClick={recreateTopic.mutateAsync}
  125. confirm={
  126. <>
  127. Are you sure want to recreate <b>{topicName}</b> topic?
  128. </>
  129. }
  130. danger
  131. permission={{
  132. resource: ResourceType.TOPIC,
  133. action: [Action.MESSAGES_READ, Action.CREATE, Action.DELETE],
  134. value: topicName,
  135. }}
  136. >
  137. Recreate Topic
  138. </ActionDropdownItem>
  139. <ActionDropdownItem
  140. onClick={deleteTopicHandler}
  141. confirm={
  142. <>
  143. Are you sure want to remove <b>{topicName}</b> topic?
  144. </>
  145. }
  146. disabled={!isTopicDeletionAllowed}
  147. danger
  148. permission={{
  149. resource: ResourceType.TOPIC,
  150. action: Action.DELETE,
  151. value: topicName,
  152. }}
  153. >
  154. Remove Topic
  155. {!isTopicDeletionAllowed && (
  156. <DropdownItemHint>
  157. The topic deletion is restricted at the application
  158. <br />
  159. configuration level
  160. </DropdownItemHint>
  161. )}
  162. </ActionDropdownItem>
  163. </Dropdown>
  164. </PageHeading>
  165. <Navbar role="navigation">
  166. <NavLink
  167. to="."
  168. className={({ isActive }) => (isActive ? 'is-active' : '')}
  169. end
  170. >
  171. Overview
  172. </NavLink>
  173. <ActionNavLink
  174. to={clusterTopicMessagesRelativePath}
  175. className={({ isActive }) => (isActive ? 'is-active' : '')}
  176. permission={{
  177. resource: ResourceType.TOPIC,
  178. action: Action.MESSAGES_READ,
  179. value: topicName,
  180. }}
  181. >
  182. Messages
  183. </ActionNavLink>
  184. <NavLink
  185. to={clusterTopicConsumerGroupsRelativePath}
  186. className={({ isActive }) => (isActive ? 'is-active' : '')}
  187. >
  188. Consumers
  189. </NavLink>
  190. <NavLink
  191. to={clusterTopicSettingsRelativePath}
  192. className={({ isActive }) => (isActive ? 'is-active' : '')}
  193. >
  194. Settings
  195. </NavLink>
  196. <NavLink
  197. to={clusterTopicStatisticsRelativePath}
  198. className={({ isActive }) => (isActive ? 'is-active' : '')}
  199. >
  200. Statistics
  201. </NavLink>
  202. </Navbar>
  203. <Suspense fallback={<PageLoader />}>
  204. <Routes>
  205. <Route index element={<Overview />} />
  206. <Route
  207. path={clusterTopicMessagesRelativePath}
  208. element={<Messages />}
  209. />
  210. <Route
  211. path={clusterTopicSettingsRelativePath}
  212. element={<Settings />}
  213. />
  214. <Route
  215. path={clusterTopicConsumerGroupsRelativePath}
  216. element={<TopicConsumerGroups />}
  217. />
  218. <Route
  219. path={clusterTopicStatisticsRelativePath}
  220. element={<Statistics />}
  221. />
  222. <Route path={clusterTopicEditRelativePath} element={<Edit />} />
  223. </Routes>
  224. </Suspense>
  225. <SlidingSidebar
  226. open={isSidebarOpen}
  227. onClose={closeSidebar}
  228. title="Produce Message"
  229. >
  230. <Suspense fallback={<PageLoader />}>
  231. <SendMessage closeSidebar={closeSidebar} />
  232. </Suspense>
  233. </SlidingSidebar>
  234. </>
  235. );
  236. };
  237. export default Topic;