Files
parcer/src/components/organisms/BrowserWindow.tsx

144 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-06-14 21:47:42 +05:30
import React, { useCallback, useEffect, useState } from 'react';
import { useSocketStore } from '../../context/socket';
import Canvas from "../atoms/canvas";
import { useBrowserDimensionsStore } from "../../context/browserDimensions";
2024-07-23 21:58:28 +05:30
import { Highlighter } from "../atoms/Highlighter";
import { GenericModal } from '../atoms/GenericModal';
2024-07-24 20:54:13 +05:30
import { useActionContext } from '../../context/browserActions';
import { useBrowserSteps } from '../../context/browserSteps';
2024-07-25 22:50:45 +05:30
import { ConfirmationBox } from "../atoms/ConfirmationBox";
2024-06-14 21:47:42 +05:30
export const BrowserWindow = () => {
const [canvasRef, setCanvasReference] = useState<React.RefObject<HTMLCanvasElement> | undefined>(undefined);
const [screenShot, setScreenShot] = useState<string>("");
2024-06-14 23:17:32 +05:30
const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string } | null>(null);
const [showConfirmation, setShowConfirmation] = useState(false);
2024-06-14 21:47:42 +05:30
const { socket } = useSocketStore();
const { width, height } = useBrowserDimensionsStore();
2024-07-24 23:28:48 +05:30
const { getText, getScreenshot } = useActionContext();
const { addBrowserStep } = useBrowserSteps();
2024-06-14 21:47:42 +05:30
2024-07-23 21:58:28 +05:30
const onMouseMove = (e: MouseEvent) => {
if (canvasRef && canvasRef.current && highlighterData) {
const canvasRect = canvasRef.current.getBoundingClientRect();
// mousemove outside the browser window
2024-07-23 21:58:28 +05:30
if (
e.pageX < canvasRect.left
|| e.pageX > canvasRect.right
|| e.pageY < canvasRect.top
|| e.pageY > canvasRect.bottom
) {
setHighlighterData(null);
}
}
};
2024-06-14 21:47:42 +05:30
const screencastHandler = useCallback((data: string) => {
setScreenShot(data);
}, [screenShot]);
2024-06-14 21:47:42 +05:30
2024-06-14 23:17:32 +05:30
useEffect(() => {
2024-06-14 21:47:42 +05:30
if (socket) {
socket.on("screencast", screencastHandler);
}
if (canvasRef?.current) {
drawImage(screenShot, canvasRef.current);
2024-07-23 21:59:20 +05:30
} else {
console.log('Canvas is not initialized');
2024-06-14 21:47:42 +05:30
}
return () => {
socket?.off("screencast", screencastHandler);
}
}, [screenShot, canvasRef, socket, screencastHandler]);
2024-06-14 23:17:32 +05:30
const highlighterHandler = useCallback((data: { rect: DOMRect, selector: string }) => {
2024-06-14 21:47:42 +05:30
setHighlighterData(data);
}, [highlighterData])
2024-06-14 21:47:42 +05:30
2024-06-14 23:17:32 +05:30
useEffect(() => {
2024-07-23 21:58:28 +05:30
document.addEventListener('mousemove', onMouseMove, false);
2024-06-14 21:47:42 +05:30
if (socket) {
socket.on("highlighter", highlighterHandler);
}
return () => {
2024-07-23 21:58:28 +05:30
document.removeEventListener('mousemove', onMouseMove);
2024-06-14 21:47:42 +05:30
socket?.off("highlighter", highlighterHandler);
};
2024-07-23 21:58:28 +05:30
}, [socket, onMouseMove]);
2024-06-25 22:39:29 +05:30
const handleClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (highlighterData && canvasRef?.current) {
const canvasRect = canvasRef.current.getBoundingClientRect();
const clickX = e.clientX - canvasRect.left;
const clickY = e.clientY - canvasRect.top;
2024-07-24 18:13:53 +05:30
const highlightRect = highlighterData.rect;
if (
clickX >= highlightRect.left &&
clickX <= highlightRect.right &&
clickY >= highlightRect.top &&
clickY <= highlightRect.bottom
) {
2024-07-26 22:57:49 +05:30
addBrowserStep('Enter Label Here...', highlighterData.selector);
}
}
};
const handleConfirmation = (confirmed: boolean) => {
if (confirmed) {
console.log(`User confirmed interaction with: ${highlighterData?.selector}`);
} else {
console.log('User declined interaction');
}
setShowConfirmation(false);
};
2024-06-14 21:47:42 +05:30
return (
<div onClick={handleClick}>
{
2024-07-26 02:23:52 +05:30
getText === true && showConfirmation ? (
2024-07-24 18:13:53 +05:30
<GenericModal
isOpen={showConfirmation}
onClose={() => setShowConfirmation(false)}
canBeClosed={false}
>
<ConfirmationBox
selector={highlighterData?.selector || ''}
onYes={() => handleConfirmation(true)}
onNo={() => handleConfirmation(false)}
/>
</GenericModal>
) : null
}
2024-07-26 02:23:32 +05:30
{(getText === true && !showConfirmation && highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?
2024-07-23 21:58:28 +05:30
<Highlighter
unmodifiedRect={highlighterData?.rect}
displayedSelector={highlighterData?.selector}
width={width}
height={height}
canvasRect={canvasRef.current.getBoundingClientRect()}
/>
: null}
2024-06-14 23:17:32 +05:30
<Canvas
onCreateRef={setCanvasReference}
width={width}
height={height}
2024-06-14 21:47:42 +05:30
/>
2024-07-21 23:51:55 +05:30
</div>
2024-06-14 21:47:42 +05:30
);
};
2024-06-14 23:17:32 +05:30
const drawImage = (image: string, canvas: HTMLCanvasElement): void => {
2024-06-14 21:47:42 +05:30
const ctx = canvas.getContext('2d');
2024-06-14 21:47:42 +05:30
const img = new Image();
2024-06-14 21:47:42 +05:30
img.src = image;
img.onload = () => {
URL.revokeObjectURL(img.src);
2024-07-14 00:46:39 +05:30
ctx?.drawImage(img, 0, 0, 1280, 720);
2024-06-14 21:47:42 +05:30
};
2024-07-14 00:46:39 +05:30
};