Skip to content

Commit

Permalink
Social: Fix connection refresh logic (#38350)
Browse files Browse the repository at this point in the history
* Only use the useRefreshConnections hook

* changelog

* Fixup versions
  • Loading branch information
gmjuhasz authored Jul 17, 2024
1 parent f80e0a5 commit 0dfd5c9
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed double request issue and simplifed refresh logic
1 change: 0 additions & 1 deletion projects/js-packages/publicize-components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import './src/social-store';

export { default as Connection } from './src/components/connection';
export { default as ConnectionVerify } from './src/components/connection-verify';
export { default as Form } from './src/components/form';
export { default as SocialPreviewsModal } from './src/components/social-previews/modal';
export { default as SocialPreviewsPanel } from './src/components/social-previews/panel';
Expand Down
2 changes: 1 addition & 1 deletion projects/js-packages/publicize-components/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-publicize-components",
"version": "0.56.1",
"version": "0.56.2-alpha",
"description": "A library of JS components required by the Publicize editor plugin",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/publicize-components/#readme",
"bugs": {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import useImageGeneratorConfig from '../../hooks/use-image-generator-config';
import useMediaDetails from '../../hooks/use-media-details';
import useMediaRestrictions, { NO_MEDIA_ERROR } from '../../hooks/use-media-restrictions';
import useRefreshAutoConversionSettings from '../../hooks/use-refresh-auto-conversion-settings';
import useRefreshConnections from '../../hooks/use-refresh-connections';
import useSocialMediaConnections from '../../hooks/use-social-media-connections';
import { store as socialStore } from '../../social-store';
import { ThemedConnectionsModal as ManageConnectionsModal } from '../manage-connections-modal';
Expand All @@ -41,7 +40,6 @@ import { ValidationNotice } from './validation-notice';
*/
export default function PublicizeForm() {
const { connections, hasConnections, hasEnabledConnections } = useSocialMediaConnections();
const refreshConnections = useRefreshConnections();
const { isEnabled: isSocialImageGeneratorEnabledForPost } = useImageGeneratorConfig();
const { shouldShowNotice, NOTICES } = useDismissNotice();
const {
Expand Down Expand Up @@ -94,8 +92,6 @@ export default function PublicizeForm() {
refreshAutoConversionSettings();
}

refreshConnections();

return (
<Wrapper>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { store as editorStore } from '@wordpress/editor';
import { Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import usePublicizeConfig from '../../hooks/use-publicize-config';
import useRefreshConnections from '../../hooks/use-refresh-connections';
import { usePostJustPublished } from '../../hooks/use-saving-post';
import useSelectSocialMediaConnections from '../../hooks/use-social-media-connections';
import PublicizeConnectionVerify from '../connection-verify';
import PublicizeForm from '../form';
import { ManualSharing } from '../manual-sharing';
import { SharePostRow } from '../share-post';
Expand All @@ -21,6 +21,7 @@ import './global.scss';
const PublicizePanel = ( { prePublish, children } ) => {
const { refresh, hasConnections, hasEnabledConnections } = useSelectSocialMediaConnections();
const isPostPublished = useSelect( select => select( editorStore ).isCurrentPostPublished(), [] );
const refreshConnections = useRefreshConnections();

const { isPublicizeEnabled, hidePublicizeFeature, togglePublicizeFeature } = usePublicizeConfig();

Expand All @@ -42,6 +43,8 @@ const PublicizePanel = ( { prePublish, children } ) => {
? {}
: { title: __( 'Share this post', 'jetpack' ), className: styles.panel };

refreshConnections();

return (
<PanelWrapper { ...wrapperProps }>
{ children }
Expand All @@ -64,7 +67,6 @@ const PublicizePanel = ( { prePublish, children } ) => {
/>
) }

<PublicizeConnectionVerify />
<PublicizeForm />
<SharePostRow />
</Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ import useSelectSocialMediaConnections from '../use-social-media-connections';
*/
export default function useRefreshConnections() {
const shouldAutoRefresh = useRef( false );
const isInitialRefresh = useRef( true );

const pageHasFocus = usePageVisibility();
const { refresh: refreshConnections } = useSelectSocialMediaConnections();

const initialRefresh = useDebounce( refreshConnections, 0 );
const debouncedRefresh = useDebounce( refreshConnections, 2000 );

return () => {
if ( isInitialRefresh.current ) {
initialRefresh();
isInitialRefresh.current = false;
}

if ( ! pageHasFocus ) {
shouldAutoRefresh.current = true;
debouncedRefresh.cancel();
Expand Down

0 comments on commit 0dfd5c9

Please sign in to comment.