import { Button, FoldableCard } from '@automattic/components'; import clsx from 'clsx'; import { localize } from 'i18n-calypso'; import { Component } from 'react'; import { connect } from 'react-redux'; import { withLocalizedMoment } from 'calypso/components/localized-moment'; import safeProtocolUrl from 'calypso/lib/safe-protocol-url'; import ConnectedApplicationIcon from 'calypso/me/connected-application-icon'; import { recordGoogleEvent } from 'calypso/state/analytics/actions'; import { deleteConnectedApplication } from 'calypso/state/connected-applications/actions'; import './style.scss'; class ConnectedApplicationItem extends Component { static defaultProps = { isPlaceholder: false, }; recordClickEvent = ( action, label = null ) => { this.props.recordGoogleEvent( 'Me', 'Clicked on ' + action, label ); }; getClickHandler = ( action ) => { return () => this.recordClickEvent( action ); }; disconnect = ( event ) => { if ( this.props.isPlaceholder ) { return; } const { connection: { title, ID }, } = this.props; event.stopPropagation(); this.recordClickEvent( 'Disconnect Connected Application Link', title ); this.props.deleteConnectedApplication( ID ); }; renderAccessScopeBadge() { const { connection: { scope, site }, } = this.props; let meta = ''; if ( ! this.props.connection ) { return; } if ( 'auth' === scope ) { meta = this.props.translate( 'Authentication' ); } else if ( 'global' === scope ) { meta = this.props.translate( 'Global' ); } else if ( site ) { meta = site.site_name; } if ( meta.length ) { return { meta }; } } renderScopeMessage() { const { connection: { scope, site }, } = this.props; let message; if ( ! this.props.connection ) { return; } if ( 'global' === scope ) { message = this.props.translate( 'This connection is allowed to manage all of your blogs on WordPress.com, ' + 'including any Jetpack blogs that are connected to your WordPress.com account.' ); } else if ( 'auth' === scope ) { message = this.props.translate( 'This connection is not allowed to manage any of your blogs.' ); } else if ( false !== site ) { message = this.props.translate( 'This connection is only allowed to access {{siteLink}}%(siteName)s{{/siteLink}}', { components: { siteLink: ( ), }, args: { siteName: site.site_name, }, } ); } if ( ! message ) { return; } return (

{ this.props.translate( 'Access scope' ) }

{ this.renderAccessScopeBadge() }

{ message }

); } renderDetail() { const { connection: { URL, authorized, permissions }, } = this.props; if ( this.props.isPlaceholder ) { return; } return (

{ this.props.translate( 'Application website' ) }

{ safeProtocolUrl( URL ) }

{ this.props.translate( '{{detailTitle}}Authorized on{{/detailTitle}}{{detailDescription}}%(date)s{{/detailDescription}}', { components: { // eslint-disable-next-line jsx-a11y/heading-has-content detailTitle:

, detailDescription: (

), }, args: { date: this.props.moment( authorized ).format( 'lll' ), }, } ) }

{ this.renderScopeMessage() }

{ this.props.translate( 'Access permissions' ) }

); } renderHeader() { return (

{ this.props.connection.title }

); } renderSummary() { return (
{ this.props.isPlaceholder ? ( ) : ( ) }
); } render() { const classes = clsx( { 'connected-application-item': true, 'is-placeholder': this.props.isPlaceholder, } ); return ( { this.renderDetail() } ); } } export default connect( null, { deleteConnectedApplication, recordGoogleEvent, } )( localize( withLocalizedMoment( ConnectedApplicationItem ) ) );