Skip to content

Commit

Permalink
Merge pull request #499 from newfold-labs/enhance/improve-performance
Browse files Browse the repository at this point in the history
Enhance AI Generation time
  • Loading branch information
diwanshuster authored Mar 4, 2024
2 parents aa10d23 + 5a6e99f commit b6f61b8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../../../../../constants';

import { useSelect } from '@wordpress/data';
import { stepTheFork } from '../../../../steps/TheFork/step';
import { store as nfdOnboardingStore } from '../../../../store';
import StepNavigation from './step-navigation';

Expand Down Expand Up @@ -44,7 +45,7 @@ const SiteGenHeader = () => {
<Fill name={ `${ HEADER_SITEGEN }/${ HEADER_TOP }` }>
<>
<AdminBar />
{ isHeaderNavigationEnabled && (
{ currentStep !== stepTheFork && (
<ProgressBar progress={ progress } />
) }
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import classNames from 'classnames';
import { setFlow } from '../../../utils/api/flow';
import {
generateSiteGenMeta,
getHomepages,
getSiteGenIdentifiers,
} from '../../../utils/api/siteGen';
import Footer from '../../Footer';
Expand Down Expand Up @@ -122,15 +123,33 @@ const SiteGen = () => {
skipCache
);
if ( data !== null ) {
// A Identifier request was sucessfuly made with valid response
currentData.sitegen.siteGenMetaStatus.currentStatus += 1;
if (
currentData.sitegen.siteGenMetaStatus.currentStatus ===
currentData.sitegen.siteGenMetaStatus.totalCount
) {
// Once all requests are completed use cache to get data
currentData.sitegen.skipCache = false;

// Increase count after site meta calls to ensure systematic call of homepages
currentData.sitegen.siteGenMetaStatus.totalCount += 1;
setCurrentOnboardingData( currentData );

// Get the homepages and set that in flow
const response = await getHomepages(
currentData.sitegen.siteDetails.prompt
);

if ( response.body ) {
currentData.sitegen.homepages.data = response.body;
}
currentData.sitegen.siteGenMetaStatus.currentStatus += 1;
}
// Sync the current request changed to State
setCurrentOnboardingData( currentData );

// Sets the Site Title and Taglin in Live Preview
if ( identifier === 'site_config' ) {
editEntityRecord( 'root', 'site', undefined, {
title: data.site_title,
Expand All @@ -139,6 +158,7 @@ const SiteGen = () => {
}
}
} catch ( err ) {
// Check if it failed then retry again
if ( retryCount < MAX_RETRIES_SITE_GEN ) {
return performSiteGenMetaGeneration(
siteInfo,
Expand All @@ -148,20 +168,34 @@ const SiteGen = () => {
);
}

// If the retry also did not work show the error state
setFailedApi( ( prevState ) => {
// If the error doesn't exist add it to the Failed List
if ( ! prevState.includes( identifier ) ) {
return [ ...prevState, identifier ];
}
return prevState;
} );
// Activate the Error Page
currentData.sitegen.siteGenErrorStatus = true;
setCurrentOnboardingData( currentData );
}
}

async function generateSiteGenData() {
async function generateSiteGenData( forceRun = false ) {
// ForceRun tells us to bypass the page check or not
// because the requests failed on Logo step is retried in Experience step

// Start the API Requests when the loader is shown.
if ( ! location.pathname.includes( 'experience' ) ) {
if ( ! location.pathname.includes( 'site-logo' ) && ! forceRun ) {
return;
}

// If all the requests have been made already skip this
if (
currentData.sitegen.siteGenMetaStatus.currentStatus ===
currentData.sitegen.siteGenMetaStatus.totalCount
) {
return;
}

Expand All @@ -181,9 +215,9 @@ const SiteGen = () => {
identifiers = identifiers.body;

currentData.sitegen.siteGenMetaStatus.currentStatus = 0;

setCurrentOnboardingData( currentData );
}

const siteInfo = {
site_description: currentData.sitegen?.siteDetails?.prompt,
};
Expand Down Expand Up @@ -278,7 +312,7 @@ const SiteGen = () => {
prevSiteGenErrorStatus.current === true &&
siteGenErrorStatus === false
) {
generateSiteGenData();
generateSiteGenData( true );
syncStoreToDB();
}
prevSiteGenErrorStatus.current = siteGenErrorStatus;
Expand Down
79 changes: 38 additions & 41 deletions src/OnboardingSPA/steps/SiteGen/SiteLogo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import AIHeading from '../../../components/Heading/AIHeading';
import CommonLayout from '../../../components/Layouts/Common';
import NextButtonSiteGen from '../../../components/Button/NextButtonSiteGen';
import ImageUploaderWithText from '../../../components/ImageUploader/components/ImageUploaderWithText';
import SitegenAiStateHandler from '../../../components/StateHandlers/SitegenAi';
import {
OnboardingEvent,
trackOnboardingEvent,
Expand Down Expand Up @@ -99,49 +98,47 @@ const SiteGenSiteLogo = () => {

const content = getContents();
return (
<SitegenAiStateHandler>
<CommonLayout
isCentered
className="nfd-onboarding-step--site-gen__site-logo"
>
<div className="nfd-onboarding-step--site-gen__site-logo__container">
<AIHeading title={ content.heading } />
<ImageUploaderWithText
image={ siteLogo }
imageSetter={ handleSiteLogo }
<CommonLayout
isCentered
className="nfd-onboarding-step--site-gen__site-logo"
>
<div className="nfd-onboarding-step--site-gen__site-logo__container">
<AIHeading title={ content.heading } />
<ImageUploaderWithText
image={ siteLogo }
imageSetter={ handleSiteLogo }
/>
<div className="nfd-onboarding-step--site-gen__site-logo__container__buttons">
<SkipButton
callback={ () => resetSiteLogo() }
className="nfd-onboarding-step--site-gen__site-logo__container__buttons__skip"
text={ content.buttons.skip }
/>
<div className="nfd-onboarding-step--site-gen__site-logo__container__buttons">
<SkipButton
callback={ () => resetSiteLogo() }
className="nfd-onboarding-step--site-gen__site-logo__container__buttons__skip"
text={ content.buttons.skip }
/>
{ isLargeViewport && (
<NextButtonSiteGen
callback={ () => {
if ( siteLogo ) {
trackOnboardingEvent(
new OnboardingEvent(
ACTION_LOGO_ADDED
),
{
source: SITEGEN_FLOW,
}
);
}
} }
text={ content.buttons.next }
disabled={
siteLogo === undefined || siteLogo?.id === 0
? true
: false
{ isLargeViewport && (
<NextButtonSiteGen
callback={ () => {
if ( siteLogo ) {
trackOnboardingEvent(
new OnboardingEvent(
ACTION_LOGO_ADDED
),
{
source: SITEGEN_FLOW,
}
);
}
/>
) }
</div>
} }
text={ content.buttons.next }
disabled={
siteLogo === undefined || siteLogo?.id === 0
? true
: false
}
/>
) }
</div>
</CommonLayout>
</SitegenAiStateHandler>
</div>
</CommonLayout>
);
};

Expand Down

0 comments on commit b6f61b8

Please sign in to comment.