fix: navigate tour to mailbox when starting from another page
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from "react";
|
import { createContext, useContext, useState, useCallback, useEffect, type ReactNode } from "react";
|
||||||
import { useRouter } from "@/i18n/navigation";
|
import { useRouter, usePathname } from "@/i18n/navigation";
|
||||||
import { useAuthStore } from "@/stores/auth-store";
|
import { useAuthStore } from "@/stores/auth-store";
|
||||||
import { useCalendarStore } from "@/stores/calendar-store";
|
import { useCalendarStore } from "@/stores/calendar-store";
|
||||||
import { useWebDAVStore } from "@/stores/webdav-store";
|
import { useWebDAVStore } from "@/stores/webdav-store";
|
||||||
@@ -34,6 +34,7 @@ export function useTour() {
|
|||||||
|
|
||||||
export function TourProvider({ children }: { children: ReactNode }) {
|
export function TourProvider({ children }: { children: ReactNode }) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const pathname = usePathname();
|
||||||
const { isDemoMode } = useAuthStore();
|
const { isDemoMode } = useAuthStore();
|
||||||
const { supportsCalendar } = useCalendarStore();
|
const { supportsCalendar } = useCalendarStore();
|
||||||
const { supportsWebDAV } = useWebDAVStore();
|
const { supportsWebDAV } = useWebDAVStore();
|
||||||
@@ -63,9 +64,16 @@ export function TourProvider({ children }: { children: ReactNode }) {
|
|||||||
// If the resume step is beyond the current steps, start from 0
|
// If the resume step is beyond the current steps, start from 0
|
||||||
if (resumeStep >= steps.length) resumeStep = 0;
|
if (resumeStep >= steps.length) resumeStep = 0;
|
||||||
|
|
||||||
|
// Most steps live on the mailbox — navigate there (or to the step's specific page)
|
||||||
|
// so we don't start the tour on a page where the targets don't exist.
|
||||||
|
const targetPage = steps[resumeStep]?.page ?? "/";
|
||||||
|
if (pathname !== targetPage) {
|
||||||
|
router.push(targetPage);
|
||||||
|
}
|
||||||
|
|
||||||
setCurrentStep(resumeStep);
|
setCurrentStep(resumeStep);
|
||||||
setIsActive(true);
|
setIsActive(true);
|
||||||
}, [steps.length]);
|
}, [steps, pathname, router]);
|
||||||
|
|
||||||
const stopTour = useCallback(() => {
|
const stopTour = useCallback(() => {
|
||||||
setIsActive(false);
|
setIsActive(false);
|
||||||
@@ -90,14 +98,17 @@ export function TourProvider({ children }: { children: ReactNode }) {
|
|||||||
}
|
}
|
||||||
const next = currentStep + 1;
|
const next = currentStep + 1;
|
||||||
const nextStepDef = steps[next];
|
const nextStepDef = steps[next];
|
||||||
|
const currentStepDef = steps[currentStep];
|
||||||
setCurrentStep(next);
|
setCurrentStep(next);
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(TOUR_CURRENT_STEP_KEY, String(next));
|
localStorage.setItem(TOUR_CURRENT_STEP_KEY, String(next));
|
||||||
} catch { /* */ }
|
} catch { /* */ }
|
||||||
|
|
||||||
// Navigate if the next step requires a different page
|
// Navigate when the next step is on a different page (treat "no page" as the mailbox)
|
||||||
if (nextStepDef?.page) {
|
const nextPage = nextStepDef?.page ?? "/";
|
||||||
router.push(nextStepDef.page);
|
const currentPage = currentStepDef?.page ?? "/";
|
||||||
|
if (nextPage !== currentPage) {
|
||||||
|
router.push(nextPage);
|
||||||
}
|
}
|
||||||
}, [currentStep, steps, completeTour, router]);
|
}, [currentStep, steps, completeTour, router]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user