-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a99f937
commit 5dcc5c3
Showing
6 changed files
with
118 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters