Order home group sections by daily score.
This commit is contained in:
parent
3677de58c3
commit
ab883ea777
|
@ -7,12 +7,11 @@ import { Row } from 'web/components/layout/row'
|
||||||
import { Page } from 'web/components/page'
|
import { Page } from 'web/components/page'
|
||||||
import { SiteLink } from 'web/components/site-link'
|
import { SiteLink } from 'web/components/site-link'
|
||||||
import { Title } from 'web/components/title'
|
import { Title } from 'web/components/title'
|
||||||
import { useMemberGroupsSubscription } from 'web/hooks/use-group'
|
|
||||||
import { useTracking } from 'web/hooks/use-tracking'
|
import { useTracking } from 'web/hooks/use-tracking'
|
||||||
import { useUser } from 'web/hooks/use-user'
|
import { useUser } from 'web/hooks/use-user'
|
||||||
import { updateUser } from 'web/lib/firebase/users'
|
import { updateUser } from 'web/lib/firebase/users'
|
||||||
import { track } from 'web/lib/service/analytics'
|
import { track } from 'web/lib/service/analytics'
|
||||||
import { getHomeItems, TrendingGroupsSection } from '.'
|
import { getHomeItems } from '.'
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
|
@ -27,8 +26,7 @@ export default function Home() {
|
||||||
setHomeSections(newHomeSections)
|
setHomeSections(newHomeSections)
|
||||||
}
|
}
|
||||||
|
|
||||||
const groups = useMemberGroupsSubscription(user)
|
const { sections } = getHomeItems(homeSections)
|
||||||
const { sections } = getHomeItems(groups ?? [], homeSections)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Page>
|
<Page>
|
||||||
|
@ -38,14 +36,8 @@ export default function Home() {
|
||||||
<DoneButton />
|
<DoneButton />
|
||||||
</Row>
|
</Row>
|
||||||
|
|
||||||
<Col className="gap-8 md:flex-row">
|
<Col className="flex-1">
|
||||||
<Col className="flex-1">
|
<ArrangeHome sections={sections} setSectionIds={updateHomeSections} />
|
||||||
<ArrangeHome
|
|
||||||
sections={sections}
|
|
||||||
setSectionIds={updateHomeSections}
|
|
||||||
/>
|
|
||||||
</Col>
|
|
||||||
<TrendingGroupsSection className="flex-1" user={user} full />
|
|
||||||
</Col>
|
</Col>
|
||||||
</Col>
|
</Col>
|
||||||
</Page>
|
</Page>
|
||||||
|
|
|
@ -8,7 +8,7 @@ import {
|
||||||
import { PlusCircleIcon, XCircleIcon } from '@heroicons/react/outline'
|
import { PlusCircleIcon, XCircleIcon } from '@heroicons/react/outline'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { toast, Toaster } from 'react-hot-toast'
|
import { toast, Toaster } from 'react-hot-toast'
|
||||||
import { Dictionary } from 'lodash'
|
import { Dictionary, sortBy, sum } from 'lodash'
|
||||||
|
|
||||||
import { Page } from 'web/components/page'
|
import { Page } from 'web/components/page'
|
||||||
import { Col } from 'web/components/layout/col'
|
import { Col } from 'web/components/layout/col'
|
||||||
|
@ -62,16 +62,16 @@ export default function Home() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const groups = useMemberGroupsSubscription(user)
|
const { sections } = getHomeItems(user?.homeSections ?? [])
|
||||||
|
|
||||||
const { sections } = getHomeItems(groups ?? [], user?.homeSections ?? [])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user && !user.homeSections && sections.length > 0 && groups) {
|
if (user && !user.homeSections && sections.length > 0) {
|
||||||
// Save initial home sections.
|
// Save initial home sections.
|
||||||
updateUser(user.id, { homeSections: sections.map((s) => s.id) })
|
updateUser(user.id, { homeSections: sections.map((s) => s.id) })
|
||||||
}
|
}
|
||||||
}, [user, sections, groups])
|
}, [user, sections])
|
||||||
|
|
||||||
|
const groups = useMemberGroupsSubscription(user)
|
||||||
|
|
||||||
const groupContracts = useContractsByDailyScoreGroups(
|
const groupContracts = useContractsByDailyScoreGroups(
|
||||||
groups?.map((g) => g.slug)
|
groups?.map((g) => g.slug)
|
||||||
|
@ -94,14 +94,15 @@ export default function Home() {
|
||||||
<LoadingIndicator />
|
<LoadingIndicator />
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{sections.map((section) =>
|
{sections.map((section) => renderSection(section, user))}
|
||||||
renderSection(section, user, groups, groupContracts)
|
|
||||||
)}
|
|
||||||
|
|
||||||
<TrendingGroupsSection user={user} />
|
<TrendingGroupsSection user={user} />
|
||||||
|
|
||||||
|
{renderGroupSections(user, groups, groupContracts)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</Col>
|
</Col>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="fixed bottom-[70px] right-3 z-20 inline-flex items-center rounded-full border border-transparent bg-indigo-600 p-4 text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 lg:hidden"
|
className="fixed bottom-[70px] right-3 z-20 inline-flex items-center rounded-full border border-transparent bg-indigo-600 p-4 text-white shadow-sm hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 lg:hidden"
|
||||||
|
@ -123,17 +124,12 @@ const HOME_SECTIONS = [
|
||||||
{ label: 'New', id: 'newest' },
|
{ label: 'New', id: 'newest' },
|
||||||
]
|
]
|
||||||
|
|
||||||
export const getHomeItems = (groups: Group[], sections: string[]) => {
|
export const getHomeItems = (sections: string[]) => {
|
||||||
// Accommodate old home sections.
|
// Accommodate old home sections.
|
||||||
if (!isArray(sections)) sections = []
|
if (!isArray(sections)) sections = []
|
||||||
|
|
||||||
const items: { id: string; label: string; group?: Group }[] = [
|
const items: { id: string; label: string; group?: Group }[] = [
|
||||||
...HOME_SECTIONS,
|
...HOME_SECTIONS,
|
||||||
...groups.map((g) => ({
|
|
||||||
label: g.name,
|
|
||||||
id: g.id,
|
|
||||||
group: g,
|
|
||||||
})),
|
|
||||||
]
|
]
|
||||||
const itemsById = keyBy(items, 'id')
|
const itemsById = keyBy(items, 'id')
|
||||||
|
|
||||||
|
@ -152,12 +148,7 @@ export const getHomeItems = (groups: Group[], sections: string[]) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderSection(
|
function renderSection(section: { id: string; label: string }, user: User) {
|
||||||
section: { id: string; label: string },
|
|
||||||
user: User,
|
|
||||||
groups: Group[] | undefined,
|
|
||||||
groupContracts: Dictionary<CPMMBinaryContract[]> | undefined
|
|
||||||
) {
|
|
||||||
const { id, label } = section
|
const { id, label } = section
|
||||||
if (id === 'daily-movers') {
|
if (id === 'daily-movers') {
|
||||||
return <DailyMoversSection key={id} userId={user.id} />
|
return <DailyMoversSection key={id} userId={user.id} />
|
||||||
|
@ -178,25 +169,47 @@ function renderSection(
|
||||||
<SearchSection key={id} label={label} sort={sort.value} user={user} />
|
<SearchSection key={id} label={label} sort={sort.value} user={user} />
|
||||||
)
|
)
|
||||||
|
|
||||||
if (groups && groupContracts) {
|
return null
|
||||||
const group = groups.find((g) => g.id === id)
|
}
|
||||||
if (group) {
|
|
||||||
const contracts = groupContracts[group.slug].filter(
|
function renderGroupSections(
|
||||||
(c) => Math.abs(c.probChanges.day) >= 0.01
|
user: User,
|
||||||
)
|
groups: Group[] | undefined,
|
||||||
if (contracts.length === 0) return null
|
groupContracts: Dictionary<CPMMBinaryContract[]> | undefined
|
||||||
return (
|
) {
|
||||||
<GroupSection
|
if (!groups || !groupContracts) {
|
||||||
key={id}
|
return <LoadingIndicator />
|
||||||
group={group}
|
|
||||||
user={user}
|
|
||||||
contracts={contracts}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
const filteredGroups = groups.filter((g) => groupContracts[g.slug])
|
||||||
|
const orderedGroups = sortBy(filteredGroups, (g) =>
|
||||||
|
// Sort by sum of top two daily scores.
|
||||||
|
sum(
|
||||||
|
sortBy(groupContracts[g.slug].map((c) => c.dailyScore))
|
||||||
|
.reverse()
|
||||||
|
.slice(0, 2)
|
||||||
|
)
|
||||||
|
).reverse()
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{orderedGroups.map((group) => {
|
||||||
|
const contracts = groupContracts[group.slug].filter(
|
||||||
|
(c) => Math.abs(c.probChanges.day) >= 0.01
|
||||||
|
)
|
||||||
|
if (contracts.length === 0) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<GroupSection
|
||||||
|
key={group.id}
|
||||||
|
group={group}
|
||||||
|
user={user}
|
||||||
|
contracts={contracts}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function SectionHeader(props: {
|
function SectionHeader(props: {
|
||||||
|
@ -371,9 +384,7 @@ export function TrendingGroupsSection(props: {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className={className}>
|
<Col className={className}>
|
||||||
<SectionHeader label="Trending groups" href="/explore-groups">
|
<SectionHeader label="Trending groups" href="/explore-groups" />
|
||||||
{!full && <CustomizeButton className="mb-1" />}
|
|
||||||
</SectionHeader>
|
|
||||||
<Row className="flex-wrap gap-2">
|
<Row className="flex-wrap gap-2">
|
||||||
{chosenGroups.map((g) => (
|
{chosenGroups.map((g) => (
|
||||||
<PillButton
|
<PillButton
|
||||||
|
|
Loading…
Reference in New Issue
Block a user