Files
TheGame/packages/web/lib/hooks/opensea.ts
δυς 317ba7c9e5 Refactor Setup Wizard & Profile Modal Configuration Panes (#1127)
* incorporating configuration panes from #1078

* standardizing player hero subcomponents & removing `owner` var from `useProfileField` 🚪

* switching box type, ambiguating overly-specific names, & massaging heights 📱

* reordering profile details sections, updating deployment environment, & conditionally wrapping the hero elements 🎬

* fixing render of color disposition selector 🕍

* self code review changes: removed some `;`s 🎋

* getting yarn typecheck && yarn lint to pass post rebase 🏇🏾

* handling "missing <div> in <button>" error with mounted check & setting HTTP return codes for OpenSea API endpoint 🕺

* `ProfileWizard` extends `Wizard`, roles display better on mobile, & pronouns use `ProfileWizardPane` 🍊

* properly encapsulating ETH address regex 🚲

* adding some tasks 🏥

* fixed skills layou

* handling default values? 

* corrections for revision comments from @dan13ram (UI bugs to follow) 🌋

* cleaning up memberships & explorer type 🧫

* refactoring player roles configuration and display to use `WizardPane` 🔮

* bunches of mobile fixes & repairing the display of deserialized skills 📟

* removing redirect in static props & formatting memberships display for responsiveness 🪆

* improving comprehensability of `/me` & more mobile tweaking 🍦

* various spacing fixes & a “try again” button for connecting 🫕

* maybe fixed issue with saving images for fields with default values 🇩🇿

* switched roles selection to a bounded `SimpleGrid` to fix sizing weirdness 🧰

* fix: removed verify on brightId button

* fix: clean up username page

* formatting & fixing skills issues 🥩

* reformatting NFT display 🚂

* adding `/join` 🚉

* style: peth's suggestions

* added mainnet required message

* style: slight tweak of megamenu item positions

* chore(release): 0.2.0

Co-authored-by: tenfinney <scott@1box.onmicrosoft.com>
Co-authored-by: dan13ram <dan13ram@gmail.com>
Co-authored-by: vidvidvid <weetopol@gmail.com>
2022-02-28 14:06:16 -05:00

73 lines
1.9 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 { assets, error } = await res.json();
if (error) throw new Error(error);
if (!assets) throw new Error(`Received ${assets} assets`);
return assets;
};