2022-06-14 09:35:51 +00:00
|
|
|
import { Answer } from 'common/answer'
|
2022-07-15 21:16:00 +00:00
|
|
|
import { searchInAny } from 'common/util/parse'
|
2022-06-14 09:35:51 +00:00
|
|
|
import { sortBy } from 'lodash'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { ContractsGrid } from 'web/components/contract/contracts-list'
|
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
|
|
|
import { useContracts } from 'web/hooks/use-contracts'
|
|
|
|
import {
|
|
|
|
Sort,
|
|
|
|
useInitialQueryAndSort,
|
|
|
|
} from 'web/hooks/use-sort-and-query-params'
|
|
|
|
|
2022-07-01 16:26:45 +00:00
|
|
|
const MAX_CONTRACTS_RENDERED = 100
|
|
|
|
|
2022-06-22 23:45:48 +00:00
|
|
|
export default function ContractSearchFirestore(props: {
|
2022-06-14 09:35:51 +00:00
|
|
|
querySortOptions?: {
|
|
|
|
defaultSort: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}
|
2022-06-24 17:08:06 +00:00
|
|
|
additionalFilter?: {
|
|
|
|
creatorId?: string
|
|
|
|
tag?: string
|
|
|
|
}
|
2022-06-14 09:35:51 +00:00
|
|
|
}) {
|
|
|
|
const contracts = useContracts()
|
2022-06-24 17:08:06 +00:00
|
|
|
const { querySortOptions, additionalFilter } = props
|
2022-06-14 09:35:51 +00:00
|
|
|
|
|
|
|
const { initialSort, initialQuery } = useInitialQueryAndSort(querySortOptions)
|
|
|
|
const [sort, setSort] = useState(initialSort || 'newest')
|
|
|
|
const [query, setQuery] = useState(initialQuery)
|
|
|
|
|
2022-07-15 21:16:00 +00:00
|
|
|
let matches = (contracts ?? []).filter((c) =>
|
|
|
|
searchInAny(
|
|
|
|
query,
|
|
|
|
c.question,
|
|
|
|
c.creatorName,
|
|
|
|
c.lowercaseTags.map((tag) => `#${tag}`).join(' '),
|
|
|
|
((c as any).answers ?? []).map((answer: Answer) => answer.text).join(' ')
|
|
|
|
)
|
2022-06-14 09:35:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if (sort === 'newest') {
|
|
|
|
matches.sort((a, b) => b.createdTime - a.createdTime)
|
|
|
|
} else if (sort === 'resolve-date') {
|
|
|
|
matches = sortBy(matches, (contract) => -1 * (contract.resolutionTime ?? 0))
|
|
|
|
} else if (sort === 'oldest') {
|
|
|
|
matches.sort((a, b) => a.createdTime - b.createdTime)
|
|
|
|
} else if (sort === 'close-date') {
|
|
|
|
matches = sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours)
|
|
|
|
matches = sortBy(
|
|
|
|
matches,
|
|
|
|
(contract) =>
|
|
|
|
(sort === 'close-date' ? -1 : 1) * (contract.closeTime ?? Infinity)
|
|
|
|
)
|
|
|
|
} else if (sort === 'most-traded') {
|
|
|
|
matches.sort((a, b) => b.volume - a.volume)
|
2022-07-19 22:29:41 +00:00
|
|
|
} else if (sort === 'score') {
|
|
|
|
matches.sort((a, b) => (b.popularityScore ?? 0) - (a.popularityScore ?? 0))
|
2022-06-14 09:35:51 +00:00
|
|
|
} else if (sort === '24-hour-vol') {
|
|
|
|
// Use lodash for stable sort, so previous sort breaks all ties.
|
|
|
|
matches = sortBy(matches, ({ volume7Days }) => -1 * volume7Days)
|
|
|
|
matches = sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours)
|
|
|
|
}
|
|
|
|
|
2022-06-24 17:08:06 +00:00
|
|
|
if (additionalFilter) {
|
|
|
|
const { creatorId, tag } = additionalFilter
|
|
|
|
|
|
|
|
if (creatorId) {
|
|
|
|
matches = matches.filter((c) => c.creatorId === creatorId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag) {
|
|
|
|
matches = matches.filter((c) =>
|
|
|
|
c.lowercaseTags.includes(tag.toLowerCase())
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 16:26:45 +00:00
|
|
|
matches = matches.slice(0, MAX_CONTRACTS_RENDERED)
|
|
|
|
|
2022-06-23 17:12:57 +00:00
|
|
|
const showTime = ['close-date', 'closed'].includes(sort)
|
|
|
|
? 'close-date'
|
|
|
|
: sort === 'resolve-date'
|
|
|
|
? 'resolve-date'
|
|
|
|
: undefined
|
|
|
|
|
2022-06-14 09:35:51 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{/* Show a search input next to a sort dropdown */}
|
|
|
|
<div className="mt-2 mb-8 flex justify-between gap-2">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
value={query}
|
|
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
|
|
placeholder="Search markets"
|
|
|
|
className="input input-bordered w-full"
|
|
|
|
/>
|
|
|
|
<select
|
|
|
|
className="select select-bordered"
|
|
|
|
value={sort}
|
|
|
|
onChange={(e) => setSort(e.target.value as Sort)}
|
|
|
|
>
|
|
|
|
<option value="newest">Newest</option>
|
|
|
|
<option value="oldest">Oldest</option>
|
2022-07-19 22:29:41 +00:00
|
|
|
<option value="score">Most popular</option>
|
2022-06-14 09:35:51 +00:00
|
|
|
<option value="most-traded">Most traded</option>
|
|
|
|
<option value="24-hour-vol">24h volume</option>
|
|
|
|
<option value="close-date">Closing soon</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
{contracts === undefined ? (
|
|
|
|
<LoadingIndicator />
|
|
|
|
) : (
|
|
|
|
<ContractsGrid
|
|
|
|
contracts={matches}
|
|
|
|
loadMore={() => {}}
|
|
|
|
hasMore={false}
|
2022-06-23 17:12:57 +00:00
|
|
|
showTime={showTime}
|
2022-06-14 09:35:51 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|