File size: 5,149 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// import { openSavePluginIfStoreDirty } from "./DirtyStore";

// TODO: This is annoying. Decided not to use, but keep it commented now.

// interface ExitIntentOptions {
//     /**
//      * Distance in pixels from the top of the page to trigger exit intent
//      */
//     threshold?: number;
//     /** Maximum number of times to trigger the callback */
//     maxDisplays?: number;
//     /** Time in milliseconds between checks */
//     eventThrottle?: number;
//     /** Function to call when exit intent is detected */
//     onExitIntent?: () => void;
// }

// /**
//  * Creates an exit intent detector that triggers when the mouse moves toward the
//  * top of the window or leaves through the top edge.
//  *
//  * @param {ExitIntentOptions} options                Configuration options for
//  *                                                   exit intent detection.
//  * @param {number}            options.threshold      Distance in pixels from top
//  *                                                   to trigger (default: 10).
//  * @param {number}            options.maxDisplays    Maximum times to trigger
//  *                                                   (default: 10000).
//  * @param {number}            options.eventThrottle  Minimum ms between triggers
//  *                                                   (default: 100).
//  * @param {Function}          options.onExitIntent   Callback when exit intent
//  *                                                   detected.
//  * @returns {Function} Cleanup function to remove event listeners.
//  */
// function _createExitIntent({
//     threshold = 10,
//     maxDisplays = 10000,
//     eventThrottle = 100,
//     onExitIntent = () => {
//         return;
//     },
// }: ExitIntentOptions = {}): () => void {
//     let displayCount = 0;
//     let lastTime = 0;
//     // let lastTriggerTime = 0;
//     let exitTimer: number | null = null;
//     const exitDelay = 500; // Time mouse must stay above threshold

//     /**
//      * Throttles function calls.
//      *
//      * @param {Function} callback  Function to throttle.
//      * @returns {void}
//      */
//     const throttle = function (callback: () => void): void {
//         const now = Date.now();
//         if (now - lastTime >= eventThrottle) {
//             callback();
//             lastTime = now;
//         }
//     };

//     /**
//      * Clears the exit timer if it exists.
//      *
//      * @returns {void}
//      */
//     const clearExitTimer = function (): void {
//         if (exitTimer !== null) {
//             window.clearTimeout(exitTimer);
//             exitTimer = null;
//         }
//     };

//     /**
//      * Handles mouse movement events.
//      *
//      * @param {MouseEvent} event  Mouse event containing cursor position.
//      * @returns {void}
//      */
//     const handleMouseMove = function (event: MouseEvent): void {
//         throttle(() => {
//             if (displayCount >= maxDisplays) {
//                 return;
//             }

//             if (event.clientY <= threshold && event.clientY > 0) {
//                 // Mouse is above threshold - start timer
//                 clearExitTimer();
//                 exitTimer = window.setTimeout(() => {
//                     displayCount++;
//                     // lastTriggerTime = Date.now();
//                     onExitIntent();
//                     exitTimer = null;
//                 }, exitDelay);
//             } else {
//                 // Mouse is below threshold - clear timer
//                 clearExitTimer();
//             }
//         });
//     };

//     /**
//      * Handles mouse leave events.
//      *
//      * @param {MouseEvent} event  Mouse event containing exit position.
//      * @returns {void}
//      */
//     const handleMouseLeave = function (event: MouseEvent): void {
//         throttle(() => {
//             clearExitTimer(); // Clear any pending timer
//             if (
//                 event.clientY <= 0 && // Left through top of window
//                 displayCount < maxDisplays
//             ) {
//                 displayCount++;
//                 // lastTriggerTime = Date.now();
//                 onExitIntent();
//             }
//         });
//     };

//     // Add event listeners
//     document.addEventListener("mousemove", handleMouseMove);
//     document.addEventListener("mouseleave", handleMouseLeave);

//     // Return cleanup function
//     return () => {
//         clearExitTimer();
//         document.removeEventListener("mousemove", handleMouseMove);
//         document.removeEventListener("mouseleave", handleMouseLeave);
//     };
// }

// /**
//  * Sets up exit intent detection with default settings.
//  *
//  * @returns {Promise<void>} A promise that resolves when setup is complete.
//  */
// export async function setupExitIntent(): Promise<void> {
//     _createExitIntent({
//         threshold: 15,
//         maxDisplays: 10000,
//         eventThrottle: 15,
//         onExitIntent: () => {
//             openSavePluginIfStoreDirty(false);
//         },
//     });
// }