import React from 'react'; import { useFormContext, Controller } from 'react-hook-form'; import { NOT_SET, BYTES_IN_GB } from 'lib/constants'; import { ClusterName, TopicConfigParams, TopicName } from 'redux/interfaces'; import { ErrorMessage } from '@hookform/error-message'; import Select, { SelectOption } from 'components/common/Select/Select'; import Input from 'components/common/Input/Input'; import { Button } from 'components/common/Button/Button'; import { InputLabel } from 'components/common/Input/InputLabel.styled'; import { FormError } from 'components/common/Input/Input.styled'; import { StyledForm } from 'components/common/Form/Form.styled'; import { clusterTopicPath } from 'lib/paths'; import { useNavigate } from 'react-router-dom'; import useAppParams from 'lib/hooks/useAppParams'; import CustomParams from './CustomParams/CustomParams'; import TimeToRetain from './TimeToRetain'; import * as S from './TopicForm.styled'; export interface Props { config?: TopicConfigParams; topicName?: TopicName; partitionCount?: number; replicationFactor?: number; inSyncReplicas?: number; retentionBytes?: number; cleanUpPolicy?: string; isEditing?: boolean; isSubmitting: boolean; onSubmit: (e: React.BaseSyntheticEvent) => Promise; } const CleanupPolicyOptions: Array = [ { value: 'delete', label: 'Delete' }, { value: 'compact', label: 'Compact' }, { value: 'compact,delete', label: 'Compact,Delete' }, ]; export const getCleanUpPolicyValue = (cleanUpPolicy?: string) => { if (!cleanUpPolicy) return undefined; return CleanupPolicyOptions.find((option: SelectOption) => { return ( option.value.toString().replace(/,/g, '_') === cleanUpPolicy?.toLowerCase() ); })?.value.toString(); }; const RetentionBytesOptions: Array = [ { value: NOT_SET, label: 'Not Set' }, { value: BYTES_IN_GB, label: '1 GB' }, { value: BYTES_IN_GB * 10, label: '10 GB' }, { value: BYTES_IN_GB * 20, label: '20 GB' }, { value: BYTES_IN_GB * 50, label: '50 GB' }, ]; const TopicForm: React.FC = ({ config, retentionBytes, topicName, isEditing, isSubmitting, onSubmit, cleanUpPolicy, }) => { const { control, formState: { errors, isDirty, isValid }, reset, } = useFormContext(); const navigate = useNavigate(); const { clusterName } = useAppParams<{ clusterName: ClusterName }>(); const getCleanUpPolicy = getCleanUpPolicyValue(cleanUpPolicy) || CleanupPolicyOptions[0].value; const getRetentionBytes = RetentionBytesOptions.find((option: SelectOption) => { return option.value === retentionBytes; })?.value || RetentionBytesOptions[0].value; const onCancel = () => { reset(); navigate(clusterTopicPath(clusterName, topicName)); }; return (
Topic Name * {!isEditing && (
Number of partitions *
)}
Cleanup policy (
{!isEditing && (
Replication Factor
)}
Max size on disk in GB (
Custom parameters
); }; export default TopicForm;