2022-07-22 22:28:53 +00:00
|
|
|
import { debounce, sortBy } from 'lodash'
|
2022-06-22 16:35:50 +00:00
|
|
|
import Link from 'next/link'
|
2022-07-14 15:09:12 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { Group } from 'common/group'
|
|
|
|
import { CreateGroupButton } from 'web/components/groups/create-group-button'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Title } from 'web/components/title'
|
2022-07-14 15:09:12 +00:00
|
|
|
import { useGroups, useMemberGroupIds, useMembers } from 'web/hooks/use-group'
|
2022-06-22 16:35:50 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { groupPath, listAllGroups } from 'web/lib/firebase/groups'
|
|
|
|
import { getUser, User } from 'web/lib/firebase/users'
|
|
|
|
import { Tabs } from 'web/components/layout/tabs'
|
2022-06-29 16:00:43 +00:00
|
|
|
import { SiteLink } from 'web/components/site-link'
|
|
|
|
import clsx from 'clsx'
|
2022-07-13 21:11:22 +00:00
|
|
|
import { Avatar } from 'web/components/avatar'
|
|
|
|
import { JoinOrLeaveGroupButton } from 'web/components/groups/groups-button'
|
2022-07-14 15:09:12 +00:00
|
|
|
import { UserLink } from 'web/components/user-page'
|
2022-07-15 21:16:00 +00:00
|
|
|
import { searchInAny } from 'common/util/parse'
|
2022-07-22 16:56:03 +00:00
|
|
|
import { SEO } from 'web/components/SEO'
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
export async function getStaticProps() {
|
|
|
|
const groups = await listAllGroups().catch((_) => [])
|
|
|
|
|
|
|
|
const creators = await Promise.all(
|
|
|
|
groups.map((group) => getUser(group.creatorId))
|
|
|
|
)
|
|
|
|
const creatorsDict = Object.fromEntries(
|
|
|
|
creators.map((creator) => [creator.id, creator])
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
groups: groups,
|
|
|
|
creatorsDict,
|
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Groups(props: {
|
|
|
|
groups: Group[]
|
|
|
|
creatorsDict: { [k: string]: User }
|
|
|
|
}) {
|
|
|
|
const [creatorsDict, setCreatorsDict] = useState(props.creatorsDict)
|
|
|
|
|
|
|
|
const groups = useGroups() ?? props.groups
|
|
|
|
const user = useUser()
|
|
|
|
const memberGroupIds = useMemberGroupIds(user) || []
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// Load User object for creator of new Groups.
|
|
|
|
const newGroups = groups.filter(({ creatorId }) => !creatorsDict[creatorId])
|
|
|
|
if (newGroups.length > 0) {
|
|
|
|
Promise.all(newGroups.map(({ creatorId }) => getUser(creatorId))).then(
|
|
|
|
(newUsers) => {
|
|
|
|
const newUsersDict = Object.fromEntries(
|
|
|
|
newUsers.map((user) => [user.id, user])
|
|
|
|
)
|
|
|
|
setCreatorsDict({ ...creatorsDict, ...newUsersDict })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}, [creatorsDict, groups])
|
|
|
|
|
|
|
|
const [query, setQuery] = useState('')
|
|
|
|
|
|
|
|
// List groups with the highest question count, then highest member count
|
|
|
|
// TODO use find-active-contracts to sort by?
|
|
|
|
const matches = sortBy(groups, [
|
|
|
|
(group) => -1 * group.contractIds.length,
|
|
|
|
(group) => -1 * group.memberIds.length,
|
2022-07-15 21:16:00 +00:00
|
|
|
]).filter((g) =>
|
|
|
|
searchInAny(
|
|
|
|
query,
|
|
|
|
g.name,
|
|
|
|
g.about || '',
|
|
|
|
creatorsDict[g.creatorId].username
|
|
|
|
)
|
2022-06-22 16:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const matchesOrderedByRecentActivity = sortBy(groups, [
|
2022-07-15 12:52:08 +00:00
|
|
|
(group) =>
|
|
|
|
-1 *
|
|
|
|
(group.mostRecentChatActivityTime ??
|
|
|
|
group.mostRecentContractAddedTime ??
|
|
|
|
group.mostRecentActivityTime),
|
2022-07-15 21:16:00 +00:00
|
|
|
]).filter((g) =>
|
|
|
|
searchInAny(
|
|
|
|
query,
|
|
|
|
g.name,
|
|
|
|
g.about || '',
|
|
|
|
creatorsDict[g.creatorId].username
|
|
|
|
)
|
2022-06-22 16:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Not strictly necessary, but makes the "hold delete" experience less laggy
|
|
|
|
const debouncedQuery = debounce(setQuery, 50)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
2022-07-22 16:56:03 +00:00
|
|
|
<SEO
|
|
|
|
title="Groups"
|
|
|
|
description="Manifold Groups are communities centered around a collection of prediction markets. Discuss and compete on questions with your friends."
|
|
|
|
url="/groups"
|
|
|
|
/>
|
2022-06-22 16:35:50 +00:00
|
|
|
<Col className="items-center">
|
2022-07-13 21:11:22 +00:00
|
|
|
<Col className="w-full max-w-2xl px-4 sm:px-2">
|
|
|
|
<Row className="items-center justify-between">
|
|
|
|
<Title text="Explore groups" />
|
|
|
|
{user && <CreateGroupButton user={user} goToGroupOnSubmit={true} />}
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<div className="mb-6 text-gray-500">
|
|
|
|
Discuss and compete on questions with a group of friends.
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Tabs
|
|
|
|
currentPageForAnalytics={'groups'}
|
|
|
|
tabs={[
|
|
|
|
...(user && memberGroupIds.length > 0
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
title: 'My Groups',
|
|
|
|
content: (
|
|
|
|
<Col>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
onChange={(e) => debouncedQuery(e.target.value)}
|
|
|
|
placeholder="Search your groups"
|
|
|
|
className="input input-bordered mb-4 w-full"
|
2022-06-22 16:35:50 +00:00
|
|
|
/>
|
2022-07-13 21:11:22 +00:00
|
|
|
|
|
|
|
<div className="flex flex-wrap justify-center gap-4">
|
|
|
|
{matchesOrderedByRecentActivity
|
|
|
|
.filter((match) =>
|
|
|
|
memberGroupIds.includes(match.id)
|
|
|
|
)
|
|
|
|
.map((group) => (
|
|
|
|
<GroupCard
|
|
|
|
key={group.id}
|
|
|
|
group={group}
|
|
|
|
creator={creatorsDict[group.creatorId]}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []),
|
|
|
|
{
|
|
|
|
title: 'All',
|
|
|
|
content: (
|
|
|
|
<Col>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
onChange={(e) => debouncedQuery(e.target.value)}
|
|
|
|
placeholder="Search groups"
|
|
|
|
className="input input-bordered mb-4 w-full"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="flex flex-wrap justify-center gap-4">
|
|
|
|
{matches.map((group) => (
|
|
|
|
<GroupCard
|
|
|
|
key={group.id}
|
|
|
|
group={group}
|
|
|
|
creator={creatorsDict[group.creatorId]}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function GroupCard(props: { group: Group; creator: User | undefined }) {
|
|
|
|
const { group, creator } = props
|
|
|
|
return (
|
2022-07-14 13:57:33 +00:00
|
|
|
<Col className="relative min-w-[20rem] max-w-xs gap-1 rounded-xl bg-white p-8 shadow-md hover:bg-gray-100">
|
2022-06-22 16:35:50 +00:00
|
|
|
<Link href={groupPath(group.slug)}>
|
2022-07-13 21:11:22 +00:00
|
|
|
<a className="absolute left-0 right-0 top-0 bottom-0 z-0" />
|
2022-06-22 16:35:50 +00:00
|
|
|
</Link>
|
2022-07-13 21:11:22 +00:00
|
|
|
<div>
|
|
|
|
<Avatar
|
2022-07-18 14:35:59 +00:00
|
|
|
className={'absolute top-2 right-2 z-10'}
|
2022-07-13 21:11:22 +00:00
|
|
|
username={creator?.username}
|
|
|
|
avatarUrl={creator?.avatarUrl}
|
|
|
|
noLink={false}
|
|
|
|
size={12}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-06-22 16:35:50 +00:00
|
|
|
<Row className="items-center justify-between gap-2">
|
|
|
|
<span className="text-xl">{group.name}</span>
|
|
|
|
</Row>
|
2022-07-13 21:11:22 +00:00
|
|
|
<Row>{group.contractIds.length} questions</Row>
|
|
|
|
<Row className="text-sm text-gray-500">
|
|
|
|
<GroupMembersList group={group} />
|
|
|
|
</Row>
|
|
|
|
<Row>
|
|
|
|
<div className="text-sm text-gray-500">{group.about}</div>
|
|
|
|
</Row>
|
|
|
|
<Col className={'mt-2 h-full items-start justify-end'}>
|
|
|
|
<JoinOrLeaveGroupButton group={group} className={'z-10 w-24'} />
|
|
|
|
</Col>
|
2022-06-22 16:35:50 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
2022-06-29 16:00:43 +00:00
|
|
|
|
2022-07-14 15:09:12 +00:00
|
|
|
function GroupMembersList(props: { group: Group }) {
|
|
|
|
const { group } = props
|
|
|
|
const maxMembersToShow = 3
|
|
|
|
const members = useMembers(group, maxMembersToShow).filter(
|
|
|
|
(m) => m.id !== group.creatorId
|
|
|
|
)
|
|
|
|
if (group.memberIds.length === 1) return <div />
|
|
|
|
return (
|
|
|
|
<div className="text-neutral flex flex-wrap gap-1">
|
|
|
|
<span className={'text-gray-500'}>Other members</span>
|
|
|
|
{members.slice(0, maxMembersToShow).map((member, i) => (
|
|
|
|
<div key={member.id} className={'flex-shrink'}>
|
|
|
|
<UserLink name={member.name} username={member.username} />
|
|
|
|
{members.length > 1 && i !== members.length - 1 && <span>,</span>}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
{group.memberIds.length > maxMembersToShow && (
|
|
|
|
<span> & {group.memberIds.length - maxMembersToShow} more</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-22 22:28:53 +00:00
|
|
|
export function GroupLinkItem(props: { group: Group; className?: string }) {
|
2022-06-29 16:00:43 +00:00
|
|
|
const { group, className } = props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SiteLink
|
|
|
|
href={groupPath(group.slug)}
|
|
|
|
className={clsx('z-10 truncate', className)}
|
|
|
|
>
|
|
|
|
{group.name}
|
|
|
|
</SiteLink>
|
|
|
|
)
|
|
|
|
}
|