Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayadav09 committed Jan 8, 2025
1 parent 130d40b commit aa85701
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/components/HelpCenter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect, useState, useMemo, useRef } from '@wordpress/element';
import apiFetch from '@wordpress/api-fetch';
import { debounce } from 'lodash';
import moduleAI from '@newfold-labs/wp-module-ai';
import SearchResults from './SearchResults';
Expand Down Expand Up @@ -43,34 +42,36 @@ const HelpCenter = ( props ) => {
setHelpEnabled( false );
}
};

useEffect( () => {
getHelpStatus();
}, [] );
if ( props.refresh && searchInput !== '' ) {
setSearchInput( '' );
}
}, [ props.refresh ] );

useEffect( () => {
// Check help center capability
getHelpStatus();

// Fetch initial data
fetchInitialData();

// Add event listener for localStorage changes
const updateVisibility = () => {
setVisible( LocalStorageUtils.getHelpVisible() );
};

// Add the event listener on component mount
window.addEventListener( 'storage', updateVisibility );

// Remove the event listener when the component unmounts
return () => {
// Cancel any debounced calls
debouncedResults.cancel();

// Remove the storage event listener
window.removeEventListener( 'storage', updateVisibility );
};
}, [] );

useEffect( () => {
if ( props.refresh && searchInput !== '' ) {
setSearchInput( '' );
}
}, [ props.refresh ] );

useEffect( () => {
fetchInitialData();
}, [] );

useEffect( () => {
if ( initComplete ) {
adjustPadding();
Expand All @@ -82,12 +83,6 @@ const HelpCenter = ( props ) => {
adjustPadding();
}, [ showSuggestions ] );

// Clear any debounce problems
useEffect( () => {
debouncedResults.cancel();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );

const populateSearchResult = ( postContent, postId, postTitle ) => {
const resultContentFormatted =
postContent && postContent.replace( /\n/g, '<br />' );
Expand Down

0 comments on commit aa85701

Please sign in to comment.