@mojito-inc/secondary-market
Version:
Mojito secondary market is the platform to purchase NFT.
2 lines (1 loc) • 4.89 kB
JavaScript
import{useState as l,useMemo as e,useCallback as t,useEffect as i}from"react";import{useNFT as o}from"@mojito-inc/core-service";import{ButtonType as n}from"../constant/index.js";import{getPrice as r}from"../utils/getPrice.utils.js";import{getValidURL as a}from"../utils/getValidURL.utils.js";var d;!function(l){l.RECENTLY_LISTED="recently_listed",l.RECENTLY_MINTED="recently_minted",l.INITIALLY_MINTED="initially_minted",l.PRICE_LOW_TO_HIGH="price_low_to_high",l.PRICE_HIGH_TO_LOW="price_high_to_low"}(d||(d={}));const u=d=>{const{getAllRegistryTokensV2:u,getCollectionTraits:c}=o(),[s,v]=l(!1),[I,E]=l(null),[m,p]=l(0),[_,D]=l(!1),[T,y]=l([]),[f,L]=l(!1),[N,g]=l(!1),A=e((()=>d.walletAddress),[d.walletAddress]),C=t((()=>{D(!0)}),[]),O=t(((l,e,t,i)=>{const o=(null==l?void 0:l.toLowerCase())===(null==A?void 0:A.toLowerCase());if(A){if(e&&o)return"OPEN_FOR_SALE"===e?n.LIST_ITEM:"SALE_PENDING"===e?n.EDIT_LIST_ITEM:n.ENQUIRE;if(("OPEN_FOR_SALE"===e||"SALE_PENDING"===e)&&!o)return n.MAKE_OFFER}return"SALE_PENDING"===e?n.BUY_NOW:n.MAKE_OFFER}),[A]),R=t(((l,e,t)=>{const i=(null==l?void 0:l.toLowerCase())===(null==A?void 0:A.toLowerCase());return"SALE_PENDING"===e&&!i&&t?n.BUY_NOW:n.ENQUIRE}),[A]),k=t(((l,e,t)=>{if(l){return r(t,l,e)}return{price:[],label:""}}),[]),w=t(((l,e,t,i,o)=>{const r=R(l,e,t),a=O(l,e,i,o);return r===n.BUY_NOW?"Buy Now":a===n.EDIT_LIST_ITEM?"View Listing":a===n.MAKE_OFFER?"Make Offer":"List Item"}),[R,O]),M=t((async()=>{var l,e,t,i,o;try{v(!0),D(!1),E(null);const n=await u({orgID:d.orgID,marketplaceID:d.marketplaceID,limit:null==d?void 0:d.limit,offset:null==d?void 0:d.offset,searchKey:null==d?void 0:d.search,filters:null==d?void 0:d.filters,buyerAddress:null==d?void 0:d.walletAddress,filterByMetadata:null==d?void 0:d.metaDataFilters,registryID:null==d?void 0:d.selectedCollection,currency:null==d?void 0:d.currencyType},{fetchPolicy:"no-cache"}),r=null===(e=null===(l=n.data)||void 0===l?void 0:l.getAllRegistryTokensV2)||void 0===e?void 0:e.data,c=null==r?void 0:r.map((l=>{var e,t,i,o,n,r,u,c,s,v,I,E,m,p,_,D,T,y,f,L,N,g;const A=w(null!==(e=l.TokenOwnerAddress)&&void 0!==e?e:"",null!==(t=l.Status)&&void 0!==t?t:"",l.isBuyNowEnabled,l.isMakeOfferEnabled,l.isOfferExist),C=k(l.Price,null!==(o=null===(i=l.listedOrderInfo)||void 0===i?void 0:i.price)&&void 0!==o?o:[],A);return{cardDetail:{tokenImage:a(l.mediaSourceURL||l.MetaData.image||""),thumbnailImage:a(l.MetaData.image||""),tokenName:null!==(n=l.MetaData.name)&&void 0!==n?n:"",tokenSubtitle:null!==(r=l.MetaData.description)&&void 0!==r?r:"",collectionName:null!==(u=l.Artist.artistName)&&void 0!==u?u:"",collectionType:"solana"===(null==d?void 0:d.walletType)?"SOLANA":"ERC721"===l.TokenType?"ERC-721":"ERC-1155",cryptoPrice:null!==(v=null===(s=null===(c=null==C?void 0:C.price)||void 0===c?void 0:c[1])||void 0===s?void 0:s.value)&&void 0!==v?v:0,usdPrice:null!==(m=null===(E=null===(I=null==C?void 0:C.price)||void 0===I?void 0:I[0])||void 0===E?void 0:E.value)&&void 0!==m?m:0,priceLabel:null!==(_=null===(p=null==C?void 0:C.label)||void 0===p?void 0:p.toUpperCase())&&void 0!==_?_:"",cryptoLabel:"solana"===(null==d?void 0:d.walletType)?"VIN":null!==(y=null===(T=null===(D=null==C?void 0:C.price)||void 0===D?void 0:D[1])||void 0===T?void 0:T.unit)&&void 0!==y?y:"ETH",buttonName:A},id:null!==(f=l.NFTTokenID)&&void 0!==f?f:"",tokenId:null!==(N=null===(L=l.TokenID)||void 0===L?void 0:L.toString())&&void 0!==N?N:"",contractAddress:null!==(g=l.Registry.ContractAddress)&&void 0!==g?g:""}}));return E(null!=c?c:[]),p(null!==(o=null===(i=null===(t=null==n?void 0:n.data)||void 0===t?void 0:t.getAllRegistryTokensV2)||void 0===i?void 0:i.totalCount)&&void 0!==o?o:0),v(!1),c}catch(l){return D(!1),v(!1),E(null),null}}),[null==d?void 0:d.filters,null==d?void 0:d.limit,d.marketplaceID,null==d?void 0:d.offset,d.orgID,null==d?void 0:d.search,null==d?void 0:d.walletAddress,null==d?void 0:d.metaDataFilters,null==d?void 0:d.selectedCollection,u,null==d?void 0:d.walletType,w,null==d?void 0:d.currencyType,k]),h=t((()=>{g(!0)}),[]),S=t((async()=>{var l,e;try{g(!1),L(!0),y([]);const t=null===(e=(await c({orgID:d.orgID,marketplaceID:d.marketplaceID,registryID:null!==(l=d.selectedCollection)&&void 0!==l?l:void 0},{fetchPolicy:"no-cache"})).data)||void 0===e?void 0:e.GetCollectionTraitforSMP,i=null==t?void 0:t.map((l=>({title:l.traitType,type:l.dataType,values:l.traitValues.map((l=>{var e;return{title:l.traitValue,value:l.traitValue,count:null!==(e=l.count)&&void 0!==e?e:0}}))})));return y(null!=i?i:[]),L(!1),i}catch(l){return g(!1),L(!1),y([]),null}}),[d.marketplaceID,d.orgID,c,d.selectedCollection]);return i((()=>{_&&(M(),D(!1))}),[_,M]),i((()=>{N&&(g(!1),S())}),[N,S,d.selectedCollection]),{collectionsLoading:s,traitsLoading:f,collectionsData:I,totalCollections:m,traitsData:T,fetchCollection:C,fetchMetaDataDetails:h}};export{d as RegistryTokenFilterInput,u as useCollections};