feat: use ref for getText (it works finally!)

This commit is contained in:
karishmas6
2024-07-26 04:19:19 +05:30
parent c6bcec9ce1
commit 99e92797b8

View File

@@ -28,6 +28,7 @@ const Canvas = ({ width, height, onCreateRef }: CanvasProps) => {
const { socket } = useSocketStore(); const { socket } = useSocketStore();
const { setLastAction, lastAction } = useGlobalInfoStore(); const { setLastAction, lastAction } = useGlobalInfoStore();
const { getText, getScreenshot } = useActionContext(); const { getText, getScreenshot } = useActionContext();
const getTextRef = useRef(getText);
const notifyLastAction = (action: string) => { const notifyLastAction = (action: string) => {
if (lastAction !== action) { if (lastAction !== action) {
@@ -38,6 +39,10 @@ const Canvas = ({ width, height, onCreateRef }: CanvasProps) => {
const lastMousePosition = useRef<Coordinates>({ x: 0, y: 0 }); const lastMousePosition = useRef<Coordinates>({ x: 0, y: 0 });
//const lastWheelPosition = useRef<ScrollDeltas>({ deltaX: 0, deltaY: 0 }); //const lastWheelPosition = useRef<ScrollDeltas>({ deltaX: 0, deltaY: 0 });
useEffect(() => {
getTextRef.current = getText;
}, [getText]);
const onMouseEvent = useCallback((event: MouseEvent) => { const onMouseEvent = useCallback((event: MouseEvent) => {
if (socket) { if (socket) {
const coordinates = { const coordinates = {
@@ -47,7 +52,7 @@ const Canvas = ({ width, height, onCreateRef }: CanvasProps) => {
switch (event.type) { switch (event.type) {
case 'mousedown': case 'mousedown':
const clickCoordinates = getMappedCoordinates(event, canvasRef.current, width, height); const clickCoordinates = getMappedCoordinates(event, canvasRef.current, width, height);
if (getText === true) { if (getTextRef.current === true) {
console.log('get text') console.log('get text')
} else { } else {
socket.emit('input:mousedown', clickCoordinates); socket.emit('input:mousedown', clickCoordinates);
@@ -82,7 +87,7 @@ const Canvas = ({ width, height, onCreateRef }: CanvasProps) => {
return; return;
} }
} }
}, [socket, getText]); }, [socket]);
const onKeyboardEvent = useCallback((event: KeyboardEvent) => { const onKeyboardEvent = useCallback((event: KeyboardEvent) => {
if (socket) { if (socket) {