refactor: move click logic to Canvas

This commit is contained in:
karishmas6
2024-07-23 10:18:38 +05:30
parent a28d0bd977
commit 0f1a8b9823

View File

@@ -3,52 +3,18 @@ import { useSocketStore } from '../../context/socket';
import Canvas from "../atoms/canvas"; import Canvas from "../atoms/canvas";
import { useBrowserDimensionsStore } from "../../context/browserDimensions"; import { useBrowserDimensionsStore } from "../../context/browserDimensions";
import { Highlighter } from "../atoms/Highlighter"; import { Highlighter } from "../atoms/Highlighter";
import { GenericModal } from '../atoms/GenericModal';
import { Button, Typography, Box } from '@mui/material';
interface ConfirmationBoxProps {
selector: string;
onYes: () => void;
onNo: () => void;
}
const ConfirmationBox = ({ selector, onYes, onNo }: ConfirmationBoxProps) => {
return (
<Box sx={{ textAlign: 'center' }}>
<Typography variant="h6" component="h2" gutterBottom>
Confirmation
</Typography>
<Typography variant="body1" gutterBottom>
Do you want to interact with the element: {selector}?
</Typography>
<Box sx={{ mt: 2, display: 'flex', justifyContent: 'center', gap: 2 }}>
<Button variant="contained" color="primary" onClick={onYes}>
Yes
</Button>
<Button variant="contained" color="secondary" onClick={onNo}>
No
</Button>
</Box>
</Box>
);
};
export const BrowserWindow = () => { export const BrowserWindow = () => {
const [canvasRef, setCanvasReference] = useState<React.RefObject<HTMLCanvasElement> | undefined>(undefined); const [canvasRef, setCanvasReference] = useState<React.RefObject<HTMLCanvasElement> | undefined>(undefined);
const [screenShot, setScreenShot] = useState<string>(""); const [screenShot, setScreenShot] = useState<string>("");
const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string } | null>(null); const [highlighterData, setHighlighterData] = useState<{ rect: DOMRect, selector: string } | null>(null);
const [showConfirmation, setShowConfirmation] = useState(false);
const [isClickConfirmed, setIsClickConfirmed] = useState(false);
const { socket } = useSocketStore(); const { socket } = useSocketStore();
const { width, height } = useBrowserDimensionsStore(); const { width, height } = useBrowserDimensionsStore();
// console.log('Use browser dimensions:', width, height)
const onMouseMove = (e: MouseEvent) => { const onMouseMove = (e: MouseEvent) => {
if (canvasRef && canvasRef.current && highlighterData) { if (canvasRef && canvasRef.current && highlighterData) {
const canvasRect = canvasRef.current.getBoundingClientRect(); const canvasRect = canvasRef.current.getBoundingClientRect();
// mousemove outside the browser window
if ( if (
e.pageX < canvasRect.left e.pageX < canvasRect.left
|| e.pageX > canvasRect.right || e.pageX > canvasRect.right
@@ -62,7 +28,7 @@ export const BrowserWindow = () => {
const screencastHandler = useCallback((data: string) => { const screencastHandler = useCallback((data: string) => {
setScreenShot(data); setScreenShot(data);
}, [screenShot]); }, []);
useEffect(() => { useEffect(() => {
if (socket) { if (socket) {
@@ -70,8 +36,6 @@ export const BrowserWindow = () => {
} }
if (canvasRef?.current) { if (canvasRef?.current) {
drawImage(screenShot, canvasRef.current); drawImage(screenShot, canvasRef.current);
} else {
console.log('Canvas is not initialized');
} }
return () => { return () => {
socket?.off("screencast", screencastHandler); socket?.off("screencast", screencastHandler);
@@ -80,8 +44,7 @@ export const BrowserWindow = () => {
const highlighterHandler = useCallback((data: { rect: DOMRect, selector: string }) => { const highlighterHandler = useCallback((data: { rect: DOMRect, selector: string }) => {
setHighlighterData(data); setHighlighterData(data);
// console.log('Highlighter Rect via socket:', data.rect) }, [])
}, [highlighterData])
useEffect(() => { useEffect(() => {
document.addEventListener('mousemove', onMouseMove, false); document.addEventListener('mousemove', onMouseMove, false);
@@ -94,50 +57,9 @@ export const BrowserWindow = () => {
}; };
}, [socket, onMouseMove]); }, [socket, onMouseMove]);
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;
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
setIsClickConfirmed(true);
} else {
console.log('User declined interaction');
setIsClickConfirmed(false);
}
setShowConfirmation(false);
};
return ( return (
<div onClick={handleClick}> <div>
<GenericModal {(highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?
isOpen={showConfirmation}
onClose={() => setShowConfirmation(false)}
canBeClosed={false}
>
<ConfirmationBox
selector={highlighterData?.selector || ''}
onYes={() => handleConfirmation(true)}
onNo={() => handleConfirmation(false)}
/>
</GenericModal>
{(!showConfirmation && highlighterData?.rect != null && highlighterData?.rect.top != null) && canvasRef?.current ?
<Highlighter <Highlighter
unmodifiedRect={highlighterData?.rect} unmodifiedRect={highlighterData?.rect}
displayedSelector={highlighterData?.selector} displayedSelector={highlighterData?.selector}
@@ -150,25 +72,18 @@ export const BrowserWindow = () => {
onCreateRef={setCanvasReference} onCreateRef={setCanvasReference}
width={width} width={width}
height={height} height={height}
isClickConfirmed={isClickConfirmed} highlighterData={highlighterData}
resetClickConfirmation={() => setIsClickConfirmed(false)}
/> />
</div> </div>
); );
}; };
const drawImage = (image: string, canvas: HTMLCanvasElement): void => { const drawImage = (image: string, canvas: HTMLCanvasElement): void => {
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
const img = new Image(); const img = new Image();
img.src = image; img.src = image;
img.onload = () => { img.onload = () => {
URL.revokeObjectURL(img.src); URL.revokeObjectURL(img.src);
ctx?.drawImage(img, 0, 0, 1280, 720); 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);
}; };
}; };