All files / src/components ProductWalkthrough.vue

96.05% Statements 73/76
89.47% Branches 34/38
100% Functions 17/17
100% Lines 64/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170  21x                                           21x               21x 21x 21x   21x 21x               37x           3x 3x 3x       3x 3x   3x 3x 3x 3x 3x 1x 1x     3x 3x 3x 3x 3x 1x 1x     3x 3x       14x                       21x 21x 21x       17x 17x   13x 95x 93x   93x 14x   13x   12x                           2x 2x     12x 12x       16x 16x 16x 16x     21x 23x   1x         21x 39x   9x       21x 21x 20x 14x     21x 1x 1x     21x    
<template>
  <span class="d-none" aria-hidden="true" />
</template>
 
<script setup lang="ts">
// @implements UC-ONBOARD-001.1, UC-ONBOARD-002.1, UC-ONBOARD-003.1
/**
 * Shows the product walkthrough: on each page, the help panels the user has
 * not seen yet (the app tour first, then the page's own panels), one after the
 * other, with driver.js. Every panel shown is marked seen right away, so it
 * never comes back, whether the user goes through the tour or leaves it.
 *
 * Besides the usual previous/next, each panel offers "Later" (nothing until
 * tomorrow) and "Don't show again" (nothing until reset from the profile
 * page). Closing a panel (× or Escape) only hides the rest until next session.
 */
import { onMounted, onUnmounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { driver, type Driver, type DriveStep, type PopoverDOM, type State } from 'driver.js'
import { useWalkthroughStore } from '@/stores/walkthrough'
import { useUILanguage } from '@/composables/useUILanguage'
import { findStepElement, stepsForRoute, type WalkthroughStep } from '@/utils/walkthroughSteps'
 
const props = withDefaults(
  defineProps<{
    /** Lets the page render (and load its data) before looking for targets */
    startDelay?: number
  }>(),
  { startDelay: 800 }
)
 
const route = useRoute()
const store = useWalkthroughStore()
const { t } = useUILanguage()
 
let activeDriver: Driver | null = null
let startTimer: ReturnType<typeof setTimeout> | null = null
 
/**
 * Automated browsers (Playwright e2e runs) never get the walkthrough on their
 * own: it would cover the page under test. The walkthrough e2e test opts in
 * by overriding navigator.webdriver.
 */
function isAutomatedBrowser(): boolean {
  return typeof navigator !== 'undefined' && navigator.webdriver === true
}
 
function onPopoverRender(popover: PopoverDOM, { state }: { state: State }) {
  // Seen as soon as it renders: driver.js skips onHighlighted for a panel left
  // (next/previous) before its highlight animation ends
  const stepId = state.activeStep?.data?.stepId
  if (typeof stepId === 'string') void store.markSeen(stepId)
  addFooterActions(popover)
}
 
function addFooterActions(popover: PopoverDOM) {
  const actions = document.createElement('div')
  actions.className = 'walkthrough-actions'
 
  const later = document.createElement('button')
  later.type = 'button'
  later.className = 'walkthrough-later-btn'
  later.textContent = t('walkthrough.later')
  later.addEventListener('click', () => {
    stop()
    void store.snooze()
  })
 
  const never = document.createElement('button')
  never.type = 'button'
  never.className = 'walkthrough-never-btn'
  never.textContent = t('walkthrough.dontShowAgain')
  never.addEventListener('click', () => {
    stop()
    void store.dismiss()
  })
 
  actions.append(later, never)
  popover.wrapper.insertBefore(actions, popover.footer.nextSibling)
}
 
function toDriveStep(step: WalkthroughStep, element: Element | null): DriveStep {
  return {
    element: element ?? undefined,
    popover: {
      title: t(`${step.key}.title`),
      description: t(`${step.key}.description`),
      side: step.side
    },
    data: { stepId: step.id }
  }
}
 
function stop() {
  const current = activeDriver
  activeDriver = null
  current?.destroy()
}
 
function start() {
  startTimer = null
  if (activeDriver || !store.canShow()) return
 
  const steps = stepsForRoute(route.name as string | undefined).flatMap((step) => {
    if (store.isSeen(step.id)) return []
    const element = findStepElement(step)
    // A targeted step whose element isn't there yet waits for a later visit
    if (step.target && !element) return []
    return [toDriveStep(step, element)]
  })
  if (steps.length === 0) return
 
  const tour = driver({
    steps,
    showProgress: steps.length > 1,
    progressText: t('walkthrough.progress'),
    nextBtnText: t('walkthrough.next'),
    prevBtnText: t('walkthrough.previous'),
    doneBtnText: t('walkthrough.done'),
    overlayOpacity: 0.5,
    popoverClass: 'driver-popover-info driver-popover-walkthrough',
    // Clicking the highlighted element (a link, a menu) would navigate away mid-tour
    disableActiveInteraction: true,
    onPopoverRender,
    onDestroyStarted: () => {
      // Closed before its last panel: leave the remaining ones for next session
      if (tour.hasNextStep()) store.pauseForSession()
      stop()
    }
  })
  activeDriver = tour
  tour.drive()
}
 
function schedule() {
  Iif (startTimer) clearTimeout(startTimer)
  stop()
  Iif (isAutomatedBrowser()) return
  startTimer = setTimeout(start, props.startDelay)
}
 
watch(
  () => route.fullPath,
  () => {
    Eif (store.loaded) schedule()
  }
)
 
// A reset from the profile page shows the panels of the current page again
watch(
  () => store.seenSteps.length === 0 && !store.dismissed && !store.snoozedUntil,
  (isFresh, wasFresh) => {
    if (isFresh && !wasFresh && store.loaded) schedule()
  }
)
 
onMounted(async () => {
  if (isAutomatedBrowser()) return
  await store.load()
  schedule()
})
 
onUnmounted(() => {
  Iif (startTimer) clearTimeout(startTimer)
  stop()
})
 
defineExpose({ start, stop })
</script>