Debian Codegraph @

addons/point_of_sale/static/src/app/hooks/use_tours.js

raw
1 import { tourState } from "@web_tour/js/tour_state";
2 import { TourSelectorPopup } from "../components/tour_selector_popup/tour_selector_popup";
3 import { useService } from "@web/core/utils/hooks";
4 import { makeAwaitable } from "../utils/make_awaitable_dialog";
5
6 export default function useTours() {
7 const tour = useService("tour_service");
8 const dialog = useService("dialog");
9 const states = {
10 selectedTours: new Set(),
11 running: false,
12 index: 0,
13 };
14
15 let fakeTourInterval = null;
16
17 tourState.clear();
18
19 const toggle = async () => {
20 states.index = 0;
21 states.running = !states.running;
22 clearInterval(fakeTourInterval);
23
24 if (!states.running) {
25 tourState.clear();
26 return;
27 }
28
29 const tours = await makeAwaitable(dialog, TourSelectorPopup, {});
30 if (!tours || !tours.length) {
31 tourState.clear();
32 states.running = false;
33 return;
34 }
35
36 states.selectedTours = tours;
37 fakeTourInterval = setInterval(() => {
38 const state = tourState.getCurrentTour();
39 if (!state) {
40 runTour();
41 }
42 }, 500);
43 };
44
45 const runTour = async () => {
46 try {
47 if (states.index >= states.selectedTours.length) {
48 states.index = 0;
49 }
50 await tour.startTour(states.selectedTours[states.index], {
51 throw: false,
52 });
53
54 states.index++;
55 } catch (error) {
56 console.warn("Error in tour", error);
57 }
58 };
59
60 return { toggle };
61 }