import { Button, Gridicon, FormLabel, Tooltip } from '@automattic/components'; import { saveAs } from 'browser-filesaver'; import Clipboard from 'clipboard'; import { localize } from 'i18n-calypso'; import { flowRight as compose } from 'lodash'; import PropTypes from 'prop-types'; import { createRef, Component } from 'react'; import ReactDom from 'react-dom'; import { connect } from 'react-redux'; import ButtonGroup from 'calypso/components/button-group'; import FormButton from 'calypso/components/forms/form-button'; import FormCheckbox from 'calypso/components/forms/form-checkbox'; import { withLocalizedMoment } from 'calypso/components/localized-moment'; import Notice from 'calypso/components/notice'; import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { getCurrentUserName } from 'calypso/state/current-user/selectors'; import './style.scss'; class Security2faBackupCodesList extends Component { static displayName = 'Security2faBackupCodesList'; static defaultProps = { backupCodes: [], }; static propTypes = { onNextStep: PropTypes.func.isRequired, }; state = { userAgrees: false, printCodesTooltip: false, downloadCodesTooltip: false, copyCodesTooltip: false, }; popup = false; copyCodesButtonRef = createRef(); printCodesButtonRef = createRef(); downloadCodesButtonRef = createRef(); componentDidMount() { // Configure clipboard to be triggered on clipboard button press const button = ReactDom.findDOMNode( this.copyCodesButtonRef.current ); this.clipboard = new Clipboard( button, { text: () => this.getBackupCodePlainText( this.props.backupCodes ), } ); this.clipboard.on( 'success', this.onCopy ); } componentWillUnmount() { // Cleanup clipboard object this.clipboard.destroy(); } openPopup = () => { this.popup = window.open(); if ( null === this.popup ) { this.setState( { lastError: this.props.translate( 'Please disable your pop-up blocker and try again.' ), } ); return false; } this.setState( { lastError: false } ); return true; }; onPrint = () => { this.props.recordGoogleEvent( 'Me', 'Clicked On 2fa Print Backup Codes Button' ); if ( this.openPopup() ) { this.doPopup( this.props.backupCodes ); } }; onCopy = () => { this.props.recordGoogleEvent( 'Me', 'Clicked On 2fa Copy to clipboard Button' ); }; saveCodesToFile = () => { this.props.recordGoogleEvent( 'Me', 'Clicked On 2fa Save Backup Codes Button' ); const backupCodes = this.props.backupCodes.join( '\n' ); const toSave = new globalThis.Blob( [ backupCodes ], { type: 'text/plain;charset=utf-8' } ); saveAs( toSave, `${ this.props.username }-backup-codes.txt` ); }; getBackupCodePlainText( backupCodes ) { if ( backupCodes.length > 0 ) { return backupCodes.join( '\n' ); } } enableDownloadCodesTooltip = () => { this.setState( { downloadCodesTooltip: true } ); }; disableDownloadCodesTooltip = () => { this.setState( { downloadCodesTooltip: false } ); }; enablePrintCodesTooltip = () => { this.setState( { printCodesTooltip: true } ); }; disablePrintCodesTooltip = () => { this.setState( { printCodesTooltip: false } ); }; enableCopyCodesTooltip = () => { this.setState( { copyCodesTooltip: true } ); }; disableCopyCodesTooltip = () => { this.setState( { copyCodesTooltip: false } ); }; getBackupCodeHTML( codes ) { const datePrinted = this.props.moment().format( 'lll' ); let row; let html = '
' + this.props.translate( 'WordPress.com backup verification codes for %s', { args: this.props.username, } ) + '
'; html += '| ' + ( row + 1 ) + '. ' + '' + codes[ row * 2 ] + '' + ' | ' + '' + ( row + 6 ) + '. ' + '' + codes[ row * 2 + 1 ] + '' + ' | ' + '
' + this.props.translate( 'Printed: %(datePrinted)s', { args: { datePrinted: datePrinted, }, } ) + '
'; html += '{ this.props.translate( 'We ask that you print this list of ten unique, ' + 'one-time-use backup codes and keep the list in a safe place.' ) }