import type { SelectOption } from '@rocket.chat/fuselage'; import { Box, TextInput, Button, Margins, Select, FieldError, FieldGroup, Field, FieldRow } from '@rocket.chat/fuselage'; import { GenericModal } from '@rocket.chat/ui-client'; import { useSetModal, useToastMessageDispatch, useUserId, useEndpoint } from '@rocket.chat/ui-contexts'; import { useCallback, useId, useMemo } from 'react'; import { Controller, useForm } from 'react-hook-form'; import { Trans, useTranslation } from 'react-i18next'; type AddTokenFormData = { name: string; bypassTwoFactor: string; }; export type AddTokenProps = { reload: () => void; }; const AddToken = ({ reload }: AddTokenProps) => { const { t } = useTranslation(); const userId = useUserId(); const setModal = useSetModal(); const createTokenFn = useEndpoint('POST', '/v1/users.generatePersonalAccessToken'); const dispatchToastMessage = useToastMessageDispatch(); const initialValues = useMemo(() => ({ name: '', bypassTwoFactor: 'require' }), []); const { handleSubmit, control, reset, formState: { errors }, } = useForm({ defaultValues: initialValues }); const twoFactorAuthOptions: SelectOption[] = useMemo( () => [ ['require', t('Require_Two_Factor_Authentication')], ['bypass', t('Ignore_Two_Factor_Authentication')], ], [t], ); const handleAddToken = useCallback( async ({ name: tokenName, bypassTwoFactor }: AddTokenFormData) => { try { const { token } = await createTokenFn({ tokenName, bypassTwoFactor: bypassTwoFactor === 'bypass' }); const handleDismissModal = () => { setModal(null); reload(); reset(); }; setModal( , ); } catch (error) { dispatchToastMessage({ type: 'error', message: error }); } }, [createTokenFn, dispatchToastMessage, reload, reset, setModal, t, userId], ); const nameErrorId = useId(); return ( (value.trim() ? undefined : t('Please_provide_a_name_for_your_token')) }} render={({ field }) => ( )} />