-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Social: Disable sharing UI for invalid connections (#39346)
* Move checkConnectionCode utility function to a common place * Create getBrokenConnections selector * Create useIsSharingPossible hook * Disable reshare if sharing not possible at all * Disable post publish share status if not sharing * Improve JSDoc comment * Add changelog * Add explanatory comment
- Loading branch information
1 parent
4e697d6
commit 313dad5
Showing
9 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
...ages/publicize-components/changelog/update-social-disable-sharing-for-invalid-connections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: changed | ||
|
||
Social: Disable reshare button and hide post publish share status when all the enabled connections are invalid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
projects/js-packages/publicize-components/src/hooks/use-is-sharing-possible/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useSelect } from '@wordpress/data'; | ||
import useAttachedMedia from '../../hooks/use-attached-media'; | ||
import useFeaturedImage from '../../hooks/use-featured-image'; | ||
import useMediaDetails from '../../hooks/use-media-details'; | ||
import useMediaRestrictions from '../../hooks/use-media-restrictions'; | ||
import { NO_MEDIA_ERROR } from '../../hooks/use-media-restrictions/constants'; | ||
import useSocialMediaConnections from '../../hooks/use-social-media-connections'; | ||
import { store as socialStore } from '../../social-store'; | ||
|
||
/** | ||
* Returns whether sharing is possible based on the current media and connections. | ||
* | ||
* Returns true if at least one of the enabled connections is valid for sharing, false otherwise. | ||
* | ||
* @return {boolean} Whether sharing is possible. | ||
*/ | ||
export function useIsSharingPossible() { | ||
const { enabledConnections } = useSocialMediaConnections(); | ||
const { attachedMedia } = useAttachedMedia(); | ||
const featuredImageId = useFeaturedImage(); | ||
|
||
const mediaId = attachedMedia[ 0 ]?.id || featuredImageId; | ||
const { validationErrors, isConvertible } = useMediaRestrictions( | ||
enabledConnections, | ||
useMediaDetails( mediaId )[ 0 ] | ||
); | ||
|
||
const brokenConnectionIds = useSelect( select => { | ||
return select( socialStore ) | ||
.getBrokenConnections() | ||
.map( c => c.connection_id ); | ||
}, [] ); | ||
|
||
// Sharing will be possible if any of the enabled connections are valid for sharing. | ||
return enabledConnections.some( function isValidForSharing( { connection_id, service_name } ) { | ||
// Return early if the connection is broken | ||
if ( brokenConnectionIds.includes( connection_id ) ) { | ||
return false; | ||
} | ||
|
||
// Return early if the connection is not in the validation errors | ||
if ( ! ( connection_id in validationErrors ) ) { | ||
return true; | ||
} | ||
|
||
// We need some media for Instagram | ||
if ( | ||
service_name === 'instagram-business' && | ||
Object.values( validationErrors ).includes( NO_MEDIA_ERROR ) | ||
) { | ||
return false; | ||
} | ||
|
||
// Media won't be shared if it's not convertible | ||
return isConvertible; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e-components/src/components/form/utils.ts → ...icize-components/src/utils/connections.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters