-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathBlockEditForm.tsx
185 lines (177 loc) · 5.99 KB
/
BlockEditForm.tsx
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
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import ChatBubbleOutlineOutlinedIcon from "@mui/icons-material/ChatBubbleOutlineOutlined";
import SettingsApplicationsIcon from "@mui/icons-material/SettingsApplications";
import { FormControlLabel, Grid, Switch, Tab, Tabs } from "@mui/material";
import { FC, Fragment, useEffect, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { ContentContainer, ContentItem } from "@/app-components/dialogs";
import { Input } from "@/app-components/inputs/Input";
import TriggerIcon from "@/app-components/svg/TriggerIcon";
import { TabPanel } from "@/app-components/tabs/TabPanel";
import { useUpdate } from "@/hooks/crud/useUpdate";
import { useToast } from "@/hooks/useToast";
import { useTranslate } from "@/hooks/useTranslate";
import { EntityType } from "@/services/types";
import { IBlock, IBlockAttributes } from "@/types/block.types";
import { ComponentFormProps } from "@/types/common/dialogs.types";
import { OutgoingMessageFormat } from "@/types/message.types";
import BlockFormProvider from "./form/BlockFormProvider";
import { MessageForm } from "./form/MessageForm";
import { OptionsForm } from "./form/OptionsForm";
import { TriggersForm } from "./form/TriggersForm";
type TSelectedTab = "triggers" | "options" | "messages";
export const BlockEditForm: FC<ComponentFormProps<IBlock>> = ({
data: block,
Wrapper = Fragment,
WrapperProps,
...rest
}) => {
const { t } = useTranslate();
const [selectedTab, setSelectedTab] = useState<TSelectedTab>("triggers");
const handleChange = (
_event: React.SyntheticEvent,
newValue: TSelectedTab,
) => {
setSelectedTab(newValue);
};
const { toast } = useToast();
const { mutate: updateBlock } = useUpdate(EntityType.BLOCK, {
onError: () => {
rest.onError?.();
toast.error(t("message.internal_server_error"));
},
onSuccess: () => {
rest.onSuccess?.();
toast.success(t("message.success_save"));
},
});
const DEFAULT_VALUES = {
name: block?.name || "",
patterns: block?.patterns || [],
outcomes: block?.outcomes || [],
trigger_labels: block?.trigger_labels || [],
trigger_channels: block?.trigger_channels || [],
options: block?.options || {
typing: 0,
content: {
display: OutgoingMessageFormat.list,
top_element_style: "compact",
limit: 2,
},
assignTo: block?.options?.assignTo,
fallback: block?.options?.fallback || {
active: true,
message: [],
max_attempts: 1,
},
},
assign_labels: block?.assign_labels || [],
message: block?.message || [""],
capture_vars: block?.capture_vars || [],
} as IBlockAttributes;
const methods = useForm<IBlockAttributes>({
defaultValues: DEFAULT_VALUES,
});
const {
reset,
register,
formState: { errors },
handleSubmit,
control,
} = methods;
const validationRules = {
name: {
required: t("message.name_is_required"),
},
};
const onSubmitForm = (params: IBlockAttributes) => {
if (block) {
updateBlock({ id: block.id, params });
}
};
const onSubmitError = () => {
toast.error(t("message.missing_fields_error"));
};
useEffect(() => {
if (block) {
reset(DEFAULT_VALUES);
} else {
reset();
}
}, [block, reset]);
return (
<Wrapper
onSubmit={handleSubmit(onSubmitForm, onSubmitError)}
{...WrapperProps}
>
<BlockFormProvider methods={methods} block={block || undefined}>
<ContentContainer>
<ContentItem display="flex" gap={5}>
<Input
label={t("placeholder.name")}
{...register("name", validationRules.name)}
error={!!errors.name}
autoFocus
helperText={errors.name ? errors.name.message : null}
/>
<Controller
name="starts_conversation"
control={control}
defaultValue={block?.starts_conversation}
render={({ field }) => (
<FormControlLabel
label={t(`label.starts_conversation`)}
{...field}
control={<Switch checked={field.value} />}
/>
)}
/>
</ContentItem>
<ContentItem>
<Tabs
orientation="horizontal"
value={selectedTab}
onChange={handleChange}
>
<Tab
value="triggers"
label={t("label.triggers")}
icon={<TriggerIcon />}
iconPosition="start"
/>
<Tab
value="options"
label={t("label.options")}
icon={<SettingsApplicationsIcon />}
iconPosition="start"
/>
<Tab
value="messages"
label={t("label.message")}
icon={<ChatBubbleOutlineOutlinedIcon />}
iconPosition="start"
/>
</Tabs>
<Grid sx={{ padding: 2 }}>
<TabPanel value={selectedTab} index="triggers">
<TriggersForm />
</TabPanel>
<TabPanel value={selectedTab} index="options">
<OptionsForm />
</TabPanel>
<TabPanel value={selectedTab} index="messages">
<MessageForm />
</TabPanel>
</Grid>
</ContentItem>
</ContentContainer>
</BlockFormProvider>
</Wrapper>
);
};