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-07-21 17:45:47 +00:00
|
|
|
import { track, 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'
|
2022-07-20 05:23:00 +00:00
|
|
|
import { Group, NEW_USER_GROUP_SLUGS } from 'common/group'
|
2022-07-15 20:06:33 +00:00
|
|
|
import { PillButton } from './buttons/pill-button'
|
2022-07-19 23:22:55 +00:00
|
|
|
import { sortBy } from 'lodash'
|
2022-07-20 05:23:00 +00:00
|
|
|
import { DEFAULT_CATEGORY_GROUPS } from 'common/categories'
|
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-19 22:29:41 +00:00
|
|
|
{ label: 'Most popular', value: indexPrefix + 'contracts-score' },
|
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-19 22:29:41 +00:00
|
|
|
export const DEFAULT_SORT = 'score'
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
type filter = 'personal' | 'open' | 'closed' | 'resolved' | 'all'
|
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()
|
2022-07-19 23:22:55 +00:00
|
|
|
const memberGroups = (useMemberGroups(user?.id) ?? []).filter(
|
|
|
|
(group) => !NEW_USER_GROUP_SLUGS.includes(group.slug)
|
|
|
|
)
|
2022-07-20 05:23:00 +00:00
|
|
|
const memberGroupSlugs =
|
|
|
|
memberGroups.length > 0
|
|
|
|
? memberGroups.map((g) => g.slug)
|
|
|
|
: DEFAULT_CATEGORY_GROUPS.map((g) => g.slug)
|
|
|
|
|
|
|
|
const memberPillGroups = sortBy(
|
2022-07-19 23:22:55 +00:00
|
|
|
memberGroups.filter((group) => group.contractIds.length > 0),
|
|
|
|
(group) => group.contractIds.length
|
|
|
|
).reverse()
|
2022-07-20 05:23:00 +00:00
|
|
|
|
|
|
|
const defaultPillGroups = DEFAULT_CATEGORY_GROUPS as Group[]
|
|
|
|
|
|
|
|
const pillGroups =
|
|
|
|
memberPillGroups.length > 0 ? memberPillGroups : defaultPillGroups
|
|
|
|
|
2022-07-13 21:11:22 +00:00
|
|
|
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-19 22:29:41 +00:00
|
|
|
: querySortOptions?.defaultSort ?? DEFAULT_SORT
|
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-07-20 19:49:14 +00:00
|
|
|
const pillsEnabled = !additionalFilter
|
2022-07-21 17:45:47 +00:00
|
|
|
|
2022-07-20 20:05:48 +00:00
|
|
|
const [pillFilter, setPillFilter] = useState<string | undefined>(undefined)
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-07-21 17:45:47 +00:00
|
|
|
const selectFilter = (pill: string | undefined) => () => {
|
|
|
|
setPillFilter(pill)
|
|
|
|
track('select search category', { category: pill ?? 'all' })
|
|
|
|
}
|
|
|
|
|
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-19 23:22:55 +00:00
|
|
|
additionalFilter?.creatorId
|
|
|
|
? `creatorId:${additionalFilter.creatorId}`
|
|
|
|
: '',
|
|
|
|
additionalFilter?.tag ? `lowercaseTags:${additionalFilter.tag}` : '',
|
|
|
|
additionalFilter?.groupSlug
|
|
|
|
? `groupSlugs:${additionalFilter.groupSlug}`
|
|
|
|
: '',
|
2022-07-20 20:30:07 +00:00
|
|
|
pillFilter && pillFilter !== 'personal' && pillFilter !== 'your-bets'
|
|
|
|
? `groupSlugs:${pillFilter}`
|
|
|
|
: '',
|
2022-07-19 23:22:55 +00:00
|
|
|
pillFilter === 'personal'
|
2022-07-13 21:11:22 +00:00
|
|
|
? // Show contracts in groups that the user is a member of
|
2022-07-19 23:22:55 +00:00
|
|
|
memberGroupSlugs
|
|
|
|
.map((slug) => `groupSlugs:${slug}`)
|
2022-07-13 21:11:22 +00:00
|
|
|
// 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}`) ?? []
|
|
|
|
)
|
2022-07-20 20:30:07 +00:00
|
|
|
: '',
|
|
|
|
// Subtract contracts you bet on from For you.
|
|
|
|
pillFilter === 'personal' && user ? `uniqueBettorIds:-${user.id}` : '',
|
|
|
|
pillFilter === 'your-bets' && user
|
|
|
|
? // Show contracts bet on by the user
|
|
|
|
`uniqueBettorIds:${user.id}`
|
2022-07-13 21:11:22 +00:00
|
|
|
: '',
|
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(','),
|
2022-07-19 23:22:55 +00:00
|
|
|
memberGroupSlugs.join(','),
|
2022-07-13 21:11:22 +00:00
|
|
|
(follows ?? []).join(','),
|
2022-07-19 23:22:55 +00:00
|
|
|
pillFilter,
|
2022-07-13 21:11:22 +00:00
|
|
|
])
|
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-19 23:22:55 +00:00
|
|
|
<select
|
|
|
|
className="!select !select-bordered"
|
|
|
|
value={filter}
|
|
|
|
onChange={(e) => setFilter(e.target.value as filter)}
|
2022-07-21 17:45:47 +00:00
|
|
|
onBlur={trackCallback('select search filter', { filter })}
|
2022-07-19 23:22:55 +00:00
|
|
|
>
|
|
|
|
<option value="open">Open</option>
|
|
|
|
<option value="closed">Closed</option>
|
|
|
|
<option value="resolved">Resolved</option>
|
|
|
|
<option value="all">All</option>
|
|
|
|
</select>
|
2022-07-10 02:04:50 +00:00
|
|
|
{!hideOrderSelector && (
|
|
|
|
<SortBy
|
|
|
|
items={sortIndexes}
|
|
|
|
classNames={{
|
|
|
|
select: '!select !select-bordered',
|
|
|
|
}}
|
2022-07-21 17:45:47 +00:00
|
|
|
onBlur={trackCallback('select search sort', { sort })}
|
2022-07-10 02:04:50 +00:00
|
|
|
/>
|
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-20 19:49:14 +00:00
|
|
|
{pillsEnabled && (
|
2022-07-19 23:22:55 +00:00
|
|
|
<Row className="scrollbar-hide items-start gap-2 overflow-x-auto">
|
|
|
|
<PillButton
|
|
|
|
key={'all'}
|
|
|
|
selected={pillFilter === undefined}
|
2022-07-21 17:45:47 +00:00
|
|
|
onSelect={selectFilter(undefined)}
|
2022-07-19 23:22:55 +00:00
|
|
|
>
|
|
|
|
All
|
|
|
|
</PillButton>
|
|
|
|
<PillButton
|
|
|
|
key={'personal'}
|
|
|
|
selected={pillFilter === 'personal'}
|
2022-07-21 17:45:47 +00:00
|
|
|
onSelect={selectFilter('personal')}
|
2022-07-19 23:22:55 +00:00
|
|
|
>
|
|
|
|
For you
|
|
|
|
</PillButton>
|
2022-07-20 05:23:00 +00:00
|
|
|
|
2022-07-20 20:30:07 +00:00
|
|
|
<PillButton
|
|
|
|
key={'your-bets'}
|
|
|
|
selected={pillFilter === 'your-bets'}
|
2022-07-21 17:45:47 +00:00
|
|
|
onSelect={selectFilter('your-bets')}
|
2022-07-20 20:30:07 +00:00
|
|
|
>
|
|
|
|
Your bets
|
|
|
|
</PillButton>
|
|
|
|
|
2022-07-19 23:22:55 +00:00
|
|
|
{pillGroups.map(({ name, slug }) => {
|
|
|
|
return (
|
|
|
|
<PillButton
|
|
|
|
key={slug}
|
|
|
|
selected={pillFilter === slug}
|
2022-07-21 17:45:47 +00:00
|
|
|
onSelect={selectFilter(slug)}
|
2022-07-19 23:22:55 +00:00
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</PillButton>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</Row>
|
|
|
|
)}
|
2022-07-15 20:06:33 +00:00
|
|
|
|
|
|
|
<Spacer h={3} />
|
2022-07-13 21:11:22 +00:00
|
|
|
|
|
|
|
{filter === 'personal' &&
|
|
|
|
(follows ?? []).length === 0 &&
|
2022-07-19 23:22:55 +00:00
|
|
|
memberGroupSlugs.length === 0 ? (
|
2022-07-13 21:11:22 +00:00
|
|
|
<>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
|
|
|
)
|
|
|
|
}
|