Change useContracts to return undefined instead of 'loading'. Serve static props on tag page
This commit is contained in:
parent
16ab0e0c37
commit
d1c6e5bc91
|
@ -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)
|
||||||
|
|
|
@ -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,7 +47,6 @@ 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}
|
||||||
|
@ -55,7 +54,6 @@ export default function Markets(props: {
|
||||||
sort={sort}
|
sort={sort}
|
||||||
setSort={setSort}
|
setSort={setSort}
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
</Page>
|
</Page>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
<SearchableGrid
|
||||||
contracts={contracts}
|
contracts={taggedContracts}
|
||||||
query={query}
|
query={query}
|
||||||
setQuery={setQuery}
|
setQuery={setQuery}
|
||||||
sort={sort}
|
sort={sort}
|
||||||
setSort={setSort}
|
setSort={setSort}
|
||||||
/>
|
/>
|
||||||
)}
|
|
||||||
</Page>
|
</Page>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user