TopicForm.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import React from 'react';
  2. import { useFormContext, Controller } from 'react-hook-form';
  3. import { NOT_SET, BYTES_IN_GB } from 'lib/constants';
  4. import { ClusterName, TopicConfigParams, TopicName } from 'redux/interfaces';
  5. import { ErrorMessage } from '@hookform/error-message';
  6. import Select, { SelectOption } from 'components/common/Select/Select';
  7. import Input from 'components/common/Input/Input';
  8. import { Button } from 'components/common/Button/Button';
  9. import { InputLabel } from 'components/common/Input/InputLabel.styled';
  10. import { FormError } from 'components/common/Input/Input.styled';
  11. import { StyledForm } from 'components/common/Form/Form.styled';
  12. import { clusterTopicPath } from 'lib/paths';
  13. import { useNavigate } from 'react-router-dom';
  14. import useAppParams from 'lib/hooks/useAppParams';
  15. import CustomParams from './CustomParams/CustomParams';
  16. import TimeToRetain from './TimeToRetain';
  17. import * as S from './TopicForm.styled';
  18. export interface Props {
  19. config?: TopicConfigParams;
  20. topicName?: TopicName;
  21. partitionCount?: number;
  22. replicationFactor?: number;
  23. inSyncReplicas?: number;
  24. retentionBytes?: number;
  25. cleanUpPolicy?: string;
  26. isEditing?: boolean;
  27. isSubmitting: boolean;
  28. onSubmit: (e: React.BaseSyntheticEvent) => Promise<void>;
  29. }
  30. const CleanupPolicyOptions: Array<SelectOption> = [
  31. { value: 'delete', label: 'Delete' },
  32. { value: 'compact', label: 'Compact' },
  33. { value: 'compact,delete', label: 'Compact,Delete' },
  34. ];
  35. export const getCleanUpPolicyValue = (cleanUpPolicy?: string) => {
  36. if (!cleanUpPolicy) return undefined;
  37. return CleanupPolicyOptions.find((option: SelectOption) => {
  38. return (
  39. option.value.toString().replace(/,/g, '_') ===
  40. cleanUpPolicy?.toLowerCase()
  41. );
  42. })?.value.toString();
  43. };
  44. const RetentionBytesOptions: Array<SelectOption> = [
  45. { value: NOT_SET, label: 'Not Set' },
  46. { value: BYTES_IN_GB, label: '1 GB' },
  47. { value: BYTES_IN_GB * 10, label: '10 GB' },
  48. { value: BYTES_IN_GB * 20, label: '20 GB' },
  49. { value: BYTES_IN_GB * 50, label: '50 GB' },
  50. ];
  51. const TopicForm: React.FC<Props> = ({
  52. config,
  53. retentionBytes,
  54. topicName,
  55. isEditing,
  56. isSubmitting,
  57. onSubmit,
  58. cleanUpPolicy,
  59. }) => {
  60. const {
  61. control,
  62. formState: { errors, isDirty, isValid },
  63. reset,
  64. } = useFormContext();
  65. const navigate = useNavigate();
  66. const { clusterName } = useAppParams<{ clusterName: ClusterName }>();
  67. const getCleanUpPolicy =
  68. getCleanUpPolicyValue(cleanUpPolicy) || CleanupPolicyOptions[0].value;
  69. const getRetentionBytes =
  70. RetentionBytesOptions.find((option: SelectOption) => {
  71. return option.value === retentionBytes;
  72. })?.value || RetentionBytesOptions[0].value;
  73. const onCancel = () => {
  74. reset();
  75. navigate(clusterTopicPath(clusterName, topicName));
  76. };
  77. return (
  78. <StyledForm onSubmit={onSubmit} aria-label="topic form">
  79. <fieldset disabled={isSubmitting}>
  80. <fieldset disabled={isEditing}>
  81. <S.Column>
  82. <S.NameField>
  83. <InputLabel htmlFor="topicFormName">Topic Name *</InputLabel>
  84. <Input
  85. id="topicFormName"
  86. autoFocus
  87. name="name"
  88. placeholder="Topic Name"
  89. defaultValue={topicName}
  90. />
  91. <FormError>
  92. <ErrorMessage errors={errors} name="name" />
  93. </FormError>
  94. </S.NameField>
  95. </S.Column>
  96. <S.Column>
  97. {!isEditing && (
  98. <div>
  99. <InputLabel htmlFor="topicFormNumberOfPartitions">
  100. Number of partitions *
  101. </InputLabel>
  102. <Input
  103. id="topicFormNumberOfPartitions"
  104. type="number"
  105. placeholder="Number of partitions"
  106. min="1"
  107. name="partitions"
  108. positiveOnly
  109. integerOnly
  110. />
  111. <FormError>
  112. <ErrorMessage errors={errors} name="partitions" />
  113. </FormError>
  114. </div>
  115. )}
  116. <div>
  117. <InputLabel
  118. id="topicFormCleanupPolicyLabel"
  119. htmlFor="topicFormCleanupPolicy"
  120. >
  121. Cleanup policy
  122. </InputLabel>
  123. <Controller
  124. defaultValue={CleanupPolicyOptions[0].value}
  125. control={control}
  126. name="cleanupPolicy"
  127. render={({ field: { name, onChange } }) => (
  128. <Select
  129. id="topicFormCleanupPolicy"
  130. aria-labelledby="topicFormCleanupPolicyLabel"
  131. name={name}
  132. value={getCleanUpPolicy}
  133. onChange={onChange}
  134. minWidth="250px"
  135. options={CleanupPolicyOptions}
  136. />
  137. )}
  138. />
  139. </div>
  140. </S.Column>
  141. </fieldset>
  142. <S.Column>
  143. <div>
  144. <InputLabel htmlFor="topicFormMinInSyncReplicas">
  145. Min In Sync Replicas
  146. </InputLabel>
  147. <Input
  148. id="topicFormMinInSyncReplicas"
  149. type="number"
  150. placeholder="Min In Sync Replicas"
  151. min="1"
  152. name="minInSyncReplicas"
  153. positiveOnly
  154. integerOnly
  155. />
  156. <FormError>
  157. <ErrorMessage errors={errors} name="minInSyncReplicas" />
  158. </FormError>
  159. </div>
  160. {!isEditing && (
  161. <div>
  162. <InputLabel htmlFor="topicFormReplicationFactor">
  163. Replication Factor
  164. </InputLabel>
  165. <Input
  166. id="topicFormReplicationFactor"
  167. type="number"
  168. placeholder="Replication Factor"
  169. min="1"
  170. name="replicationFactor"
  171. positiveOnly
  172. integerOnly
  173. />
  174. <FormError>
  175. <ErrorMessage errors={errors} name="replicationFactor" />
  176. </FormError>
  177. </div>
  178. )}
  179. </S.Column>
  180. <S.Column>
  181. <div>
  182. <TimeToRetain isSubmitting={isSubmitting} />
  183. </div>
  184. </S.Column>
  185. <S.Column>
  186. <div>
  187. <InputLabel
  188. id="topicFormRetentionBytesLabel"
  189. htmlFor="topicFormRetentionBytes"
  190. >
  191. Max size on disk in GB
  192. </InputLabel>
  193. <Controller
  194. control={control}
  195. name="retentionBytes"
  196. defaultValue={RetentionBytesOptions[0].value}
  197. render={({ field: { name, onChange } }) => (
  198. <Select
  199. id="topicFormRetentionBytes"
  200. aria-labelledby="topicFormRetentionBytesLabel"
  201. name={name}
  202. value={getRetentionBytes}
  203. onChange={onChange}
  204. minWidth="100%"
  205. options={RetentionBytesOptions}
  206. />
  207. )}
  208. />
  209. </div>
  210. <div>
  211. <InputLabel htmlFor="topicFormMaxMessageBytes">
  212. Maximum message size in bytes
  213. </InputLabel>
  214. <Input
  215. id="topicFormMaxMessageBytes"
  216. type="number"
  217. placeholder="Maximum message size"
  218. min="1"
  219. name="maxMessageBytes"
  220. positiveOnly
  221. integerOnly
  222. />
  223. <FormError>
  224. <ErrorMessage errors={errors} name="maxMessageBytes" />
  225. </FormError>
  226. </div>
  227. </S.Column>
  228. <S.CustomParamsHeading>Custom parameters</S.CustomParamsHeading>
  229. <CustomParams
  230. config={config}
  231. isSubmitting={isSubmitting}
  232. isEditing={isEditing}
  233. />
  234. <S.ButtonWrapper>
  235. <Button
  236. type="button"
  237. buttonType="primary"
  238. buttonSize="L"
  239. onClick={onCancel}
  240. >
  241. Cancel
  242. </Button>
  243. <Button
  244. type="submit"
  245. buttonType="primary"
  246. buttonSize="L"
  247. disabled={!isValid || isSubmitting || !isDirty}
  248. >
  249. {isEditing ? 'Update topic' : 'Create topic'}
  250. </Button>
  251. </S.ButtonWrapper>
  252. </fieldset>
  253. </StyledForm>
  254. );
  255. };
  256. export default TopicForm;