Skip to content

Commit

Permalink
Social | Migrate shares data from initial state to new script data (#…
Browse files Browse the repository at this point in the history
…38988)

* Social | Migrate shares data from initial state to new script data

* Add changelog

* Fix the export
  • Loading branch information
manzoorwanijk authored Aug 21, 2024
1 parent e2e6b98 commit bae165e
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Social: Migrated shares data to the new script data
1 change: 1 addition & 0 deletions projects/js-packages/publicize-components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export * from './src/hooks/use-saving-post';
export * from './src/hooks/use-post-meta';
export * from './src/components/share-buttons';
export * from './src/components/manage-connections-modal';
export * from './src/utils/shares-data';
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { combineReducers } from '@wordpress/data';
import connectionData from './connection-data';
import jetpackSettings from './jetpack-settings';
import sharesData from './shares-data';
import siteData from './site-data';
import socialImageGeneratorSettings from './social-image-generator-settings';

const reducer = combineReducers( {
sharesData,
siteData,
connectionData,
jetpackSettings,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import * as connectionDataSelectors from './connection-data';
import jetpackSettingSelectors from './jetpack-settings';
import * as sharesData from './shares-data';
import siteDataSelectors from './site-data';
import socialImageGeneratorSettingsSelectors from './social-image-generator-settings';

const selectors = {
...siteDataSelectors,
...connectionDataSelectors,
...jetpackSettingSelectors,
...sharesData,
...socialImageGeneratorSettingsSelectors,
userConnectionUrl: state => state.userConnectionUrl,
useAdminUiV1: state => state.useAdminUiV1,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export type SharesData = {
to_be_publicized_count: number;
publicized_count: number;
shared_posts_count: number;
};

export type ConnectionStatus = 'ok' | 'broken';

export type Connection = {
Expand Down Expand Up @@ -40,8 +34,6 @@ export type JetpackSettings = {

// TODO we should have a consistent structure across all the pages - editor, dashboard, admin page etc.
export type SocialStoreState = {
connectionData: ConnectionData;
sharesData: SharesData;
// on post editor
hasPaidPlan?: boolean;
// on Jetack Social admin page
Expand Down
13 changes: 7 additions & 6 deletions projects/js-packages/publicize-components/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export type SharesData = {
to_be_publicized_count: number;
publicized_count: number;
shared_posts_count: number;
};

export interface FeatureFlags {
useAdminUiV1: boolean;
}
Expand All @@ -22,6 +28,7 @@ export interface SocialScriptData {
is_publicize_enabled: boolean;
feature_flags: FeatureFlags;
supported_services: Array< ConnectionService >;
shares_data: SharesData;
}

type JetpackSettingsSelectors = {
Expand All @@ -42,11 +49,6 @@ type ConnectionDataSelectors = {
hasConnections: () => boolean;
};

type SharesDataSelectors = {
getSharesCount: () => number;
getPostsCount: () => number;
};

type SiteDataSelectors = {
getSiteData: () => Array< object >;
getSiteTitle: () => string;
Expand All @@ -73,6 +75,5 @@ type SocialImageGeneratorSettingsSelectors = {
*/
export type SocialStoreSelectors = JetpackSettingsSelectors &
ConnectionDataSelectors &
SharesDataSelectors &
SiteDataSelectors &
SocialImageGeneratorSettingsSelectors;
47 changes: 47 additions & 0 deletions projects/js-packages/publicize-components/src/utils/shares-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getSocialScriptData } from './script-data';

/**
* Returns the shares data.
*
* @return {import('../types/types').SharesData} Shares data
*/
export function getSharesData() {
return getSocialScriptData().shares_data;
}
/**
* Returns the total number of shares already used.
*
* @return {number} Total number of shares used
*/
export function getSharesUsedCount() {
return getSharesData().publicized_count ?? 0;
}

/**
* Returns the number of shares scheduled.
*
* @return {number} Number of shares scheduled
*/
export function getScheduledSharesCount() {
return getSharesData().to_be_publicized_count ?? 0;
}

/**
* Returns the total number of shares used and scheduled.
*
* @return {number} Total number of shares used and scheduled
*/
export function getTotalSharesCount() {
const count = getSharesUsedCount() + getScheduledSharesCount();

return Math.max( count, 0 );
}

/**
* Number of posts shared this month
*
* @return {number} Number of posts shared this month
*/
export function getSharedPostsCount() {
return getSharesData().shared_posts_count ?? 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Social: Migrated shares data to the new script data
13 changes: 12 additions & 1 deletion projects/packages/publicize/src/class-publicize-script-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Automattic\Jetpack\Current_Plan;
use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
use Automattic\Jetpack\Status\Host;
use Jetpack_Options;

/**
* Publicize_Script_Data class.
Expand Down Expand Up @@ -84,6 +85,7 @@ public static function get_admin_script_data() {
'is_publicize_enabled' => Utils::is_publicize_active(),
'feature_flags' => self::get_feature_flags(),
'supported_services' => array(),
'shares_data' => array(),
);

if ( ! Utils::is_publicize_active() ) {
Expand All @@ -102,10 +104,10 @@ public static function get_admin_script_data() {
array(
'api_paths' => self::get_api_paths(),
'supported_services' => self::get_supported_services(),
'shares_data' => self::get_shares_data(),
/**
* 'store' => self::get_store_script_data(),
* 'urls' => self::get_urls(),
* 'shares_data' => self::get_shares_data(),
*/
)
);
Expand Down Expand Up @@ -153,6 +155,15 @@ public static function has_feature_flag( $feature ): bool {
return Current_Plan::supports( 'social-' . $feature );
}

/**
* Get the shares data.
*
* @return ?array
*/
public static function get_shares_data() {
return self::publicize()->get_publicize_shares_info( Jetpack_Options::get_option( 'id' ) );
}

/**
* Get the list of supported Publicize services.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Social: Migrated shares data to the new script data
17 changes: 8 additions & 9 deletions projects/plugins/social/src/js/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
getUserLocale,
} from '@automattic/jetpack-components';
import { ConnectionError, useConnectionErrorNotice } from '@automattic/jetpack-connection';
import { store as socialStore } from '@automattic/jetpack-publicize-components';
import {
store as socialStore,
getTotalSharesCount,
getSharedPostsCount,
} from '@automattic/jetpack-publicize-components';
import { getScriptData } from '@automattic/jetpack-script-data';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
Expand All @@ -18,21 +22,18 @@ import styles from './styles.module.scss';
const Header = () => {
const connectionData = window.jetpackSocialInitialState.connectionData ?? {};
const {
// TODO - Replace some of these with data from initial state
connectionsAdminUrl,
hasConnections,
isModuleEnabled,
newPostUrl,
postsCount,
totalShareCount,
} = useSelect( select => {
const store = select( socialStore );
return {
connectionsAdminUrl: connectionData.adminUrl,
hasConnections: store.getConnections().length > 0,
isModuleEnabled: store.isModuleEnabled(),
newPostUrl: `${ store.getAdminUrl() }post-new.php`,
postsCount: store.getSharedPostsCount(),
totalShareCount: store.getTotalSharesCount(),
};
} );
// TODO - Replace this with a utility function like `getSocialFeatureFlags` when available
Expand Down Expand Up @@ -87,14 +88,12 @@ const Header = () => {
{
icon: <SocialIcon />,
label: __( 'Total shares past 30 days', 'jetpack-social' ),
loading: null === totalShareCount,
value: formatter.format( totalShareCount ),
value: formatter.format( getTotalSharesCount() ),
},
{
icon: <Icon icon={ postList } />,
label: __( 'Posted this month', 'jetpack-social' ),
loading: null === postsCount,
value: formatter.format( postsCount ),
value: formatter.format( getSharedPostsCount() ),
},
] }
/>
Expand Down
8 changes: 0 additions & 8 deletions projects/plugins/social/src/js/components/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ type ConnectionDataSelectors = {
hasConnections: () => boolean;
};

type SharesDataSelectors = {
getSharesCount: () => number;
getPostsCount: () => number;
isShareLimitEnabled: () => boolean;
numberOfSharesRemaining: () => number;
};

type SiteDataSelectors = {
getSiteData: () => Array< object >;
getSiteTitle: () => string;
Expand Down Expand Up @@ -58,7 +51,6 @@ type SocialNotesSettingsSelectors = {
*/
export type SocialStoreSelectors = JetpackSettingsSelectors &
ConnectionDataSelectors &
SharesDataSelectors &
SiteDataSelectors &
SocialImageGeneratorSettingsSelectors &
SocialNotesSettingsSelectors;

0 comments on commit bae165e

Please sign in to comment.