Skip to content

Commit

Permalink
Fix connection selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
manzoorwanijk committed Dec 13, 2024
1 parent dbd2445 commit 13e91e7
Showing 1 changed file with 44 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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<import("../types").Connection>} The connections list
* @return {Array<Connection>} 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 );
Expand All @@ -26,8 +27,8 @@ export function getConnectionById( state, connectionId ) {
/**
* Returns the broken connections.
*
* @param {import("../types").SocialStoreState} state - State object.
* @return {Array<import("../types").Connection>} List of broken connections.
* @param {SocialStoreState} state - State object.
* @return {Array<Connection>} List of broken connections.
*/
export function getBrokenConnections( state ) {
return getConnections( state ).filter( connection => {
Expand All @@ -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<import("../types").Connections>} The connections.
*/
Expand All @@ -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 ) {
Expand All @@ -59,35 +60,35 @@ export function hasConnections( state ) {
/**
* Returns the failed Publicize connections.
*
* @param {import("../types").SocialStoreState} state - State object.
* @return {Array<import("../types").Connection>} List of connections.
* @param {SocialStoreState} state - State object.
* @return {Array<Connection>} 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<import("../types").Connection>} List of service names that need reauthentication.
* @param {SocialStoreState} state - State object.
* @return {Array<Connection>} 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<import("../types").Connection>} List of enabled connections.
* @return {Array<Connection>} List of enabled connections.
*/
export function getEnabledConnections( state ) {
return getConnections( state ).filter( connection => connection.enabled );
Expand All @@ -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<import("../types").Connection>} List of disabled connections.
* @return {Array<Connection>} List of disabled connections.
*/
export function getDisabledConnections( state ) {
return getConnections( state ).filter( connection => ! connection.enabled );
Expand All @@ -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.
*/
Expand All @@ -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;
}
}

Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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 ) {
Expand All @@ -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<AbortController>} The abort controllers.
*/
Expand All @@ -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
*/
Expand All @@ -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.
*/
Expand Down

0 comments on commit 13e91e7

Please sign in to comment.