2021-12-14 01:09:16 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2021-12-14 10:27:22 +00:00
|
|
|
import { ContractsGrid } from '../components/contracts-list'
|
2021-12-14 01:09:16 +00:00
|
|
|
import { Header } from '../components/header'
|
2021-12-14 10:23:32 +00:00
|
|
|
import { compute, listAllContracts } from '../lib/firebase/contracts'
|
2021-12-14 01:09:16 +00:00
|
|
|
import { Contract } from '../lib/firebase/contracts'
|
2021-12-14 08:35:20 +00:00
|
|
|
|
2021-12-14 01:09:16 +00:00
|
|
|
export default function Markets() {
|
|
|
|
const [contracts, setContracts] = useState<Contract[]>([])
|
|
|
|
useEffect(() => {
|
|
|
|
listAllContracts().then(setContracts)
|
|
|
|
}, [])
|
|
|
|
|
2021-12-14 08:57:27 +00:00
|
|
|
const [query, setQuery] = useState('')
|
2021-12-15 07:44:41 +00:00
|
|
|
type Sort = 'createdTime' | 'volume' | 'resolved' | 'all'
|
2021-12-14 09:03:16 +00:00
|
|
|
const [sort, setSort] = useState('volume')
|
2021-12-14 08:57:27 +00:00
|
|
|
|
|
|
|
function check(corpus: String) {
|
|
|
|
return corpus.toLowerCase().includes(query.toLowerCase())
|
|
|
|
}
|
2021-12-15 07:44:41 +00:00
|
|
|
let matches = contracts.filter(
|
2021-12-14 08:57:27 +00:00
|
|
|
(c) => check(c.question) || check(c.description) || check(c.creatorName)
|
|
|
|
)
|
|
|
|
|
2021-12-15 07:44:41 +00:00
|
|
|
if (sort === 'createdTime' || sort === 'resolved' || sort === 'all') {
|
2021-12-14 08:57:27 +00:00
|
|
|
matches.sort((a, b) => b.createdTime - a.createdTime)
|
|
|
|
} else if (sort === 'volume') {
|
2021-12-14 10:27:22 +00:00
|
|
|
matches.sort((a, b) => compute(b).volume - compute(a).volume)
|
2021-12-14 08:57:27 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 07:44:41 +00:00
|
|
|
if (sort !== 'all') {
|
|
|
|
// Filter for (or filter out) resolved contracts
|
|
|
|
matches = matches.filter((c) =>
|
|
|
|
sort === 'resolved' ? c.resolution : !c.resolution
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-14 01:09:16 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Header />
|
|
|
|
<div className="max-w-4xl py-8 mx-auto">
|
2021-12-14 08:57:27 +00:00
|
|
|
{/* Show a search input next to a sort dropdown */}
|
2021-12-15 07:44:41 +00:00
|
|
|
<div className="flex justify-between gap-2 my-8">
|
2021-12-14 08:57:27 +00:00
|
|
|
<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="volume">Most traded</option>
|
2021-12-14 09:03:16 +00:00
|
|
|
<option value="createdTime">Newest first</option>
|
2021-12-15 07:44:41 +00:00
|
|
|
<option value="resolved">Resolved</option>
|
|
|
|
<option value="all">All markets</option>
|
2021-12-14 08:57:27 +00:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
2021-12-15 07:44:41 +00:00
|
|
|
<ContractsGrid contracts={matches} />
|
2021-12-14 01:09:16 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|