diff --git a/src/components/Canvas.tsx b/src/components/Canvas.tsx index 449ff6e..b263db2 100644 --- a/src/components/Canvas.tsx +++ b/src/components/Canvas.tsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from "react"; -import {Button, Container, Flex, RadioGroup, Text} from "@radix-ui/themes"; +import { Button, Container, Flex, RadioGroup, Text } from "@radix-ui/themes"; import { HexColorPicker } from "react-colorful"; import { TransactionBlock } from "@mysten/sui.js/transactions"; import { @@ -11,18 +11,22 @@ import { useGetCanvas } from "../useGetCanvas"; import { SuiObjectData } from "@mysten/sui.js/client"; const cursorSizes = { - Small: 1, - Medium: 2, - Large: 3, -} + Small: 1, + Medium: 2, + Large: 3, +}; enum CursorSizes { - Small = 'Small', - Medium = 'Medium', - Large = 'Large', + Small = "Small", + Medium = "Medium", + Large = "Large", } -const cursorSelection: CursorSizes[] = [CursorSizes.Small, CursorSizes.Medium, CursorSizes.Large]; +const cursorSelection: CursorSizes[] = [ + CursorSizes.Small, + CursorSizes.Medium, + CursorSizes.Large, +]; function getCoords(size: CursorSizes, [x, y]: [number, number]) { if (size === CursorSizes.Small) { @@ -32,7 +36,7 @@ function getCoords(size: CursorSizes, [x, y]: [number, number]) { const sizeToNum = cursorSizes[size]; const coords = []; - const gridSize = sizeToNum * 2 - 1 + const gridSize = sizeToNum * 2 - 1; const halfGridSize = Math.floor(gridSize / 2); for (let i = -halfGridSize; i <= halfGridSize; i++) { @@ -58,6 +62,7 @@ const Canvas: React.FC = () => { const [gridColors, setGridColors] = useState([]); const [localGrid, setLocalGrid] = useState([]); + const [length, setLength] = useState(0); // Dragging Mouse const [isDragging, setIsDragging] = useState(false); @@ -69,7 +74,7 @@ const Canvas: React.FC = () => { ) => { if (isDragging) { const coords = getCoords(cursorSize, [rowIndex, colIndex]); - const newColors = [] + const newColors = []; for (let row = 0; row < localGrid.length; row++) { newColors.push(localGrid[row].slice()); } @@ -93,14 +98,18 @@ const Canvas: React.FC = () => { useEffect(() => { if (!isLoading && !error && data?.data) { const currentCanvas = getArrayFields(data.data); - if (currentCanvas) { - setGridColors(currentCanvas); - // Create a new grid with the same dimensions as currentCanvas - const newLocalGrid = Array.from({ length: currentCanvas.length }, () => - Array(currentCanvas[0].length).fill("0"), - ); - - setLocalGrid(newLocalGrid); + setGridColors(currentCanvas); + if (length !== currentCanvas.length) { + setLength(currentCanvas.length); + if (currentCanvas) { + // Create a new grid with the same dimensions as currentCanvas + const newLocalGrid = Array.from( + { length: currentCanvas.length }, + () => Array(currentCanvas[0].length).fill("0"), + ); + + setLocalGrid(newLocalGrid); + } } } }, [data, isLoading, error]); @@ -121,7 +130,9 @@ const Canvas: React.FC = () => { ), transactionBlock.pure(newDeltas[idx][0]), transactionBlock.pure(newDeltas[idx][1]), - transactionBlock.pure(localGrid[newDeltas[idx][0]][newDeltas[idx][1]]), + transactionBlock.pure( + localGrid[newDeltas[idx][0]][newDeltas[idx][1]], + ), ], }); } @@ -175,28 +186,40 @@ const Canvas: React.FC = () => { return (
- { - setColor(color); - setCursorSize(CursorSizes.Small); - }} /> + { + setColor(color); + setCursorSize(CursorSizes.Small); + }} + /> - - { - setCursorSize(value as CursorSizes); - }}> + + { + setCursorSize(value as CursorSizes); + }} + > - {cursorSelection.map(cursorSize => { + {cursorSelection.map((cursorSize) => { return ( {cursorSize} - )})} + ); + })}