Skip to content

Commit

Permalink
feat: improve top section trending projects (#4190)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk authored Jan 12, 2024
1 parent 4e6592c commit 05b4232
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
46 changes: 40 additions & 6 deletions src/components/Home/TopSection/TopSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import { SectionHeading } from 'components/Home/SectionHeading'
import { ProjectTag } from 'components/ProjectTags/ProjectTag'
import { XLButton } from 'components/buttons/XLButton'
import { HOMEPAGE } from 'constants/fathomEvents'
import { useTrendingProjects } from 'hooks/useProjects'
import { PV_V1 } from 'constants/pv'
import {
DEFAULT_TRENDING_PROJECTS_LIMIT,
useDBProjectsQuery,
useTrendingProjects,
} from 'hooks/useProjects'
import { trackFathomGoal } from 'lib/fathom'
import { ProjectTagName } from 'models/project-tags'
import Link from 'next/link'

const TRENDING_PROJECTS_LIMIT = 10
import { getSubgraphIdForProject } from 'utils/graph'

const HEADER_TAGS: ProjectTagName[] = [
'dao',
Expand All @@ -26,10 +30,40 @@ const HEADER_TAGS: ProjectTagName[] = [
'business',
]

// These projects will render if there isn't enough trending projects.
const BACKUP_PROJECTS = [
getSubgraphIdForProject(PV_V1, 199), // moondao
getSubgraphIdForProject(PV_V1, 36), // constituiondao
getSubgraphIdForProject(PV_V1, 323), // assange
// add more as needed
]

export function TopSection() {
const { data: trendingProjects, isLoading } = useTrendingProjects(
TRENDING_PROJECTS_LIMIT,
DEFAULT_TRENDING_PROJECTS_LIMIT,
)
const { data: backupProjects } = useDBProjectsQuery({
ids: BACKUP_PROJECTS,
})

const remainderProjectCount =
DEFAULT_TRENDING_PROJECTS_LIMIT - (trendingProjects?.length ?? 0)

const renderBackup =
trendingProjects && backupProjects && remainderProjectCount
? backupProjects
.slice(0, remainderProjectCount)
.filter(
p =>
!trendingProjects
.map(proj => proj.projectId)
.includes(p.projectId),
)
: []

const renderProjects = trendingProjects
? [...trendingProjects, ...renderBackup]
: undefined

return (
<SectionContainer className="pt-6 pb-24 md:px-0 md:pt-10">
Expand Down Expand Up @@ -87,9 +121,9 @@ export function TopSection() {
</XLButton>
</Link>
</div>
{!isLoading && trendingProjects ? (
{!isLoading && renderProjects ? (
<ProjectCarousel
items={trendingProjects?.map(p => (
items={renderProjects?.map(p => (
<HomepageProjectCard project={p} key={p.id} />
))}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { HistoricalConfigurationPanel } from './HistoricalConfigurationPanel'

export const HistorySubPanel = () => {
const { projectId } = useProjectMetadata()
const { fundingCycle } = useProjectContext()

const [isFetchingMore, setIsFetchingMore] = useState<boolean>()
const { data, fetchMore, loading, error } = usePastFundingCycles({
projectId,
currentFcNumber: fundingCycle?.number.toNumber() ?? 0,
})

const isLoading = loading || isFetchingMore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ import {
useFundingCyclesQuery,
} from 'generated/graphql'
import { client } from 'lib/apollo/client'
import { useMemo } from 'react'

const DEFAULT_PAGE_SIZE = 5

export function usePastFundingCycles({
projectId,
currentFcNumber,
pageSize,
}: {
projectId: number | undefined
currentFcNumber: number | undefined
pageSize?: number
}) {
const now = useMemo(() => Math.floor(Date.now() / 1000), [])

return useFundingCyclesQuery({
client,
variables: {
where: {
projectId,
endTimestamp_lt: now, // Only already ended
number_lt: currentFcNumber,
},
orderBy: FundingCycle_OrderBy.number,
orderDirection: OrderDirection.desc,
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export function useDBProjectsInfiniteQuery(
)
}

export const DEFAULT_TRENDING_PROJECTS_LIMIT = 10

export function useTrendingProjects(count: number) {
return useQuery(['trending-projects', count], async () => {
const res = await axios.get<Json<DBProject>[]>(
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/projects/trending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const handler: NextApiHandler = async (req, res) => {
query: TrendingProjectsDocument,
variables: {
where: {
// trendingScore_gt: '0', // Turned off because there wasn't sufficient number of projects to fulfill `first`.
trendingScore_gt: '0' as any, // eslint-disable-line @typescript-eslint/no-explicit-any
...(ARCHIVED_SUBGRAPH_IDS.length
? { id_not_in: ARCHIVED_SUBGRAPH_IDS }
: {}), // `id_not_in: <empty-array>` will return 0 results
Expand Down

2 comments on commit 05b4232

@vercel
Copy link

@vercel vercel bot commented on 05b4232 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 05b4232 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.