Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gallery improvements + Fixes #268

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/dapp/components/Gallery/Cell/Cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import styles from './Cell.module.scss';

const MAX_PRIORITY_ITEMS = 24;

const Cell = ({ assetId, isRevealed, kind, isMiddleCell }) => {
const Cell = ({ assetId, tokenId, isRevealed, kind, isMiddleCell }) => {
const { breakpoint } = useBreakpoint(BREAKPOINTS, 'desktop');

const isMobile = breakpoint === 'mobile';
Expand All @@ -22,7 +22,7 @@ const Cell = ({ assetId, isRevealed, kind, isMiddleCell }) => {

const imageSrc = useMemo(() => {
if (isRevealed && !isDev)
return `https://storageapi.fleek.co/${FLEEK_BUCKET_ID}/assets/${assetId}.png`;
return `https://storageapi2.fleek.co/${FLEEK_BUCKET_ID}/assets/${assetId}.png`;

if (isRevealed && isDev) return '/assets/revealed-token.png';

Expand All @@ -37,7 +37,7 @@ const Cell = ({ assetId, isRevealed, kind, isMiddleCell }) => {

return (
<div
key={`cell-${assetId}`}
key={`cell-${tokenId}`}
className={cn(styles.cellContainer, {
[styles.middle]: isMiddleCell,
[styles.all]: kind === ALL,
Expand All @@ -51,10 +51,10 @@ const Cell = ({ assetId, isRevealed, kind, isMiddleCell }) => {
{...imageSize}
{...imageSize}
className={styles.image}
priority={hasPriority}
priority={hasPriority.toString()}
/>
</Zoom>
<span className={styles.assetId}>#{assetId}</span>
<span className={styles.tokenId}>#{tokenId}</span>
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/components/Gallery/Cell/Cell.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
width: 360px;
}

.assetId {
.tokenId {
top: -22px;
position: relative;
left: 6px;
Expand Down
11 changes: 6 additions & 5 deletions packages/dapp/components/Gallery/Grid/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const Grid = ({ items, kind }) => {

const chunks = useMemo(() => chunk(items, columns), [items, columns]);

const isMiddleCell = (assetId) => {
const isMiddleCell = (tokenId) => {
if (kind === ME) return false;

return chunks.some((item) => {
return item[1]?.assetId === assetId;
return item[1]?.tokenId === tokenId;
});
};

Expand All @@ -39,11 +39,12 @@ const Grid = ({ items, kind }) => {
[styles.mobileGrid]: isMobile,
})}
>
{items?.map(({ assetId, isRevealed }) => {
const isMiddle = isMiddleCell(assetId);
{items?.map(({ assetId, tokenId, isRevealed }) => {
const isMiddle = isMiddleCell(tokenId);
return (
<Cell
key={assetId}
key={tokenId}
tokenId={tokenId}
assetId={assetId}
isRevealed={isRevealed}
kind={kind}
Expand Down
24 changes: 18 additions & 6 deletions packages/dapp/hooks/useGallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ const useGallery = () => {
Ethernauts.totalSupply().then(Number),
]);

const maxBatchRevealed = Math.floor(totalSupply / batchSize - 1);
const maxTokenIdRevealed = totalSupply - (totalSupply % batchSize) - 1;

const offsets = [];

for (let batchId = 0; batchId <= maxBatchRevealed; batchId++) {
const randomNumber = await Ethernauts.getRandomNumberForBatch(batchId);
const offset = Number(randomNumber.mod(batchSize));
offsets.push(offset);
}

const logs = await provider.getLogs(filter);

const galleryItems = await logs.reduce(async (pGalleryItems, curr) => {
Expand All @@ -58,16 +67,19 @@ const useGallery = () => {

const to = parsedLog.args.to.toLowerCase();
const tokenId = parsedLog.args.tokenId.toNumber();
const isRevealed = tokenId <= maxTokenIdRevealed;
const batchNumber = Math.floor(tokenId / batchSize);
const maxTokenIdInBatch = batchSize * (batchNumber + 1) - 1;
const offset = offsets[batchNumber];

const assetId = await Ethernauts.getAssetIdForTokenId(tokenId);

// getAssetIdForTokenId returns "assetId,boolean" so it takes the first position;
const [formattedAssetId] = assetId.toString().split(',');
let assetId = tokenId + offset;
if (assetId > maxTokenIdInBatch) assetId -= batchSize;

const item = {
assetId: formattedAssetId,
tokenId,
assetId,
owner: to,
isRevealed: tokenId <= maxTokenIdRevealed,
isRevealed,
};

// Push all nfts
Expand Down
2 changes: 1 addition & 1 deletion packages/dapp/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
};
},
images: {
domains: ['https://storageapi.fleek.co/'],
domains: ['https://storageapi.fleek.co/', 'https://storageapi2.fleek.co/'],
},
// Fix fast refresh for Windows users
webpackDevMiddleware: (config) => {
Expand Down