-
Notifications
You must be signed in to change notification settings - Fork 421
Expand file tree
/
Copy pathtopic-input.tsx
More file actions
122 lines (106 loc) · 3.75 KB
/
topic-input.tsx
File metadata and controls
122 lines (106 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright 2022 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file https://github.com/redpanda-data/redpanda/blob/dev/licenses/bsl.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
import {
Checkbox,
FormControl,
FormErrorMessage,
FormHelperText,
Grid,
Input,
isMultiValue,
Select,
} from '@redpanda-data/ui';
import { useMemo, useState } from 'react';
import { useTopicsQuery } from '../../../../../react-query/api/topic';
import type { Property } from '../../../../../state/connect/state';
import { ExpandableText } from '../../../../misc/expandable-text';
const setPropertyValue = (property: Property, value: Property['value']) => {
property.value = value;
};
const incrementErrorIndex = (property: Property) => {
property.currentErrorIndex += 1;
};
export const TopicInput = (p: { properties: Property[]; connectorType: 'sink' | 'source' }) => {
const propsMap = useMemo(() => new Map(p.properties.map((prop) => [prop.name, prop])), [p.properties]);
const topicsRegex = p.properties.find((x) => x.name === 'topics.regex');
const initialSelection = topicsRegex?.value ? 'topics.regex' : 'topics';
const [selected, setSelected] = useState(initialSelection);
const { data: topicsData } = useTopicsQuery();
const property = propsMap.get(selected);
const isRegex = selected === 'topics.regex';
if (!property) {
return null;
}
const showErrors = property.errors.length > 0;
const errors = showErrors ? property.errors : property.lastErrors;
const errorToShow = showErrors ? errors[property.currentErrorIndex % errors.length] : undefined;
const cycleError = showErrors
? () => {
incrementErrorIndex(property);
}
: undefined;
return (
<Grid gap="10" templateColumns="1fr">
<FormControl position="relative">
{propsMap.has('topics.regex') && (
<Checkbox
isChecked={isRegex}
onChange={(e) => {
setPropertyValue(property, '');
setSelected(e.target.checked ? 'topics.regex' : 'topics');
}}
>
Use regular expressions
</Checkbox>
)}
<FormHelperText mb={15}>
<ExpandableText maxChars={60}>{property.entry.definition.documentation}</ExpandableText>
</FormHelperText>
{/* A 'source' connector imports data into the cluster. So we let the user choose the name of the topic directly */}
{isRegex || p.connectorType === 'source' ? (
<Input
autoComplete="off"
onChange={(e) => {
setPropertyValue(property, e.target.value);
}}
spellCheck={false}
value={String(property.value)}
/>
) : (
<Select
isMulti
onChange={(v) => {
if (isMultiValue(v)) {
setPropertyValue(property, v.map(({ value }) => value)?.join(',') ?? []);
}
}}
options={topicsData?.topics?.map((x) => ({ value: x.topicName, label: x.topicName })) ?? []}
value={
property.value
? property.value
?.toString()
.split(',')
.map((val) => ({
value: val,
label: val,
}))
: []
}
/>
)}
{Boolean(showErrors) && <FormErrorMessage onClick={cycleError}>{errorToShow}</FormErrorMessage>}
</FormControl>
{/* <Box p="4" >
<h2>Matching Topics</h2>
</Box> */}
</Grid>
);
};