Skip to content

Commit

Permalink
T365634: Fix warnings (#122)
Browse files Browse the repository at this point in the history
* Fix preview.js warning

* Fix popover.js warning

* Fix inline.js warning

* Wrap insertControllersMenu with a useCallback

* Fix tooltip.js warnings
  • Loading branch information
medied authored Jun 12, 2024
1 parent ee478a0 commit f06b10d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const InlineEditUI = ( {
useEffect( () => {
setTitle( activeAttributes.title || getTextContent( slice( value ) ) );
setLang( activeAttributes.lang || getSiteLanguage() );
}, [ activeAttributes ] );
}, [ activeAttributes, value ] );

useEffect( () => {
if ( title ) {
Expand Down
2 changes: 1 addition & 1 deletion src/link/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const WikipediaPreviewPopover = ( {
if ( e.target.className === 'components-popover__content' ) {
stopViewingPreview();
}
}, [] );
}, [ stopViewingPreview ] );

const setPlacement = () => {
if ( isTextNearTheEdge( anchor ) ) {
Expand Down
9 changes: 5 additions & 4 deletions src/link/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
useState,
useEffect,
useLayoutEffect,
useCallback,
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import wikipediaPreview from 'wikipedia-preview';
Expand All @@ -27,7 +28,7 @@ export const PreviewEditUI = ( {
setSelectingSection( true );
};

const insertControllersMenu = () => {
const insertControllersMenu = useCallback( () => {
const preview = document.querySelector( '.wikipediapreview' );
const previewHeader = document.querySelector(
'.wikipediapreview-header'
Expand Down Expand Up @@ -56,7 +57,7 @@ export const PreviewEditUI = ( {
.querySelector( '.wikipediapreview-edit-preview-container' )
.setAttribute( 'dir', preview.getAttribute( 'dir' ) );
}
};
}, [] );

useEffect( () => {
const { title, lang } = activeAttributes;
Expand All @@ -81,7 +82,7 @@ export const PreviewEditUI = ( {
?.removeEventListener( 'click', toggleControllersMenu );
};
}
}, [ previewHtml, selectingSection ] );
}, [ previewHtml, selectingSection, insertControllersMenu ] );

useLayoutEffect( () => {
document
Expand All @@ -92,7 +93,7 @@ export const PreviewEditUI = ( {
.querySelector( '.wikipediapreview-header-closebtn' )
?.removeEventListener( 'click', onForceClose );
};
}, [ previewHtml, selectingSection ] );
}, [ previewHtml, onForceClose, selectingSection ] );

return (
<>
Expand Down
34 changes: 17 additions & 17 deletions src/link/tooltip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global wikipediapreviewCustomTooltip */
import { __ } from '@wordpress/i18n';
import { Popover } from '@wordpress/components';
import { useEffect, useState } from '@wordpress/element';
import { useEffect, useState, useCallback } from '@wordpress/element';

export const CustomTooltip = ( {
anchorRef,
Expand All @@ -23,19 +23,26 @@ export const CustomTooltip = ( {
} );
};

const finishDisplayingTooltip = () => {
const finishDisplayingTooltip = useCallback( () => {
setDisplayTooltip( false );
wikipediapreviewCustomTooltip.tooltipDuration = 1;
updateStoredProperty( 'duration' );
};
}, [] );

const clearTimeouts = () => {
const clearTimeouts = useCallback( () => {
timeoutIds.forEach( ( id ) => {
clearTimeout( id );
} );
};
}, [ timeoutIds ] );

const waitFiveSecsThenHideTooltip = useCallback( () => {
const fiveSecId = setTimeout( () => {
finishDisplayingTooltip();
}, 5000 );
setTimeoutIds( ( timeoutIds ) => [ ...timeoutIds, fiveSecId ] );
}, [ finishDisplayingTooltip ] );

const waitOneSecThenDisplayTooltip = () => {
const waitOneSecThenDisplayTooltip = useCallback( () => {
const oneSecId = setTimeout( () => {
if ( anchorRef.current ) {
setDisplayTooltip( true );
Expand All @@ -47,34 +54,27 @@ export const CustomTooltip = ( {
}
}, 1000 );
setTimeoutIds( ( timeoutIds ) => [ ...timeoutIds, oneSecId ] );
};

const waitFiveSecsThenHideTooltip = () => {
const fiveSecId = setTimeout( () => {
finishDisplayingTooltip();
}, 5000 );
setTimeoutIds( ( timeoutIds ) => [ ...timeoutIds, fiveSecId ] );
};
}, [ anchorRef, tooltipDisplayedCount, waitFiveSecsThenHideTooltip ] );

useEffect( () => {
if ( tooltipDisplayedCount < tooltipDisplayedLimit && tooltipDisplayedFullDuration < 1 ) {
waitOneSecThenDisplayTooltip();
}
}, [] );
}, [ tooltipDisplayedCount, tooltipDisplayedFullDuration, waitOneSecThenDisplayTooltip ] );

useEffect( () => {
// Clear all timeouts when unmounting
return () => {
clearTimeouts();
};
}, [ timeoutIds ] );
}, [ timeoutIds, clearTimeouts ] );

useEffect( () => {
if ( addingPreview ) {
clearTimeouts();
finishDisplayingTooltip();
}
}, [ addingPreview ] );
}, [ addingPreview, clearTimeouts, finishDisplayingTooltip ] );

return (
<div>
Expand Down

0 comments on commit f06b10d

Please sign in to comment.