TopicForm.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. autoComplete="off"
  91. />
  92. <FormError>
  93. <ErrorMessage errors={errors} name="name" />
  94. </FormError>
  95. </S.NameField>
  96. </S.Column>
  97. <S.Column>
  98. {!isEditing && (
  99. <div>
  100. <InputLabel htmlFor="topicFormNumberOfPartitions">
  101. Number of Partitions *
  102. </InputLabel>
  103. <Input
  104. id="topicFormNumberOfPartitions"
  105. type="number"
  106. placeholder="Number of Partitions"
  107. min="1"
  108. name="partitions"
  109. positiveOnly
  110. integerOnly
  111. />
  112. <FormError>
  113. <ErrorMessage errors={errors} name="partitions" />
  114. </FormError>
  115. </div>
  116. )}
  117. <div>
  118. <InputLabel
  119. id="topicFormCleanupPolicyLabel"
  120. htmlFor="topicFormCleanupPolicy"
  121. >
  122. Cleanup policy
  123. </InputLabel>
  124. <Controller
  125. defaultValue={CleanupPolicyOptions[0].value}
  126. control={control}
  127. name="cleanupPolicy"
  128. render={({ field: { name, onChange } }) => (
  129. <Select
  130. id="topicFormCleanupPolicy"
  131. aria-labelledby="topicFormCleanupPolicyLabel"
  132. name={name}
  133. value={getCleanUpPolicy}
  134. onChange={onChange}
  135. minWidth="250px"
  136. options={CleanupPolicyOptions}
  137. />
  138. )}
  139. />
  140. </div>
  141. </S.Column>
  142. </fieldset>
  143. <S.Column>
  144. <div>
  145. <InputLabel htmlFor="topicFormMinInSyncReplicas">
  146. Min In Sync Replicas
  147. </InputLabel>
  148. <Input
  149. id="topicFormMinInSyncReplicas"
  150. type="number"
  151. placeholder="Min In Sync Replicas"
  152. min="1"
  153. name="minInSyncReplicas"
  154. positiveOnly
  155. integerOnly
  156. />
  157. <FormError>
  158. <ErrorMessage errors={errors} name="minInSyncReplicas" />
  159. </FormError>
  160. </div>
  161. {!isEditing && (
  162. <div>
  163. <InputLabel htmlFor="topicFormReplicationFactor">
  164. Replication Factor
  165. </InputLabel>
  166. <Input
  167. id="topicFormReplicationFactor"
  168. type="number"
  169. placeholder="Replication Factor"
  170. min="1"
  171. name="replicationFactor"
  172. positiveOnly
  173. integerOnly
  174. />
  175. <FormError>
  176. <ErrorMessage errors={errors} name="replicationFactor" />
  177. </FormError>
  178. </div>
  179. )}
  180. </S.Column>
  181. <S.Column>
  182. <div>
  183. <TimeToRetain isSubmitting={isSubmitting} />
  184. </div>
  185. </S.Column>
  186. <S.Column>
  187. <div>
  188. <InputLabel
  189. id="topicFormRetentionBytesLabel"
  190. htmlFor="topicFormRetentionBytes"
  191. >
  192. Max size on disk in GB
  193. </InputLabel>
  194. <Controller
  195. control={control}
  196. name="retentionBytes"
  197. defaultValue={RetentionBytesOptions[0].value}
  198. render={({ field: { name, onChange } }) => (
  199. <Select
  200. id="topicFormRetentionBytes"
  201. aria-labelledby="topicFormRetentionBytesLabel"
  202. name={name}
  203. value={getRetentionBytes}
  204. onChange={onChange}
  205. minWidth="100%"
  206. options={RetentionBytesOptions}
  207. />
  208. )}
  209. />
  210. </div>
  211. <div>
  212. <InputLabel htmlFor="topicFormMaxMessageBytes">
  213. Maximum message size in bytes
  214. </InputLabel>
  215. <S.MessageSizeInput
  216. id="topicFormMaxMessageBytes"
  217. type="number"
  218. placeholder="Maximum message size"
  219. min="1"
  220. name="maxMessageBytes"
  221. positiveOnly
  222. integerOnly
  223. />
  224. <FormError>
  225. <ErrorMessage errors={errors} name="maxMessageBytes" />
  226. </FormError>
  227. </div>
  228. </S.Column>
  229. <S.CustomParamsHeading>Custom parameters</S.CustomParamsHeading>
  230. <CustomParams
  231. config={config}
  232. isSubmitting={isSubmitting}
  233. isEditing={isEditing}
  234. />
  235. <S.ButtonWrapper>
  236. <Button
  237. type="button"
  238. buttonType="secondary"
  239. buttonSize="L"
  240. onClick={onCancel}
  241. >
  242. Cancel
  243. </Button>
  244. <Button
  245. type="submit"
  246. buttonType="primary"
  247. buttonSize="L"
  248. disabled={!isValid || isSubmitting || !isDirty}
  249. >
  250. {isEditing ? 'Update topic' : 'Create topic'}
  251. </Button>
  252. </S.ButtonWrapper>
  253. </fieldset>
  254. </StyledForm>
  255. );
  256. };
  257. export default TopicForm;