fix: 修复从场景返回首页时误弹续学提示的问题
问题:用户从对话/体悟/内化场景点击「离开」返回故居首页时,会立即弹出「继续学习」提示,干扰正常流程。 修复: - 新增 previousPath 模块级变量记录上一个路由 - 检测进入首页时上一个路由是否为本故居的 place/reflection/internalization/draw - 如果是刚从场景离开的,不弹窗,让用户自主选择下一步 影响文件: - src/hooks/useResidenceContinue.ts Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
73f6df92ce
commit
165374b986
1 file changed
+37
-2
@@ -1,5 +1,5 @@
|
|||||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
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 { usePuzzleStore } from '@/store/puzzleStore'
|
||||||
import {
|
import {
|
||||||
getResidenceStageCompletion,
|
getResidenceStageCompletion,
|
||||||
@@ -15,6 +15,12 @@ import { useVideoContextSafe } from '@/contexts/VideoContext'
|
|||||||
*/
|
*/
|
||||||
const promptedResidences = new Set<string>()
|
const promptedResidences = new Set<string>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 记录上一次访问的路由,用于判断是否刚从场景离开。
|
||||||
|
* 模块级变量,跨 hook 实例共享。
|
||||||
|
*/
|
||||||
|
let previousPath = ''
|
||||||
|
|
||||||
export interface UseResidenceContinueParams {
|
export interface UseResidenceContinueParams {
|
||||||
/** 故居标识(同时也是路由里的 starName),如 'maozedong' */
|
/** 故居标识(同时也是路由里的 starName),如 'maozedong' */
|
||||||
residence: string
|
residence: string
|
||||||
@@ -44,6 +50,7 @@ export interface UseResidenceContinueResult {
|
|||||||
* 3. 本会话内该故居尚未自动弹过
|
* 3. 本会话内该故居尚未自动弹过
|
||||||
* 4. 故居已解锁 && 已开始 && 未全部完成
|
* 4. 故居已解锁 && 已开始 && 未全部完成
|
||||||
* 5. 能解析出有效的续学目标
|
* 5. 能解析出有效的续学目标
|
||||||
|
* 6. 不是刚从本故居场景页面离开的(避免干扰正常流程)
|
||||||
*
|
*
|
||||||
* 弹一次后即标记,之后再回到 home 不再自动弹(由首页的三个阶段按钮继续)。
|
* 弹一次后即标记,之后再回到 home 不再自动弹(由首页的三个阶段按钮继续)。
|
||||||
*/
|
*/
|
||||||
@@ -54,6 +61,7 @@ export function useResidenceContinue({
|
|||||||
reflectionRoute,
|
reflectionRoute,
|
||||||
}: UseResidenceContinueParams): UseResidenceContinueResult {
|
}: UseResidenceContinueParams): UseResidenceContinueResult {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
const location = useLocation()
|
||||||
const { moduleName } = useParams<{ moduleName: string }>()
|
const { moduleName } = useParams<{ moduleName: string }>()
|
||||||
const video = useVideoContextSafe()
|
const video = useVideoContextSafe()
|
||||||
const contentVisible = video ? video.isContentVisible : true
|
const contentVisible = video ? video.isContentVisible : true
|
||||||
@@ -71,6 +79,24 @@ export function useResidenceContinue({
|
|||||||
// 没开始过的新故居不打扰,让学生自己点「一」正常开始
|
// 没开始过的新故居不打扰,让学生自己点「一」正常开始
|
||||||
if (!isResidenceStarted(residence, puzzles)) return
|
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)
|
const completion = getResidenceStageCompletion(residence, puzzles)
|
||||||
if (completion.residenceComplete) return
|
if (completion.residenceComplete) return
|
||||||
|
|
||||||
@@ -84,7 +110,16 @@ export function useResidenceContinue({
|
|||||||
targetRef.current = target
|
targetRef.current = target
|
||||||
promptedResidences.add(residence)
|
promptedResidences.add(residence)
|
||||||
setVisible(true)
|
setVisible(true)
|
||||||
}, [isLoaded, storeReady, contentVisible, residence, puzzles, lastVisitedPlace, reflectionRoute])
|
}, [
|
||||||
|
isLoaded,
|
||||||
|
storeReady,
|
||||||
|
contentVisible,
|
||||||
|
residence,
|
||||||
|
puzzles,
|
||||||
|
lastVisitedPlace,
|
||||||
|
reflectionRoute,
|
||||||
|
location.pathname,
|
||||||
|
])
|
||||||
|
|
||||||
const onContinue = useCallback(() => {
|
const onContinue = useCallback(() => {
|
||||||
setVisible(false)
|
setVisible(false)
|
||||||
|
|||||||
Reference in new issue
Block a user