Commit e9301aa7 authored by 666's avatar 666

feat: 优化代码

parent 83123b6f
...@@ -11,20 +11,27 @@ import wrapStyles from '../../components/main-header.module.scss' ...@@ -11,20 +11,27 @@ import wrapStyles from '../../components/main-header.module.scss'
const useStepDetails = (stageDetail, targetSteps) => { const useStepDetails = (stageDetail, targetSteps) => {
return useMemo(() => { return useMemo(() => {
const details = targetSteps const details = targetSteps.reduce((result, targetStep) => {
.map(targetStep => { const detail = stageDetail?.nextSteps?.find(
const detail = stageDetail?.nextSteps?.find( step => step.nextStep === targetStep
step => step.nextStep === targetStep )
) const index = stageDetail?.nextSteps?.findIndex(
const index = stageDetail?.nextSteps?.findIndex( step => step.nextStep === targetStep
step => step.nextStep === targetStep )
)
return { if (detail !== undefined && index !== -1) {
detail, if (['S', 'A', 'B'].includes(targetStep)) {
index, result.level = { detail, index }
} else if (['10', '20', '30'].includes(targetStep)) {
result.gear = { detail, index }
} else if (targetStep === 'upGear') {
result.upGear = { detail, index }
} }
}) }
.filter(item => item.detail !== undefined && item.index !== -1)
return result
}, {})
return details return details
}, [stageDetail?.nextSteps, targetSteps]) }, [stageDetail?.nextSteps, targetSteps])
} }
...@@ -33,7 +40,7 @@ const MessageRenderer = ({ step, level, gear, upGear }) => { ...@@ -33,7 +40,7 @@ const MessageRenderer = ({ step, level, gear, upGear }) => {
const renderMessage = useCallback(() => { const renderMessage = useCallback(() => {
const detailIndexMap = { level, gear, upGear } const detailIndexMap = { level, gear, upGear }
const targetStep = Object.keys(detailIndexMap).find( const targetStep = Object.keys(detailIndexMap).find(
key => detailIndexMap[key].index + 2 === step key => detailIndexMap[key]?.index + 2 === step
) )
if (!targetStep) { if (!targetStep) {
...@@ -88,8 +95,7 @@ export const Scene = ({ children, index }: { index: number, children: React.Reac ...@@ -88,8 +95,7 @@ export const Scene = ({ children, index }: { index: number, children: React.Reac
const guideComplete = useStoreState(state => state.prepareModel.passGuideComplete) const guideComplete = useStoreState(state => state.prepareModel.passGuideComplete)
const updateState = useStoreActions(actions => actions.prepareModel.updateState) const updateState = useStoreActions(actions => actions.prepareModel.updateState)
const stageDetail = useStoreState(state => state.gameModel.stageDetail) const stageDetail = useStoreState(state => state.gameModel.stageDetail)
const [level, gear, upGear] = useStepDetails(stageDetail, ['S', 'A', 'B', '10', '20', '30', 'upGear']) const { level, gear, upGear } = useStepDetails(stageDetail, ['S', 'A', 'B', '10', '20', '30', 'upGear'])
const scrollToBottom = useCallback((scrollTop?: number) => { const scrollToBottom = useCallback((scrollTop?: number) => {
pageScrollTo({ pageScrollTo({
duration: 600, duration: 600,
...@@ -109,15 +115,19 @@ export const Scene = ({ children, index }: { index: number, children: React.Reac ...@@ -109,15 +115,19 @@ export const Scene = ({ children, index }: { index: number, children: React.Reac
}, [guideComplete, updateState]) }, [guideComplete, updateState])
const onClick = () => { const onClick = () => {
const stepDetails = [level, gear, upGear] const stepDetails = { level, gear, upGear }
const activeSteps = stepDetails.filter(item => item.detail) const activeSteps = Object.values(stepDetails).filter(item => item)
const activeStepIndex = activeSteps.length const activeStepIndex = activeSteps.length
if (step === activeStepIndex + 1) { if (step === activeStepIndex + 1) {
scrollToBottom(0) if (activeStepIndex >= 2) {
setTimeout(() => { scrollToBottom(0)
setTimeout(() => {
updateState({ passStep: -1 })
}, 666)
} else {
updateState({ passStep: -1 }) updateState({ passStep: -1 })
}, 666) }
} else { } else {
scrollToBottom() scrollToBottom()
updateState({ passStep: step + 1 }) updateState({ passStep: step + 1 })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment