diff --git a/src/hooks/useResidenceContinue.ts b/src/hooks/useResidenceContinue.ts index 7fab6f3..cfda116 100644 --- a/src/hooks/useResidenceContinue.ts +++ b/src/hooks/useResidenceContinue.ts @@ -1,5 +1,5 @@ import { useCallback, useEffect, useRef, useState } from 'react' -import { useNavigate, useParams } from 'react-router-dom' +import { useNavigate, useParams, useLocation } from 'react-router-dom' import { usePuzzleStore } from '@/store/puzzleStore' import { getResidenceStageCompletion, @@ -15,6 +15,12 @@ import { useVideoContextSafe } from '@/contexts/VideoContext' */ const promptedResidences = new Set() +/** + * 记录上一次访问的路由,用于判断是否刚从场景离开。 + * 模块级变量,跨 hook 实例共享。 + */ +let previousPath = '' + export interface UseResidenceContinueParams { /** 故居标识(同时也是路由里的 starName),如 'maozedong' */ residence: string @@ -44,6 +50,7 @@ export interface UseResidenceContinueResult { * 3. 本会话内该故居尚未自动弹过 * 4. 故居已解锁 && 已开始 && 未全部完成 * 5. 能解析出有效的续学目标 + * 6. 不是刚从本故居场景页面离开的(避免干扰正常流程) * * 弹一次后即标记,之后再回到 home 不再自动弹(由首页的三个阶段按钮继续)。 */ @@ -54,6 +61,7 @@ export function useResidenceContinue({ reflectionRoute, }: UseResidenceContinueParams): UseResidenceContinueResult { const navigate = useNavigate() + const location = useLocation() const { moduleName } = useParams<{ moduleName: string }>() const video = useVideoContextSafe() const contentVisible = video ? video.isContentVisible : true @@ -71,6 +79,24 @@ export function useResidenceContinue({ // 没开始过的新故居不打扰,让学生自己点「一」正常开始 if (!isResidenceStarted(residence, puzzles)) return + // 检查上一个路由:如果是从本故居的 place/reflection/internalization 来的,不弹 + // 例如从 /revolutionary/maozedong/place1 回到 /revolutionary/maozedong/home + const currentPath = location.pathname + const isFromResidenceScene = + previousPath && + (previousPath.includes(`/${residence}/place`) || + previousPath.includes(`/${residence}/reflection`) || + previousPath.includes(`/${residence}/internalization`) || + previousPath.includes(`/${residence}/draw`)) + + // 更新 previousPath 供下次判断 + previousPath = currentPath + + if (isFromResidenceScene) { + // 刚从场景离开,不弹窗 + return + } + const completion = getResidenceStageCompletion(residence, puzzles) if (completion.residenceComplete) return @@ -84,7 +110,16 @@ export function useResidenceContinue({ targetRef.current = target promptedResidences.add(residence) setVisible(true) - }, [isLoaded, storeReady, contentVisible, residence, puzzles, lastVisitedPlace, reflectionRoute]) + }, [ + isLoaded, + storeReady, + contentVisible, + residence, + puzzles, + lastVisitedPlace, + reflectionRoute, + location.pathname, + ]) const onContinue = useCallback(() => { setVisible(false)