diff --git a/src/platform/onboarding/onboardingTourStore.ts b/src/platform/onboarding/onboardingTourStore.ts index 73d53439db6..602ce8a0860 100644 --- a/src/platform/onboarding/onboardingTourStore.ts +++ b/src/platform/onboarding/onboardingTourStore.ts @@ -1,3 +1,4 @@ +import { whenever } from '@vueuse/core' import { defineStore } from 'pinia' import { computed, ref, shallowRef, watch } from 'vue' @@ -127,6 +128,14 @@ export const useOnboardingTourStore = defineStore('onboardingTour', () => { } } stepIdx.value = idx + if (nextStep.onEnter) { + try { + await nextStep.onEnter(signal) + } catch (error) { + if (!signal.aborted) console.error('coachmark onEnter failed', error) + } + if (signal.aborted) return + } trackTour('step_shown') } @@ -169,13 +178,9 @@ export const useOnboardingTourStore = defineStore('onboardingTour', () => { } for (const [entryPath, trigger] of useTourTriggers()) { - watch( - trigger.autoOpen, - (visible) => { - if (visible) startTour(entryPath) - }, - { immediate: true } - ) + whenever(trigger.autoOpen, () => startTour(entryPath), { + immediate: true + }) watch(trigger.holds, (holding) => { if (!holding && activeTour.value === entryPath) finish('skipped', { markSeen: false, skipReason: 'trigger_lost' }) @@ -192,19 +197,26 @@ export const useOnboardingTourStore = defineStore('onboardingTour', () => { void settingStore.set(TOUR_SEEN_SETTING, [...seen, entryPath]) } - function startTour(entryPath: EntryPath, force = false) { - if (steps.value.length) return - if (!force && hasSeenTour(entryPath)) return - const resolved = resolveSteps(TOURS[entryPath], targetMounted) - if (!resolved.length) return + function begin(entryPath: EntryPath): boolean { + if (steps.value.length) return false + const definition = TOURS[entryPath] + const built = Array.isArray(definition) ? definition : definition() + const resolved = resolveSteps(built, targetMounted) + if (!resolved.length) return false steps.value = resolved activeTour.value = entryPath trackTour('started') void showStep(0) + return true + } + + function startTour(entryPath: EntryPath): boolean { + if (hasSeenTour(entryPath)) return false + return begin(entryPath) } function replayTour(entryPath: EntryPath) { - startTour(entryPath, true) + begin(entryPath) } return { @@ -219,6 +231,7 @@ export const useOnboardingTourStore = defineStore('onboardingTour', () => { countedStepIdx, countedStepsTotal, waitingForTarget, + startTour, replayTour, next, back, diff --git a/src/platform/onboarding/onboardingTours.ts b/src/platform/onboarding/onboardingTours.ts index c803811fad5..19c71a5eb1c 100644 --- a/src/platform/onboarding/onboardingTours.ts +++ b/src/platform/onboarding/onboardingTours.ts @@ -40,8 +40,11 @@ export interface CoachStep { /** Renders the landing dialog instead of a spotlight. */ landing?: boolean image?: string + onEnter?: (signal: AbortSignal) => void | Promise } +export type TourDefinition = CoachStep[] | (() => CoachStep[]) + /** * Fixes the running step set (and so the step count) at tour start: drops steps * whose own target isn't mounted, keeping targetless and deferred steps. @@ -55,7 +58,7 @@ export function resolveSteps( ) } -export const TOURS: Record = { +export const TOURS: Record = { appMode: [ { name: 'landing',