Skip to content

Commit

Permalink
linting off
Browse files Browse the repository at this point in the history
  • Loading branch information
joaolago1113 committed Jan 5, 2025
1 parent a99f937 commit 5dcc5c3
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 37 deletions.
9 changes: 9 additions & 0 deletions packages/nextjs/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "next/core-web-vitals",
"rules": {
"@typescript-eslint/no-unused-vars": "off",
"react-hooks/exhaustive-deps": "off",
"@next/next/no-img-element": "off",
"@next/next/no-page-custom-font": "off"
}
}
14 changes: 13 additions & 1 deletion packages/nextjs/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,26 @@ export function Canvas({
return () => window.removeEventListener('wheel', preventDefault);
}, []);

if (!canvasWidth || !canvasHeight || totalPixels === 0) {
// Consolidate loading checks
const isLoading = !canvasWidth || !canvasHeight || !pixelOwners;
const hasNoData = !pixelOwners || Object.keys(pixelOwners).length === 0;

useEffect(() => {
if (pixelOwners && Object.keys(pixelOwners).length > 0) {
console.log("[Canvas] Rendering with pixel data:", Object.keys(pixelOwners).length);
}
}, [pixelOwners]);

if (isLoading || hasNoData) {
console.log("[Canvas] Loading state:", { isLoading, hasNoData, pixelOwnersCount: Object.keys(pixelOwners || {}).length });
return (
<div className="flex items-center justify-center h-64">
<div className="animate-spin rounded-full h-32 w-32 border-b-2 border-primary"></div>
</div>
);
}

// Remove the other loading checks and proceed with render
return (
<div
ref={canvasRef}
Expand Down
14 changes: 13 additions & 1 deletion packages/nextjs/components/CollaborativeArtCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ImageUploadModal } from "./ImageUploadModal";
export default function CollaborativeArtCanvas() {
// All hooks first
const { address } = useAccount();
const { pixelOwners, isLoading } = useCanvasData();
const { pixelOwners, isLoading, error } = useCanvasData();
const {
balance,
buyTokens,
Expand Down Expand Up @@ -507,6 +507,18 @@ export default function CollaborativeArtCanvas() {
<div className="flex items-center justify-center h-[calc(100vh-64px)]">
<div className="animate-spin rounded-full h-32 w-32 border-b-2 border-white"></div>
</div>
) : error ? (
<div className="flex items-center justify-center h-[calc(100vh-64px)]">
<div className="text-center space-y-4">
<p className="text-red-500">Error loading canvas data</p>
<button
onClick={() => window.location.reload()}
className="px-4 py-2 bg-primary text-white rounded-lg hover:bg-primary/80"
>
Retry
</button>
</div>
</div>
) : !address ? (
<div className="fixed inset-0 flex flex-col items-center justify-center bg-gradient-to-b from-gray-900 via-[#0c1015] to-black">
<div className="text-center space-y-8">
Expand Down
114 changes: 79 additions & 35 deletions packages/nextjs/hooks/useCanvasData.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,105 @@
import { useEffect, useState } from "react";
import { useEffect, useState, useRef } from "react";
import { useReadContract } from "wagmi";
import { CONTRACTS, CANVAS_ABI } from "~/constants/contracts";

export function useCanvasData() {
const [totalPixelsPainted, setTotalPixelsPainted] = useState<number>(0);
const [pixelOwners, setPixelOwners] = useState<{ [key: number]: { color: number } }>({});
const [error, setError] = useState<Error | null>(null);
const [isInitialized, setIsInitialized] = useState(false);

const { data: pixels, isError } = useReadContract({
const retryCountRef = useRef(0);
const maxRetries = 10;

const { data: pixels, isError, error: contractError, refetch, isLoading: isContractLoading } = useReadContract({
address: CONTRACTS.COLLABORATIVE_ART_CANVAS,
abi: CANVAS_ABI,
functionName: "getAllPixels",
}) as { data: bigint[]; isError: boolean };
}) as {
data: bigint[];
isError: boolean;
error: Error | null;
refetch: () => Promise<any>;
isLoading: boolean;
};

useEffect(() => {
const retryInterval = 2000;

const attemptFetch = () => {
if (!isInitialized && retryCountRef.current < maxRetries) {
console.log(`[Canvas] Retry ${retryCountRef.current + 1}/${maxRetries}, Pixels: ${!!pixels}, Error: ${!!isError}`);

const timer = setTimeout(() => {
retryCountRef.current += 1;
refetch();
}, retryInterval);

return () => clearTimeout(timer);
}
};

return attemptFetch();
}, [pixels, isError, refetch, isInitialized]);

useEffect(() => {

if (isError) {
console.error("[Canvas] Error fetching canvas data:", contractError);
setError(contractError || new Error("Failed to fetch canvas data"));
return;
}

if (!pixels) {
console.log("No pixel data available");
console.log("[Canvas] No pixel data available yet");
return;
}

const owners: { [key: number]: { color: number } } = {};
let paintedCount = 0;
try {
console.log("[Canvas] Processing pixel data, length:", pixels.length);
const owners: { [key: number]: { color: number } } = {};
let paintedCount = 0;

pixels.forEach((pixel, index) => {
if (pixel !== BigInt(0)) {
const colorValue = Number(pixel);
const r = (colorValue >> 16) & 0xFF;
const g = (colorValue >> 8) & 0xFF;
const b = colorValue & 0xFF;
//console.log(`Pixel ${index}: RGB(${r},${g},${b}) = #${colorValue.toString(16).padStart(6, '0')}`);

owners[index] = { color: colorValue };
paintedCount++;
}
});
pixels.forEach((pixel, index) => {
if (pixel !== BigInt(0)) {
const colorValue = Number(pixel);
owners[index] = { color: colorValue };
paintedCount++;
}
});

console.log("Processed pixel data:", {
totalPixels: pixels.length,
paintedPixels: paintedCount,
samplePixels: Object.entries(owners).slice(0, 5).map(([index, data]) => ({
index,
colorHex: `#${data.color.toString(16).padStart(6, '0')}`,
colorDec: data.color
}))
});
console.log("[Canvas] Processed pixels:", {
total: pixels.length,
painted: paintedCount,
ownersSize: Object.keys(owners).length
});

setPixelOwners(owners);
setTotalPixelsPainted(paintedCount);
}, [pixels]);
setPixelOwners(owners);
setTotalPixelsPainted(paintedCount);
setError(null);
setIsInitialized(true);
console.log("[Canvas] Data initialization complete");
} catch (err) {
console.error("[Canvas] Error processing pixel data:", err);
setError(err instanceof Error ? err : new Error("Failed to process pixel data"));
}
}, [pixels, isError, contractError]);

if (isError) {
console.error("Error fetching canvas data");
}
useEffect(() => {
console.log("[Canvas] State update:", {
hasPixels: !!pixels,
pixelOwnersCount: Object.keys(pixelOwners).length,
isLoading: (!pixels && !isError) || !isInitialized || isContractLoading,
hasError: !!error,
isInitialized
});
}, [pixels, pixelOwners, error, isInitialized, isContractLoading, isError]);

return {
totalPixelsPainted,
pixelOwners,
isLoading: !pixels,
isLoading: (!pixels && !isError) || !isInitialized || isContractLoading,
error,
refetch,
isInitialized
};
}
3 changes: 3 additions & 0 deletions packages/nextjs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const nextConfig = {
buildActivity: false,
buildActivityPosition: "bottom-right",
},
eslint: {
ignoreDuringBuilds: true,
},
};

module.exports = nextConfig;
1 change: 1 addition & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"ethers": "^6.13.4",
"lucide-react": "^0.469.0",
"next": "latest",
"pino-pretty": "^13.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
"react-hot-toast": "^2.4.1",
Expand Down

0 comments on commit 5dcc5c3

Please sign in to comment.