2022-05-22 08:36:05 +00:00
|
|
|
import { sortBy, debounce } from 'lodash'
|
2022-01-26 18:44:16 +00:00
|
|
|
import Link from 'next/link'
|
2022-01-24 05:47:20 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Fold } from 'common/fold'
|
|
|
|
import { CreateFoldButton } from 'web/components/folds/create-fold-button'
|
|
|
|
import { FollowFoldButton } from 'web/components/folds/follow-fold-button'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { TagsList } from 'web/components/tags-list'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { UserLink } from 'web/components/user-page'
|
|
|
|
import { useFolds, useFollowedFoldIds } from 'web/hooks/use-fold'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { foldPath, listAllFolds } from 'web/lib/firebase/folds'
|
|
|
|
import { getUser, User } from 'web/lib/firebase/users'
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
export async function getStaticProps() {
|
|
|
|
const folds = await listAllFolds().catch((_) => [])
|
|
|
|
|
|
|
|
const curators = await Promise.all(
|
|
|
|
folds.map((fold) => getUser(fold.curatorId))
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const curatorsDict = Object.fromEntries(
|
2022-01-24 05:47:20 +00:00
|
|
|
curators.map((curator) => [curator.id, curator])
|
|
|
|
)
|
2022-01-21 23:21:46 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
folds,
|
2022-01-24 05:47:20 +00:00
|
|
|
curatorsDict,
|
2022-01-21 23:21:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 05:47:20 +00:00
|
|
|
export default function Folds(props: {
|
|
|
|
folds: Fold[]
|
2022-05-22 08:36:05 +00:00
|
|
|
curatorsDict: { [k: string]: User }
|
2022-01-24 05:47:20 +00:00
|
|
|
}) {
|
|
|
|
const [curatorsDict, setCuratorsDict] = useState(props.curatorsDict)
|
2022-01-21 23:21:46 +00:00
|
|
|
|
2022-05-10 21:49:24 +00:00
|
|
|
const folds = useFolds() ?? props.folds
|
2022-01-21 23:21:46 +00:00
|
|
|
const user = useUser()
|
2022-03-31 05:35:20 +00:00
|
|
|
const followedFoldIds = useFollowedFoldIds(user) || []
|
2022-01-21 23:21:46 +00:00
|
|
|
|
2022-01-24 05:47:20 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// Load User object for curator of new Folds.
|
|
|
|
const newFolds = folds.filter(({ curatorId }) => !curatorsDict[curatorId])
|
|
|
|
if (newFolds.length > 0) {
|
|
|
|
Promise.all(newFolds.map(({ curatorId }) => getUser(curatorId))).then(
|
|
|
|
(newUsers) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const newUsersDict = Object.fromEntries(
|
2022-01-24 05:47:20 +00:00
|
|
|
newUsers.map((user) => [user.id, user])
|
|
|
|
)
|
|
|
|
setCuratorsDict({ ...curatorsDict, ...newUsersDict })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2022-05-10 21:49:24 +00:00
|
|
|
}, [curatorsDict, folds])
|
2022-03-10 18:14:06 +00:00
|
|
|
|
|
|
|
const [query, setQuery] = useState('')
|
|
|
|
// Copied from contracts-list.tsx; extract if we copy this again
|
|
|
|
const queryWords = query.toLowerCase().split(' ')
|
2022-05-26 21:41:24 +00:00
|
|
|
function check(corpus: string) {
|
2022-03-10 18:14:06 +00:00
|
|
|
return queryWords.every((word) => corpus.toLowerCase().includes(word))
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:49:24 +00:00
|
|
|
// List followed folds first, then folds with the highest follower count
|
2022-05-22 08:36:05 +00:00
|
|
|
const matches = sortBy(folds, [
|
2022-05-10 21:49:24 +00:00
|
|
|
(fold) => !followedFoldIds.includes(fold.id),
|
|
|
|
(fold) => -1 * fold.followCount,
|
|
|
|
]).filter(
|
2022-03-10 18:14:06 +00:00
|
|
|
(f) =>
|
|
|
|
check(f.name) ||
|
|
|
|
check(f.about || '') ||
|
|
|
|
check(curatorsDict[f.curatorId].username) ||
|
|
|
|
check(f.lowercaseTags.map((tag) => `#${tag}`).join(' '))
|
|
|
|
)
|
|
|
|
// Not strictly necessary, but makes the "hold delete" experience less laggy
|
2022-05-22 08:36:05 +00:00
|
|
|
const debouncedQuery = debounce(setQuery, 50)
|
2022-01-24 05:47:20 +00:00
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<Col className="items-center">
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="w-full max-w-xl">
|
2022-01-27 22:37:43 +00:00
|
|
|
<Col className="px-4 sm:px-0">
|
2022-02-11 18:40:22 +00:00
|
|
|
<Row className="items-center justify-between">
|
2022-02-01 18:12:55 +00:00
|
|
|
<Title text="Explore communities" />
|
2022-01-27 22:37:43 +00:00
|
|
|
{user && <CreateFoldButton />}
|
|
|
|
</Row>
|
2022-01-21 23:21:46 +00:00
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mb-6 text-gray-500">
|
2022-02-01 18:12:55 +00:00
|
|
|
Communities on Manifold are centered around a collection of
|
2022-03-25 05:53:12 +00:00
|
|
|
markets. Follow a community to personalize your feed!
|
2022-01-27 22:37:43 +00:00
|
|
|
</div>
|
2022-03-10 18:14:06 +00:00
|
|
|
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
onChange={(e) => debouncedQuery(e.target.value)}
|
|
|
|
placeholder="Search communities"
|
|
|
|
className="input input-bordered mb-4 w-full"
|
|
|
|
/>
|
2022-01-27 22:37:43 +00:00
|
|
|
</Col>
|
2022-01-24 22:58:51 +00:00
|
|
|
|
2022-02-03 18:47:26 +00:00
|
|
|
<Col className="gap-4">
|
2022-03-10 18:14:06 +00:00
|
|
|
{matches.map((fold) => (
|
2022-01-26 20:27:13 +00:00
|
|
|
<FoldCard
|
2022-01-26 18:44:16 +00:00
|
|
|
key={fold.id}
|
2022-01-26 20:27:13 +00:00
|
|
|
fold={fold}
|
|
|
|
curator={curatorsDict[fold.curatorId]}
|
|
|
|
/>
|
2022-01-21 23:21:46 +00:00
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
2022-01-26 20:27:13 +00:00
|
|
|
|
|
|
|
function FoldCard(props: { fold: Fold; curator: User | undefined }) {
|
|
|
|
const { fold, curator } = props
|
2022-02-03 18:47:26 +00:00
|
|
|
const tags = fold.tags.slice(1)
|
2022-01-26 20:27:13 +00:00
|
|
|
return (
|
|
|
|
<Col
|
|
|
|
key={fold.id}
|
2022-02-11 18:40:22 +00:00
|
|
|
className="relative gap-1 rounded-xl bg-white p-8 shadow-md hover:bg-gray-100"
|
2022-01-26 20:27:13 +00:00
|
|
|
>
|
|
|
|
<Link href={foldPath(fold)}>
|
|
|
|
<a className="absolute left-0 right-0 top-0 bottom-0" />
|
|
|
|
</Link>
|
2022-02-11 18:40:22 +00:00
|
|
|
<Row className="items-center justify-between gap-2">
|
2022-02-03 18:47:26 +00:00
|
|
|
<span className="text-xl">{fold.name}</span>
|
2022-01-26 20:27:13 +00:00
|
|
|
<FollowFoldButton className="z-10 mb-1" fold={fold} />
|
|
|
|
</Row>
|
2022-02-11 18:40:22 +00:00
|
|
|
<Row className="items-center gap-2 text-sm text-gray-500">
|
2022-01-26 20:27:13 +00:00
|
|
|
<div>{fold.followCount} followers</div>
|
|
|
|
<div>•</div>
|
|
|
|
<Row>
|
|
|
|
<div className="mr-1">Curated by</div>
|
|
|
|
<UserLink
|
|
|
|
className="text-neutral"
|
|
|
|
name={curator?.name ?? ''}
|
|
|
|
username={curator?.username ?? ''}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
</Row>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="text-sm text-gray-500">{fold.about}</div>
|
2022-02-03 18:47:26 +00:00
|
|
|
{tags.length > 0 && (
|
|
|
|
<TagsList className="mt-4" tags={tags} noLink noLabel />
|
|
|
|
)}
|
2022-01-26 20:27:13 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|