import React, { useCallback, useEffect, useState } from 'react'; import { useSocketStore } from '../../context/socket'; import Canvas from "../atoms/canvas"; import { useBrowserDimensionsStore } from "../../context/browserDimensions"; import { Highlighter } from "../atoms/Highlighter"; import { GenericModal } from '../atoms/GenericModal'; import { Button, Typography, Box } from '@mui/material'; import { useActionContext } from '../../context/browserActions'; interface ConfirmationBoxProps { selector: string; onYes: () => void; onNo: () => void; } const ConfirmationBox = ({ selector, onYes, onNo }: ConfirmationBoxProps) => { return ( Confirmation Do you want to interact with the element: {selector}? ); }; export const BrowserWindow = () => { const [canvasRef, setCanvasReference] = useState | undefined>(undefined); const [screenShot, setScreenShot] = useState(""); const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string } | null>(null); const [showConfirmation, setShowConfirmation] = useState(false); const { socket } = useSocketStore(); const { width, height } = useBrowserDimensionsStore(); const { getText, getScreenshot } = useActionContext(); console.log('Use browser dimensions:', width, height) const onMouseMove = (e: MouseEvent) => { if (canvasRef && canvasRef.current && highlighterData) { const canvasRect = canvasRef.current.getBoundingClientRect(); // mousemove outside the browser window if ( e.pageX < canvasRect.left || e.pageX > canvasRect.right || e.pageY < canvasRect.top || e.pageY > canvasRect.bottom ) { setHighlighterData(null); } } }; const screencastHandler = useCallback((data: string) => { setScreenShot(data); }, [screenShot]); useEffect(() => { if (socket) { socket.on("screencast", screencastHandler); } if (canvasRef?.current) { drawImage(screenShot, canvasRef.current); } else { console.log('Canvas is not initialized'); } return () => { socket?.off("screencast", screencastHandler); } }, [screenShot, canvasRef, socket, screencastHandler]); const highlighterHandler = useCallback((data: { rect: DOMRect, selector: string }) => { setHighlighterData(data); console.log('Highlighter Rect via socket:', data.rect) }, [highlighterData]) useEffect(() => { document.addEventListener('mousemove', onMouseMove, false); if (socket) { socket.on("highlighter", highlighterHandler); } return () => { document.removeEventListener('mousemove', onMouseMove); socket?.off("highlighter", highlighterHandler); }; }, [socket, onMouseMove]); const handleClick = (e: React.MouseEvent) => { if (highlighterData && canvasRef?.current) { const canvasRect = canvasRef.current.getBoundingClientRect(); const clickX = e.clientX - canvasRect.left; const clickY = e.clientY - canvasRect.top; const highlightRect = highlighterData.rect; if ( clickX >= highlightRect.left && clickX <= highlightRect.right && clickY >= highlightRect.top && clickY <= highlightRect.bottom ) { setShowConfirmation(true); } } }; const handleConfirmation = (confirmed: boolean) => { if (confirmed) { console.log(`User confirmed interaction with: ${highlighterData?.selector}`); // Here you can add logic to interact with the element } else { console.log('User declined interaction'); } setShowConfirmation(false); }; return (
{ showConfirmation ? ( setShowConfirmation(false)} canBeClosed={false} > handleConfirmation(true)} onNo={() => handleConfirmation(false)} /> ) : null } {(getText && !showConfirmation && highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ? : null}
); }; const drawImage = (image: string, canvas: HTMLCanvasElement): void => { const ctx = canvas.getContext('2d'); const img = new Image(); img.src = image; img.onload = () => { URL.revokeObjectURL(img.src); ctx?.drawImage(img, 0, 0, 1280, 720); console.log('Image drawn on canvas:', img.width, img.height); console.log('Image drawn on canvas:', canvas.width, canvas.height); }; };