Files
TheGame/packages/web/lib/hooks/opensea.ts
Arsenije Savic ea42bea2c0 Dashboard fixes; closes #1209, #1210, #1223, & #1224
* Remove connect to progress if the network is not mainnet

* Move xpHelpers to the utils

* Add tests to the utils package

* Add xpHelpers test

* Add convertToRoman util function

* Display current season label dynamically

* Add onboarding text if player does not have xp

* Remove ConnectToProgress import

* Add connect to progress if the network is not mainnet

* Show edit button if network is not main

* Refactor xp helper season start

* displaying modal when chain change is needed 🛬

* using Unicode Roman numerals Ⅲ

* slight formatting fixes 🗯
2022-05-18 01:48:07 -04:00

78 lines
2.0 KiB
TypeScript

import { Maybe } from '@metafam/utils';
import { Player } from 'graphql/autogen/types';
import { useEffect, useState } from 'react';
import { Collectible } from 'utils/openseaHelpers';
export const useOpenSeaCollectibles = ({
player: { ethereumAddress: owner },
}: {
player: Player;
}): {
favorites: Array<Collectible>;
data: Array<Collectible>;
loading: boolean;
error: Maybe<string>;
} => {
const [favorites, setFavorites] = useState<Array<Collectible>>([]);
const [data, setData] = useState<Array<Collectible>>([]);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<Maybe<string>>(null);
useEffect(() => {
const load = async () => {
setLoading(true);
setError(null);
try {
const allData = await fetchAllOpenSeaData(owner);
setData(allData);
setFavorites(allData.slice(0, 3));
} catch (err) {
setError((err as Error).message);
} finally {
setLoading(false);
}
};
if (owner) {
load();
}
}, [owner]);
return { favorites, data, loading, error };
};
const fetchAllOpenSeaData = async (
owner: string,
): Promise<Array<Collectible>> => {
let offset = 0;
let data: Array<Collectible> = [];
let lastData: Array<Collectible> = [];
const limit = 50;
do {
// eslint-disable-next-line no-await-in-loop
lastData = await fetchOpenSeaData(owner, offset, limit);
data = data.concat(lastData);
offset += limit;
} while (lastData.length > 0);
return data;
};
const fetchOpenSeaData = async (
owner: string,
offset: number,
limit: number,
): Promise<Array<Collectible>> => {
const res = await fetch(
`/api/opensea?owner=${owner}&offset=${offset}&limit=${limit}`,
);
const body = await res.text();
const { assets, error } = JSON.parse(body);
if (error) {
// eslint-disable-next-line no-console
console.error({ error, body });
throw new Error(error);
}
if (!assets) throw new Error(`Received ${assets} assets`);
return assets;
};