From 6a0b577aebd87725ed87bc3cf07a7506441de936 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Wed, 5 Oct 2022 00:54:38 -0500 Subject: [PATCH] Small home refactor --- web/pages/home/index.tsx | 55 ++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/web/pages/home/index.tsx b/web/pages/home/index.tsx index 9e5314a5..7b3f79d2 100644 --- a/web/pages/home/index.tsx +++ b/web/pages/home/index.tsx @@ -21,7 +21,6 @@ import { Group } from 'common/group' import { SiteLink } from 'web/components/site-link' import { usePrivateUser, useUser } from 'web/hooks/use-user' import { - useMemberGroupIds, useMemberGroupsSubscription, useTrendingGroups, } from 'web/hooks/use-group' @@ -80,6 +79,7 @@ export default function Home() { const dailyTrendingContracts = useContractsByDailyScoreNotBetOn(user?.id, 6) const groups = useMemberGroupsSubscription(user) + const trendingGroups = useTrendingGroups() const groupContracts = useContractsByDailyScoreGroups( groups?.map((g) => g.slug) ) @@ -113,16 +113,26 @@ export default function Home() { ) : ( <> - {renderSections(user, sections, { + {renderSections(sections, { score: trendingContracts, newest: newContracts, 'daily-trending': dailyTrendingContracts, 'daily-movers': dailyMovers, })} - - - {renderGroupSections(user, groups, groupContracts)} + {groups && groupContracts && trendingGroups.length > 0 ? ( + <> + + {renderGroupSections(user, groups, groupContracts)} + + ) : ( + + )} )} @@ -171,7 +181,6 @@ export const getHomeItems = (sections: string[]) => { } function renderSections( - user: User, sections: { id: string; label: string }[], sectionContracts: { 'daily-movers': CPMMBinaryContract[] @@ -192,7 +201,7 @@ function renderSections( } if (id === 'daily-trending') { return ( - ) })} @@ -216,13 +225,9 @@ function renderSections( function renderGroupSections( user: User, - groups: Group[] | undefined, - groupContracts: Dictionary | undefined + groups: Group[], + groupContracts: Dictionary ) { - if (!groups || !groupContracts) { - return - } - const filteredGroups = groups.filter((g) => groupContracts[g.slug]) const orderedGroups = sortBy(filteredGroups, (g) => // Sort by sum of top two daily scores. @@ -285,7 +290,7 @@ function SectionHeader(props: { ) } -function ContractsSection(props: { +function SearchSection(props: { label: string contracts: CPMMBinaryContract[] sort: Sort @@ -406,15 +411,16 @@ function DailyStats(props: { } export function TrendingGroupsSection(props: { - user: User | null | undefined + user: User + myGroups: Group[] + trendingGroups: Group[] className?: string }) { - const { user, className } = props - const memberGroupIds = useMemberGroupIds(user) || [] + const { user, myGroups, trendingGroups, className } = props - const groups = useTrendingGroups().filter( - (g) => !memberGroupIds.includes(g.id) - ) + const myGroupIds = new Set(myGroups.map((g) => g.id)) + + const groups = trendingGroups.filter((g) => !myGroupIds.has(g.id)) const count = 20 const chosenGroups = groups.slice(0, count) @@ -433,10 +439,9 @@ export function TrendingGroupsSection(props: { { - if (!user) return - if (memberGroupIds.includes(g.id)) leaveGroup(g, user?.id) + if (myGroupIds.has(g.id)) leaveGroup(g, user.id) else { const homeSections = (user.homeSections ?? []) .filter((id) => id !== g.id)