import React, { useEffect, useState } from 'react' import axios from 'axios' import { Box, TapArea, Button, Text, Link, Image} from 'gestalt' import { HeaderText } from 'geru-components' import {resolveToWalletAddress, getParsedNftAccountsByOwner,} from "@nfteyez/sol-rayz"; import InfiniteScroll from 'react-infinite-scroller' import { useEditor } from "@layerhub-io/react" import Masonry from 'react-masonry-component' import { Scrollbars } from 'react-custom-scrollbars' const loading_nfts_text = 'Fetching NFTs...'; const wallet_empty_text = 'No nfts to display'; const PhantomWallet = () => { //(window as any).global = window; const editor = useEditor() const [nfts, setNFTs] = useState([]) const [fetching, setFetching] = useState(false) const [provider, setProvider] = useState( undefined ); const [walletKey, setWalletKey] = useState( undefined ); useEffect(() => { const provider = getProvider(); if (provider) setProvider(provider); else setProvider(undefined); }, []); //useEffect(() => { // getNfts() //}, [walletKey]) //const getNfts = async () => { // setFetching(true) // try { // const address = walletKey; // const publicAddress = await resolveToWalletAddress({ // text: address // }); // const nftArray = await getParsedNftAccountsByOwner({ // publicAddress, // }); // nftArray.map(async nft => { // const solNFTs = await axios.get(nft.data.uri).then(res => { // if(res) { // let nfts = {} // Object.assign(nfts, { // name: res.data.name, // description: res.data.description, // image: res.data.image, // }) // return nfts // } // }); // setNFTs(nfts => [...nfts, solNFTs] ); // }) // } catch (error) { // console.log("Error thrown, while fetching NFTs", error.message); // } //} const getProvider = (): PhantomProvider | undefined => { if ("solana" in window) { // @ts-ignore const provider = window.solana as any; if (provider.isPhantom) return provider as PhantomProvider; } }; const connectWallet = async () => { // @ts-ignore const { solana } = window; if (solana) { try { const response = await solana.connect(); console.log("wallet account ", response.publicKey.toString()); setWalletKey(response.publicKey.toString()); } catch (err) { // { code: 4001, message: 'User rejected the request.' } } } }; const disconnectWallet = async () => { // @ts-ignore const { solana } = window; if (walletKey && solana) { await (solana as PhantomProvider).disconnect(); setWalletKey(undefined); } }; const addImageToCanvas = item => { const options = { type: 'StaticImage', metadata: { src: item.image }, } editor.objects.add(options) } //// detect phantom provider exists return ( Phantom {/*{provider && walletKey && ( Connected )}*/} {/* { walletKey && nfts.length > 0 ? ( { nfts.map((item, index) => ( {item.name} addImageToCanvas(item)}> {item.image} )) } ) : null } */} {/**/} {/* {!provider && ( No provider found. Install The Phantom Browser extension )} */} ) } type DisplayEncoding = "utf8" | "hex"; type PhantomEvent = "disconnect" | "connect" | "accountChanged"; type PhantomRequestMethod = | "connect" | "disconnect" | "signTransaction" | "signAllTransactions" | "signMessage"; interface ConnectOpts { onlyIfTrusted: boolean; } interface PhantomProvider { publicKey: any | null; isConnected: boolean | null; signTransaction: (transaction: any) => Promise; signAllTransactions: (transactions: any[]) => Promise; signMessage: ( message: Uint8Array | string, display?: DisplayEncoding ) => Promise; connect: (opts?: Partial) => Promise<{ publicKey: any }>; disconnect: () => Promise; on: (event: PhantomEvent, handler: (args: any) => void) => void; request: (method: PhantomRequestMethod, params: any) => Promise; } export default PhantomWallet