/* eslint-disable wpcalypso/jsx-classname-namespace */ import { Gridicon } from '@automattic/components'; import { Icon, starEmpty } from '@wordpress/icons'; import { localize } from 'i18n-calypso'; import { filter, some } from 'lodash'; import PropTypes from 'prop-types'; import { Component } from 'react'; import { connect } from 'react-redux'; import { gaRecordEvent } from 'calypso/lib/analytics/ga'; import { recordTracksEvent } from 'calypso/lib/analytics/tracks'; import { decodeEntities } from 'calypso/lib/formatting'; import getCurrentRouteParameterized from 'calypso/state/selectors/get-current-route-parameterized'; import { getSelectedSiteId } from 'calypso/state/ui/selectors'; import ButtonsLabelEditor from './label-editor'; import ButtonsPreviewAction from './preview-action'; import ButtonsPreviewButtons from './preview-buttons'; import ButtonsTray from './tray'; class SharingButtonsPreview extends Component { static displayName = 'SharingButtonsPreview'; static propTypes = { isPrivateSite: PropTypes.bool, style: PropTypes.oneOf( [ 'icon-text', 'icon', 'text', 'official' ] ), label: PropTypes.string, buttons: PropTypes.array, showLike: PropTypes.bool, showReblog: PropTypes.bool, onLabelChange: PropTypes.func, onButtonsChange: PropTypes.func, }; static defaultProps = { style: 'icon', buttons: [], showLike: true, showReblog: true, onLabelChange: function () {}, onButtonsChange: function () {}, }; state = { isEditingLabel: false, buttonsTrayVisibility: null, }; toggleEditLabel = () => { const { path } = this.props; const isEditingLabel = ! this.state.isEditingLabel; this.setState( { isEditingLabel: isEditingLabel } ); if ( isEditingLabel ) { this.hideButtonsTray(); recordTracksEvent( 'calypso_sharing_buttons_edit_text_click', { path } ); gaRecordEvent( 'Sharing', 'Clicked Edit Text Link' ); } else { recordTracksEvent( 'calypso_sharing_buttons_edit_text_close_click', { path } ); gaRecordEvent( 'Sharing', 'Clicked Edit Text Done Button' ); } }; showButtonsTray = ( visibility ) => { const { path } = this.props; this.setState( { isEditingLabel: false, buttonsTrayVisibility: visibility, } ); if ( 'hidden' === visibility ) { recordTracksEvent( 'calypso_sharing_buttons_more_button_click', { path } ); gaRecordEvent( 'Sharing', 'Clicked More Button Link', visibility ); } else { recordTracksEvent( 'calypso_sharing_buttons_edit_button_click', { path } ); gaRecordEvent( 'Sharing', 'Clicked Edit Button Link', visibility ); } }; hideButtonsTray = () => { const { path } = this.props; if ( ! this.state.buttonsTrayVisibility ) { return; } // Hide button tray by resetting state to default this.setState( { buttonsTrayVisibility: null } ); recordTracksEvent( 'calypso_sharing_buttons_edit_buttons_close_click', { path } ); gaRecordEvent( 'Sharing', 'Clicked Edit Buttons Done Button' ); }; getButtonsTrayToggleButtonLabel = ( visibility, enabledButtonsExist ) => { if ( 'visible' === visibility ) { if ( enabledButtonsExist ) { return this.props.translate( 'Edit sharing buttons', { context: 'Sharing: Buttons edit label', } ); } return this.props.translate( 'Add sharing buttons', { context: 'Sharing: Buttons edit label', } ); } else if ( enabledButtonsExist ) { return this.props.translate( 'Edit “More” buttons', { context: 'Sharing: Buttons edit label', } ); } return this.props.translate( 'Add “More” button', { context: 'Sharing: Buttons edit label', } ); }; getButtonsTrayToggleButtonElement = ( visibility ) => { const enabledButtonsExist = some( this.props.buttons, { visibility: visibility, enabled: true, } ); return ( { this.getButtonsTrayToggleButtonLabel( visibility, enabledButtonsExist ) } ); }; // 16 is used in the preview to match the buttons on the frontend of the website. getReblogButtonElement = () => { if ( this.props.showReblog ) { return (
{ /* eslint-disable wpcalypso/jsx-gridicon-size */ } { /* 16 is used in the preview to match the buttons on the frontend of the website. */ } { /* eslint-disable wpcalypso/jsx-gridicon-size */ } { this.props.translate( 'Reblog' ) }
); } }; getLikeButtonElement = () => { if ( this.props.showLike ) { return (
{ /* 16 is used in the preview to match the buttons on the frontend of the website. */ } { /* eslint-disable wpcalypso/jsx-gridicon-size */ } { this.props.translate( 'Like' ) }
Matt Mullenweg
{ this.props.translate( '1 like' ) }
); } }; getPreviewButtonsElement = () => { const enabledButtons = filter( this.props.buttons, { enabled: true } ); if ( enabledButtons.length ) { return ( ); } }; render() { return (
{ this.props.translate( 'Edit label text', { context: 'Sharing: Buttons edit label', } ) }

{ this.props.translate( 'Preview' ) }

{ decodeEntities( this.props.label ) }
{ this.getPreviewButtonsElement() }
{ this.getReblogButtonElement() } { this.getLikeButtonElement() }
{ this.getButtonsTrayToggleButtonElement( 'visible' ) } { this.getButtonsTrayToggleButtonElement( 'hidden' ) }
); } } export default connect( ( state ) => { return { path: getCurrentRouteParameterized( state, getSelectedSiteId( state ) ) }; } )( localize( SharingButtonsPreview ) );