import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings'; import { Icon, Box } from '@rocket.chat/fuselage'; import { Field, FieldGroup, ToggleSwitch, TextInput, TextAreaInput, FieldHint, FieldLabel, FieldRow, FieldError, } from '@rocket.chat/fuselage-forms'; import { useStableCallback } from '@rocket.chat/fuselage-hooks'; import { GenericModal } from '@rocket.chat/ui-client'; import { useTranslation, useEndpoint, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; import { useMutation } from '@tanstack/react-query'; import { useState } from 'react'; import { useForm, Controller } from 'react-hook-form'; import { useEncryptedRoomDescription } from '../../navbar/NavBarPagesGroup/actions/useEncryptedRoomDescription'; import { useGoToRoom } from '../../views/room/hooks/useGoToRoom'; import RoomAutoComplete from '../RoomAutoComplete'; import UserAutoCompleteMultiple from '../UserAutoCompleteMultiple'; import DefaultParentRoomField from './DefaultParentRoomField'; type CreateDiscussionFormValues = { name: string; parentRoom: IRoom['_id']; encrypted: boolean; usernames: Array; firstMessage: string; topic: string; }; export type CreateDiscussionProps = { parentMessageId?: IMessage['_id']; encryptedParentRoom?: boolean; onClose: () => void; onSuccess?: (rid: string) => void | Promise; defaultParentRoom?: IRoom['_id']; nameSuggestion?: string; }; const CreateDiscussion = ({ onClose, onSuccess, defaultParentRoom, parentMessageId, nameSuggestion, encryptedParentRoom = false, }: CreateDiscussionProps) => { const t = useTranslation(); const [encryptedDisabled, setEncryptedDisabled] = useState(encryptedParentRoom); const { formState: { errors }, handleSubmit, control, watch, setValue, } = useForm({ defaultValues: { name: nameSuggestion || '', parentRoom: '', encrypted: encryptedParentRoom, usernames: [], firstMessage: '', topic: '', }, }); const onParentRoomChange = useStableCallback((room: IRoom | undefined) => { if (!room) { return; } setValue('encrypted', room.encrypted === true); setEncryptedDisabled(room.encrypted === true); }); const { encrypted } = watch(); const createDiscussion = useEndpoint('POST', '/v1/rooms.createDiscussion'); const goToRoom = useGoToRoom(); const dispatchToastMessage = useToastMessageDispatch(); const createDiscussionMutation = useMutation({ mutationFn: createDiscussion, onSuccess: ({ discussion }) => { goToRoom(discussion._id); void onSuccess?.(discussion._id); onClose(); }, onError: (error) => { dispatchToastMessage({ type: 'error', message: error }); }, }); const handleCreate = async ({ name, parentRoom, encrypted, usernames, firstMessage, topic }: CreateDiscussionFormValues) => { createDiscussionMutation.mutate({ prid: defaultParentRoom || parentRoom, t_name: name, users: usernames, reply: encrypted ? undefined : firstMessage, topic, ...(parentMessageId && { pmid: parentMessageId }), }); }; const getEncryptedHint = useEncryptedRoomDescription('discussion'); return ( } confirmText={t('Create')} cancelText={t('Cancel')} confirmLoading={createDiscussionMutation.isPending} > {t('Discussion_description')} {t('Discussion_target_channel')} {defaultParentRoom && ( } /> )} {!defaultParentRoom && ( ( (encrypted ? : null)} /> )} /> )} {errors.parentRoom && {errors.parentRoom.message}} {t('Name')} ( } error={errors.name?.message} /> )} /> {errors.name && {errors.name.message}} {t('Topic')} } /> {t('Displayed_next_to_name')} {t('Members')} } /> {t('Discussion_first_message_title')} } /> {encrypted ? ( {t('Discussion_first_message_disabled_due_to_e2e')} ) : ( {t('First_message_hint')} )} {t('Encrypted')} } /> {getEncryptedHint({ isPrivate: true, encrypted })} ); }; export default CreateDiscussion;