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
|
import { useRouter } from 'next/router';
import { useEffect, useState, useRef } from 'react';
import Image from 'next/image';
import Link from 'next/link';
import { getAuth } from '~/libs/auth';
import { X } from 'lucide-react';
const PagePopupInformation = () => {
const router = useRouter();
const isHomePage = router.pathname === '/';
const auth = getAuth();
const [active, setActive] = useState(false);
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const popupRef = useRef(null);
const [position, setPosition] = useState({ x: 20, y: window.innerHeight - 170 });
const dragStartPos = useRef({ x: 0, y: 0 });
const isDragging = useRef(false);
useEffect(() => {
const getData = async () => {
try {
const res = await fetch(`/api/hero-banner?type=dragable-banner`);
const { data } = await res.json();
if (Array.isArray(data) && data.length > 0 && data[0]?.image) {
setData(data);
} else {
setActive(false);
}
} catch (error) {
console.error('Failed to fetch popup banner:', error);
}
setLoading(false);
};
if (isHomePage && !auth) {
setActive(true);
getData();
}
}, [isHomePage, auth]);
const handleMouseDown = (e) => {
e.preventDefault();
dragStartPos.current = { x: e.clientX - position.x, y: e.clientY - position.y };
isDragging.current = false;
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', handleMouseUp);
};
const handleMouseMove = (e) => {
isDragging.current = true;
let newX = e.clientX - dragStartPos.current.x;
let newY = e.clientY - dragStartPos.current.y;
const popupWidth = popupRef.current?.offsetWidth || 0;
const popupHeight = popupRef.current?.offsetHeight || 0;
const maxX = window.innerWidth - popupWidth - 20;
const maxY = window.innerHeight - popupHeight - 20;
newX = Math.max(0, Math.min(newX, maxX));
newY = Math.max(0, Math.min(newY, maxY));
setPosition({ x: newX, y: newY });
};
const handleMouseUp = () => {
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('mouseup', handleMouseUp);
};
// Touch Events (Mobile)
const handleTouchStart = (e) => {
if (e.touches.length === 1) {
const touch = e.touches[0];
dragStartPos.current = { x: touch.clientX - position.x, y: touch.clientY - position.y };
isDragging.current = false;
window.addEventListener('touchmove', handleTouchMove, { passive: false });
window.addEventListener('touchend', handleTouchEnd);
}
};
const handleTouchMove = (e) => {
if (e.touches.length === 1) {
e.preventDefault();
isDragging.current = true;
let newX = e.touches[0].clientX - dragStartPos.current.x;
let newY = e.touches[0].clientY - dragStartPos.current.y;
const popupWidth = popupRef.current?.offsetWidth || 0;
const popupHeight = popupRef.current?.offsetHeight || 0;
const maxX = window.innerWidth - popupWidth - 20;
const maxY = window.innerHeight - popupHeight - 20;
newX = Math.max(0, Math.min(newX, maxX));
newY = Math.max(0, Math.min(newY, maxY));
setPosition({ x: newX, y: newY });
}
};
const handleTouchEnd = () => {
window.removeEventListener('touchmove', handleTouchMove);
window.removeEventListener('touchend', handleTouchEnd);
};
if (!active || !data || loading || !Array.isArray(data) || !data[0]?.image) return null;
const banner = data[0];
return (
<div className="fixed inset-0 z-[9999] pointer-events-none">
<div
ref={popupRef}
className="absolute pointer-events-auto"
style={{
transform: `translate(${position.x}px, ${position.y}px)`,
cursor: 'grab',
width: '85px',
}}
>
<div className="relative w-full h-auto">
<div
onMouseDown={handleMouseDown}
onTouchStart={handleTouchStart}
className="cursor-grab active:cursor-grabbing"
>
<Link
href={typeof banner.url === 'boolean' && banner.url === false ? '/' : banner.url}
onClick={(e) => {
if (isDragging.current) {
e.preventDefault();
isDragging.current = false;
} else {
setActive(false);
}
}}
draggable="false"
>
<Image
src={banner.image}
alt={banner.name || 'popup'}
width={85}
height={85}
className="w-full h-auto select-none"
draggable="false"
/>
</Link>
</div>
<button
onClick={() => setActive(false)}
className="absolute -top-2 -right-2 z-10 p-1 bg-gray-500 rounded-full hover:bg-gray-600 transition-colors"
aria-label="Close popup"
>
<X className="w-3 h-3 text-white" />
</button>
</div>
</div>
</div>
);
};
export default PagePopupInformation;
|