11"use client" ;
22
3- import { useCallback , useEffect , useRef , useState } from "react" ;
3+ import { useState } from "react" ;
44import { usePathname } from "next/navigation" ;
5- import { ErrorComponent } from "../../../components/punk-society/ErrorComponent " ;
6- import { LoadingBars } from "../../../components/punk-society/LoadingBars " ;
7- import { NewsFeed } from "../../../components/punk-society/NewsFeed " ;
5+ import BookmarkedArticles from "../_components/BookmarkedArticles " ;
6+ import BoughtArticles from "../_components/BoughtArticles " ;
7+ import ListedArticles from "../_components/ListedArticles " ;
88import ProfileInfo from "../_components/ProfileInfo" ;
99import { NextPage } from "next" ;
10- import { useScaffoldEventHistory } from "~~/hooks/scaffold-eth" ;
11- import { notification } from "~~/utils/scaffold-eth" ;
12- import { getMetadataFromIPFS } from "~~/utils/simpleNFT/ipfs-fetch" ;
1310import { NFTMetaData } from "~~/utils/simpleNFT/nftsMetadata" ;
1411
1512export interface Post extends Partial < NFTMetaData > {
@@ -24,120 +21,20 @@ export interface Post extends Partial<NFTMetaData> {
2421}
2522
2623const ProfilePage : NextPage = ( ) => {
27- const [ articles , setArticles ] = useState < Post [ ] > ( [ ] ) ;
28- const [ loading , setLoading ] = useState ( true ) ;
29- const [ loadingMore , setLoadingMore ] = useState ( true ) ;
30- const [ page , setPage ] = useState ( 1 ) ; // Start from page 1 to get the last post first
3124 const [ activeTab , setActiveTab ] = useState ( "Listed" ) ;
3225
3326 const handleTabClick = ( tab : any ) => {
3427 setActiveTab ( tab ) ;
3528 } ;
3629
37- const observer = useRef < IntersectionObserver | null > ( null ) ;
38-
3930 const pathname = usePathname ( ) ;
4031 const address = pathname . split ( "/" ) . pop ( ) ;
4132
42- const {
43- data : createEvents ,
44- // isLoading: createIsLoadingEvents,
45- error : createErrorReadingEvents ,
46- } = useScaffoldEventHistory ( {
47- contractName : "BasedShop" ,
48- eventName : "ArticleCreated" ,
49- fromBlock : 0n ,
50- watch : true ,
51- } ) ;
52-
53- const fetchArticles = useCallback (
54- async ( page : number ) => {
55- if ( ! createEvents ) return ;
56-
57- setLoadingMore ( true ) ;
58- try {
59- // Calculate the start and end indices for the current page
60- const start = ( page - 1 ) * 8 ;
61- const end = page * 8 ;
62- const eventsToFetch = createEvents . slice ( start , end ) ;
63-
64- const articlesUpdate : Post [ ] = [ ] ;
65-
66- for ( const event of eventsToFetch ) {
67- try {
68- const { args } = event ;
69- const user = args ?. user ;
70- const tokenURI = args ?. tokenURI ;
71- const date = args ?. date ;
72- const price = args ?. price ;
73- const amount = args ?. amount ;
74-
75- if ( args ?. user !== address ) continue ;
76- if ( ! tokenURI ) continue ;
77-
78- const ipfsHash = tokenURI . replace ( "https://ipfs.io/ipfs/" , "" ) ;
79- const nftMetadata : NFTMetaData = await getMetadataFromIPFS ( ipfsHash ) ;
80-
81- // Temporary fix for V1
82- // Check if the image attribute is valid and does not point to [object Object]
83- if ( nftMetadata . image === "https://ipfs.io/ipfs/[object Object]" ) {
84- console . warn ( `Skipping post with invalid image URL: ${ nftMetadata . image } ` ) ;
85- continue ;
86- }
87-
88- articlesUpdate . push ( {
89- listingId : undefined ,
90- uri : tokenURI ,
91- user : user || "" ,
92- date : date ?. toString ( ) || "" ,
93- price : price ?. toString ( ) || "" ,
94- amount : amount ?. toString ( ) || "" ,
95- ...nftMetadata ,
96- } ) ;
97- } catch ( e ) {
98- notification . error ( "Error fetching articles" ) ;
99- console . error ( e ) ;
100- }
101- }
102-
103- setArticles ( prevArticles => [ ...prevArticles , ...articlesUpdate ] ) ;
104- } catch ( error ) {
105- notification . error ( "Failed to load articles" ) ;
106- } finally {
107- setLoadingMore ( false ) ;
108- }
109- } ,
110- [ createEvents , address ] ,
111- ) ;
112-
113- useEffect ( ( ) => {
114- setLoading ( true ) ;
115- fetchArticles ( page ) . finally ( ( ) => setLoading ( false ) ) ;
116- } , [ page , fetchArticles ] ) ;
117-
118- const lastPostElementRef = useCallback (
119- ( node : any ) => {
120- if ( loadingMore ) return ;
121- if ( observer . current ) observer . current . disconnect ( ) ;
122- observer . current = new IntersectionObserver ( entries => {
123- if ( entries [ 0 ] . isIntersecting ) {
124- setPage ( prevPage => prevPage + 1 ) ;
125- }
126- } ) ;
127- if ( node ) observer . current . observe ( node ) ;
128- } ,
129- [ loadingMore ] ,
130- ) ;
131-
13233 // Ensure the address is available before rendering the component
13334 if ( ! address ) {
13435 return < p > Inexistent address, try again...</ p > ;
13536 }
13637
137- if ( createErrorReadingEvents ) {
138- return < ErrorComponent message = { createErrorReadingEvents ?. message || "Error loading events" } /> ;
139- }
140-
14138 return (
14239 < >
14340 < ProfileInfo address = { address } />
@@ -146,16 +43,10 @@ const ProfilePage: NextPage = () => {
14643 < button className = { `tab ${ activeTab === "Listed" ? "active" : "" } ` } onClick = { ( ) => handleTabClick ( "Listed" ) } >
14744 Listed
14845 </ button >
149- < button
150- className = { `tab text-red-600 ${ activeTab === "Bought" ? "active" : "" } ` }
151- onClick = { ( ) => handleTabClick ( "Bought" ) }
152- >
46+ < button className = { `tab ${ activeTab === "Bought" ? "active" : "" } ` } onClick = { ( ) => handleTabClick ( "Bought" ) } >
15347 Bought
15448 </ button >
155- < button
156- className = { `tab text-red-600 ${ activeTab === "Saved" ? "active" : "" } ` }
157- onClick = { ( ) => handleTabClick ( "Saved" ) }
158- >
49+ < button className = { `tab 0 ${ activeTab === "Saved" ? "active" : "" } ` } onClick = { ( ) => handleTabClick ( "Saved" ) } >
15950 Saved
16051 </ button >
16152 < button
@@ -171,15 +62,9 @@ const ProfilePage: NextPage = () => {
17162 Revenue
17263 </ button >
17364 </ div >
174- { loading && page === 1 ? (
175- < LoadingBars />
176- ) : articles . length === 0 ? (
177- < p > This user has no articles</ p >
178- ) : (
179- < NewsFeed articles = { articles } />
180- ) }
181- < div ref = { lastPostElementRef } > </ div >
182- { page !== 1 && loadingMore && < LoadingBars /> }
65+ { activeTab === "Listed" && < ListedArticles /> }
66+ { activeTab === "Bought" && < BoughtArticles /> }
67+ { activeTab === "Saved" && < BookmarkedArticles /> }
18368 </ div >
18469 </ >
18570 ) ;
0 commit comments