@@ -9,6 +9,7 @@ import { nonSigningSocialFeedClient } from "@/client-creators/socialFeedClient";
99import { PostResult } from "@/contracts-clients/teritori-social-feed/TeritoriSocialFeed.types" ;
1010import { GnoNetworkInfo , NetworkKind , parseNetworkObjectId } from "@/networks" ;
1111import { gnoZenaoNetwork } from "@/networks/gno-zenao" ;
12+ import { gnoZenaoStagingNetwork } from "@/networks/gno-zenao-staging" ;
1213import { TERITORI_FEED_ID } from "@/utils/feed/constants" ;
1314import { decodeGnoPost } from "@/utils/feed/gno" ;
1415import { extractGnoJSONResponse , extractGnoJSONString } from "@/utils/gno" ;
@@ -17,6 +18,7 @@ import { postViewToPost, postViewsFromJson } from "@/utils/zenao";
1718
1819export type FetchCommentResponse = {
1920 list : Post [ ] ;
21+ totalCount : number | undefined ;
2022} | null ;
2123
2224const combineFetchCommentPages = ( pages : FetchCommentResponse [ ] ) =>
@@ -37,17 +39,18 @@ const fetchTeritoriComments = async (
3739 networkId,
3840 } ) ;
3941
40- const subComment = await client . querySubPosts ( {
42+ const comments = await client . querySubPosts ( {
4143 count : 5 ,
4244 from : pageParam || 0 ,
4345 sort : "desc" ,
4446 identifier : parentId || "" ,
4547 } ) ;
4648
4749 return {
48- list : subComment . map ( ( subPostRes : PostResult ) =>
50+ list : comments . map ( ( subPostRes : PostResult ) =>
4951 postResultToPost ( networkId , subPostRes ) ,
5052 ) ,
53+ totalCount : comments . length ,
5154 } ;
5255} ;
5356
@@ -65,7 +68,7 @@ const fetchGnoComments = async (
6568 `GetComments(${ TERITORI_FEED_ID } , ${ parentId } , ${ offset } , ${ limit } )` ,
6669 ) ;
6770
68- const posts : Post [ ] = [ ] ;
71+ const comments : Post [ ] = [ ] ;
6972
7073 const gnoPosts = extractGnoJSONString ( output ) ;
7174 for ( const gnoPost of gnoPosts ) {
@@ -77,14 +80,16 @@ const fetchGnoComments = async (
7780 ownState : false , // FIXME: find a way to get the user's reaction state from on-chain post
7881 } ) ) ;
7982
80- posts . push ( { ...post , reactions : postReactions } ) ;
83+ comments . push ( { ...post , reactions : postReactions } ) ;
8184 }
8285
8386 return {
84- list : posts . sort ( ( p1 , p2 ) => p2 . createdAt - p1 . createdAt ) ,
87+ list : comments . sort ( ( p1 , p2 ) => p2 . createdAt - p1 . createdAt ) ,
88+ totalCount : comments . length ,
8589 } ;
8690} ;
8791
92+ const gnoZenaoCommentsLimit = 10 ;
8893const fetchGnoZenaoComments = async (
8994 selectedNetwork : GnoNetworkInfo ,
9095 parentId : string ,
@@ -94,33 +99,38 @@ const fetchGnoZenaoComments = async (
9499 if ( ! selectedNetwork . socialFeedsPkgPath ) return { list : [ ] , totalCount : 0 } ;
95100 callerAddress = callerAddress || "" ;
96101
97- const limit = 100 ; // For now hardcode to load max 100 comments
102+ const limit = gnoZenaoCommentsLimit ;
103+ const offset = pageParam * limit ;
98104 const tags = "" ;
99105 const provider = new GnoJSONRPCProvider ( selectedNetwork . endpoint ) ;
100106
101107 const output = await provider . evaluateExpression (
102108 selectedNetwork . socialFeedsPkgPath || "" ,
103- `postViewsToJSON(GetChildrenPosts("${ parentId } ", ${ pageParam * limit } , ${ limit } , "${ tags } ", "${ callerAddress } "))` ,
109+ `postViewsToJSON(GetChildrenPosts("${ parentId } ", ${ offset } , ${ limit } , "${ tags } ", "${ callerAddress } "))` ,
104110 ) ;
105111 const raw = extractGnoJSONResponse ( output ) ;
106112 const postViews = postViewsFromJson ( raw ) ;
107113 return {
108- list : postViews . map ( ( postView ) => postViewToPost ( postView ) ) ,
114+ list : postViews . map ( ( postView ) =>
115+ postViewToPost ( postView , selectedNetwork . id ) ,
116+ ) ,
117+ totalCount : postViews . length ,
109118 } ;
110119} ;
111120
112121const useFetchCommentsRaw = ( { parentId, totalCount, enabled } : ConfigType ) => {
113122 const selectedWallet = useSelectedWallet ( ) ;
123+ const [ parentNetwork , localIdentifier ] = parseNetworkObjectId ( parentId ) ;
114124
115125 const data = useInfiniteQuery < FetchCommentResponse > (
116126 [ "FetchComment" , parentId ] ,
117127 async ( { pageParam = 0 } ) => {
118- const [ parentNetwork , localIdentifier ] = parseNetworkObjectId ( parentId ) ;
119128 let comments : FetchCommentResponse ;
120129 // Gno Zenao network
121130 if (
122131 parentNetwork ?. kind === NetworkKind . Gno &&
123- parentNetwork ?. id === gnoZenaoNetwork . id
132+ ( parentNetwork ?. id === gnoZenaoNetwork . id ||
133+ parentNetwork ?. id === gnoZenaoStagingNetwork . id )
124134 ) {
125135 return fetchGnoZenaoComments (
126136 parentNetwork ,
@@ -144,13 +154,26 @@ const useFetchCommentsRaw = ({ parentId, totalCount, enabled }: ConfigType) => {
144154 return comments ;
145155 } ,
146156 {
147- getNextPageParam : ( _ , pages ) => {
148- const postsLength = combineFetchCommentPages ( pages ) . length ;
149-
150- if ( ( totalCount || 0 ) > postsLength ) {
151- return postsLength ;
157+ getNextPageParam : ( lastPage , pages ) => {
158+ if (
159+ parentNetwork ?. kind === NetworkKind . Gno &&
160+ ( parentNetwork ?. id === gnoZenaoNetwork . id ||
161+ parentNetwork ?. id === gnoZenaoStagingNetwork . id )
162+ ) {
163+ if (
164+ lastPage ?. totalCount &&
165+ lastPage . totalCount >= gnoZenaoCommentsLimit
166+ ) {
167+ return pages . length ;
168+ }
169+ } else {
170+ const postsLength = combineFetchCommentPages ( pages ) . length ;
171+
172+ if ( ( totalCount || 0 ) > postsLength ) {
173+ return postsLength ;
174+ }
175+ return null ;
152176 }
153- return null ;
154177 } ,
155178 staleTime : Infinity ,
156179 refetchOnWindowFocus : false ,
0 commit comments