/* * 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> = ({ data: block, Wrapper = Fragment, WrapperProps, ...rest }) => { const { t } = useTranslate(); const [selectedTab, setSelectedTab] = useState("triggers"); const handleChange = ( _event: React.SyntheticEvent, newValue: TSelectedTab, ) => { setSelectedTab(newValue); }; const { toast } = useToast(); const { mutateAsync: 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 || [], 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({ defaultValues: DEFAULT_VALUES, }); const { reset, register, formState: { errors }, handleSubmit, control, } = methods; const validationRules = { name: { required: t("message.name_is_required"), }, }; const onSubmitForm = async (params: IBlockAttributes) => { if (block) { updateBlock({ id: block.id, params }); } }; useEffect(() => { if (block) { reset(DEFAULT_VALUES); } else { reset(); } }, [block, reset]); return ( ( } /> )} /> } iconPosition="start" /> } iconPosition="start" /> } iconPosition="start" /> ); };