import { mapValues, groupBy, sumBy, sum, sortBy, debounce, uniqBy, } from 'lodash' import { useState, useMemo } from 'react' import { charities, Charity as CharityType } from 'common/charity' import { CharityCard } from 'web/components/charity/charity-card' import { Col } from 'web/components/layout/col' import { Spacer } from 'web/components/layout/spacer' import { Page } from 'web/components/page' import { Title } from 'web/components/title' import { getAllCharityTxns } from 'web/lib/firebase/txns' import { manaToUSD } from 'common/util/format' import { quadraticMatches } from 'common/quadratic-funding' import { Txn } from 'common/txn' import { useTracking } from 'web/hooks/use-tracking' import { searchInAny } from 'common/util/parse' import { getUser } from 'web/lib/firebase/users' import { SiteLink } from 'web/components/site-link' import { User } from 'common/user' import { SEO } from 'web/components/SEO' import { Input } from 'web/components/input' export async function getStaticProps() { let txns = await getAllCharityTxns() // Sort by newest txns first txns = sortBy(txns, 'createdTime').reverse() const totals = mapValues(groupBy(txns, 'toId'), (txns) => sumBy(txns, (txn) => txn.amount) ) const totalRaised = sum(Object.values(totals)) const sortedCharities = sortBy(charities, [ (charity) => (charity.tags?.includes('Featured') ? 0 : 1), (charity) => -totals[charity.id], ]) const matches = quadraticMatches(txns, totalRaised) const numDonors = uniqBy(txns, (txn) => txn.fromId).length const mostRecentDonor = txns[0] ? await getUser(txns[0].fromId) : null const mostRecentCharity = txns[0]?.toId ?? '' return { props: { totalRaised, charities: sortedCharities, matches, txns, numDonors, mostRecentDonor, mostRecentCharity, }, revalidate: 60, } } type Stat = { name: string stat: string url?: string } function DonatedStats(props: { stats: Stat[] }) { const { stats } = props return (