11import { Spinner } from "@chakra-ui/react" ;
2+ import { BaseDonorValues , useDataLayer } from "data-layer" ;
23import { useEffect , useState } from "react" ;
34import { useSelector } from "react-redux" ;
45import { useParams } from "react-router-dom" ;
56import { RootState } from "../../../reducers" ;
67import { ProjectStats } from "../../../reducers/projects" ;
78import RoundDetailsCard from "./RoundDetailsCard" ;
89import StatCard from "./StatCard" ;
10+ import { allChains } from "../../../utils/wagmi" ;
911
1012export const slugify = ( input : string ) : string =>
1113 input
@@ -19,9 +21,12 @@ export default function RoundStats() {
1921 const NAText = "N/A" ;
2022
2123 const params = useParams ( ) ;
24+ const dataLayer = useDataLayer ( ) ;
2225 const [ details , setDetails ] = useState < any > ( [ ] ) ;
2326 const [ allTimeStats , setAllTimeStats ] = useState < any > ( {
2427 allTimeReceived : 0 ,
28+ totalCrowdFunding : 0 ,
29+ totalDirectDonations : 0 ,
2530 allTimeContributions : 0 ,
2631 roundsLength : 0 ,
2732 } ) ;
@@ -41,57 +46,101 @@ export default function RoundStats() {
4146 } ) ;
4247
4348 useEffect ( ( ) => {
44- const detailsTmp : any [ ] = [ ] ;
45- let allTime = {
46- allTimeReceived : 0 ,
47- allTimeContributions : 0 ,
48- roundsLength : 0 ,
49- } ;
49+ const fetch = async ( ) => {
50+ const detailsTmp : any [ ] = [ ] ;
51+ let allTime = {
52+ allTimeReceived : 0 ,
53+ totalCrowdFunding : 0 ,
54+ totalDirectDonations : 0 ,
55+ allTimeContributions : 0 ,
56+ roundsLength : 0 ,
57+ } ;
58+
59+ let totalDirectDonations = 0 ;
60+ let totalDirectDonationCount = 0 ;
5061
51- if ( props . stats ?. length > 0 ) {
52- props . stats . forEach ( ( stat ) => {
53- allTime = {
54- allTimeReceived :
55- allTime . allTimeReceived + ( stat . success ? stat . fundingReceived : 0 ) ,
56- allTimeContributions :
57- allTime . allTimeContributions +
58- ( stat . success ? stat . totalContributions : 0 ) ,
59- roundsLength : props . stats . length ,
60- } ;
61-
62- const newStat = { ...stat } ;
63- if ( newStat . uniqueContributors > 0 || newStat . totalContributions > 0 ) {
64- if ( newStat . fundingReceived === 0 ) newStat . fundingReceived = NA ;
65- if ( newStat . avgContribution === 0 ) newStat . avgContribution = NA ;
66- }
67-
68- if ( props . rounds [ stat . roundId ] ?. round ?. programName ) {
69- detailsTmp . push ( {
70- round : props . rounds [ stat . roundId ] . round ,
71- stats : { ...newStat } ,
62+ try {
63+ const directDonations : BaseDonorValues [ ] =
64+ await dataLayer . getDirectDonationsByProjectId ( {
65+ projectId : props . projectID ,
66+ chainIds : allChains . map ( ( chain ) => chain . id ) ,
7267 } ) ;
73- }
74- } ) ;
75- }
7668
77- setAllTimeStats ( allTime ) ;
78- setDetails ( detailsTmp ) ;
79- } , [ props . stats , props . rounds ] ) ;
69+ directDonations . forEach ( ( donation ) => {
70+ totalDirectDonations += donation . totalAmountDonatedInUsd ;
71+ totalDirectDonationCount += donation . totalDonationsCount ;
72+ } ) ;
73+ } catch ( e ) {
74+ console . error ( "Error fetching direct donations" , e ) ;
75+ }
76+
77+ if ( props . stats ?. length > 0 ) {
78+ props . stats . forEach ( ( stat ) => {
79+ allTime = {
80+ ...allTime ,
81+ totalCrowdFunding :
82+ allTime . totalCrowdFunding +
83+ ( stat . success ? stat . fundingReceived : 0 ) ,
84+ allTimeContributions :
85+ allTime . allTimeContributions +
86+ ( stat . success ? stat . totalContributions : 0 ) ,
87+ roundsLength : props . stats . length ,
88+ } ;
89+
90+ const newStat = { ...stat } ;
91+ if (
92+ newStat . uniqueContributors > 0 ||
93+ newStat . totalContributions > 0
94+ ) {
95+ if ( newStat . fundingReceived === 0 ) newStat . fundingReceived = NA ;
96+ if ( newStat . avgContribution === 0 ) newStat . avgContribution = NA ;
97+ }
98+
99+ if ( props . rounds [ stat . roundId ] ?. round ?. programName ) {
100+ detailsTmp . push ( {
101+ round : props . rounds [ stat . roundId ] . round ,
102+ stats : { ...newStat } ,
103+ } ) ;
104+ }
105+ } ) ;
106+ }
107+
108+ allTime . totalDirectDonations = totalDirectDonations ;
109+ allTime . allTimeReceived =
110+ allTime . totalCrowdFunding + totalDirectDonations ;
111+ allTime . allTimeContributions += totalDirectDonationCount ;
112+
113+ setAllTimeStats ( allTime ) ;
114+ setDetails ( detailsTmp ) ;
115+ } ;
116+ if ( props . projectID ) fetch ( ) ;
117+ } , [ props . stats , props . rounds , props . projectID , dataLayer ] ) ;
80118
81119 const section = (
82120 description : any ,
83121 container : any ,
84122 pt : boolean ,
85- key : string
123+ key : string ,
124+ shortRow ?: boolean
86125 ) => (
87126 < div
88127 key = { key }
89- className = { `grid md:grid-cols-7 sm:grid-cols-1 border-b border-gitcoin-grey-100 pb-10 ${
90- pt && "pt-10 "
91- } `}
128+ className = { `grid ${
129+ shortRow ? "md:grid-cols-8" : "md:grid-cols-7 "
130+ } sm:grid-cols-1 border-b border-gitcoin-grey-100 pb-10 ${ pt && "pt-10" } `}
92131 >
93- < div className = "md:col-span-2" > { description } </ div >
94- < div className = "md:col-span-4 sm:col-span-1 md:flex space-between" >
132+ < div
133+ className = { `${
134+ shortRow ? "md:col-span-2" : "md:col-span-1"
135+ } flex items-center justify-start`}
136+ >
137+ { description }
138+ </ div >
139+ < div
140+ className = { `${
141+ shortRow ? "md:col-span-5" : "md:col-span-6"
142+ } sm:col-span-1 md:flex space-between`}
143+ >
95144 { container }
96145 </ div >
97146 < div className = "md:col-span-1 sm:col-span-1" />
@@ -131,6 +180,18 @@ export default function RoundStats() {
131180 bg = "gitcoin-violet-100"
132181 tooltip = "The estimated funding received by this project. This number is not final and may change based on updated data."
133182 />
183+ < StatCard
184+ heading = "Est. Crowdfunding Received"
185+ value = { `$${ allTimeStats . totalCrowdFunding . toFixed ( 2 ) } ` }
186+ bg = "gitcoin-violet-100"
187+ tooltip = "The number of rounds this project has participated in."
188+ />
189+ < StatCard
190+ heading = "Total Direct Donations"
191+ value = { `$${ allTimeStats . totalDirectDonations . toFixed ( 2 ) } ` }
192+ bg = "gitcoin-violet-100"
193+ tooltip = "The amount of contributions this project has received as direct donations."
194+ />
134195 < StatCard
135196 heading = "No. of Contributions"
136197 value = { allTimeStats . allTimeContributions }
@@ -193,7 +254,8 @@ export default function RoundStats() {
193254 />
194255 </ > ,
195256 true ,
196- `details-${ index } `
257+ `details-${ index } ` ,
258+ true
197259 )
198260 ) }
199261 </ >
0 commit comments