Commit e9301aa7 authored by 666's avatar 666

feat: 优化代码

parent 83123b6f
......@@ -11,20 +11,27 @@ import wrapStyles from '../../components/main-header.module.scss'
const useStepDetails = (stageDetail, targetSteps) => {
return useMemo(() => {
const details = targetSteps
.map(targetStep => {
const details = targetSteps.reduce((result, targetStep) => {
const detail = stageDetail?.nextSteps?.find(
step => step.nextStep === targetStep
)
const index = stageDetail?.nextSteps?.findIndex(
step => step.nextStep === targetStep
)
return {
detail,
index,
if (detail !== undefined && index !== -1) {
if (['S', 'A', 'B'].includes(targetStep)) {
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
}, [stageDetail?.nextSteps, targetSteps])
}
......@@ -33,7 +40,7 @@ const MessageRenderer = ({ step, level, gear, upGear }) => {
const renderMessage = useCallback(() => {
const detailIndexMap = { level, gear, upGear }
const targetStep = Object.keys(detailIndexMap).find(
key => detailIndexMap[key].index + 2 === step
key => detailIndexMap[key]?.index + 2 === step
)
if (!targetStep) {
......@@ -88,8 +95,7 @@ export const Scene = ({ children, index }: { index: number, children: React.Reac
const guideComplete = useStoreState(state => state.prepareModel.passGuideComplete)
const updateState = useStoreActions(actions => actions.prepareModel.updateState)
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) => {
pageScrollTo({
duration: 600,
......@@ -109,15 +115,19 @@ export const Scene = ({ children, index }: { index: number, children: React.Reac
}, [guideComplete, updateState])
const onClick = () => {
const stepDetails = [level, gear, upGear]
const activeSteps = stepDetails.filter(item => item.detail)
const stepDetails = { level, gear, upGear }
const activeSteps = Object.values(stepDetails).filter(item => item)
const activeStepIndex = activeSteps.length
if (step === activeStepIndex + 1) {
if (activeStepIndex >= 2) {
scrollToBottom(0)
setTimeout(() => {
updateState({ passStep: -1 })
}, 666)
} else {
updateState({ passStep: -1 })
}
} else {
scrollToBottom()
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