fix(guided-tour): wrapped startSection and setCurrentStep with useCallback hooks

This commit is contained in:
vincentbpro 2022-02-14 12:21:11 +01:00
parent 466ac108a0
commit f564420630
2 changed files with 33 additions and 27 deletions

View File

@ -1,4 +1,4 @@
import React, { useReducer } from 'react';
import React, { useCallback, useReducer } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import { GuidedTourProvider } from '@strapi/helper-plugin';
@ -15,24 +15,27 @@ const GuidedTour = ({ children }) => {
init
);
const setCurrentStep = step => {
// if step is null it is intentional, we need to dispatch it
if (step !== null) {
const isStepAlreadyDone = get(guidedTourState, step);
const isStepToShow = arePreviousStepsDone(step, guidedTourState);
const setCurrentStep = useCallback(
step => {
// if step is null it is intentional, we need to dispatch it
if (step !== null) {
const isStepAlreadyDone = get(guidedTourState, step);
const isStepToShow = arePreviousStepsDone(step, guidedTourState);
if (isStepAlreadyDone || isSkipped || !isStepToShow) {
return null;
if (isStepAlreadyDone || isSkipped || !isStepToShow) {
return null;
}
}
}
persistStateToLocaleStorage.addCurrentStep(step);
persistStateToLocaleStorage.addCurrentStep(step);
return dispatch({
type: 'SET_CURRENT_STEP',
step,
});
};
return dispatch({
type: 'SET_CURRENT_STEP',
step,
});
},
[guidedTourState, isSkipped]
);
const setGuidedTourVisibility = value => {
dispatch({
@ -51,21 +54,24 @@ const GuidedTour = ({ children }) => {
});
};
const startSection = sectionName => {
const sectionSteps = guidedTourState[sectionName];
const startSection = useCallback(
sectionName => {
const sectionSteps = guidedTourState[sectionName];
if (sectionSteps) {
const isSectionToShow = arePreviousSectionsDone(sectionName, guidedTourState);
const firstStep = Object.keys(sectionSteps)[0];
const isFirstStepDone = sectionSteps[firstStep];
if (sectionSteps) {
const isSectionToShow = arePreviousSectionsDone(sectionName, guidedTourState);
const firstStep = Object.keys(sectionSteps)[0];
const isFirstStepDone = sectionSteps[firstStep];
if (isSectionToShow && !currentStep && !isFirstStepDone) {
return setCurrentStep(`${sectionName}.${firstStep}`);
if (isSectionToShow && !currentStep && !isFirstStepDone) {
return setCurrentStep(`${sectionName}.${firstStep}`);
}
}
}
return null;
};
return null;
},
[currentStep, guidedTourState, setCurrentStep]
);
const setSkipped = value => {
persistStateToLocaleStorage.setSkipped(value);

View File

@ -38,7 +38,7 @@ const ApiTokenListView = () => {
useEffect(() => {
startSection('apiTokens');
});
}, [startSection]);
useEffect(() => {
push({ search: qs.stringify({ sort: 'name:ASC' }, { encode: false }) });