@gemx-dev/heatmap-react 3.5.27 → 3.5.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/esm/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use client"
2
2
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
3
3
  import { useNodesState, ReactFlow, Controls, Background } from '@xyflow/react';
4
- import { useEffect, useMemo, useCallback } from 'react';
4
+ import { useEffect, useMemo, useState, useCallback, useRef, Fragment as Fragment$1 } from 'react';
5
5
  import { create } from 'zustand';
6
+ import { Visualizer } from '@gemx-dev/clarity-visualize';
7
+ import { createPortal } from 'react-dom';
6
8
 
7
9
  const initialNodes = { id: '1', position: { x: 0, y: 0 }, data: { label: '1' } };
8
10
  const GraphView = ({ children, width, height }) => {
@@ -36,20 +38,23 @@ const GraphView = ({ children, width, height }) => {
36
38
  return (jsxs(ReactFlow, { nodes: nodes, nodeTypes: nodeTypes, onNodesChange: onNodesChange, debug: true, minZoom: 0.5, maxZoom: 2, fitView: true, children: [jsx(Controls, {}), jsx(Background, {})] }));
37
39
  };
38
40
 
39
- const useHeatmapDataStore = create()((set, get) => ({
40
- data: undefined,
41
- clickmap: undefined,
42
- config: undefined,
43
- iframeHeight: 0,
44
- state: {
45
- hideSidebar: false,
46
- },
47
- setData: (data) => set({ data }),
48
- setClickmap: (clickmap) => set({ clickmap }),
49
- setState: (state) => set({ state: { ...get().state, ...state } }),
50
- setConfig: (config) => set({ config: { ...get().config, ...config } }),
51
- setIframeHeight: (iframeHeight) => set({ iframeHeight }),
52
- }));
41
+ const useHeatmapDataStore = create()((set, get) => {
42
+ return {
43
+ data: undefined,
44
+ clickmap: undefined,
45
+ config: undefined,
46
+ iframeHeight: 0,
47
+ state: {
48
+ hideSidebar: false,
49
+ },
50
+ setData: (data) => set({ data }),
51
+ setClickmap: (clickmap) => set({ clickmap }),
52
+ setState: (state) => set({ state: { ...get().state, ...state } }),
53
+ setConfig: (value) => set({ config: { ...get().config, ...value } }),
54
+ setConfigBy: (key, value) => set({ config: { ...get().config, [key]: value } }),
55
+ setIframeHeight: (iframeHeight) => set({ iframeHeight }),
56
+ };
57
+ });
53
58
 
54
59
  const BoxStack = ({ children, ...props }) => {
55
60
  const id = props.id;
@@ -89,6 +94,1172 @@ const BoxStack = ({ children, ...props }) => {
89
94
  return (jsx("div", { id: id, style: styleProps, children: children }));
90
95
  };
91
96
 
97
+ const ContentHeader = ({ children }) => {
98
+ return (jsx(BoxStack, { id: "gx-hm-content-header", flexDirection: "row", alignItems: "center", overflow: "auto", children: children }));
99
+ };
100
+
101
+ const HEATMAP_IFRAME = {
102
+ id: 'clarity-iframe',
103
+ title: 'Clarity Session Replay',
104
+ sandbox: 'allow-same-origin allow-scripts',
105
+ scrolling: 'no',
106
+ height: '100%',
107
+ };
108
+
109
+ const HEATMAP_CONFIG = {
110
+ paddingBlock: 0,
111
+ };
112
+ const HEATMAP_STYLE = {
113
+ wrapper: {
114
+ padding: `${HEATMAP_CONFIG.paddingBlock}px 0`,
115
+ },
116
+ };
117
+
118
+ const useClickedElement = ({ selectedElement, heatmapInfo, getRect }) => {
119
+ const [clickedElement, setClickedElement] = useState(null);
120
+ const [showMissingElement, setShowMissingElement] = useState(false);
121
+ const [shouldShowCallout, setShouldShowCallout] = useState(false);
122
+ useEffect(() => {
123
+ if (!selectedElement || !heatmapInfo?.elementMapInfo) {
124
+ setClickedElement(null);
125
+ setShowMissingElement(false);
126
+ setShouldShowCallout(false);
127
+ return;
128
+ }
129
+ const info = heatmapInfo.elementMapInfo[selectedElement];
130
+ if (!info) {
131
+ setClickedElement(null);
132
+ return;
133
+ }
134
+ const rect = getRect({ hash: selectedElement, selector: info.selector });
135
+ if (rect && heatmapInfo.sortedElements) {
136
+ const rank = heatmapInfo.sortedElements.findIndex((e) => e.hash === selectedElement) + 1;
137
+ setClickedElement({
138
+ ...rect,
139
+ hash: selectedElement,
140
+ clicks: info.totalclicks ?? 0,
141
+ rank,
142
+ selector: info.selector ?? '',
143
+ });
144
+ setShowMissingElement(false);
145
+ setShouldShowCallout(true);
146
+ }
147
+ else {
148
+ const rank = (heatmapInfo.sortedElements?.findIndex((e) => e.hash === selectedElement) ?? -1) + 1;
149
+ setClickedElement({
150
+ hash: selectedElement,
151
+ clicks: info.totalclicks ?? 0,
152
+ rank,
153
+ selector: info.selector ?? '',
154
+ left: 0,
155
+ top: 0,
156
+ width: 0,
157
+ height: 0,
158
+ });
159
+ setShowMissingElement(true);
160
+ setShouldShowCallout(false);
161
+ }
162
+ }, [selectedElement, heatmapInfo, getRect]);
163
+ return { clickedElement, showMissingElement, shouldShowCallout, setShouldShowCallout };
164
+ };
165
+
166
+ const useHeatmapEffects = ({ isVisible, isElementSidebarOpen, selectedElement, setShouldShowCallout, resetAll, }) => {
167
+ // Reset khi ẩn
168
+ useEffect(() => {
169
+ if (!isVisible)
170
+ resetAll();
171
+ }, [isVisible, resetAll]);
172
+ // Ẩn callout khi sidebar mở
173
+ useEffect(() => {
174
+ if (isElementSidebarOpen && selectedElement) {
175
+ setShouldShowCallout(false);
176
+ }
177
+ else if (!isElementSidebarOpen && selectedElement) {
178
+ setShouldShowCallout(true);
179
+ }
180
+ }, [isElementSidebarOpen, selectedElement, setShouldShowCallout]);
181
+ };
182
+
183
+ function getElementLayout(element) {
184
+ if (!element?.getBoundingClientRect)
185
+ return null;
186
+ const rect = element.getBoundingClientRect();
187
+ if (rect.width === 0 && rect.height === 0)
188
+ return null;
189
+ return {
190
+ top: rect.top,
191
+ left: rect.left,
192
+ width: rect.width,
193
+ height: rect.height,
194
+ };
195
+ }
196
+ function formatPercentage(value, decimals = 2) {
197
+ return value.toFixed(decimals);
198
+ }
199
+ function getSimpleSelector(selector) {
200
+ const parts = selector.split(' > ');
201
+ return parts[parts.length - 1] || selector;
202
+ }
203
+ function calculateRankPosition(rect, widthScale) {
204
+ const top = rect.top <= 18 ? rect.top + 3 : rect.top - 18;
205
+ const left = rect.left <= 18 ? rect.left + 3 : rect.left - 18;
206
+ return {
207
+ transform: `scale(${1.2 * widthScale})`,
208
+ top: Number.isNaN(top) ? undefined : top,
209
+ left: Number.isNaN(left) ? undefined : left,
210
+ };
211
+ }
212
+
213
+ const useHeatmapElementPosition = ({ iframeRef, parentRef, visualizer, heatmapWidth, iframeHeight, widthScale, projectId, }) => {
214
+ return useCallback((element) => {
215
+ const hash = element?.hash;
216
+ if (!iframeRef.current?.contentDocument || !hash || !visualizer)
217
+ return null;
218
+ let domElement = null;
219
+ try {
220
+ domElement = visualizer.get(hash);
221
+ }
222
+ catch (error) {
223
+ console.error('Visualizer error:', { projectId, hash, error });
224
+ return null;
225
+ }
226
+ if (!domElement)
227
+ return null;
228
+ const layout = getElementLayout(domElement);
229
+ if (!layout)
230
+ return null;
231
+ const parentEl = parentRef.current;
232
+ if (!parentEl)
233
+ return null;
234
+ const scrollOffset = parentEl.scrollTop / widthScale;
235
+ const adjustedTop = layout.top + scrollOffset;
236
+ const outOfBounds = adjustedTop < 0 ||
237
+ adjustedTop > (iframeHeight || Infinity) ||
238
+ layout.left < 0 ||
239
+ (typeof heatmapWidth === 'number' && layout.left > heatmapWidth);
240
+ if (outOfBounds)
241
+ return null;
242
+ return {
243
+ left: layout.left,
244
+ top: adjustedTop,
245
+ width: Math.min(layout.width, heatmapWidth || layout.width),
246
+ height: layout.height,
247
+ };
248
+ }, [iframeRef, parentRef, visualizer, heatmapWidth, iframeHeight, widthScale, projectId]);
249
+ };
250
+
251
+ const debounce = (fn, delay) => {
252
+ let timeout;
253
+ return (...args) => {
254
+ clearTimeout(timeout);
255
+ timeout = setTimeout(() => fn(...args), delay);
256
+ };
257
+ };
258
+ const useHoveredElement = ({ iframeRef, heatmapInfo, widthScale, getRect, onSelect, }) => {
259
+ const [hoveredElement, setHoveredElement] = useState(null);
260
+ const handleMouseMove = useCallback(debounce((event) => {
261
+ if (!iframeRef.current?.contentDocument || !heatmapInfo?.elementMapInfo) {
262
+ setHoveredElement(null);
263
+ return;
264
+ }
265
+ const iframe = iframeRef.current;
266
+ const iframeRect = iframe.getBoundingClientRect();
267
+ let x = event.clientX - iframeRect.left;
268
+ console.log(`🚀 🐥 ~ useHoveredElement ~ iframeRect.left:`, iframeRect.left);
269
+ console.log(`🚀 🐥 ~ useHoveredElement ~ event.clientX:`, event.clientX);
270
+ let y = event.clientY - iframeRect.top;
271
+ if (widthScale !== 1) {
272
+ x /= widthScale;
273
+ y /= widthScale;
274
+ }
275
+ const doc = iframe.contentDocument;
276
+ if (!doc) {
277
+ setHoveredElement(null);
278
+ return;
279
+ }
280
+ let targetElement = null;
281
+ // Best: dùng caretPositionFromPoint nếu có (Chrome, Safari)
282
+ targetElement = getElementAtPoint(doc, x, y);
283
+ if (!targetElement) {
284
+ targetElement = doc.elementFromPoint(x, y);
285
+ }
286
+ if (!targetElement) {
287
+ setHoveredElement(null);
288
+ return;
289
+ }
290
+ // Lấy hash từ nhiều attribute khả dĩ
291
+ const hash = targetElement.getAttribute('data-clarity-hash') ||
292
+ targetElement.getAttribute('data-clarity-hashalpha') ||
293
+ targetElement.getAttribute('data-clarity-hashbeta');
294
+ if (!hash || !heatmapInfo.elementMapInfo[hash]) {
295
+ setHoveredElement(null);
296
+ return;
297
+ }
298
+ const info = heatmapInfo.elementMapInfo[hash];
299
+ const position = getRect({ hash, selector: info.selector });
300
+ if (position && heatmapInfo.sortedElements) {
301
+ const rank = heatmapInfo.sortedElements.findIndex((e) => e.hash === hash) + 1;
302
+ setHoveredElement({
303
+ ...position,
304
+ hash,
305
+ clicks: info.totalclicks ?? 0,
306
+ rank,
307
+ selector: info.selector ?? '',
308
+ });
309
+ }
310
+ else {
311
+ setHoveredElement(null);
312
+ }
313
+ }, 16), // ~60fps
314
+ [iframeRef, heatmapInfo, getRect]);
315
+ const handleMouseLeave = useCallback(() => {
316
+ setHoveredElement(null);
317
+ }, []);
318
+ const handleClick = useCallback(() => {
319
+ if (hoveredElement?.hash && onSelect) {
320
+ onSelect(hoveredElement.hash);
321
+ }
322
+ }, [hoveredElement, onSelect]);
323
+ return {
324
+ hoveredElement,
325
+ handleMouseMove,
326
+ handleMouseLeave,
327
+ handleClick,
328
+ };
329
+ };
330
+ const getElementAtPoint = (doc, x, y) => {
331
+ let el = null;
332
+ if ('caretPositionFromPoint' in doc) {
333
+ el = doc.caretPositionFromPoint(x, y)?.offsetNode ?? null;
334
+ }
335
+ el = el ?? doc.elementFromPoint(x, y);
336
+ let element = el;
337
+ while (element && element.nodeType === Node.TEXT_NODE) {
338
+ element = element.parentElement;
339
+ }
340
+ return element;
341
+ };
342
+
343
+ const recreateIframe = (iframeRef, config) => {
344
+ const container = iframeRef.current?.parentElement;
345
+ if (!container)
346
+ return;
347
+ const oldIframe = iframeRef.current;
348
+ if (!oldIframe?.contentDocument?.body.innerHTML) {
349
+ return oldIframe;
350
+ }
351
+ if (oldIframe && oldIframe.parentElement) {
352
+ oldIframe.parentElement.removeChild(oldIframe);
353
+ }
354
+ const newIframe = document.createElement('iframe');
355
+ newIframe.id = HEATMAP_IFRAME.id;
356
+ newIframe.title = HEATMAP_IFRAME.title;
357
+ newIframe.sandbox = HEATMAP_IFRAME.sandbox;
358
+ newIframe.scrolling = HEATMAP_IFRAME.scrolling;
359
+ newIframe.width = config?.width ? `${config.width}px` : '100%';
360
+ // Append to container
361
+ container.appendChild(newIframe);
362
+ // Update ref
363
+ iframeRef.current = newIframe;
364
+ return newIframe;
365
+ };
366
+
367
+ function isMobileDevice(userAgent) {
368
+ if (!userAgent)
369
+ return false;
370
+ return /android|webos|iphone|ipad|ipod|blackberry|windows phone|opera mini|iemobile|mobile|silk|fennec|bada|tizen|symbian|nokia|palmsource|meego|sailfish|kindle|playbook|bb10|rim/i.test(userAgent);
371
+ }
372
+
373
+ const useHeatmapRender = () => {
374
+ const data = useHeatmapDataStore((state) => state.data);
375
+ const config = useHeatmapDataStore((state) => state.config);
376
+ const setConfig = useHeatmapDataStore((state) => state.setConfig);
377
+ const clickmap = useHeatmapDataStore((state) => state.clickmap);
378
+ const visualizerRef = useRef(null);
379
+ const iframeRef = useRef(null);
380
+ const initializeVisualizer = useCallback((envelope, userAgent) => {
381
+ const iframe = iframeRef.current;
382
+ if (!iframe?.contentWindow)
383
+ return null;
384
+ const visualizer = new Visualizer();
385
+ const mobile = isMobileDevice(userAgent);
386
+ visualizer.setup(iframe.contentWindow, {
387
+ version: envelope.version,
388
+ onresize: (width) => {
389
+ setConfig({ width });
390
+ },
391
+ mobile,
392
+ vNext: true,
393
+ locale: 'en-us',
394
+ });
395
+ return visualizer;
396
+ }, []);
397
+ // Process and render heatmap HTML
398
+ const renderHeatmap = useCallback(async (payloads) => {
399
+ if (!payloads || payloads.length === 0)
400
+ return;
401
+ let visualizer = new Visualizer();
402
+ const iframe = recreateIframe(iframeRef, config);
403
+ // const merged = visualizer.merge(payloads);
404
+ // setIframeHeight(Number(iframeRef.current?.height || 0));
405
+ // for (const decoded of payloads) {
406
+ // // Initialize on first sequence
407
+ // if (decoded.envelope.sequence === 1) {
408
+ // const userAgent = (decoded.dimension?.[0]?.data[0]?.[0] as string) || '';
409
+ // visualizer = initializeVisualizer(decoded.envelope as any, userAgent);
410
+ // if (!visualizer) return;
411
+ // visualizerRef.current = visualizer;
412
+ // }
413
+ // if (!visualizer) continue;
414
+ // // Merge and process DOM
415
+ // const merged = visualizer.merge([decoded]);
416
+ // visualizer.dom(merged.dom);
417
+ // }
418
+ // Render static HTML
419
+ if (visualizer && iframe?.contentWindow) {
420
+ await visualizer.html(payloads, iframe.contentWindow);
421
+ visualizerRef.current = visualizer;
422
+ }
423
+ }, [initializeVisualizer]);
424
+ useEffect(() => {
425
+ if (!data || data.length === 0)
426
+ return;
427
+ renderHeatmap(data);
428
+ return () => {
429
+ visualizerRef.current = null;
430
+ };
431
+ }, [config, data, renderHeatmap]);
432
+ useEffect(() => {
433
+ if (!visualizerRef.current || !clickmap || clickmap.length === 0)
434
+ return;
435
+ visualizerRef.current.clearmap();
436
+ visualizerRef.current?.clickmap(clickmap);
437
+ }, [clickmap]);
438
+ return {
439
+ iframeRef,
440
+ clarityVisualizer: visualizerRef.current,
441
+ };
442
+ };
443
+
444
+ function sortEvents(a, b) {
445
+ return a.time - b.time;
446
+ }
447
+
448
+ const useReplayRender = () => {
449
+ const data = useHeatmapDataStore((state) => state.data);
450
+ const setConfig = useHeatmapDataStore((state) => state.setConfig);
451
+ const visualizerRef = useRef(null);
452
+ const iframeRef = useRef(null);
453
+ const eventsRef = useRef([]);
454
+ const animationFrameRef = useRef(null);
455
+ const isPlayingRef = useRef(false);
456
+ // Initialize visualizer for replay
457
+ const initializeVisualizer = useCallback((envelope, userAgent) => {
458
+ const iframe = iframeRef.current;
459
+ if (!iframe?.contentWindow)
460
+ return null;
461
+ // Clear previous events
462
+ eventsRef.current = [];
463
+ const visualizer = new Visualizer();
464
+ const mobile = isMobileDevice(userAgent);
465
+ visualizer.setup(iframe.contentWindow, {
466
+ version: envelope.version,
467
+ onresize: (width) => {
468
+ setConfig({ width });
469
+ },
470
+ mobile,
471
+ vNext: true,
472
+ locale: 'en-us',
473
+ });
474
+ return visualizer;
475
+ }, [setConfig]);
476
+ // Animation loop for replay
477
+ const replayLoop = useCallback(() => {
478
+ if (!isPlayingRef.current)
479
+ return;
480
+ const events = eventsRef.current;
481
+ const visualizer = visualizerRef.current;
482
+ if (!visualizer || events.length === 0) {
483
+ animationFrameRef.current = requestAnimationFrame(replayLoop);
484
+ return;
485
+ }
486
+ const event = events[0];
487
+ const end = event.time + 16; // 60FPS => 16ms per frame
488
+ let index = 0;
489
+ // Get events within current frame
490
+ while (events[index] && events[index].time < end) {
491
+ index++;
492
+ }
493
+ // Render events for this frame
494
+ if (index > 0) {
495
+ visualizer.render(events.splice(0, index));
496
+ }
497
+ animationFrameRef.current = requestAnimationFrame(replayLoop);
498
+ }, []);
499
+ // Start replay
500
+ const startReplay = useCallback(async (payloads) => {
501
+ if (!payloads || payloads.length === 0)
502
+ return;
503
+ let visualizer = visualizerRef.current;
504
+ for (const decoded of payloads) {
505
+ // Initialize on first sequence
506
+ if (decoded.envelope.sequence === 1) {
507
+ const userAgent = decoded.dimension?.[0]?.data[0]?.[0] || '';
508
+ visualizer = initializeVisualizer(decoded.envelope, userAgent);
509
+ if (!visualizer)
510
+ return;
511
+ visualizerRef.current = visualizer;
512
+ }
513
+ if (!visualizer)
514
+ continue;
515
+ // Merge events and DOM
516
+ const merged = visualizer.merge([decoded]);
517
+ eventsRef.current = eventsRef.current.concat(merged.events).sort(sortEvents);
518
+ visualizer.dom(merged.dom);
519
+ }
520
+ // Render HTML
521
+ if (visualizer && iframeRef.current?.contentWindow) {
522
+ await visualizer.html(payloads, iframeRef.current.contentWindow);
523
+ }
524
+ // Auto-start replay
525
+ isPlayingRef.current = true;
526
+ animationFrameRef.current = requestAnimationFrame(replayLoop);
527
+ }, [initializeVisualizer, replayLoop]);
528
+ // Play control
529
+ const play = useCallback(() => {
530
+ if (!isPlayingRef.current) {
531
+ isPlayingRef.current = true;
532
+ animationFrameRef.current = requestAnimationFrame(replayLoop);
533
+ }
534
+ }, [replayLoop]);
535
+ // Pause control
536
+ const pause = useCallback(() => {
537
+ isPlayingRef.current = false;
538
+ if (animationFrameRef.current) {
539
+ cancelAnimationFrame(animationFrameRef.current);
540
+ animationFrameRef.current = null;
541
+ }
542
+ }, []);
543
+ // Main effect: Start replay when data changes
544
+ useEffect(() => {
545
+ if (!data || data.length === 0)
546
+ return;
547
+ startReplay(data);
548
+ // Cleanup
549
+ return () => {
550
+ isPlayingRef.current = false;
551
+ if (animationFrameRef.current) {
552
+ cancelAnimationFrame(animationFrameRef.current);
553
+ animationFrameRef.current = null;
554
+ }
555
+ eventsRef.current = [];
556
+ visualizerRef.current = null;
557
+ };
558
+ }, [data, startReplay]);
559
+ return {
560
+ iframeRef,
561
+ isPlaying: isPlayingRef.current,
562
+ clarityVisualizer: visualizerRef.current,
563
+ play,
564
+ pause,
565
+ };
566
+ };
567
+
568
+ const useHeatmapVizRender = (mode) => {
569
+ const heatmapResult = useMemo(() => {
570
+ switch (mode) {
571
+ case 'heatmap':
572
+ return useHeatmapRender;
573
+ case 'replay':
574
+ return useReplayRender;
575
+ }
576
+ }, [mode]);
577
+ return heatmapResult();
578
+ };
579
+
580
+ const useContainerDimensions = (props) => {
581
+ const { wrapperRef } = props;
582
+ const [containerWidth, setContainerWidth] = useState(0);
583
+ const [containerHeight, setContainerHeight] = useState(0);
584
+ const resizeObserverRef = useRef(null);
585
+ const updateDimensions = useCallback(() => {
586
+ const scrollContainer = wrapperRef.current?.parentElement?.parentElement;
587
+ if (scrollContainer) {
588
+ setContainerWidth(scrollContainer.clientWidth);
589
+ setContainerHeight(scrollContainer.clientHeight);
590
+ }
591
+ }, [wrapperRef]);
592
+ useEffect(() => {
593
+ const scrollContainer = wrapperRef.current?.parentElement?.parentElement;
594
+ if (!scrollContainer || typeof window.ResizeObserver === 'undefined') {
595
+ return;
596
+ }
597
+ resizeObserverRef.current = new ResizeObserver(updateDimensions);
598
+ resizeObserverRef.current.observe(scrollContainer);
599
+ updateDimensions();
600
+ return () => {
601
+ if (resizeObserverRef.current && scrollContainer) {
602
+ resizeObserverRef.current.unobserve(scrollContainer);
603
+ }
604
+ };
605
+ }, [wrapperRef, updateDimensions]);
606
+ return { containerWidth, containerHeight };
607
+ };
608
+
609
+ const useContentDimensions = (props) => {
610
+ const { iframeRef, config } = props;
611
+ const [contentWidth, setContentWidth] = useState(0);
612
+ useEffect(() => {
613
+ if (config?.width) {
614
+ if (iframeRef.current) {
615
+ iframeRef.current.width = `${config.width}px`;
616
+ }
617
+ setContentWidth(config.width);
618
+ }
619
+ }, [config?.width, iframeRef]);
620
+ return { contentWidth };
621
+ };
622
+
623
+ // Hook 3: Iframe Height Observer
624
+ const useIframeHeight = (props) => {
625
+ const { iframeRef, contentWidth } = props;
626
+ const iframeHeight = useHeatmapDataStore((state) => state.iframeHeight);
627
+ const setIframeHeight = useHeatmapDataStore((state) => state.setIframeHeight);
628
+ const resizeObserverRef = useRef(null);
629
+ const mutationObserverRef = useRef(null);
630
+ const updateIframeHeight = useCallback(() => {
631
+ const iframe = iframeRef.current;
632
+ if (!iframe)
633
+ return;
634
+ try {
635
+ const iframeDocument = iframe.contentDocument;
636
+ const iframeBody = iframeDocument?.body;
637
+ if (!iframeBody)
638
+ return;
639
+ const bodyHeight = Math.max(iframeBody.scrollHeight, iframeBody.offsetHeight, iframeBody.clientHeight);
640
+ if (bodyHeight > 0) {
641
+ iframe.height = `${bodyHeight}px`;
642
+ setIframeHeight(bodyHeight);
643
+ }
644
+ }
645
+ catch (error) {
646
+ console.warn('Cannot measure iframe content:', error);
647
+ }
648
+ }, [iframeRef, setIframeHeight]);
649
+ // Trigger height update when content width changes
650
+ useEffect(() => {
651
+ if (contentWidth > 0) {
652
+ // Delay to allow iframe content to reflow after width change
653
+ const timeoutId = setTimeout(() => {
654
+ updateIframeHeight();
655
+ }, 100);
656
+ return () => clearTimeout(timeoutId);
657
+ }
658
+ }, [contentWidth, updateIframeHeight]);
659
+ useEffect(() => {
660
+ const iframe = iframeRef.current;
661
+ if (!iframe)
662
+ return;
663
+ const setupObservers = () => {
664
+ try {
665
+ const iframeDocument = iframe.contentDocument;
666
+ const iframeBody = iframeDocument?.body;
667
+ if (!iframeBody)
668
+ return;
669
+ // Cleanup existing observers
670
+ if (resizeObserverRef.current) {
671
+ resizeObserverRef.current.disconnect();
672
+ }
673
+ if (mutationObserverRef.current) {
674
+ mutationObserverRef.current.disconnect();
675
+ }
676
+ // ResizeObserver for size changes
677
+ if (typeof window.ResizeObserver !== 'undefined') {
678
+ resizeObserverRef.current = new ResizeObserver(updateIframeHeight);
679
+ resizeObserverRef.current.observe(iframeBody);
680
+ }
681
+ // MutationObserver for DOM changes
682
+ if (typeof window.MutationObserver !== 'undefined') {
683
+ mutationObserverRef.current = new MutationObserver(updateIframeHeight);
684
+ mutationObserverRef.current.observe(iframeBody, {
685
+ childList: true,
686
+ subtree: true,
687
+ attributes: true,
688
+ characterData: true,
689
+ });
690
+ }
691
+ // Initial measurement
692
+ updateIframeHeight();
693
+ }
694
+ catch (error) {
695
+ console.warn('Cannot access iframe content:', error);
696
+ }
697
+ };
698
+ if (iframe.contentDocument?.readyState === 'complete') {
699
+ setupObservers();
700
+ }
701
+ else {
702
+ iframe.addEventListener('load', setupObservers, { once: true });
703
+ }
704
+ return () => {
705
+ if (resizeObserverRef.current) {
706
+ resizeObserverRef.current.disconnect();
707
+ }
708
+ if (mutationObserverRef.current) {
709
+ mutationObserverRef.current.disconnect();
710
+ }
711
+ iframe.removeEventListener('load', setupObservers);
712
+ };
713
+ }, [iframeRef, updateIframeHeight]);
714
+ return { iframeHeight };
715
+ };
716
+
717
+ const useScaleCalculation = (props) => {
718
+ const { containerWidth, contentWidth } = props;
719
+ const [scale, setScale] = useState(1);
720
+ useEffect(() => {
721
+ if (containerWidth > 0 && contentWidth > 0) {
722
+ const availableWidth = containerWidth - HEATMAP_CONFIG['paddingBlock'] * 2;
723
+ const calculatedScale = Math.min(availableWidth / contentWidth, 1);
724
+ setScale(calculatedScale);
725
+ }
726
+ }, [containerWidth, contentWidth]);
727
+ return { scale };
728
+ };
729
+
730
+ const useScrollSync = (props) => {
731
+ const { iframeRef, scale } = props;
732
+ const handleScroll = useCallback((scrollTop) => {
733
+ const iframe = iframeRef.current;
734
+ if (!iframe || scale <= 0)
735
+ return;
736
+ try {
737
+ const iframeWindow = iframe.contentWindow;
738
+ const iframeDocument = iframe.contentDocument;
739
+ if (iframeWindow && iframeDocument) {
740
+ const iframeScrollTop = scrollTop / scale;
741
+ iframe.style.top = `${iframeScrollTop}px`;
742
+ }
743
+ }
744
+ catch (error) {
745
+ console.warn('Cannot sync scroll to iframe:', error);
746
+ }
747
+ }, [iframeRef, scale]);
748
+ return { handleScroll };
749
+ };
750
+
751
+ const useHeatmapScale = (props) => {
752
+ const { wrapperRef, iframeRef, config } = props;
753
+ // 1. Observe container dimensions
754
+ const { containerWidth, containerHeight } = useContainerDimensions({ wrapperRef });
755
+ // 2. Get content dimensions from config
756
+ const { contentWidth } = useContentDimensions({ iframeRef, config });
757
+ // 3. Observe iframe height (now reacts to width changes)
758
+ const { iframeHeight } = useIframeHeight({ iframeRef, contentWidth });
759
+ // 4. Calculate scale
760
+ const { scale } = useScaleCalculation({ containerWidth, contentWidth });
761
+ // 5. Setup scroll sync
762
+ const { handleScroll } = useScrollSync({ iframeRef, scale });
763
+ return {
764
+ containerWidth,
765
+ containerHeight,
766
+ contentWidth,
767
+ iframeHeight,
768
+ scale,
769
+ scaledWidth: contentWidth * scale,
770
+ scaledHeight: iframeHeight * scale,
771
+ handleScroll,
772
+ };
773
+ };
774
+
775
+ const CLICKED_ELEMENT_ID = 'clickedElement';
776
+ const SECONDARY_CLICKED_ELEMENT_ID = 'secondaryClickedElementID';
777
+ const HOVERED_ELEMENT_ID = 'hoveredElement';
778
+ const SECONDARY_HOVERED_ELEMENT_ID = 'secondaryhoveredElementID';
779
+
780
+ const ElementCallout = ({ element, target, totalClicks, isSecondary, isRecordingView, isCompareMode, deviceType, heatmapType, language, widthScale, parentRef, }) => {
781
+ const calloutRef = useRef(null);
782
+ const [position, setPosition] = useState({
783
+ top: 0,
784
+ left: 0,
785
+ placement: 'top',
786
+ });
787
+ const percentage = formatPercentage(((element.clicks ?? 0) / totalClicks) * 100, 2);
788
+ // Calculate callout position
789
+ useEffect(() => {
790
+ const targetElement = document.querySelector(target);
791
+ const calloutElement = calloutRef.current;
792
+ if (!targetElement || !calloutElement)
793
+ return;
794
+ const calculatePosition = () => {
795
+ const targetRect = targetElement.getBoundingClientRect();
796
+ const calloutRect = calloutElement.getBoundingClientRect();
797
+ const viewportWidth = window.innerWidth;
798
+ const viewportHeight = window.innerHeight;
799
+ const padding = 12; // Space between target and callout
800
+ const arrowSize = 8;
801
+ let top = 0;
802
+ let left = 0;
803
+ let placement = 'top';
804
+ // Try positions in order: top, bottom, right, left
805
+ const positions = [
806
+ // Top
807
+ {
808
+ placement: 'top',
809
+ top: targetRect.top - calloutRect.height - padding - arrowSize,
810
+ left: targetRect.left + targetRect.width / 2 - calloutRect.width / 2,
811
+ valid: targetRect.top - calloutRect.height - padding - arrowSize > 0,
812
+ },
813
+ // Bottom
814
+ {
815
+ placement: 'bottom',
816
+ top: targetRect.bottom + padding + arrowSize,
817
+ left: targetRect.left + targetRect.width / 2 - calloutRect.width / 2,
818
+ valid: targetRect.bottom + calloutRect.height + padding + arrowSize < viewportHeight,
819
+ },
820
+ // Right
821
+ {
822
+ placement: 'right',
823
+ top: targetRect.top + targetRect.height / 2 - calloutRect.height / 2,
824
+ left: targetRect.right + padding + arrowSize,
825
+ valid: targetRect.right + calloutRect.width + padding + arrowSize < viewportWidth,
826
+ },
827
+ // Left
828
+ {
829
+ placement: 'left',
830
+ top: targetRect.top + targetRect.height / 2 - calloutRect.height / 2,
831
+ left: targetRect.left - calloutRect.width - padding - arrowSize,
832
+ valid: targetRect.left - calloutRect.width - padding - arrowSize > 0,
833
+ },
834
+ ];
835
+ // Find first valid position
836
+ const validPosition = positions.find((p) => p.valid) || positions[0];
837
+ top = validPosition.top;
838
+ left = validPosition.left;
839
+ placement = validPosition.placement;
840
+ // Keep within viewport bounds
841
+ left = Math.max(padding, Math.min(left, viewportWidth - calloutRect.width - padding));
842
+ top = Math.max(padding, Math.min(top, viewportHeight - calloutRect.height - padding));
843
+ setPosition({ top, left, placement });
844
+ };
845
+ // Initial calculation
846
+ calculatePosition();
847
+ // Recalculate on scroll/resize
848
+ const handleUpdate = () => {
849
+ requestAnimationFrame(calculatePosition);
850
+ };
851
+ window.addEventListener('scroll', handleUpdate, true);
852
+ window.addEventListener('resize', handleUpdate);
853
+ parentRef?.current?.addEventListener('scroll', handleUpdate);
854
+ return () => {
855
+ window.removeEventListener('scroll', handleUpdate, true);
856
+ window.removeEventListener('resize', handleUpdate);
857
+ parentRef?.current?.removeEventListener('scroll', handleUpdate);
858
+ };
859
+ }, [target, parentRef]);
860
+ const calloutContent = (jsxs("div", { ref: calloutRef, className: `clarity-callout clarity-callout--${position.placement} ${isSecondary ? 'clarity-callout--secondary' : ''}`, style: {
861
+ position: 'fixed',
862
+ top: position.top,
863
+ left: position.left,
864
+ zIndex: 2147483647,
865
+ }, "aria-live": "assertive", role: "tooltip", children: [jsx("div", { className: "clarity-callout__arrow" }), jsxs("div", { className: "clarity-callout__content", children: [jsx("div", { className: "clarity-callout__rank", children: element.rank }), jsxs("div", { className: "clarity-callout__stats", children: [jsx("div", { className: "clarity-callout__label", children: "Clicks" }), jsxs("div", { className: "clarity-callout__value", children: [jsx("span", { className: "clarity-callout__count", children: element.clicks?.toLocaleString(language) }), jsxs("span", { className: "clarity-callout__percentage", children: ["(", percentage, "%)"] })] })] }), !isRecordingView && !isCompareMode && (jsxs("button", { className: "clarity-callout__button", "data-clarity-id": "viewRecordingClickedFromHeatmapIframe", "data-element-hash": element.hash, "data-selector": getSimpleSelector(element.selector), "data-device-type": deviceType, "data-heatmap-type": heatmapType, children: [jsx("svg", { width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", children: jsx("path", { d: "M5 3L11 8L5 13V3Z", fill: "currentColor" }) }), "View Recording"] }))] }), jsx("style", { children: `
866
+ .clarity-callout {
867
+ background: white;
868
+ border-radius: 8px;
869
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
870
+ padding: 16px;
871
+ min-width: 200px;
872
+ max-width: 280px;
873
+ animation: clarity-callout-fade-in 0.2s ease-out;
874
+ pointer-events: auto;
875
+ }
876
+
877
+ @keyframes clarity-callout-fade-in {
878
+ from {
879
+ opacity: 0;
880
+ transform: scale(0.95);
881
+ }
882
+ to {
883
+ opacity: 1;
884
+ transform: scale(1);
885
+ }
886
+ }
887
+
888
+ .clarity-callout__arrow {
889
+ position: absolute;
890
+ width: 16px;
891
+ height: 16px;
892
+ background: white;
893
+ transform: rotate(45deg);
894
+ }
895
+
896
+ /* Arrow positions */
897
+ .clarity-callout--top .clarity-callout__arrow {
898
+ bottom: -8px;
899
+ left: 50%;
900
+ margin-left: -8px;
901
+ box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
902
+ }
903
+
904
+ .clarity-callout--bottom .clarity-callout__arrow {
905
+ top: -8px;
906
+ left: 50%;
907
+ margin-left: -8px;
908
+ box-shadow: -2px -2px 4px rgba(0, 0, 0, 0.1);
909
+ }
910
+
911
+ .clarity-callout--left .clarity-callout__arrow {
912
+ right: -8px;
913
+ top: 50%;
914
+ margin-top: -8px;
915
+ box-shadow: 2px -2px 4px rgba(0, 0, 0, 0.1);
916
+ }
917
+
918
+ .clarity-callout--right .clarity-callout__arrow {
919
+ left: -8px;
920
+ top: 50%;
921
+ margin-top: -8px;
922
+ box-shadow: -2px 2px 4px rgba(0, 0, 0, 0.1);
923
+ }
924
+
925
+ .clarity-callout__content {
926
+ position: relative;
927
+ z-index: 1;
928
+ }
929
+
930
+ .clarity-callout__rank {
931
+ display: inline-flex;
932
+ align-items: center;
933
+ justify-content: center;
934
+ width: 32px;
935
+ height: 32px;
936
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
937
+ color: white;
938
+ border-radius: 8px;
939
+ font-weight: 600;
940
+ font-size: 16px;
941
+ margin-bottom: 12px;
942
+ }
943
+
944
+ .clarity-callout--secondary .clarity-callout__rank {
945
+ background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
946
+ }
947
+
948
+ .clarity-callout__stats {
949
+ margin-bottom: 12px;
950
+ }
951
+
952
+ .clarity-callout__label {
953
+ font-size: 12px;
954
+ color: #6b7280;
955
+ margin-bottom: 4px;
956
+ font-weight: 500;
957
+ }
958
+
959
+ .clarity-callout__value {
960
+ display: flex;
961
+ align-items: baseline;
962
+ gap: 6px;
963
+ }
964
+
965
+ .clarity-callout__count {
966
+ font-size: 20px;
967
+ font-weight: 700;
968
+ color: #111827;
969
+ }
970
+
971
+ .clarity-callout__percentage {
972
+ font-size: 14px;
973
+ color: #6b7280;
974
+ font-weight: 500;
975
+ }
976
+
977
+ .clarity-callout__button {
978
+ display: flex;
979
+ align-items: center;
980
+ justify-content: center;
981
+ gap: 6px;
982
+ width: 100%;
983
+ padding: 8px 12px;
984
+ background: #667eea;
985
+ color: white;
986
+ border: none;
987
+ border-radius: 6px;
988
+ font-size: 13px;
989
+ font-weight: 600;
990
+ cursor: pointer;
991
+ transition: all 0.2s;
992
+ }
993
+
994
+ .clarity-callout__button:hover {
995
+ background: #5568d3;
996
+ transform: translateY(-1px);
997
+ box-shadow: 0 2px 8px rgba(102, 126, 234, 0.4);
998
+ }
999
+
1000
+ .clarity-callout__button:active {
1001
+ transform: translateY(0);
1002
+ }
1003
+
1004
+ .clarity-callout__button svg {
1005
+ width: 14px;
1006
+ height: 14px;
1007
+ }
1008
+ ` })] }));
1009
+ // Render to body using Portal
1010
+ return createPortal(calloutContent, document.body);
1011
+ };
1012
+
1013
+ const RankBadge = ({ index, elementRect, widthScale, clickOnElement, }) => {
1014
+ const style = calculateRankPosition(elementRect, widthScale);
1015
+ return (jsx("div", { className: "rankBadge", style: style, onClick: clickOnElement, "data-testid": "elementRank", children: index }));
1016
+ };
1017
+
1018
+ const ClickedElementOverlay = ({ element, shouldShowCallout, isSecondary, targetId, totalClicks = 1, isRecordingView, isCompareMode, deviceType, heatmapType, widthScale, }) => {
1019
+ if (!element || (element.width === 0 && element.height === 0))
1020
+ return null;
1021
+ return (jsxs(Fragment$1, { children: [jsx("div", { className: "heatmapElement", id: targetId, style: {
1022
+ top: element.top,
1023
+ left: element.left,
1024
+ width: element.width,
1025
+ height: element.height,
1026
+ } }), jsx(RankBadge, { index: element.rank, elementRect: element, widthScale: widthScale }), shouldShowCallout && (jsx(ElementCallout, { element: element, target: `#${targetId}`, totalClicks: totalClicks, isSecondary: isSecondary, isRecordingView: isRecordingView, isCompareMode: isCompareMode, deviceType: deviceType, heatmapType: heatmapType, widthScale: widthScale }))] }));
1027
+ };
1028
+
1029
+ const DefaultRankBadges = ({ elements, getRect, widthScale, hidden }) => {
1030
+ if (hidden || elements.length === 0)
1031
+ return null;
1032
+ return (jsx(Fragment, { children: elements.map((element, index) => {
1033
+ const rect = getRect(element);
1034
+ if (!rect)
1035
+ return null;
1036
+ return (jsx(RankBadge, { index: index + 1, elementRect: rect, widthScale: widthScale }, element.hash));
1037
+ }) }));
1038
+ };
1039
+
1040
+ const HoveredElementOverlay = ({ element, onClick, isSecondary, targetId, totalClicks = 1, }) => {
1041
+ if (!element)
1042
+ return null;
1043
+ return (jsxs(Fragment$1, { children: [jsx("div", { onClick: onClick, className: "heatmapElement hovered", id: targetId, style: {
1044
+ top: element.top,
1045
+ left: element.left,
1046
+ width: element.width,
1047
+ height: element.height,
1048
+ cursor: 'pointer',
1049
+ } }), jsx(RankBadge, { index: element.rank, elementRect: element, widthScale: 1, clickOnElement: onClick })] }));
1050
+ };
1051
+
1052
+ const MissingElementMessage = ({ widthScale }) => {
1053
+ return (jsx("div", { className: "missingElement", style: {
1054
+ position: 'absolute',
1055
+ top: '50%',
1056
+ left: '50%',
1057
+ transform: `translate(-50%, -50%) scale(${1 / widthScale})`,
1058
+ background: 'rgba(0, 0, 0, 0.8)',
1059
+ color: 'white',
1060
+ padding: '12px 20px',
1061
+ borderRadius: '8px',
1062
+ fontSize: '14px',
1063
+ fontWeight: '500',
1064
+ zIndex: 9999,
1065
+ pointerEvents: 'none',
1066
+ whiteSpace: 'nowrap',
1067
+ }, "aria-live": "assertive", children: "Element not visible on current screen" }));
1068
+ };
1069
+
1070
+ const HeatmapElements = (props) => {
1071
+ const { iframeRef, parentRef, visualizer, heatmapInfo, widthScale, iframeHeight, iframeDimensions, selectedElement, isElementSidebarOpen, isVisible = true, selectElement, areDefaultRanksHidden, isSecondary, ...rest } = props;
1072
+ const getRect = useHeatmapElementPosition({
1073
+ iframeRef,
1074
+ parentRef,
1075
+ visualizer,
1076
+ heatmapWidth: heatmapInfo?.width,
1077
+ iframeHeight,
1078
+ widthScale,
1079
+ projectId: props.projectId,
1080
+ });
1081
+ const { clickedElement, showMissingElement, shouldShowCallout, setShouldShowCallout } = useClickedElement({
1082
+ selectedElement,
1083
+ heatmapInfo,
1084
+ getRect,
1085
+ });
1086
+ const { hoveredElement, handleMouseMove, handleMouseLeave, handleClick } = useHoveredElement({
1087
+ iframeRef,
1088
+ heatmapInfo,
1089
+ getRect,
1090
+ onSelect: selectElement,
1091
+ widthScale,
1092
+ });
1093
+ const resetAll = () => {
1094
+ // nếu cần reset thêm state ở đây
1095
+ // setShouldShowCallout(false);
1096
+ };
1097
+ useHeatmapEffects({
1098
+ isVisible,
1099
+ isElementSidebarOpen,
1100
+ selectedElement,
1101
+ setShouldShowCallout,
1102
+ resetAll,
1103
+ });
1104
+ if (!isVisible)
1105
+ return null;
1106
+ const top10 = heatmapInfo?.sortedElements?.slice(0, 10) ?? [];
1107
+ return (jsxs("div", { onMouseMove: handleMouseMove, onMouseLeave: handleMouseLeave, className: "heatmapElements gx-hm-elements", style: iframeDimensions, children: [jsx(DefaultRankBadges, { elements: top10, getRect: getRect, widthScale: widthScale, hidden: areDefaultRanksHidden }), jsx(ClickedElementOverlay, { widthScale: widthScale, element: clickedElement, shouldShowCallout: shouldShowCallout, isSecondary: isSecondary, targetId: isSecondary ? SECONDARY_CLICKED_ELEMENT_ID : CLICKED_ELEMENT_ID, ...rest }), showMissingElement && jsx(MissingElementMessage, { widthScale: widthScale }), jsx(HoveredElementOverlay, { element: hoveredElement, onClick: handleClick, isSecondary: isSecondary, targetId: isSecondary ? SECONDARY_HOVERED_ELEMENT_ID : HOVERED_ELEMENT_ID, totalClicks: heatmapInfo?.totalClicks ?? 1 }), hoveredElement !== clickedElement && hoveredElement && (jsx(ElementCallout, { element: hoveredElement, target: `#${props.isSecondary ? SECONDARY_HOVERED_ELEMENT_ID : HOVERED_ELEMENT_ID}`, totalClicks: props.heatmapInfo?.totalClicks ?? 1, isSecondary: props.isSecondary, parentRef: props.parentRef }))] }));
1108
+ };
1109
+
1110
+ const VizElements = ({ width, height, iframeRef, wrapperRef, widthScale, }) => {
1111
+ useHeatmapDataStore((state) => state.data);
1112
+ const heatmapInfo = {
1113
+ sortedElements: [
1114
+ {
1115
+ hash: '9ebwu6a3',
1116
+ selector: 'Join our email list',
1117
+ },
1118
+ {
1119
+ hash: '350hde5d4',
1120
+ selector: 'Products',
1121
+ },
1122
+ ],
1123
+ elementMapInfo: {
1124
+ '9ebwu6a3': {
1125
+ totalclicks: 4,
1126
+ hash: '9ebwu6a3',
1127
+ },
1128
+ '350hde5d4': {
1129
+ totalclicks: 4,
1130
+ hash: '350hde5d4',
1131
+ },
1132
+ },
1133
+ totalClicks: 8,
1134
+ };
1135
+ const visualizer = {
1136
+ get: (hash) => {
1137
+ const doc = iframeRef.current?.contentDocument;
1138
+ if (!doc)
1139
+ return null;
1140
+ // Find element by hash attribute
1141
+ return doc.querySelector(`[data-clarity-hashalpha="${hash}"]`);
1142
+ },
1143
+ };
1144
+ const [selectedElement, setSelectedElement] = useState(null);
1145
+ if (!iframeRef.current)
1146
+ return null;
1147
+ return (jsx(HeatmapElements, { visualizer: visualizer, iframeRef: iframeRef, parentRef: wrapperRef, iframeHeight: window.innerHeight, widthScale: widthScale, heatmapInfo: heatmapInfo, selectedElement: selectedElement, selectElement: setSelectedElement, isVisible: true, iframeDimensions: {
1148
+ width,
1149
+ height,
1150
+ position: 'absolute',
1151
+ top: 0,
1152
+ left: 0,
1153
+ // pointerEvents: 'none',
1154
+ } }));
1155
+ };
1156
+
1157
+ const ReplayControls = () => {
1158
+ const replayResult = useReplayRender();
1159
+ return (jsxs("div", { style: {
1160
+ position: 'absolute',
1161
+ bottom: 20,
1162
+ left: '50%',
1163
+ transform: 'translateX(-50%)',
1164
+ display: 'flex',
1165
+ gap: 10,
1166
+ backgroundColor: 'rgba(0, 0, 0, 0.7)',
1167
+ padding: '10px 20px',
1168
+ borderRadius: 8,
1169
+ }, children: [jsx("button", { onClick: replayResult.play, style: {
1170
+ padding: '8px 16px',
1171
+ backgroundColor: '#4CAF50',
1172
+ color: 'white',
1173
+ border: 'none',
1174
+ borderRadius: 4,
1175
+ cursor: 'pointer',
1176
+ }, children: "Play" }), jsx("button", { onClick: replayResult.pause, style: {
1177
+ padding: '8px 16px',
1178
+ backgroundColor: '#f44336',
1179
+ color: 'white',
1180
+ border: 'none',
1181
+ borderRadius: 4,
1182
+ cursor: 'pointer',
1183
+ }, children: "Pause" })] }));
1184
+ };
1185
+
1186
+ const VizDomRenderer = ({ mode = 'heatmap' }) => {
1187
+ const config = useHeatmapDataStore((state) => state.config);
1188
+ const wrapperRef = useRef(null);
1189
+ const visualRef = useRef(null);
1190
+ const { iframeRef } = useHeatmapVizRender(mode);
1191
+ const { contentWidth, iframeHeight, scale, scaledHeight, handleScroll } = useHeatmapScale({
1192
+ wrapperRef,
1193
+ iframeRef,
1194
+ config,
1195
+ });
1196
+ const onScroll = (e) => {
1197
+ const scrollTop = e.currentTarget.scrollTop;
1198
+ handleScroll(scrollTop);
1199
+ };
1200
+ return (jsxs("div", { ref: visualRef, className: "gx-hm-visual", onScroll: onScroll, style: {
1201
+ overflow: 'hidden auto',
1202
+ display: 'flex',
1203
+ position: 'relative',
1204
+ justifyContent: 'center',
1205
+ flex: 1,
1206
+ backgroundColor: '#fff',
1207
+ // borderRadius: '8px',
1208
+ }, children: [jsx("div", { className: "gx-hm-visual-unscaled", style: {
1209
+ width: '100%',
1210
+ display: 'flex',
1211
+ justifyContent: 'center',
1212
+ alignItems: 'flex-start',
1213
+ height: scaledHeight > 0 ? `${scaledHeight + HEATMAP_CONFIG['paddingBlock']}px` : 'auto',
1214
+ padding: HEATMAP_STYLE['wrapper']['padding'],
1215
+ }, children: jsxs("div", { className: "gx-hm-wrapper", ref: wrapperRef, style: {
1216
+ width: contentWidth,
1217
+ height: iframeHeight,
1218
+ transform: `scale(${scale})`,
1219
+ transformOrigin: 'top center',
1220
+ }, children: [jsx(VizElements, { width: contentWidth, height: iframeHeight, widthScale: scale, iframeRef: iframeRef, wrapperRef: wrapperRef }), jsx("iframe", {
1221
+ // key={iframeKey}
1222
+ ref: iframeRef, ...HEATMAP_IFRAME, width: contentWidth, height: iframeHeight, scrolling: "no" })] }) }), mode === 'replay' && jsx(ReplayControls, {})] }));
1223
+ };
1224
+
1225
+ const VizDomContainer = () => {
1226
+ return (jsx(BoxStack, { id: "gx-hm-viz-container", flexDirection: "column", flex: "1 1 auto", overflow: "auto", children: jsx(BoxStack, { id: "gx-hm-content", flexDirection: "column", flex: "1 1 auto", overflow: "hidden", style: {
1227
+ // margin: '0px 16px 0px 12px',
1228
+ // borderRadius: '8px 8px 0 0',
1229
+ // borderRadius: '8px',
1230
+ minWidth: '394px',
1231
+ padding: '12px',
1232
+ background: '#f1f1f1',
1233
+ }, children: jsx(VizDomRenderer, {}) }) }));
1234
+ };
1235
+
1236
+ const SIDEBAR_WIDTH = 280;
1237
+ const LeftSidebar = ({ children }) => {
1238
+ const isHideSidebar = useHeatmapDataStore((state) => state.state.hideSidebar);
1239
+ if (isHideSidebar) {
1240
+ return null;
1241
+ }
1242
+ return (jsx("div", { className: "gx-hm-sidebar", style: {
1243
+ height: '100%',
1244
+ display: 'flex',
1245
+ ...(isHideSidebar
1246
+ ? {
1247
+ width: '0',
1248
+ transform: 'translateX(-100%)',
1249
+ visibility: 'hidden',
1250
+ }
1251
+ : { width: `${SIDEBAR_WIDTH}px` }),
1252
+ }, children: jsx("div", { className: "gx-hm-sidebar-wrapper", style: { height: '100%', width: `${SIDEBAR_WIDTH}px` }, children: children }) }));
1253
+ };
1254
+
1255
+ const WrapperPreview = ({ children }) => {
1256
+ return (jsxs("div", { className: "gx-hm-container", style: { display: 'flex', overflowY: 'hidden', flex: '1', position: 'relative' }, children: [jsx(LeftSidebar, { children: children }), jsx(VizDomContainer, {})] }));
1257
+ };
1258
+
1259
+ const WrapperLayout = ({ header, toolbar, sidebar }) => {
1260
+ return (jsxs(BoxStack, { id: "gx-hm-layout", flexDirection: "column", flex: "1", children: [jsx(ContentHeader, { children: header }), jsx(ContentHeader, { children: toolbar }), jsx(WrapperPreview, { children: sidebar })] }));
1261
+ };
1262
+
92
1263
  const HeatmapLayout = ({ data, clickmap, header, toolbar, sidebar, }) => {
93
1264
  const setData = useHeatmapDataStore((state) => state.setData);
94
1265
  const setClickmap = useHeatmapDataStore((state) => state.setClickmap);
@@ -111,7 +1282,7 @@ const HeatmapLayout = ({ data, clickmap, header, toolbar, sidebar, }) => {
111
1282
  return (jsx(BoxStack, { id: "gx-hm-project", flexDirection: "column", flex: "1", height: "100%", children: jsx(BoxStack, { id: "gx-hm-project-content", flexDirection: "column", flex: "1", children: jsx("div", { style: {
112
1283
  minHeight: '100%',
113
1284
  display: 'flex',
114
- } }) }) }));
1285
+ }, children: jsx(WrapperLayout, { header: header, toolbar: toolbar, sidebar: sidebar }) }) }) }));
115
1286
  };
116
1287
 
117
1288
  var PanelContent;