Change useContracts to return undefined instead of 'loading'. Serve static props on tag page

This commit is contained in:
jahooma 2022-01-09 14:51:20 -06:00
parent 16ab0e0c37
commit d1c6e5bc91
3 changed files with 39 additions and 31 deletions

View File

@ -7,7 +7,7 @@ import {
} from '../lib/firebase/contracts' } from '../lib/firebase/contracts'
export const useContracts = () => { export const useContracts = () => {
const [contracts, setContracts] = useState<Contract[] | 'loading'>('loading') const [contracts, setContracts] = useState<Contract[] | undefined>()
useEffect(() => { useEffect(() => {
return listenForContracts(setContracts) return listenForContracts(setContracts)

View File

@ -36,7 +36,7 @@ export default function Markets(props: {
const { query, setQuery, sort, setSort } = useQueryAndSortParams() const { query, setQuery, sort, setSort } = useQueryAndSortParams()
const readyHotContracts = hotContracts ?? props.hotContracts const readyHotContracts = hotContracts ?? props.hotContracts
const readyContracts = contracts === 'loading' ? props.contracts : contracts const readyContracts = contracts ?? props.contracts
return ( return (
<Page> <Page>
@ -47,15 +47,13 @@ export default function Markets(props: {
<Spacer h={10} /> <Spacer h={10} />
{(props.contracts || contracts !== 'loading') && ( <SearchableGrid
<SearchableGrid contracts={readyContracts}
contracts={readyContracts} query={query}
query={query} setQuery={setQuery}
setQuery={setQuery} sort={sort}
sort={sort} setSort={setSort}
setSort={setSort} />
/>
)}
</Page> </Page>
) )
} }

View File

@ -4,20 +4,34 @@ import { Page } from '../../components/page'
import { Title } from '../../components/title' import { Title } from '../../components/title'
import { useContracts } from '../../hooks/use-contracts' import { useContracts } from '../../hooks/use-contracts'
import { useQueryAndSortParams } from '../../hooks/use-sort-and-query-params' import { useQueryAndSortParams } from '../../hooks/use-sort-and-query-params'
import { Contract, listAllContracts } from '../../lib/firebase/contracts'
export default function TagPage() { export async function getStaticProps() {
const contracts = await listAllContracts().catch((_) => [])
return {
props: {
contracts,
},
revalidate: 60, // regenerate after a minute
}
}
export async function getStaticPaths() {
return { paths: [], fallback: 'blocking' }
}
export default function TagPage(props: { contracts: Contract[] }) {
const router = useRouter() const router = useRouter()
const { tag } = router.query as { tag: string } const { tag } = router.query as { tag: string }
let contracts = useContracts() const contracts = useContracts()
if (tag && contracts !== 'loading') { const taggedContracts = (contracts ?? props.contracts).filter(
contracts = contracts.filter( (contract) =>
(contract) => contract.description.toLowerCase().includes(`#${tag.toLowerCase()}`) ||
contract.description.toLowerCase().includes(`#${tag.toLowerCase()}`) || contract.question.toLowerCase().includes(`#${tag.toLowerCase()}`)
contract.question.toLowerCase().includes(`#${tag.toLowerCase()}`) )
)
}
const { query, setQuery, sort, setSort } = useQueryAndSortParams({ const { query, setQuery, sort, setSort } = useQueryAndSortParams({
defaultSort: 'most-traded', defaultSort: 'most-traded',
@ -26,17 +40,13 @@ export default function TagPage() {
return ( return (
<Page> <Page>
<Title text={`#${tag}`} /> <Title text={`#${tag}`} />
{contracts === 'loading' ? ( <SearchableGrid
<></> contracts={taggedContracts}
) : ( query={query}
<SearchableGrid setQuery={setQuery}
contracts={contracts} sort={sort}
query={query} setSort={setSort}
setQuery={setQuery} />
sort={sort}
setSort={setSort}
/>
)}
</Page> </Page>
) )
} }