Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directly show the last response if there are existing results #65

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/components/HelpCenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const HelpCenter = ( props ) => {
const [ visible, setVisible ] = useState( false );
const [ helpEnabled, setHelpEnabled ] = useState( false );
const wrapper = useRef();
const introRef = useRef();

const getHelpStatus = async () => {
try {
Expand Down Expand Up @@ -44,8 +45,12 @@ const HelpCenter = ( props ) => {
id="helpcenterResultsWrapper"
ref={ wrapper }
>
<HelpCenterIntro />
<SearchResults wrapper={ wrapper } { ...props } />
<HelpCenterIntro introRef={ introRef } />
<SearchResults
wrapper={ wrapper }
introRef={ introRef }
{ ...props }
/>
</div>
);
};
Expand Down
26 changes: 12 additions & 14 deletions src/components/HelpCenterIntro.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,34 @@ import { __ } from '@wordpress/i18n';
import { ReactComponent as AIStars } from '../icons/ai-stars.svg';
import { useRevealText, LocalStorageUtils } from '../utils';

const HelpCenterIntro = () => {
const HelpCenterIntro = ( { introRef } ) => {
const [ startReveal, setStartReveal ] = useState( false );

useEffect( () => {
// Get the stored results from localStorage using LocalStorageUtils
const storedResults = LocalStorageUtils.getResultInfo();

// Check if the length of the stored results is <= 0
if ( storedResults.length <= 0 ) {
// If true, enable reveal effect
setStartReveal( true );
} else {
// Always ensure startReveal is set, even if it's false
setStartReveal( false );
}
setStartReveal( LocalStorageUtils.getResultInfo().length <= 0 );
}, [] );

const introText = __(
'Hi! I’m your WordPress AI assistant. </br></br> Ask me how to do things in WordPress and I’ll provide step by step instructions.</br></br> I’m still learning so I don’t have all the answers just yet.',
'wp-module-help-center'
);

// const revealedIntro = useRevealText( introText, 50, startReveal );
const { displayedText: revealedIntro } = useRevealText(
introText || '',
50,
startReveal
);
return (
<div className="helpcenter-intro">
<div
className="helpcenter-intro"
ref={ introRef }
style={ {
visibility:
LocalStorageUtils.getResultInfo().length > 0
? 'hidden'
: 'visible',
} }
>
<div>
<AIStars />
</div>
Expand Down
15 changes: 12 additions & 3 deletions src/components/SearchResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { __ } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import SearchInput from './SearchInput';

const SearchResults = ( { wrapper, refresh, brand } ) => {
const SearchResults = ( { wrapper, introRef, refresh, brand } ) => {
const [ isLoading, setIsLoading ] = useState( false );
const [ noResult, setNoResult ] = useState( false );
const [ searchInput, setSearchInput ] = useState( '' );
Expand All @@ -24,6 +24,7 @@ const SearchResults = ( { wrapper, refresh, brand } ) => {
const [ isNewResult, setIsNewResult ] = useState( false );
const [ initComplete, setInitComplete ] = useState( false );
const suggestionsRef = useRef();
const resultsContainer = useRef();

useEffect( () => {
fetchInitialData();
Expand Down Expand Up @@ -125,8 +126,12 @@ const SearchResults = ( { wrapper, refresh, brand } ) => {
wrapper.current.scrollBy( {
top: scrollDistance,
left: 0,
behavior: 'smooth',
} );

setTimeout( () => {
introRef.current.style.visibility = 'visible';
resultsContainer.current.style.visibility = 'visible';
}, 500 );
};

const getResultMatches = ( query, tokensMatched, fieldsMatched ) => {
Expand Down Expand Up @@ -247,7 +252,11 @@ const SearchResults = ( { wrapper, refresh, brand } ) => {

return (
<>
<div className="hc-results-container">
<div
className="hc-results-container"
ref={ resultsContainer }
style={ { visibility: 'hidden' } }
>
{ /* Render existing results */ }
{ resultContent?.length > 0 &&
resultContent.map( ( result, index ) => (
Expand Down