2022-01-21 23:21:46 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import { useRouter } from 'next/router'
|
2022-01-24 05:47:20 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { Fold } from '../../common/fold'
|
|
|
|
import { parseWordsAsTags } from '../../common/util/parse'
|
|
|
|
import { ConfirmationButton } from '../components/confirmation-button'
|
|
|
|
import { Col } from '../components/layout/col'
|
|
|
|
import { Row } from '../components/layout/row'
|
|
|
|
import { Spacer } from '../components/layout/spacer'
|
|
|
|
import { Page } from '../components/page'
|
|
|
|
import { SiteLink } from '../components/site-link'
|
|
|
|
import { TagsList } from '../components/tags-list'
|
|
|
|
import { Title } from '../components/title'
|
|
|
|
import { UserLink } from '../components/user-page'
|
2022-01-24 05:47:20 +00:00
|
|
|
import { useFolds } from '../hooks/use-fold'
|
2022-01-21 23:21:46 +00:00
|
|
|
import { useUser } from '../hooks/use-user'
|
|
|
|
import { createFold } from '../lib/firebase/api-call'
|
|
|
|
import { foldPath, listAllFolds } from '../lib/firebase/folds'
|
|
|
|
import { getUser, User } from '../lib/firebase/users'
|
2022-01-22 21:47:24 +00:00
|
|
|
import { toCamelCase } from '../lib/util/format'
|
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-01-24 05:47:20 +00:00
|
|
|
const curatorsDict = _.fromPairs(
|
|
|
|
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[]
|
|
|
|
curatorsDict: _.Dictionary<User>
|
|
|
|
}) {
|
|
|
|
const [curatorsDict, setCuratorsDict] = useState(props.curatorsDict)
|
2022-01-21 23:21:46 +00:00
|
|
|
|
2022-01-24 05:47:20 +00:00
|
|
|
const folds = useFolds() ?? props.folds
|
2022-01-21 23:21:46 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
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) => {
|
|
|
|
const newUsersDict = _.fromPairs(
|
|
|
|
newUsers.map((user) => [user.id, user])
|
|
|
|
)
|
|
|
|
setCuratorsDict({ ...curatorsDict, ...newUsersDict })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<Col className="items-center">
|
|
|
|
<Col className="max-w-2xl w-full px-2 sm:px-0">
|
|
|
|
<Row className="justify-between items-center">
|
|
|
|
<Title text="Folds" />
|
|
|
|
{user && <CreateFoldButton />}
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<Col className="gap-4">
|
2022-01-24 05:47:20 +00:00
|
|
|
{folds.map((fold) => (
|
2022-01-21 23:21:46 +00:00
|
|
|
<Row key={fold.id} className="items-center gap-2">
|
|
|
|
<SiteLink href={foldPath(fold)}>{fold.name}</SiteLink>
|
|
|
|
<div />
|
|
|
|
<div className="text-sm text-gray-500">12 followers</div>
|
|
|
|
<div className="text-gray-500">•</div>
|
|
|
|
<Row>
|
|
|
|
<div className="text-sm text-gray-500 mr-1">Curated by</div>
|
|
|
|
<UserLink
|
|
|
|
className="text-sm text-neutral"
|
2022-01-24 05:47:20 +00:00
|
|
|
name={curatorsDict[fold.curatorId]?.name ?? ''}
|
|
|
|
username={curatorsDict[fold.curatorId]?.username ?? ''}
|
2022-01-21 23:21:46 +00:00
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
</Row>
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function CreateFoldButton() {
|
|
|
|
const [name, setName] = useState('')
|
|
|
|
const [tags, setTags] = useState('')
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
|
2022-01-22 21:47:24 +00:00
|
|
|
const updateName = (newName: string) => {
|
|
|
|
setName(newName)
|
|
|
|
setTags(toCamelCase(newName))
|
|
|
|
}
|
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
const onSubmit = async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
|
|
|
const result = await createFold({
|
|
|
|
name,
|
|
|
|
tags: parseWordsAsTags(tags),
|
|
|
|
}).then((r) => r.data || {})
|
|
|
|
|
2022-01-24 05:47:20 +00:00
|
|
|
if (result.fold) {
|
|
|
|
await router.push(foldPath(result.fold)).catch((e) => {
|
|
|
|
console.log(e)
|
|
|
|
setIsSubmitting(false)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log(result.status, result.message)
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
2022-01-21 23:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ConfirmationButton
|
|
|
|
id="create-fold"
|
|
|
|
openModelBtn={{
|
|
|
|
label: 'Create a fold',
|
|
|
|
className: clsx(
|
|
|
|
isSubmitting ? 'loading btn-disabled' : 'btn-primary',
|
|
|
|
'btn-sm'
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
submitBtn={{
|
|
|
|
label: 'Create',
|
|
|
|
className: clsx(name && tags ? 'btn-primary' : 'btn-disabled'),
|
|
|
|
}}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
>
|
|
|
|
<Title className="!mt-0" text="Create a fold" />
|
|
|
|
|
|
|
|
<Col className="text-gray-500 gap-1">
|
2022-01-22 21:47:24 +00:00
|
|
|
<div>A fold is a view of markets that match one or more tags.</div>
|
|
|
|
<div>You can further exclude individual markets.</div>
|
2022-01-21 23:21:46 +00:00
|
|
|
</Col>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-01-24 05:47:20 +00:00
|
|
|
<div>
|
2022-01-21 23:21:46 +00:00
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Fold name</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder="Your fold name"
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={name}
|
2022-01-22 21:47:24 +00:00
|
|
|
onChange={(e) => updateName(e.target.value || '')}
|
2022-01-21 23:21:46 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-01-22 21:47:24 +00:00
|
|
|
{/* <div className="form-control w-full">
|
2022-01-21 23:21:46 +00:00
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Tags</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder="Politics, Economics, Rationality"
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={tags}
|
|
|
|
onChange={(e) => setTags(e.target.value || '')}
|
|
|
|
/>
|
2022-01-22 21:47:24 +00:00
|
|
|
</div> */}
|
|
|
|
|
|
|
|
{tags && (
|
|
|
|
<>
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Primary tag</span>
|
|
|
|
</label>
|
|
|
|
<TagsList
|
|
|
|
tags={parseWordsAsTags(tags).map((tag) => `#${tag}`)}
|
|
|
|
noLink
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2022-01-24 05:47:20 +00:00
|
|
|
</div>
|
2022-01-21 23:21:46 +00:00
|
|
|
</ConfirmationButton>
|
|
|
|
)
|
|
|
|
}
|