/* 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 (