|
1 | 1 | import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
|
2 | 2 | import { useEffect, useRef, useState } from "react";
|
3 | 3 | import Image from "next/image";
|
4 |
| -import LoadingSpinner from "../ui/loading-spinner"; |
| 4 | +import LoadingSpinner from "@/components/ui/loading-spinner"; |
5 | 5 | import BlankImg from "@/public/_static/blank.gif";
|
6 | 6 | import Nav from "./nav";
|
7 | 7 | import Toolbar from "./toolbar";
|
@@ -45,37 +45,43 @@ export default function PagesViewer({
|
45 | 45 |
|
46 | 46 | const startTimeRef = useRef(Date.now());
|
47 | 47 | const pageNumberRef = useRef<number>(pageNumber);
|
| 48 | + const visibilityRef = useRef<boolean>(true); |
48 | 49 |
|
49 | 50 | // Update the previous page number after the effect hook has run
|
50 | 51 | useEffect(() => {
|
51 | 52 | pageNumberRef.current = pageNumber;
|
52 | 53 | }, [pageNumber]);
|
53 | 54 |
|
54 | 55 | useEffect(() => {
|
55 |
| - startTimeRef.current = Date.now(); // update the start time for the new page |
| 56 | + const handleVisibilityChange = () => { |
| 57 | + if (document.visibilityState === "visible") { |
| 58 | + visibilityRef.current = true; |
| 59 | + startTimeRef.current = Date.now(); // Reset start time when the page becomes visible again |
| 60 | + } else { |
| 61 | + visibilityRef.current = false; |
| 62 | + const duration = Date.now() - startTimeRef.current; |
| 63 | + trackPageView(duration); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + document.addEventListener("visibilitychange", handleVisibilityChange); |
56 | 68 |
|
57 |
| - // when component unmounts, calculate duration and track page view |
58 | 69 | return () => {
|
59 |
| - const endTime = Date.now(); |
60 |
| - const duration = Math.round(endTime - startTimeRef.current); |
61 |
| - trackPageView(duration); |
| 70 | + document.removeEventListener("visibilitychange", handleVisibilityChange); |
62 | 71 | };
|
63 |
| - }, [pageNumber]); // monitor pageNumber for changes |
| 72 | + }, []); // track page view when the page becomes visible or hidden on mount and unmount |
64 | 73 |
|
65 |
| - // Send the last page view when the user leaves the page |
66 | 74 | useEffect(() => {
|
67 |
| - const handleBeforeUnload = () => { |
68 |
| - const endTime = Date.now(); |
69 |
| - const duration = Math.round(endTime - startTimeRef.current); |
70 |
| - trackPageView(duration); |
71 |
| - }; |
72 |
| - |
73 |
| - window.addEventListener("beforeunload", handleBeforeUnload); |
| 75 | + startTimeRef.current = Date.now(); |
74 | 76 |
|
75 | 77 | return () => {
|
76 |
| - window.removeEventListener("beforeunload", handleBeforeUnload); |
| 78 | + if (visibilityRef.current) { |
| 79 | + // Only track the page view if the page is visible |
| 80 | + const duration = Date.now() - startTimeRef.current; |
| 81 | + trackPageView(duration); |
| 82 | + } |
77 | 83 | };
|
78 |
| - }, []); |
| 84 | + }, [pageNumber]); // Track page view when the page number changes |
79 | 85 |
|
80 | 86 | useEffect(() => {
|
81 | 87 | setLoadedImages((prev) =>
|
|
0 commit comments