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-07-13 21:11:22 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { useFollows } from 'web/hooks/use-follows'
|
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-07-13 21:11:22 +00:00
|
|
|
import { useMemberGroups } from 'web/hooks/use-group'
|
|
|
|
import { NEW_USER_GROUP_SLUGS } from 'common/group'
|
2022-07-15 20:06:33 +00:00
|
|
|
import { PillButton } from './buttons/pill-button'
|
|
|
|
import { toPairs } from 'lodash'
|
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' },
|
2022-07-13 21:11:22 +00:00
|
|
|
{ label: 'Most popular', value: indexPrefix + 'contracts-most-popular' },
|
2022-05-09 17:38:33 +00:00
|
|
|
{ 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' },
|
|
|
|
]
|
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
type filter = 'personal' | 'open' | 'closed' | 'resolved' | 'all'
|
2022-07-15 20:06:33 +00:00
|
|
|
const filterOptions: { [label: string]: filter } = {
|
|
|
|
All: 'all',
|
|
|
|
Open: 'open',
|
|
|
|
Closed: 'closed',
|
|
|
|
Resolved: 'resolved',
|
|
|
|
'For you': 'personal',
|
|
|
|
}
|
2022-05-09 17:38:33 +00:00
|
|
|
|
|
|
|
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-07-13 22:20:56 +00:00
|
|
|
groupSlug?: 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
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
const user = useUser()
|
|
|
|
const memberGroupSlugs = useMemberGroups(user?.id)
|
|
|
|
?.map((g) => g.slug)
|
|
|
|
.filter((s) => !NEW_USER_GROUP_SLUGS.includes(s))
|
|
|
|
const follows = useFollows(user?.id)
|
2022-05-09 17:38:33 +00:00
|
|
|
const { initialSort } = useInitialQueryAndSort(querySortOptions)
|
|
|
|
|
|
|
|
const sort = sortIndexes
|
|
|
|
.map(({ value }) => value)
|
|
|
|
.includes(`${indexPrefix}contracts-${initialSort ?? ''}`)
|
|
|
|
? initialSort
|
2022-07-13 21:11:22 +00:00
|
|
|
: querySortOptions?.defaultSort ?? 'most-popular'
|
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' : '',
|
2022-07-13 21:11:22 +00:00
|
|
|
filter === 'personal'
|
|
|
|
? // Show contracts in groups that the user is a member of
|
|
|
|
(memberGroupSlugs?.map((slug) => `groupSlugs:${slug}`) ?? [])
|
|
|
|
// Show contracts created by users the user follows
|
|
|
|
.concat(follows?.map((followId) => `creatorId:${followId}`) ?? [])
|
|
|
|
// Show contracts bet on by users the user follows
|
|
|
|
.concat(
|
|
|
|
follows?.map((followId) => `uniqueBettorIds:${followId}`) ?? []
|
|
|
|
// Show contracts bet on by the user
|
|
|
|
)
|
|
|
|
.concat(user ? `uniqueBettorIds:${user.id}` : [])
|
|
|
|
: '',
|
2022-06-05 19:06:08 +00:00
|
|
|
additionalFilter?.creatorId
|
|
|
|
? `creatorId:${additionalFilter.creatorId}`
|
|
|
|
: '',
|
2022-07-16 23:56:21 +00:00
|
|
|
additionalFilter?.tag ? `lowercaseTags:${additionalFilter.tag}` : '',
|
2022-07-13 22:20:56 +00:00
|
|
|
additionalFilter?.groupSlug
|
|
|
|
? `groupSlugs:${additionalFilter.groupSlug}`
|
|
|
|
: '',
|
2022-06-05 19:06:08 +00:00
|
|
|
].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-13 21:11:22 +00:00
|
|
|
}, [
|
|
|
|
filter,
|
|
|
|
Object.values(additionalFilter ?? {}).join(','),
|
|
|
|
(memberGroupSlugs ?? []).join(','),
|
|
|
|
(follows ?? []).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-13 21:11:22 +00:00
|
|
|
{/*// TODO track WHICH filter users are using*/}
|
2022-07-10 02:04:50 +00:00
|
|
|
{!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-15 20:06:33 +00:00
|
|
|
<Row className="gap-2">
|
|
|
|
{toPairs<filter>(filterOptions).map(([label, f]) => {
|
|
|
|
return (
|
2022-07-18 23:37:46 +00:00
|
|
|
<PillButton
|
|
|
|
key={f}
|
|
|
|
selected={filter === f}
|
|
|
|
onSelect={() => setFilter(f)}
|
|
|
|
>
|
2022-07-15 20:06:33 +00:00
|
|
|
{label}
|
|
|
|
</PillButton>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
<Spacer h={3} />
|
2022-07-13 21:11:22 +00:00
|
|
|
|
|
|
|
{filter === 'personal' &&
|
|
|
|
(follows ?? []).length === 0 &&
|
|
|
|
(memberGroupSlugs ?? []).length === 0 ? (
|
|
|
|
<>You're not following anyone, nor in any of your own groups yet.</>
|
|
|
|
) : (
|
|
|
|
<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
|
|
|
)
|
|
|
|
}
|