Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of jetpack/publicize store #34111

Merged
merged 13 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: removed

Removed jetpack/publicize store
2 changes: 1 addition & 1 deletion projects/js-packages/publicize-components/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//TODO: Work out a more explicit way of initialising the store
//where it's needed. It's not clear if we'll always want the
//store for the components, but at the moment they're tied.
import './src/store';
import './src/social-store';

export { default as Connection } from './src/components/connection';
Expand All @@ -26,6 +25,7 @@ export { default as useSharePost } from './src/hooks/use-share-post';
export { default as useDismissNotice } from './src/hooks/use-dismiss-notice';

export * from './src/social-store';
export * from './src/utils';
export * from './src/components/share-post';
export * from './src/hooks/use-saving-post';
export * from './src/components/share-buttons';
2 changes: 0 additions & 2 deletions projects/js-packages/publicize-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@
"@wordpress/icons": "9.36.0",
"@wordpress/notices": "4.13.0",
"classnames": "2.3.2",
"lodash": "4.17.21",
"prop-types": "15.8.1",
"react-page-visibility": "7.0.0",
"refx": "3.1.1",
"rememo": "4.0.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Notice, ExternalLink } from '@wordpress/components';
import { withSelect } from '@wordpress/data';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { includes } from 'lodash';
import { SOCIAL_STORE_ID } from '../../social-store';
import ConnectionToggle from '../connection-toggle';
import componentStyles from '../styles.module.scss';
Expand Down Expand Up @@ -47,7 +46,7 @@ class PublicizeConnection extends Component {
*
* @returns {boolean} True if connection must be reauthenticated.
*/
connectionNeedsReauth = () => includes( this.props.mustReauthConnections, this.props.name );
connectionNeedsReauth = () => this.props.mustReauthConnections.includes( this.props.name );
manzoorwanijk marked this conversation as resolved.
Show resolved Hide resolved

onConnectionChange = () => {
const { id } = this.props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import useRefreshConnections from '../../hooks/use-refresh-connections';
import useSocialMediaConnections from '../../hooks/use-social-media-connections';
import useSocialMediaMessage from '../../hooks/use-social-media-message';
import { SOCIAL_STORE_ID } from '../../social-store';
import { isInstagramConnectionSupported } from '../../utils';
import PublicizeConnection from '../connection';
import MediaSection from '../media-section';
import MessageBoxControl from '../message-box-control';
import Notice from '../notice';
import PublicizeSettingsButton from '../settings-button';
import styles from './styles.module.scss';

const PUBLICIZE_STORE_ID = 'jetpack/publicize';
const MONTH_IN_SECONDS = 30 * 24 * 60 * 60;

const checkConnectionCode = ( connection, code ) =>
Expand Down Expand Up @@ -69,17 +69,13 @@ export default function PublicizeForm( {
const { isEnabled: isSocialImageGeneratorEnabledForPost } = useImageGeneratorConfig();
const { dismissNotice, shouldShowNotice, NOTICES } = useDismissNotice();

const { isInstagramConnectionSupported } = useSelect( select => ( {
isInstagramConnectionSupported: select( PUBLICIZE_STORE_ID ).isInstagramConnectionSupported(),
} ) );

const hasInstagramConnection = connections.some(
connection => connection.service_name === 'instagram-business'
);

const shouldShowInstagramNotice =
! hasInstagramConnection &&
isInstagramConnectionSupported &&
isInstagramConnectionSupported() &&
shouldShowNotice( NOTICES.instagram );

const onDismissInstagramNotice = useCallback( () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { getShareMessage } from '../../utils';

/**
* Prepares the text to share.
Expand All @@ -15,8 +16,7 @@ export function useShareButtonText() {
return {
link: getEditedPostAttribute( 'link' ),
message:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
( select( 'jetpack/publicize' ) as any ).getShareMessage() ||
getShareMessage() ||
getEditedPostAttribute( 'meta' )?.jetpack_seo_html_title ||
getEditedPostAttribute( 'title' ),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
import useSocialMediaMessage from '../../hooks/use-social-media-message';
import { SOCIAL_STORE_ID, CONNECTION_SERVICE_MASTODON } from '../../social-store';
import { shouldUploadAttachedMedia } from '../../store/selectors';
import { shouldUploadAttachedMedia } from '../../utils';

const MastodonPreview = props => {
const { message } = useSocialMediaMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { __ } from '@wordpress/i18n';
import { close } from '@wordpress/icons';
import {
getAttachedMedia,
getImageGeneratorPostSettings,
getImageGeneratorSettings,
shouldUploadAttachedMedia,
} from '../../store/selectors';
} from '../../utils';
import { getSigImageUrl } from '../generated-image-preview/utils';
import { useAvailableSerivces } from './useAvailableServices';
import { getMediaSourceUrl } from './utils';
Expand Down Expand Up @@ -72,7 +72,7 @@ export default withSelect( select => {
// Use the featured image by default, if it's available.
let image = featuredImageId ? getMediaSourceUrl( getMedia( featuredImageId ) ) : '';

const sigSettings = getImageGeneratorPostSettings();
const sigSettings = getImageGeneratorSettings();
manzoorwanijk marked this conversation as resolved.
Show resolved Hide resolved

const sigImageUrl = sigSettings.enabled ? getSigImageUrl( sigSettings.token ) : '';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TwitterPreviews } from '@automattic/social-previews';
import { useSelect } from '@wordpress/data';
import React from 'react';
import { SOCIAL_STORE_ID, CONNECTION_SERVICE_TWITTER } from '../../social-store';
import { getShareMessage } from '../../utils';

/**
* The twitter tab component.
Expand All @@ -17,7 +18,6 @@ import { SOCIAL_STORE_ID, CONNECTION_SERVICE_TWITTER } from '../../social-store'
function Twitter( { title, description, image, url, media } ) {
const tweets = useSelect(
select => {
const { getShareMessage } = select( 'jetpack/publicize' );
const {
displayName: name,
profileImage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { SocialServiceIcon } from '@automattic/jetpack-components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import React, { useMemo } from 'react';
import {
isInstagramConnectionSupported,
isMastodonConnectionSupported,
isNextdoorConnectionSupported,
} from '../../utils';
import FacebookPreview from './facebook';
import GoogleSearch from './google-search';
import { Instagram } from './instagram';
Expand All @@ -17,15 +21,9 @@ import Twitter from './twitter';
* @returns {Array<{title: string, icon: React.Component, name: string, preview: React.Component}>} The list of available services.
*/
export function useAvailableSerivces() {
const { isInstagramSupported, isMastodonSupported, isNextdoorSupported } = useSelect( select => {
const store = select( 'jetpack/publicize' );

return {
isInstagramSupported: store.isInstagramConnectionSupported(),
isMastodonSupported: store.isMastodonConnectionSupported(),
isNextdoorSupported: store.isNextdoorConnectionSupported(),
};
} );
const isInstagramSupported = isInstagramConnectionSupported();
const isMastodonSupported = isMastodonConnectionSupported();
const isNextdoorSupported = isNextdoorConnectionSupported();

return useMemo(
() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useCallback } from '@wordpress/element';

const PUBLICIZE_STORE = 'jetpack/publicize';
import { getAttachedMedia, getJetpackSocialOptions, shouldUploadAttachedMedia } from '../../utils';

/**
* @typedef {object} AttachedMediaHook
Expand All @@ -20,26 +19,20 @@ const PUBLICIZE_STORE = 'jetpack/publicize';
export default function useAttachedMedia() {
const { editPost } = useDispatch( editorStore );

const { shouldUploadAttachedMedia, attachedMedia, currentOptions } = useSelect( select => ( {
shouldUploadAttachedMedia: select( PUBLICIZE_STORE ).shouldUploadAttachedMedia(),
attachedMedia: select( PUBLICIZE_STORE ).getAttachedMedia(),
currentOptions: select( PUBLICIZE_STORE ).getJetpackSocialOptions(),
} ) );

const updateJetpackSocialOptions = useCallback(
( key, value ) => {
editPost( {
meta: {
jetpack_social_options: { ...currentOptions, [ key ]: value },
jetpack_social_options: { ...getJetpackSocialOptions(), [ key ]: value },
},
} );
},
[ currentOptions, editPost ]
[ editPost ]
);

return {
attachedMedia,
shouldUploadAttachedMedia,
attachedMedia: getAttachedMedia(),
shouldUploadAttachedMedia: shouldUploadAttachedMedia(),
updateAttachedMedia: media => updateJetpackSocialOptions( 'attached_media', media ),
updateShouldUploadAttachedMedia: option =>
updateJetpackSocialOptions( 'should_upload_attached_media', option ),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useCallback } from '@wordpress/element';

const PUBLICIZE_STORE_ID = 'jetpack/publicize';
import { getJetpackSocialOptions, getImageGeneratorSettings } from '../../utils';

const getCurrentSettings = ( sigSettings, isPostPublished ) => ( {
isEnabled: sigSettings?.enabled ?? ! isPostPublished,
Expand Down Expand Up @@ -34,11 +33,6 @@ const getCurrentSettings = ( sigSettings, isPostPublished ) => ( {
export default function useImageGeneratorConfig() {
const { editPost } = useDispatch( editorStore );

const { postSettings, currentOptions } = useSelect( select => ( {
postSettings: select( PUBLICIZE_STORE_ID ).getImageGeneratorPostSettings(),
currentOptions: select( PUBLICIZE_STORE_ID ).getJetpackSocialOptions(),
} ) );

const { isPostPublished } = useSelect( select => ( {
isPostPublished: select( editorStore ).isCurrentPostPublished(),
} ) );
Expand All @@ -47,31 +41,34 @@ export default function useImageGeneratorConfig() {
settings => {
editPost( {
meta: {
jetpack_social_options: { ...currentOptions, image_generator_settings: settings },
jetpack_social_options: {
...getJetpackSocialOptions(),
image_generator_settings: settings,
},
},
} );
},
[ currentOptions, editPost ]
[ editPost ]
);

const updateProperty = useCallback(
( key, value ) => {
const settings = { ...postSettings, [ key ]: value };
const settings = { ...getImageGeneratorSettings(), [ key ]: value };
_commitPostUpdate( settings );
},
[ postSettings, _commitPostUpdate ]
[ _commitPostUpdate ]
);

const updateSettings = useCallback(
settings => {
const newSettings = { ...postSettings, ...settings };
const newSettings = { ...getImageGeneratorSettings(), ...settings };
_commitPostUpdate( newSettings );
},
[ postSettings, _commitPostUpdate ]
[ _commitPostUpdate ]
);

return {
...getCurrentSettings( currentOptions?.image_generator_settings, isPostPublished ),
...getCurrentSettings( getJetpackSocialOptions().image_generator_settings, isPostPublished ),
setIsEnabled: value => updateProperty( 'enabled', value ),
setToken: value => updateProperty( 'token', value ),
updateSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import {
getJetpackData,
getSiteFragment,
} from '@automattic/jetpack-shared-extension-utils';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import {
isPublicizeEnabled as isPublicizeFeatureEnabled,
getJetpackSocialPostAlreadyShared,
togglePublicizeFeature,
} from '../../utils';

const republicizeFeatureName = 'republicize';

Expand All @@ -17,16 +22,13 @@ const republicizeFeatureName = 'republicize';
* for toggling support for the current post.
*/
export default function usePublicizeConfig() {
const { togglePublicizeFeature } = useDispatch( 'jetpack/publicize' );
const sharesData = getJetpackData()?.social?.sharesData ?? {};
const isShareLimitEnabled = sharesData.is_share_limit_enabled;
const isRePublicizeFeatureAvailable =
getJetpackExtensionAvailability( republicizeFeatureName )?.available || isShareLimitEnabled;
const isPostPublished = useSelect( select => select( editorStore ).isCurrentPostPublished(), [] );
const isPostAlreadyShared = useSelect(
select => select( 'jetpack/publicize' ).getJetpackSocialPostAlreadyShared(),
[]
);
const isPostAlreadyShared = getJetpackSocialPostAlreadyShared();

const connectionsRootUrl =
getJetpackData()?.social?.publicizeConnectionsUrl ??
'https://wordpress.com/marketing/connections/';
Expand All @@ -37,10 +39,7 @@ export default function usePublicizeConfig() {
* and usually is handled from the UI (main toggle control),
* dispathicng the togglePublicizeFeature() action (jetpack/publicize).
*/
const isPublicizeEnabledMeta = useSelect(
select => select( 'jetpack/publicize' ).getFeatureEnableState(),
[]
);
const isPublicizeEnabledMeta = isPublicizeFeatureEnabled();

/*
* isRePublicizeUpgradableViaUpsell:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useSelect, useDispatch } from '@wordpress/data';
import { useDispatch } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { getShareMessage, getShareMessageMaxLength } from '../../utils';

/**
* @typedef {object} MessageHook
Expand All @@ -16,17 +17,9 @@ import { store as editorStore } from '@wordpress/editor';
export default function useSocialMediaMessage() {
const { editPost } = useDispatch( editorStore );

const { message, maxLength } = useSelect(
select => ( {
message: select( 'jetpack/publicize' ).getShareMessage(),
maxLength: select( 'jetpack/publicize' ).getShareMessageMaxLength(),
} ),
[]
);

return {
message,
maxLength,
message: getShareMessage(),
maxLength: getShareMessageMaxLength(),
updateMessage: function ( text ) {
editPost( {
meta: {
Expand Down
Loading
Loading