import { isEnabled } from '@automattic/calypso-config';
import { FEATURE_REPUBLICIZE } from '@automattic/calypso-products';
import page from '@automattic/calypso-router';
import { Button, Gridicon } from '@automattic/components';
import { localizeUrl } from '@automattic/i18n-utils';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import { get, includes, map, concat } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import CalendarButton from 'calypso/blocks/calendar-button';
import ButtonGroup from 'calypso/components/button-group';
import QueryPostTypes from 'calypso/components/data/query-post-types';
import QueryPublicizeConnections from 'calypso/components/data/query-publicize-connections';
import QuerySitePlans from 'calypso/components/data/query-site-plans';
import EventsTooltip from 'calypso/components/date-picker/events-tooltip';
import { withLocalizedMoment } from 'calypso/components/localized-moment';
import Notice from 'calypso/components/notice';
import NoticeAction from 'calypso/components/notice/notice-action';
import PublicizeMessage from 'calypso/components/publicize-message';
import TrackComponentView from 'calypso/lib/analytics/track-component-view';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { sectionify } from 'calypso/lib/route';
import { getCurrentUserCurrencyCode } from 'calypso/state/currency-code/selectors';
import { getCurrentUserId } from 'calypso/state/current-user/selectors';
import getPostSharePublishedActions from 'calypso/state/selectors/get-post-share-published-actions';
import getPostShareScheduledActions from 'calypso/state/selectors/get-post-share-scheduled-actions';
import getScheduledPublicizeShareActionTime from 'calypso/state/selectors/get-scheduled-publicize-share-action-time';
import isPublicizeEnabled from 'calypso/state/selectors/is-publicize-enabled';
import isSchedulingPublicizeShareAction from 'calypso/state/selectors/is-scheduling-publicize-share-action';
import isSchedulingPublicizeShareActionError from 'calypso/state/selectors/is-scheduling-publicize-share-action-error';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import {
fetchConnections as requestConnections,
sharePost,
dismissShareConfirmation,
} from 'calypso/state/sharing/publicize/actions';
import { schedulePostShareAction } from 'calypso/state/sharing/publicize/publicize-actions/actions';
import {
getSiteUserConnections,
hasFetchedConnections as siteHasFetchedConnections,
isRequestingSharePost,
sharePostFailure,
sharePostSuccessMessage,
} from 'calypso/state/sharing/publicize/selectors';
import { isRequestingSitePlans as siteIsRequestingPlans } from 'calypso/state/sites/plans/selectors';
import { getSiteSlug, getSitePlanSlug, isJetpackSite } from 'calypso/state/sites/selectors';
import ConnectionsList from './connections-list';
import NoConnectionsNotice from './no-connections-notice';
import { UpgradeToPremiumNudge } from './nudges';
import ActionsList from './publicize-actions-list';
import SharingPreviewModal from './sharing-preview-modal';
import './style.scss';
const REGEXP_PUBLICIZE_SERVICE_SKIPPED = /^_wpas_skip_(\d+)$/;
class PostShare extends Component {
static propTypes = {
// parent prps
post: PropTypes.object,
siteId: PropTypes.number,
disabled: PropTypes.bool,
showClose: PropTypes.bool,
onClose: PropTypes.func,
// connect prps
connections: PropTypes.array,
failed: PropTypes.bool,
hasFetchedConnections: PropTypes.bool,
hasRepublicizeFeature: PropTypes.bool,
isPublicizeEnabled: PropTypes.bool,
planSlug: PropTypes.string,
postId: PropTypes.number,
requestConnections: PropTypes.func,
requesting: PropTypes.bool,
siteSlug: PropTypes.string,
success: PropTypes.bool,
userCurrency: PropTypes.string,
};
static defaultProps = {
connections: [],
disabled: false,
post: {},
};
state = {
message: this.getPostPublicizeMessage(),
skipped: this.getPostPublicizeSkipped(),
showSharingPreview: false,
scheduledDate: null,
showTooltip: false,
eventsByDay: [],
};
getPostPublicizeMessage() {
return this.props.post?.metadata?.find( ( { key } ) => key === '_wpas_mess' )?.value || '';
}
getPostPublicizeSkipped() {
return (
this.props.post.metadata
?.filter( function ( meta ) {
return (
REGEXP_PUBLICIZE_SERVICE_SKIPPED.test( meta.key ) && 1 === parseInt( meta.value, 10 )
);
} )
?.map( function ( meta ) {
return parseInt( meta.key.match( REGEXP_PUBLICIZE_SERVICE_SKIPPED )[ 1 ], 10 );
} ) ?? []
);
}
hasConnections() {
return !! get( this.props, 'connections.length' );
}
toggleConnection = ( id ) => {
const skipped = this.state.skipped.slice();
const index = skipped.indexOf( id );
if ( index !== -1 ) {
skipped.splice( index, 1 );
} else {
skipped.push( id );
}
this.setState( { skipped } );
};
scheduleDate = ( date ) => {
if ( date.isBefore( Date.now() ) ) {
date = null;
}
this.setState( { scheduledDate: date } );
};
skipConnection( { ID } ) {
return this.state.skipped.indexOf( ID ) === -1;
}
isConnectionActive = ( connection ) =>
connection.status !== 'broken' &&
connection.status !== 'invalid' &&
this.skipConnection( connection );
activeConnections() {
return this.props.connections.filter( this.isConnectionActive );
}
toggleSharingPreview = () => {
const showSharingPreview = ! this.state.showSharingPreview;
if ( showSharingPreview ) {
document.documentElement.classList.add( 'no-scroll', 'is-previewing' );
} else {
document.documentElement.classList.remove( 'no-scroll', 'is-previewing' );
}
recordTracksEvent( 'calypso_publicize_share_preview_toggle', {
show: showSharingPreview,
} );
this.setState( { showSharingPreview } );
};
setMessage = ( message ) => this.setState( { message } );
dismiss = () => {
this.props.dismissShareConfirmation( this.props.siteId, this.props.postId );
};
sharePost = () => {
const { postId, siteId, connections, isJetpack } = this.props;
const servicesToPublish = connections.filter(
( connection ) => this.state.skipped.indexOf( connection.ID ) === -1
);
//Let's prepare array of service stats for tracks.
const numberOfAccountsPerService = servicesToPublish.reduce(
( counts, service ) => {
counts.service_all = counts.service_all + 1;
if ( ! counts[ 'service_' + service.service ] ) {
counts[ 'service_' + service.service ] = 0;
}
counts[ 'service_' + service.service ] = counts[ 'service_' + service.service ] + 1;
return counts;
},
{ service_all: 0 }
);
const additionalProperties = {
context_path: sectionify( page.current ),
is_jetpack: isJetpack,
blog_id: siteId,
};
const eventProperties = { ...numberOfAccountsPerService, ...additionalProperties };
if ( this.state.scheduledDate ) {
recordTracksEvent( 'calypso_publicize_share_schedule', eventProperties );
this.props.schedulePostShareAction(
siteId,
postId,
this.state.message,
this.state.scheduledDate.format( 'X' ),
servicesToPublish.map( ( connection ) => connection.ID )
);
} else {
recordTracksEvent( 'calypso_publicize_share_instantly', eventProperties );
this.props.sharePost( siteId, postId, this.state.skipped, this.state.message );
}
};
isDisabled() {
if ( this.props.disabled || this.props.requesting || this.activeConnections().length < 1 ) {
return true;
}
}
previewSharingPost = () => {};
renderMessage() {
if ( ! this.hasConnections() ) {
return;
}
const targeted = this.hasConnections()
? this.props.connections.filter( this.isConnectionActive )
: [];
const requireCount = includes( map( targeted, 'service' ), 'twitter' );
const acceptableLength = requireCount ? 280 - 23 - 23 : null;
return (