From 93d0f1e106e720599988517a7f16e81f295ca4ef Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 16 Sep 2022 15:30:36 -0500 Subject: [PATCH] Refactor arrange home --- web/components/arrange-home.tsx | 52 ++++++--------------------- web/pages/experimental/home/edit.tsx | 11 +++--- web/pages/experimental/home/index.tsx | 28 ++++++++++++++- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/web/components/arrange-home.tsx b/web/components/arrange-home.tsx index 6be187f8..96fe0e6f 100644 --- a/web/components/arrange-home.tsx +++ b/web/components/arrange-home.tsx @@ -5,21 +5,15 @@ import { MenuIcon } from '@heroicons/react/solid' import { Col } from 'web/components/layout/col' import { Row } from 'web/components/layout/row' import { Subtitle } from 'web/components/subtitle' -import { useMemberGroups } from 'web/hooks/use-group' -import { filterDefined } from 'common/util/array' -import { isArray, keyBy } from 'lodash' -import { User } from 'common/user' -import { Group } from 'common/group' +import { keyBy } from 'lodash' export function ArrangeHome(props: { - user: User | null | undefined - homeSections: string[] - setHomeSections: (sections: string[]) => void + sections: { label: string; id: string }[] + setSectionIds: (sections: string[]) => void }) { - const { user, homeSections, setHomeSections } = props + const { sections, setSectionIds } = props - const groups = useMemberGroups(user?.id) ?? [] - const { itemsById, sections } = getHomeItems(groups, homeSections) + const sectionsById = keyBy(sections, 'id') return ( section.id) + const newSectionIds = sections.map((section) => section.id) - newHomeSections.splice(source.index, 1) - newHomeSections.splice(destination.index, 0, item.id) + newSectionIds.splice(source.index, 1) + newSectionIds.splice(destination.index, 0, section.id) - setHomeSections(newHomeSections) + setSectionIds(newSectionIds) }} > @@ -105,29 +99,3 @@ const SectionItem = (props: { ) } - -export const getHomeItems = (groups: Group[], sections: string[]) => { - // Accommodate old home sections. - if (!isArray(sections)) sections = [] - - const items = [ - { label: 'Trending', id: 'score' }, - { label: 'New for you', id: 'newest' }, - { label: 'Daily movers', id: 'daily-movers' }, - ...groups.map((g) => ({ - label: g.name, - id: g.id, - })), - ] - const itemsById = keyBy(items, 'id') - - const sectionItems = filterDefined(sections.map((id) => itemsById[id])) - - // Add unmentioned items to the end. - sectionItems.push(...items.filter((item) => !sectionItems.includes(item))) - - return { - sections: sectionItems, - itemsById, - } -} diff --git a/web/pages/experimental/home/edit.tsx b/web/pages/experimental/home/edit.tsx index 8c242a34..bc529090 100644 --- a/web/pages/experimental/home/edit.tsx +++ b/web/pages/experimental/home/edit.tsx @@ -7,9 +7,11 @@ import { Row } from 'web/components/layout/row' import { Page } from 'web/components/page' import { SiteLink } from 'web/components/site-link' import { Title } from 'web/components/title' +import { useMemberGroups } from 'web/hooks/use-group' import { useTracking } from 'web/hooks/use-tracking' import { useUser } from 'web/hooks/use-user' import { updateUser } from 'web/lib/firebase/users' +import { getHomeItems } from '.' export default function Home() { const user = useUser() @@ -24,6 +26,9 @@ export default function Home() { setHomeSections(newHomeSections) } + const groups = useMemberGroups(user?.id) ?? [] + const { sections } = getHomeItems(groups, homeSections) + return ( @@ -32,11 +37,7 @@ export default function Home() { - + ) diff --git a/web/pages/experimental/home/index.tsx b/web/pages/experimental/home/index.tsx index 1e63c30a..ebbe716e 100644 --- a/web/pages/experimental/home/index.tsx +++ b/web/pages/experimental/home/index.tsx @@ -26,7 +26,6 @@ import { useTrendingGroups, } from 'web/hooks/use-group' import { Button } from 'web/components/button' -import { getHomeItems } from '../../../components/arrange-home' import { Row } from 'web/components/layout/row' import { ProbChangeTable } from 'web/components/contract/prob-change-table' import { @@ -47,6 +46,7 @@ import { ContractsGrid } from 'web/components/contract/contracts-grid' import { PillButton } from 'web/components/buttons/pill-button' import { filterDefined } from 'common/util/array' import { updateUser } from 'web/lib/firebase/users' +import { isArray, keyBy } from 'lodash' export default function Home() { const user = useUser() @@ -118,6 +118,32 @@ export default function Home() { ) } +export const getHomeItems = (groups: Group[], sections: string[]) => { + // Accommodate old home sections. + if (!isArray(sections)) sections = [] + + const items = [ + { label: 'Trending', id: 'score' }, + { label: 'New for you', id: 'newest' }, + { label: 'Daily movers', id: 'daily-movers' }, + ...groups.map((g) => ({ + label: g.name, + id: g.id, + })), + ] + const itemsById = keyBy(items, 'id') + + const sectionItems = filterDefined(sections.map((id) => itemsById[id])) + + // Add unmentioned items to the end. + sectionItems.push(...items.filter((item) => !sectionItems.includes(item))) + + return { + sections: sectionItems, + itemsById, + } +} + function SectionHeader(props: { label: string href: string