2022-05-20 02:13:54 +00:00
|
|
|
/* eslint-disable react-hooks/exhaustive-deps */
|
2022-05-09 17:38:33 +00:00
|
|
|
import algoliasearch from 'algoliasearch/lite'
|
|
|
|
import {
|
2022-06-05 19:06:08 +00:00
|
|
|
Configure,
|
2022-05-09 17:38:33 +00:00
|
|
|
InstantSearch,
|
|
|
|
SearchBox,
|
|
|
|
SortBy,
|
|
|
|
useInfiniteHits,
|
|
|
|
useSortBy,
|
|
|
|
} from 'react-instantsearch-hooks-web'
|
2022-06-05 19:06:08 +00:00
|
|
|
|
2022-07-01 22:37:30 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-05-09 17:38:33 +00:00
|
|
|
import {
|
|
|
|
Sort,
|
|
|
|
useInitialQueryAndSort,
|
|
|
|
useUpdateQueryAndSort,
|
|
|
|
} from '../hooks/use-sort-and-query-params'
|
|
|
|
import { ContractsGrid } from './contract/contracts-list'
|
|
|
|
import { Row } from './layout/row'
|
2022-06-05 19:06:08 +00:00
|
|
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
2022-05-09 17:38:33 +00:00
|
|
|
import { Spacer } from './layout/spacer'
|
2022-06-14 09:35:51 +00:00
|
|
|
import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { trackCallback } from 'web/lib/service/analytics'
|
2022-06-22 23:45:48 +00:00
|
|
|
import ContractSearchFirestore from 'web/pages/contract-search-firestore'
|
2022-05-09 17:38:33 +00:00
|
|
|
|
|
|
|
const searchClient = algoliasearch(
|
|
|
|
'GJQPAYENIF',
|
|
|
|
'75c28fc084a80e1129d427d470cf41a3'
|
|
|
|
)
|
|
|
|
|
|
|
|
const indexPrefix = ENV === 'DEV' ? 'dev-' : ''
|
|
|
|
|
|
|
|
const sortIndexes = [
|
|
|
|
{ label: 'Newest', value: indexPrefix + 'contracts-newest' },
|
|
|
|
{ label: 'Oldest', value: indexPrefix + 'contracts-oldest' },
|
|
|
|
{ label: 'Most traded', value: indexPrefix + 'contracts-most-traded' },
|
|
|
|
{ label: '24h volume', value: indexPrefix + 'contracts-24-hour-vol' },
|
2022-05-21 17:44:51 +00:00
|
|
|
{ label: 'Last updated', value: indexPrefix + 'contracts-last-updated' },
|
2022-05-09 17:38:33 +00:00
|
|
|
{ label: 'Close date', value: indexPrefix + 'contracts-close-date' },
|
|
|
|
{ label: 'Resolve date', value: indexPrefix + 'contracts-resolve-date' },
|
|
|
|
]
|
|
|
|
|
|
|
|
type filter = 'open' | 'closed' | 'resolved' | 'all'
|
|
|
|
|
|
|
|
export function ContractSearch(props: {
|
|
|
|
querySortOptions?: {
|
|
|
|
defaultSort: Sort
|
2022-05-11 04:19:13 +00:00
|
|
|
defaultFilter?: filter
|
2022-05-09 17:38:33 +00:00
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}
|
2022-05-17 17:56:10 +00:00
|
|
|
additionalFilter?: {
|
|
|
|
creatorId?: string
|
|
|
|
tag?: string
|
2022-07-01 22:37:30 +00:00
|
|
|
excludeContractIds?: string[]
|
2022-05-17 17:56:10 +00:00
|
|
|
}
|
2022-06-05 00:00:13 +00:00
|
|
|
onContractClick?: (contract: Contract) => void
|
2022-07-01 22:37:30 +00:00
|
|
|
showPlaceHolder?: boolean
|
|
|
|
hideOrderSelector?: boolean
|
|
|
|
overrideGridClassName?: string
|
|
|
|
hideQuickBet?: boolean
|
2022-05-09 17:38:33 +00:00
|
|
|
}) {
|
2022-06-05 00:14:14 +00:00
|
|
|
const {
|
|
|
|
querySortOptions,
|
|
|
|
additionalFilter,
|
|
|
|
onContractClick,
|
2022-07-01 22:37:30 +00:00
|
|
|
overrideGridClassName,
|
|
|
|
hideOrderSelector,
|
|
|
|
showPlaceHolder,
|
|
|
|
hideQuickBet,
|
2022-06-05 00:14:14 +00:00
|
|
|
} = props
|
2022-05-09 17:38:33 +00:00
|
|
|
|
|
|
|
const { initialSort } = useInitialQueryAndSort(querySortOptions)
|
|
|
|
|
|
|
|
const sort = sortIndexes
|
|
|
|
.map(({ value }) => value)
|
|
|
|
.includes(`${indexPrefix}contracts-${initialSort ?? ''}`)
|
|
|
|
? initialSort
|
2022-06-05 19:06:08 +00:00
|
|
|
: querySortOptions?.defaultSort ?? '24-hour-vol'
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-05-11 04:19:13 +00:00
|
|
|
const [filter, setFilter] = useState<filter>(
|
|
|
|
querySortOptions?.defaultFilter ?? 'open'
|
|
|
|
)
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-06-05 19:06:08 +00:00
|
|
|
const { filters, numericFilters } = useMemo(() => {
|
|
|
|
let filters = [
|
|
|
|
filter === 'open' ? 'isResolved:false' : '',
|
|
|
|
filter === 'closed' ? 'isResolved:false' : '',
|
|
|
|
filter === 'resolved' ? 'isResolved:true' : '',
|
|
|
|
additionalFilter?.creatorId
|
|
|
|
? `creatorId:${additionalFilter.creatorId}`
|
|
|
|
: '',
|
|
|
|
additionalFilter?.tag ? `lowercaseTags:${additionalFilter.tag}` : '',
|
|
|
|
].filter((f) => f)
|
|
|
|
// Hack to make Algolia work.
|
|
|
|
filters = ['', ...filters]
|
|
|
|
|
|
|
|
const numericFilters = [
|
|
|
|
filter === 'open' ? `closeTime > ${Date.now()}` : '',
|
|
|
|
filter === 'closed' ? `closeTime <= ${Date.now()}` : '',
|
|
|
|
].filter((f) => f)
|
|
|
|
|
|
|
|
return { filters, numericFilters }
|
2022-07-07 20:45:26 +00:00
|
|
|
}, [filter, Object.values(additionalFilter ?? {}).join(',')])
|
2022-05-17 17:56:10 +00:00
|
|
|
|
2022-06-03 04:52:14 +00:00
|
|
|
const indexName = `${indexPrefix}contracts-${sort}`
|
|
|
|
|
2022-07-01 16:26:45 +00:00
|
|
|
if (IS_PRIVATE_MANIFOLD || process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
|
2022-06-24 17:27:01 +00:00
|
|
|
return (
|
|
|
|
<ContractSearchFirestore
|
|
|
|
querySortOptions={querySortOptions}
|
|
|
|
additionalFilter={additionalFilter}
|
|
|
|
/>
|
|
|
|
)
|
2022-06-14 09:35:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
return (
|
2022-06-05 19:06:08 +00:00
|
|
|
<InstantSearch searchClient={searchClient} indexName={indexName}>
|
2022-05-18 14:42:52 +00:00
|
|
|
<Row className="gap-1 sm:gap-2">
|
2022-05-09 17:38:33 +00:00
|
|
|
<SearchBox
|
|
|
|
className="flex-1"
|
2022-07-01 22:37:30 +00:00
|
|
|
placeholder={showPlaceHolder ? `Search ${filter} contracts` : ''}
|
2022-05-09 17:38:33 +00:00
|
|
|
classNames={{
|
|
|
|
form: 'before:top-6',
|
2022-05-18 14:42:52 +00:00
|
|
|
input: '!pl-10 !input !input-bordered shadow-none w-[100px]',
|
2022-07-10 02:04:50 +00:00
|
|
|
resetIcon: 'mt-2 hidden sm:flex',
|
2022-05-18 14:42:52 +00:00
|
|
|
}}
|
|
|
|
/>
|
2022-07-10 02:04:50 +00:00
|
|
|
<select
|
|
|
|
className="!select !select-bordered"
|
|
|
|
value={filter}
|
|
|
|
onChange={(e) => setFilter(e.target.value as filter)}
|
|
|
|
onBlur={trackCallback('select search filter')}
|
|
|
|
>
|
|
|
|
<option value="open">Open</option>
|
|
|
|
<option value="closed">Closed</option>
|
|
|
|
<option value="resolved">Resolved</option>
|
|
|
|
<option value="all">All</option>
|
|
|
|
</select>
|
|
|
|
{!hideOrderSelector && (
|
|
|
|
<SortBy
|
|
|
|
items={sortIndexes}
|
|
|
|
classNames={{
|
|
|
|
select: '!select !select-bordered',
|
|
|
|
}}
|
|
|
|
onBlur={trackCallback('select search sort')}
|
|
|
|
/>
|
2022-07-01 22:37:30 +00:00
|
|
|
)}
|
2022-06-05 19:06:08 +00:00
|
|
|
<Configure
|
|
|
|
facetFilters={filters}
|
|
|
|
numericFilters={numericFilters}
|
|
|
|
// Page resets on filters change.
|
|
|
|
page={0}
|
|
|
|
/>
|
2022-05-09 17:38:33 +00:00
|
|
|
</Row>
|
2022-05-18 14:42:52 +00:00
|
|
|
|
|
|
|
<Spacer h={3} />
|
|
|
|
|
2022-07-07 20:45:26 +00:00
|
|
|
<ContractSearchInner
|
|
|
|
querySortOptions={querySortOptions}
|
|
|
|
onContractClick={onContractClick}
|
|
|
|
overrideGridClassName={overrideGridClassName}
|
|
|
|
hideQuickBet={hideQuickBet}
|
|
|
|
excludeContractIds={additionalFilter?.excludeContractIds}
|
|
|
|
/>
|
2022-05-09 17:38:33 +00:00
|
|
|
</InstantSearch>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ContractSearchInner(props: {
|
|
|
|
querySortOptions?: {
|
|
|
|
defaultSort: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}
|
2022-06-05 00:00:13 +00:00
|
|
|
onContractClick?: (contract: Contract) => void
|
2022-07-01 22:37:30 +00:00
|
|
|
overrideGridClassName?: string
|
|
|
|
hideQuickBet?: boolean
|
|
|
|
excludeContractIds?: string[]
|
2022-05-09 17:38:33 +00:00
|
|
|
}) {
|
2022-07-01 22:37:30 +00:00
|
|
|
const {
|
|
|
|
querySortOptions,
|
|
|
|
onContractClick,
|
|
|
|
overrideGridClassName,
|
|
|
|
hideQuickBet,
|
|
|
|
excludeContractIds,
|
|
|
|
} = props
|
2022-05-09 17:38:33 +00:00
|
|
|
const { initialQuery } = useInitialQueryAndSort(querySortOptions)
|
|
|
|
|
|
|
|
const { query, setQuery, setSort } = useUpdateQueryAndSort({
|
|
|
|
shouldLoadFromStorage: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setQuery(initialQuery)
|
|
|
|
}, [initialQuery])
|
|
|
|
|
|
|
|
const { currentRefinement: index } = useSortBy({
|
|
|
|
items: [],
|
|
|
|
})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setQuery(query)
|
|
|
|
}, [query])
|
|
|
|
|
2022-05-13 19:01:38 +00:00
|
|
|
const isFirstRender = useRef(true)
|
2022-05-09 17:38:33 +00:00
|
|
|
useEffect(() => {
|
2022-05-13 19:01:38 +00:00
|
|
|
if (isFirstRender.current) {
|
|
|
|
isFirstRender.current = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
const sort = index.split('contracts-')[1] as Sort
|
|
|
|
if (sort) {
|
|
|
|
setSort(sort)
|
|
|
|
}
|
|
|
|
}, [index])
|
|
|
|
|
2022-05-21 17:51:41 +00:00
|
|
|
const [isInitialLoad, setIsInitialLoad] = useState(true)
|
|
|
|
useEffect(() => {
|
|
|
|
const id = setTimeout(() => setIsInitialLoad(false), 1000)
|
|
|
|
return () => clearTimeout(id)
|
|
|
|
}, [])
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-05-21 17:51:41 +00:00
|
|
|
const { showMore, hits, isLastPage } = useInfiniteHits()
|
2022-07-01 22:37:30 +00:00
|
|
|
let contracts = hits as any as Contract[]
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-05-21 17:51:41 +00:00
|
|
|
if (isInitialLoad && contracts.length === 0) return <></>
|
2022-05-17 17:56:10 +00:00
|
|
|
|
2022-06-23 17:12:57 +00:00
|
|
|
const showTime = index.endsWith('close-date')
|
|
|
|
? 'close-date'
|
|
|
|
: index.endsWith('resolve-date')
|
|
|
|
? 'resolve-date'
|
|
|
|
: undefined
|
|
|
|
|
2022-07-01 22:37:30 +00:00
|
|
|
if (excludeContractIds)
|
|
|
|
contracts = contracts.filter((c) => !excludeContractIds.includes(c.id))
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
return (
|
2022-05-17 17:56:10 +00:00
|
|
|
<ContractsGrid
|
|
|
|
contracts={contracts}
|
|
|
|
loadMore={showMore}
|
|
|
|
hasMore={!isLastPage}
|
2022-06-23 17:12:57 +00:00
|
|
|
showTime={showTime}
|
2022-06-05 00:00:13 +00:00
|
|
|
onContractClick={onContractClick}
|
2022-07-01 22:37:30 +00:00
|
|
|
overrideGridClassName={overrideGridClassName}
|
|
|
|
hideQuickBet={hideQuickBet}
|
2022-05-17 17:56:10 +00:00
|
|
|
/>
|
2022-05-09 17:38:33 +00:00
|
|
|
)
|
|
|
|
}
|