-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathAddAIGatewayForm.tsx
More file actions
192 lines (182 loc) · 6.17 KB
/
AddAIGatewayForm.tsx
File metadata and controls
192 lines (182 loc) · 6.17 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React, { useCallback, useEffect, useRef } from "react";
import {
Form,
MenuItem,
Select, TextField, useTheme
} from "@wso2/oxygen-ui";
import { Check } from "@wso2/oxygen-ui-icons-react";
import { useListEnvironments } from "@agent-management-platform/api-client";
import type { AddGatewayFormValues } from "../form/schema";
import { useParams } from "react-router-dom";
function toSlug(value: string): string {
return value
.toLowerCase()
.trim()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-]/g, "")
.replace(/^-+|-+$/g, "");
}
interface AddAIGatewayFormProps {
formData: AddGatewayFormValues;
setFormData: React.Dispatch<React.SetStateAction<AddGatewayFormValues>>;
errors: Partial<Record<keyof AddGatewayFormValues, string>>;
setFieldError: (
field: keyof AddGatewayFormValues,
error: string | undefined
) => void;
validateField: (
field: keyof AddGatewayFormValues,
value: unknown,
fullData?: AddGatewayFormValues
) => string | undefined;
}
export const AddAIGatewayForm: React.FC<AddAIGatewayFormProps> = ({
formData,
setFormData,
errors,
setFieldError,
validateField,
}) => {
const { orgId } = useParams<{ orgId: string }>();
const theme = useTheme();
const { data: environments = [] } = useListEnvironments({
orgName: orgId,
});
const hasInitializedEnvironments = useRef(false);
useEffect(() => {
if (environments.length > 0 && !hasInitializedEnvironments.current) {
hasInitializedEnvironments.current = true;
const firstEnvId = environments[0].id;
if (firstEnvId) {
setFormData((prev) => ({
...prev,
environmentIds: [firstEnvId],
}));
}
}
}, [environments, setFormData]);
const handleFieldChange = useCallback(
(field: keyof AddGatewayFormValues, value: unknown) => {
const newData = { ...formData, [field]: value };
setFormData(newData);
const error = validateField(field, value, newData);
setFieldError(field, error);
},
[formData, setFormData, validateField, setFieldError]
);
useEffect(() => {
if (formData.displayName) {
const slug = toSlug(formData.displayName);
if (slug !== formData.name) {
setFormData((prev: AddGatewayFormValues) => ({ ...prev, name: slug }));
const error = validateField("name", slug, { ...formData, name: slug });
setFieldError("name", error);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [formData.displayName, setFieldError]);
const isNameValid =
formData.name &&
/^[a-z0-9-]+$/.test(formData.name) &&
formData.name.length <= 64;
return (
<Form.Stack spacing={3}>
<Form.Section>
<Form.Subheader>Gateway Details</Form.Subheader>
<Form.Stack spacing={2}>
<Form.ElementWrapper label="Name" name="displayName">
<TextField
id="displayName"
placeholder="e.g., Production Gateway"
value={formData.displayName}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
handleFieldChange("displayName", e.target.value)
}
error={!!errors.displayName || !!errors.name}
helperText={
errors.displayName || errors.name
}
fullWidth
slotProps={{
input: {
endAdornment:
!!formData.displayName && isNameValid ? (
<Check
size={20}
color={theme.vars?.palette.success.main}
/>
) : null,
},
}}
/>
</Form.ElementWrapper>
<Form.ElementWrapper label="Virtual Host" name="vhost">
<TextField
id="vhost"
placeholder="e.g., api.production.example.com"
value={formData.vhost}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
handleFieldChange("vhost", e.target.value)
}
error={!!errors.vhost}
helperText={errors.vhost || "FQDN or IP for the gateway"}
fullWidth
/>
</Form.ElementWrapper>
{environments.length > 1 && (
<Form.ElementWrapper
label="Environments"
name="environmentIds"
>
<Select
multiple
value={formData.environmentIds ?? []}
onChange={(e: { target: { value: unknown } }) => {
const v = e.target.value;
handleFieldChange(
"environmentIds",
Array.isArray(v) ? v : [v]
);
}}
renderValue={(selected) =>
(selected as string[])
.map((id) => {
const env = environments.find((e) => e.id === id);
return env?.displayName || env?.name || id;
})
.join(", ")
}
fullWidth
displayEmpty
>
{environments
.filter((env) => env.id)
.map((env) => (
<MenuItem key={env.id} value={env.id!}>
{env.displayName || env.name}
</MenuItem>
))}
</Select>
</Form.ElementWrapper>
)}
</Form.Stack>
</Form.Section>
</Form.Stack>
);
};