From 13e91e70c435924c4bf09988210614cff4f597a5 Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 13 Dec 2024 16:05:04 +0530 Subject: [PATCH] Fix connection selectors --- ...{connection-data.js => connection-data.ts} | 98 +++++++++---------- 1 file changed, 44 insertions(+), 54 deletions(-) rename projects/js-packages/publicize-components/src/social-store/selectors/{connection-data.js => connection-data.ts} (57%) diff --git a/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.js b/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.ts similarity index 57% rename from projects/js-packages/publicize-components/src/social-store/selectors/connection-data.js rename to projects/js-packages/publicize-components/src/social-store/selectors/connection-data.ts index 307b105afa7cd..d7de8606a6576 100644 --- a/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.js +++ b/projects/js-packages/publicize-components/src/social-store/selectors/connection-data.ts @@ -1,23 +1,24 @@ import { REQUEST_TYPE_DEFAULT } from '../actions/constants'; +import { Connection, SocialStoreState } from '../types'; /** * Returns the connections list from the store. * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * - * @return {Array} The connections list + * @return {Array} The connections list */ -export function getConnections( state ) { +export function getConnections( state: SocialStoreState ): Array< Connection > { return state.connectionData?.connections ?? []; } /** * Return a connection by its ID. * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} connectionId - The connection ID. + * @param {SocialStoreState} state - State object. + * @param {string} connectionId - The connection ID. * - * @return {import("../types").Connection | undefined} The connection. + * @return {Connection | undefined} The connection. */ export function getConnectionById( state, connectionId ) { return getConnections( state ).find( connection => connection.connection_id === connectionId ); @@ -26,8 +27,8 @@ export function getConnectionById( state, connectionId ) { /** * Returns the broken connections. * - * @param {import("../types").SocialStoreState} state - State object. - * @return {Array} List of broken connections. + * @param {SocialStoreState} state - State object. + * @return {Array} List of broken connections. */ export function getBrokenConnections( state ) { return getConnections( state ).filter( connection => { @@ -38,8 +39,8 @@ export function getBrokenConnections( state ) { /** * Returns connections by service name/ID. * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} serviceName - The service name. + * @param {SocialStoreState} state - State object. + * @param {string} serviceName - The service name. * * @return {Array} The connections. */ @@ -49,7 +50,7 @@ export function getConnectionsByService( state, serviceName ) { /** * Returns whether there are connections in the store. - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * @return {boolean} Whether there are connections. */ export function hasConnections( state ) { @@ -59,35 +60,35 @@ export function hasConnections( state ) { /** * Returns the failed Publicize connections. * - * @param {import("../types").SocialStoreState} state - State object. - * @return {Array} List of connections. + * @param {SocialStoreState} state - State object. + * @return {Array} List of connections. */ export function getFailedConnections( state ) { const connections = getConnections( state ); - return connections.filter( connection => false === connection.test_success ); + return connections.filter( connection => 'broken' === connection.status ); } /** * Returns a list of Publicize connection service names that require reauthentication from users. * iFor example, when LinkedIn switched its API from v1 to v2. * - * @param {import("../types").SocialStoreState} state - State object. - * @return {Array} List of service names that need reauthentication. + * @param {SocialStoreState} state - State object. + * @return {Array} List of service names that need reauthentication. */ export function getMustReauthConnections( state ) { const connections = getConnections( state ); return connections - .filter( connection => 'must_reauth' === connection.test_success ) + .filter( connection => 'broken' === connection.status ) .map( connection => connection.service_name ); } /** * Returns the Publicize connections that are enabled. * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * - * @return {Array} List of enabled connections. + * @return {Array} List of enabled connections. */ export function getEnabledConnections( state ) { return getConnections( state ).filter( connection => connection.enabled ); @@ -96,9 +97,9 @@ export function getEnabledConnections( state ) { /** * Returns the Publicize connections that are disabled. * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * - * @return {Array} List of disabled connections. + * @return {Array} List of disabled connections. */ export function getDisabledConnections( state ) { return getConnections( state ).filter( connection => ! connection.enabled ); @@ -107,10 +108,10 @@ export function getDisabledConnections( state ) { /** * Get the profile details for a connection * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} service - The service name. - * @param {object} args - Arguments. - * @param {boolean} args.forceDefaults - Whether to use default values. + * @param {SocialStoreState} state - State object. + * @param {string} service - The service name. + * @param {object} args - Arguments. + * @param {boolean} args.forceDefaults - Whether to use default values. * * @return {object} The profile details. */ @@ -125,22 +126,11 @@ export function getConnectionProfileDetails( state, service, { forceDefaults = f ); if ( connection ) { - const { - display_name, - profile_display_name, - profile_picture, - external_display, - external_name, - } = connection; - - displayName = 'twitter' === service ? profile_display_name : display_name || external_display; - username = 'twitter' === service ? display_name : connection.username; - profileImage = profile_picture; + const { display_name, profile_picture, external_handle } = connection; - // Connections schema is a mess - if ( 'bluesky' === service ) { - username = external_name; - } + displayName = display_name; + username = external_handle; + profileImage = profile_picture; } } @@ -150,7 +140,7 @@ export function getConnectionProfileDetails( state, service, { forceDefaults = f /** * Get the connections being deleted. * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * @return {import("../types").ConnectionData['deletingConnections']} The connection being deleted. */ export function getDeletingConnections( state ) { @@ -160,7 +150,7 @@ export function getDeletingConnections( state ) { /** * Get the connections being updated. * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * @return {import("../types").ConnectionData['updatingConnections']} The connection being updated. */ export function getUpdatingConnections( state ) { @@ -170,7 +160,7 @@ export function getUpdatingConnections( state ) { /** * Get the account being reconnected * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * @return {import("../types").ConnectionData['reconnectingAccount']} The account being reconnected. */ export function getReconnectingAccount( state ) { @@ -180,8 +170,8 @@ export function getReconnectingAccount( state ) { /** * Get the abort controllers for a specific request type. * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} requestType - The request type. + * @param {SocialStoreState} state - State object. + * @param {string} requestType - The request type. * * @return {Array} The abort controllers. */ @@ -192,35 +182,35 @@ export function getAbortControllers( state, requestType = REQUEST_TYPE_DEFAULT ) /** * Whether a mastodon account is already connected. * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} username - The mastodon username. + * @param {SocialStoreState} state - State object. + * @param {string} handle - The mastodon handle. * * @return {boolean} Whether the mastodon account is already connected. */ -export function isMastodonAccountAlreadyConnected( state, username ) { +export function isMastodonAccountAlreadyConnected( state, handle ) { return getConnectionsByService( state, 'mastodon' ).some( connection => { - return connection.external_display === username; + return connection.external_handle === handle; } ); } /** * Whether a Bluesky account is already connected. * - * @param {import("../types").SocialStoreState} state - State object. - * @param {string} handle - The Bluesky handle. + * @param {SocialStoreState} state - State object. + * @param {string} handle - The Bluesky handle. * * @return {boolean} Whether the Bluesky account is already connected. */ export function isBlueskyAccountAlreadyConnected( state, handle ) { return getConnectionsByService( state, 'bluesky' ).some( connection => { - return connection.external_name === handle; + return connection.external_handle === handle; } ); } /** * Returns the latest KeyringResult from the store. * - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * * @return {import("../types").KeyringResult} The KeyringResult */ @@ -230,7 +220,7 @@ export function getKeyringResult( state ) { /** * Whether the connections modal is open. - * @param {import("../types").SocialStoreState} state - State object. + * @param {SocialStoreState} state - State object. * * @return {boolean} Whether the connections modal is open. */