import type { IOAuthApps, Serialized } from '@rocket.chat/core-typings'; import { Button, ButtonGroup, TextInput, Field, FieldLabel, FieldRow, FieldError, FieldHint, PasswordInput, TextAreaInput, ToggleSwitch, FieldGroup, } from '@rocket.chat/fuselage'; import { GenericModal, ContextualbarScrollableContent } from '@rocket.chat/ui-client'; import { useSetModal, useToastMessageDispatch, useRoute, useAbsoluteUrl, useTranslation, useEndpoint } from '@rocket.chat/ui-contexts'; import type { ComponentProps } from 'react'; import { useCallback, useId, useMemo } from 'react'; import type { SubmitHandler } from 'react-hook-form'; import { useForm, Controller } from 'react-hook-form'; type EditOAuthAddAppPayload = { name: string; active: boolean; redirectUri: string; }; export type EditOauthAppProps = { onChange: () => void; data: Serialized; } & Omit, 'data'>; const EditOauthApp = ({ onChange, data, ...props }: EditOauthAppProps) => { const t = useTranslation(); const dispatchToastMessage = useToastMessageDispatch(); const { register, handleSubmit, formState: { errors }, control, } = useForm({ defaultValues: { name: data.name, active: data.active, redirectUri: Array.isArray(data.redirectUri) ? data.redirectUri.join('\n') : data.redirectUri, }, mode: 'all', }); const setModal = useSetModal(); const router = useRoute('admin-oauth-apps'); const close = useCallback(() => router.push({}), [router]); const absoluteUrl = useAbsoluteUrl(); const authUrl = useMemo(() => absoluteUrl('oauth/authorize'), [absoluteUrl]); const tokenUrl = useMemo(() => absoluteUrl('oauth/token'), [absoluteUrl]); const saveApp = useEndpoint('POST', '/v1/oauth-apps.update'); const deleteApp = useEndpoint('POST', '/v1/oauth-apps.delete'); const onSubmit: SubmitHandler = async (newData: EditOAuthAddAppPayload) => { try { await saveApp({ ...newData, appId: data._id }); dispatchToastMessage({ type: 'success', message: t('Application_updated') }); onChange(); } catch (error) { dispatchToastMessage({ type: 'error', message: error }); } }; const onDeleteConfirm = useCallback(async () => { try { await deleteApp({ appId: data._id }); dispatchToastMessage({ type: 'success', message: t('Your_entry_has_been_deleted') }); close(); } catch (error) { dispatchToastMessage({ type: 'error', message: error }); } finally { setModal(null); } }, [data._id, close, deleteApp, dispatchToastMessage, setModal, t]); const openConfirmDelete = (): void => setModal( setModal(null)} onClose={(): void => setModal(null)} confirmText={t('Delete')} > {t('Application_delete_warning')} , ); const nameField = useId(); const redirectUriField = useId(); const clientIdField = useId(); const clientSecretField = useId(); const authUrlField = useId(); const tokenUrlField = useId(); return ( {t('Active')} } /> {t('Application_Name')} {t('Give_the_application_a_name_This_will_be_seen_by_your_users')} {errors?.name && {t('Required_field', { field: t('Name') })}} {t('Redirect_URI')} {t('After_OAuth2_authentication_users_will_be_redirected_to_this_URL')} {errors?.redirectUri && {t('Required_field', { field: t('Redirect_URI') })}} {t('Client_ID')} {t('Client_Secret')} {t('Authorization_URL')} {t('Access_Token_URL')} ); }; export default EditOauthApp;