import clsx from 'clsx' import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd' 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' export function ArrangeHome(props: { user: User | null | undefined homeSections: string[] setHomeSections: (sections: string[]) => void }) { const { user, homeSections, setHomeSections } = props const groups = useMemberGroups(user?.id) ?? [] const { itemsById, sections } = getHomeItems(groups, homeSections) return ( { const { destination, source, draggableId } = e if (!destination) return const item = itemsById[draggableId] const newHomeSections = sections.map((section) => section.id) newHomeSections.splice(source.index, 1) newHomeSections.splice(destination.index, 0, item.id) setHomeSections(newHomeSections) }} > ) } function DraggableList(props: { title: string items: { id: string; label: string }[] }) { const { title, items } = props return ( {(provided) => ( {items.map((item, index) => ( {(provided, snapshot) => (
)}
))} {provided.placeholder} )}
) } const SectionItem = (props: { item: { id: string; label: string } className?: string }) => { const { item, className } = props return (
) } 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, } }