Skip to content

Commit

Permalink
Quick back from clicking contract card on home! Preserves search state.
Browse files Browse the repository at this point in the history
  • Loading branch information
jahooma committed Jun 5, 2022
1 parent cd12628 commit 5152be5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
8 changes: 6 additions & 2 deletions web/components/contract-search.tsx
Expand Up @@ -56,8 +56,9 @@ export function ContractSearch(props: {
category?: string
}
showCategorySelector: boolean
onContractClick?: (contract: Contract) => void
}) {
const { querySortOptions, additionalFilter, showCategorySelector } = props
const { querySortOptions, additionalFilter, showCategorySelector, onContractClick } = props

const user = useUser()
const follows = useFollows(user?.id)
Expand Down Expand Up @@ -151,6 +152,7 @@ export function ContractSearch(props: {
category: category === 'following' ? 'all' : category,
...additionalFilter,
}}
onContractClick={onContractClick}
/>
)}
</InstantSearch>
Expand All @@ -168,8 +170,9 @@ export function ContractSearchInner(props: {
tag?: string
category?: string
}
onContractClick?: (contract: Contract) => void
}) {
const { querySortOptions, filter, additionalFilter } = props
const { querySortOptions, filter, additionalFilter, onContractClick } = props
const { initialQuery } = useInitialQueryAndSort(querySortOptions)

const { query, setQuery, setSort } = useUpdateQueryAndSort({
Expand Down Expand Up @@ -235,6 +238,7 @@ export function ContractSearchInner(props: {
loadMore={showMore}
hasMore={!isLastPage}
showCloseTime={index.endsWith('close-date')}
onContractClick={onContractClick}
/>
)
}
Expand Down
16 changes: 12 additions & 4 deletions web/components/contract/contract-card.tsx
Expand Up @@ -28,8 +28,9 @@ export function ContractCard(props: {
showHotVolume?: boolean
showCloseTime?: boolean
className?: string
onClick?: () => void
}) {
const { showHotVolume, showCloseTime, className } = props
const { showHotVolume, showCloseTime, className, onClick } = props
const contract = useContractWithPreload(props.contract) ?? props.contract
const { question, outcomeType } = contract
const { resolution } = contract
Expand Down Expand Up @@ -61,9 +62,16 @@ export function ContractCard(props: {
'peer absolute -left-6 -top-4 -bottom-4 right-0 z-10'
)}
>
<Link href={contractPath(contract)}>
<a className="absolute top-0 left-0 right-0 bottom-0" />
</Link>
{onClick ? (
<a
className="absolute top-0 left-0 right-0 bottom-0"
onClick={onClick}
/>
) : (
<Link href={contractPath(contract)}>
<a className="absolute top-0 left-0 right-0 bottom-0" />
</Link>
)}
</div>
<AvatarDetails contract={contract} />
<p
Expand Down
4 changes: 3 additions & 1 deletion web/components/contract/contracts-list.tsx
Expand Up @@ -12,8 +12,9 @@ export function ContractsGrid(props: {
loadMore: () => void
hasMore: boolean
showCloseTime?: boolean
onContractClick?: (contract: Contract) => void
}) {
const { contracts, showCloseTime, hasMore, loadMore } = props
const { contracts, showCloseTime, hasMore, loadMore, onContractClick } = props

const [elem, setElem] = useState<HTMLElement | null>(null)
const isBottomVisible = useIsVisible(elem)
Expand Down Expand Up @@ -43,6 +44,7 @@ export function ContractsGrid(props: {
contract={contract}
key={contract.id}
showCloseTime={showCloseTime}
onClick={() => onContractClick?.(contract)}
/>
))}
</ul>
Expand Down
68 changes: 58 additions & 10 deletions web/pages/home.tsx
@@ -1,31 +1,79 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'
import Router from 'next/router'

import { Page } from 'web/components/page'
import { Col } from 'web/components/layout/col'
import { useUser } from 'web/hooks/use-user'
import { ContractSearch } from 'web/components/contract-search'
import { Contract } from 'common/contract'
import { ContractPageContent } from './[username]/[contractSlug]'
import { getContractFromSlug } from 'web/lib/firebase/contracts'

const Home = () => {
const user = useUser()

const [contract, setContract] = useState<Contract | undefined>()

useEffect(() => {
const onBack = () => {
const path = location.pathname.split('/').slice(1)
if (path[0] === 'home') setContract(undefined)
else {
const [username, contractSlug] = path
if (!username || !contractSlug) setContract(undefined)
else {
// Show contract if route is to a contract: '/[username]/[contractSlug]'.
getContractFromSlug(contractSlug).then((c) => {
setContract(c)
window.scrollTo(0, 0)
})
}
}
}

window.addEventListener('popstate', onBack)
return () => window.removeEventListener('popstate', onBack)
}, [])

if (user === null) {
Router.replace('/')
return <></>
}

return (
<Page assertUser="signed-in">
<Col className="mx-auto w-full p-2">
<ContractSearch
querySortOptions={{
shouldLoadFromStorage: true,
defaultSort: '24-hour-vol',
<>
<Page assertUser="signed-in" suspend={!!contract}>
<Col className="mx-auto w-full p-2">
<ContractSearch
querySortOptions={{
shouldLoadFromStorage: true,
defaultSort: '24-hour-vol',
}}
showCategorySelector
onContractClick={(c) => {
// Show contract without navigating to contract page.
setContract(c)
// Update the url without switching pages in Nextjs.
history.pushState(null, '', `/${c.creatorUsername}/${c.slug}`)
window.scrollTo(0, 0)
}}
/>
</Col>
</Page>

{contract && (
<ContractPageContent
contract={contract}
username={contract.creatorUsername}
slug={contract.slug}
bets={[]}
comments={[]}
backToHome={() => {
history.back()
}}
showCategorySelector
/>
</Col>
</Page>
)}
</>
)
}

Expand Down

3 comments on commit 5152be5

@vercel
Copy link

@vercel vercel bot commented on 5152be5 Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

manifold-og-image – ./og-image/

manifold-og-image-mantic.vercel.app
manifold-og-image-git-main-mantic.vercel.app
manifold-og-image.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 5152be5 Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./docs

docs-pi-teal.vercel.app
docs-git-main-mantic.vercel.app
docs-mantic.vercel.app
docs.manifold.markets

@vercel
Copy link

@vercel vercel bot commented on 5152be5 Jun 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.