Refactor arrange home

This commit is contained in:
James Grugett 2022-09-16 15:30:36 -05:00
parent 7c584ac009
commit 93d0f1e106
3 changed files with 43 additions and 48 deletions

View File

@ -5,21 +5,15 @@ import { MenuIcon } from '@heroicons/react/solid'
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row' import { Row } from 'web/components/layout/row'
import { Subtitle } from 'web/components/subtitle' import { Subtitle } from 'web/components/subtitle'
import { useMemberGroups } from 'web/hooks/use-group' import { keyBy } from 'lodash'
import { filterDefined } from 'common/util/array'
import { isArray, keyBy } from 'lodash'
import { User } from 'common/user'
import { Group } from 'common/group'
export function ArrangeHome(props: { export function ArrangeHome(props: {
user: User | null | undefined sections: { label: string; id: string }[]
homeSections: string[] setSectionIds: (sections: string[]) => void
setHomeSections: (sections: string[]) => void
}) { }) {
const { user, homeSections, setHomeSections } = props const { sections, setSectionIds } = props
const groups = useMemberGroups(user?.id) ?? [] const sectionsById = keyBy(sections, 'id')
const { itemsById, sections } = getHomeItems(groups, homeSections)
return ( return (
<DragDropContext <DragDropContext
@ -27,14 +21,14 @@ export function ArrangeHome(props: {
const { destination, source, draggableId } = e const { destination, source, draggableId } = e
if (!destination) return if (!destination) return
const item = itemsById[draggableId] const section = sectionsById[draggableId]
const newHomeSections = sections.map((section) => section.id) const newSectionIds = sections.map((section) => section.id)
newHomeSections.splice(source.index, 1) newSectionIds.splice(source.index, 1)
newHomeSections.splice(destination.index, 0, item.id) newSectionIds.splice(destination.index, 0, section.id)
setHomeSections(newHomeSections) setSectionIds(newSectionIds)
}} }}
> >
<Row className="relative max-w-md gap-4"> <Row className="relative max-w-md gap-4">
@ -105,29 +99,3 @@ const SectionItem = (props: {
</div> </div>
) )
} }
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,
}
}

View File

@ -7,9 +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 { useMemberGroups } 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 { getHomeItems } from '.'
export default function Home() { export default function Home() {
const user = useUser() const user = useUser()
@ -24,6 +26,9 @@ export default function Home() {
setHomeSections(newHomeSections) setHomeSections(newHomeSections)
} }
const groups = useMemberGroups(user?.id) ?? []
const { sections } = getHomeItems(groups, homeSections)
return ( return (
<Page> <Page>
<Col className="pm:mx-10 gap-4 px-4 pb-6 pt-2"> <Col className="pm:mx-10 gap-4 px-4 pb-6 pt-2">
@ -32,11 +37,7 @@ export default function Home() {
<DoneButton /> <DoneButton />
</Row> </Row>
<ArrangeHome <ArrangeHome sections={sections} setSectionIds={updateHomeSections} />
user={user}
homeSections={homeSections}
setHomeSections={updateHomeSections}
/>
</Col> </Col>
</Page> </Page>
) )

View File

@ -26,7 +26,6 @@ import {
useTrendingGroups, useTrendingGroups,
} from 'web/hooks/use-group' } from 'web/hooks/use-group'
import { Button } from 'web/components/button' import { Button } from 'web/components/button'
import { getHomeItems } from '../../../components/arrange-home'
import { Row } from 'web/components/layout/row' import { Row } from 'web/components/layout/row'
import { ProbChangeTable } from 'web/components/contract/prob-change-table' import { ProbChangeTable } from 'web/components/contract/prob-change-table'
import { import {
@ -47,6 +46,7 @@ import { ContractsGrid } from 'web/components/contract/contracts-grid'
import { PillButton } from 'web/components/buttons/pill-button' import { PillButton } from 'web/components/buttons/pill-button'
import { filterDefined } from 'common/util/array' import { filterDefined } from 'common/util/array'
import { updateUser } from 'web/lib/firebase/users' import { updateUser } from 'web/lib/firebase/users'
import { isArray, keyBy } from 'lodash'
export default function Home() { export default function Home() {
const user = useUser() 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: { function SectionHeader(props: {
label: string label: string
href: string href: string