11// Copyright 2020-2022 SubQuery Pte Ltd authors & contributors
22// SPDX-License-Identifier: Apache-2.0
33
4- import React , { useMemo , useRef , useState } from 'react' ;
4+ import React , { useEffect , useMemo , useRef , useState } from 'react' ;
55import { useSearchParams } from 'react-router-dom' ;
66import SearchOutlined from '@ant-design/icons/SearchOutlined' ;
7+ import { gql , useLazyQuery } from '@apollo/client' ;
78import ProjectCard from '@components/ProjectCard' ;
89import RpcError from '@components/RpcError' ;
910import { useProjectMetadata } from '@containers' ;
@@ -50,7 +51,7 @@ export interface UseProjectListProps {
5051 makeRedirectHref ?: ( projectId : string ) => string ;
5152}
5253
53- const pageSize = 10 ;
54+ const pageSize = 30 ;
5455
5556export const useProjectList = ( props : UseProjectListProps = { } ) => {
5657 const {
@@ -61,9 +62,81 @@ export const useProjectList = (props: UseProjectListProps = {}) => {
6162 onProjectClick,
6263 } = props ;
6364 const [ , setSearchParams ] = useSearchParams ( ) ;
64- const [ getProjects , { error } ] = useGetProjectsLazyQuery ( {
65- variables : { offset : 0 , type : [ ProjectType . SUBQUERY , ProjectType . SUBGRAPH ] } ,
66- } ) ;
65+ const [ getProjects , { error } ] = useLazyQuery (
66+ gql `
67+ query GetProjects(
68+ $offset: Int
69+ $type: [ProjectType!] = [SUBQUERY, RPC]
70+ $orderBy: [ProjectsOrderBy!] = ID_ASC
71+ $ids: [String!] = [""]
72+ ${ account ? '$account: String' : '' }
73+ $now: Datetime = "1970-01-01T00:00:00"
74+ ) {
75+ projects(first: ${ pageSize } , offset: $offset, orderBy: $orderBy, filter: { ${
76+ account ? `owner: { equalTo: $account },` : ''
77+ } id: { notIn: $ids }, type: { in: $type } }) {
78+ totalCount
79+ nodes {
80+ id
81+ owner
82+ metadata
83+ deploymentMetadata
84+ deploymentId
85+ updatedTimestamp
86+ createdTimestamp
87+ type
88+ deployments {
89+ nodes {
90+ id
91+ serviceAgreements {
92+ aggregates {
93+ sum {
94+ lockedAmount
95+ }
96+ }
97+ totalCount
98+ }
99+ serviceAgreementsActive: serviceAgreements(
100+ filter: { endTime: { greaterThanOrEqualTo: $now } }
101+ ) {
102+ totalCount
103+ }
104+ indexers(
105+ filter: { indexer: { active: { equalTo: true } }, status: { notEqualTo: TERMINATED } }
106+ ) {
107+ totalCount
108+ }
109+ deploymentBoosterSummaries {
110+ groupedAggregates(groupBy: PROJECT_ID) {
111+ sum {
112+ totalAmount
113+ }
114+ }
115+ }
116+
117+ deploymentBoosterSummariesByDeploymentId: deploymentBoosterSummaries {
118+ groupedAggregates(groupBy: DEPLOYMENT_ID) {
119+ sum {
120+ totalAmount
121+ }
122+ keys
123+ }
124+ }
125+ }
126+ }
127+ totalReward
128+ }
129+ }
130+ }
131+ ` ,
132+ {
133+ variables : {
134+ offset : 0 ,
135+ type : [ ProjectType . SUBQUERY , ProjectType . SUBGRAPH ] ,
136+ account : account || undefined ,
137+ } ,
138+ } ,
139+ ) ;
67140
68141 const [ getProject , { error : topError , loading : topLoading } ] = useGetProjectLazyQuery ( ) ;
69142
@@ -86,19 +159,7 @@ export const useProjectList = (props: UseProjectListProps = {}) => {
86159 const [ showPublishModal , setShowPublishModal ] = React . useState ( false ) ;
87160 const [ orderBy , setOrderBy ] = React . useState ( ProjectsOrderBy . TOTAL_REWARD_DESC ) ;
88161 const { getProjectBySearch } = useLocalProjects ( ) ;
89-
90- // const loadTopProject = async () => {
91- // // this is a hard code, for kepler-network project.
92- // // we want to top it.
93- // const res = await getProject({
94- // variables: {
95- // id: '0x06',
96- // },
97- // });
98- // if (res.data?.project) {
99- // setTopProject(res.data?.project);
100- // }
101- // };
162+ const [ previouseAccount , setPreviouseAccount ] = useState ( account ) ;
102163
103164 const loadMore = async ( options ?: {
104165 refresh ?: boolean ;
@@ -134,6 +195,7 @@ export const useProjectList = (props: UseProjectListProps = {}) => {
134195 const params = isSearch
135196 ? {
136197 offset : options ?. refresh ? 0 : fetchedProejcts . current . length ,
198+ account : account || undefined ,
137199 ...searchParams ,
138200 }
139201 : {
@@ -157,7 +219,6 @@ export const useProjectList = (props: UseProjectListProps = {}) => {
157219 // filter once or twice is the same.
158220 const nonEmptyProjects = res . data . projects ?. nodes . filter ( notEmpty ) . filter ( notEmpty ) ;
159221 const mergered = options ?. refresh ? [ ...nonEmptyProjects ] : [ ...fetchedProejcts . current , ...nonEmptyProjects ] ;
160- // TODO: filter by backend.
161222 setProjects ( mergered . filter ( ( proj ) => ( account ? account . toLowerCase ( ) === proj . owner . toLowerCase ( ) : true ) ) ) ;
162223 fetchedProejcts . current = mergered ;
163224 updatedLength = mergered . length ;
@@ -414,9 +475,22 @@ export const useProjectList = (props: UseProjectListProps = {}) => {
414475 loading ,
415476 projects ,
416477 orderBy ,
478+ account ,
417479 onProjectClick ,
418480 ] ) ;
419481
482+ useEffect ( ( ) => {
483+ if ( account && account !== previouseAccount ) {
484+ setProjects ( [ ] ) ;
485+ loadMore ( {
486+ refresh : true ,
487+ } ) . then ( ( res ) => {
488+ mutate ( res ) ;
489+ } ) ;
490+ setPreviouseAccount ( account ) ;
491+ }
492+ } , [ account ] ) ;
493+
420494 return {
421495 listsWithSearch,
422496 loading,
0 commit comments