import { Button, CompactCard, Gridicon } from '@automattic/components'; import { localizeUrl } from '@automattic/i18n-utils'; import { useMobileBreakpoint } from '@automattic/viewport-react'; import { useTranslate } from 'i18n-calypso'; import { useState } from 'react'; import { useDispatch } from 'react-redux'; import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; import InlineSupportLink from 'calypso/components/inline-support-link'; import SectionHeader from 'calypso/components/section-header'; import useAppPasswordsQuery from 'calypso/data/application-passwords/use-app-passwords-query'; import useCreateAppPasswordMutation from 'calypso/data/application-passwords/use-create-app-password-mutation'; import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { errorNotice } from 'calypso/state/notices/actions'; import NewAppPasswordForm from './form'; import AppPasswordsList from './list.jsx'; import NewAppPassword from './new-password'; import './style.scss'; function ApplicationPasswords() { const translate = useTranslate(); const dispatch = useDispatch(); const [ applicationName, setApplicationName ] = useState( '' ); const [ showAddPasswordForm, setShowAddPasswordForm ] = useState( false ); const isMobile = useMobileBreakpoint(); const { data: appPasswords } = useAppPasswordsQuery(); const { createApplicationPassword, isLoading: isCreatingAppPassword, data: newAppPasswordData, reset: clearNewApplicationPassword, } = useCreateAppPasswordMutation( { onError() { dispatch( errorNotice( translate( 'There was a problem creating your application password. Please try again.' ), { duration: 8000, } ) ); }, } ); const newAppPassword = newAppPasswordData?.application_password; const getClickHandler = ( action, callback ) => ( event ) => { dispatch( recordGoogleEvent( 'Me', `Clicked on ${ action }` ) ); callback?.( event ); }; return (
{ ! newAppPassword && ( ) } { newAppPassword ? ( { clearNewApplicationPassword(); setApplicationName( '' ); setShowAddPasswordForm( false ); } ) } /> ) : ( { createApplicationPassword( appName ); setApplicationName( appName ); } } onClickGenerate={ getClickHandler( 'Generate New Application Password Button' ) } onClickCancel={ getClickHandler( 'Cancel Generate New Application Password Button', () => setShowAddPasswordForm( ! showAddPasswordForm ) ) } /> ) } { ! newAppPassword && ( <> { translate( 'With Two-Step Authentication active, you can generate a custom password for ' + 'each third-party application you authorize to use your WordPress.com account. ' + 'You can revoke access for an individual application here if you ever need to.' ) }{ ' ' } ) } { ! showAddPasswordForm && ! newAppPassword && ( ) }
); } export default ApplicationPasswords;