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 {
|
|
|
|
InstantSearch,
|
|
|
|
SearchBox,
|
|
|
|
SortBy,
|
2022-05-17 17:56:10 +00:00
|
|
|
useCurrentRefinements,
|
2022-05-09 17:38:33 +00:00
|
|
|
useInfiniteHits,
|
|
|
|
useRange,
|
|
|
|
useRefinementList,
|
|
|
|
useSortBy,
|
|
|
|
} from 'react-instantsearch-hooks-web'
|
|
|
|
import { Contract } from '../../common/contract'
|
|
|
|
import {
|
|
|
|
Sort,
|
|
|
|
useInitialQueryAndSort,
|
|
|
|
useUpdateQueryAndSort,
|
|
|
|
} from '../hooks/use-sort-and-query-params'
|
|
|
|
import { ContractsGrid } from './contract/contracts-list'
|
|
|
|
import { Row } from './layout/row'
|
2022-05-13 19:01:38 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2022-05-09 17:38:33 +00:00
|
|
|
import { Spacer } from './layout/spacer'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { ENV } from 'common/envs/constants'
|
2022-05-17 17:56:10 +00:00
|
|
|
import { CategorySelector } from './feed/category-selector'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
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
|
|
|
|
category?: string
|
|
|
|
}
|
|
|
|
showCategorySelector: boolean
|
2022-05-09 17:38:33 +00:00
|
|
|
}) {
|
2022-05-17 17:56:10 +00:00
|
|
|
const { querySortOptions, additionalFilter, showCategorySelector } = props
|
2022-05-09 17:38:33 +00:00
|
|
|
|
2022-05-17 17:56:10 +00:00
|
|
|
const user = useUser()
|
2022-05-09 17:38:33 +00:00
|
|
|
const { initialSort } = useInitialQueryAndSort(querySortOptions)
|
|
|
|
|
|
|
|
const sort = sortIndexes
|
|
|
|
.map(({ value }) => value)
|
|
|
|
.includes(`${indexPrefix}contracts-${initialSort ?? ''}`)
|
|
|
|
? initialSort
|
|
|
|
: querySortOptions?.defaultSort
|
|
|
|
|
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-05-17 17:56:10 +00:00
|
|
|
const [category, setCategory] = useState<string>('all')
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
if (!sort) return <></>
|
|
|
|
return (
|
|
|
|
<InstantSearch
|
|
|
|
searchClient={searchClient}
|
|
|
|
indexName={`${indexPrefix}contracts-${sort}`}
|
2022-05-09 19:47:18 +00:00
|
|
|
key={`search-${
|
2022-05-17 17:56:10 +00:00
|
|
|
additionalFilter?.tag ?? additionalFilter?.creatorId ?? ''
|
2022-05-09 19:47:18 +00:00
|
|
|
}`}
|
2022-05-09 17:38:33 +00:00
|
|
|
>
|
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"
|
|
|
|
classNames={{
|
|
|
|
form: 'before:top-6',
|
2022-05-18 14:42:52 +00:00
|
|
|
input: '!pl-10 !input !input-bordered shadow-none w-[100px]',
|
|
|
|
resetIcon: 'mt-2 hidden sm:flex',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<select
|
|
|
|
className="!select !select-bordered"
|
|
|
|
value={filter}
|
|
|
|
onChange={(e) => setFilter(e.target.value as filter)}
|
|
|
|
>
|
|
|
|
<option value="open">Open</option>
|
|
|
|
<option value="closed">Closed</option>
|
|
|
|
<option value="resolved">Resolved</option>
|
|
|
|
<option value="all">All</option>
|
|
|
|
</select>
|
|
|
|
<SortBy
|
|
|
|
items={sortIndexes}
|
|
|
|
classNames={{
|
|
|
|
select: '!select !select-bordered',
|
2022-05-09 17:38:33 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Row>
|
2022-05-18 14:42:52 +00:00
|
|
|
|
|
|
|
<Spacer h={3} />
|
|
|
|
|
|
|
|
{showCategorySelector && (
|
|
|
|
<CategorySelector
|
|
|
|
className="mb-2"
|
|
|
|
user={user}
|
|
|
|
category={category}
|
|
|
|
setCategory={setCategory}
|
2022-05-17 17:56:10 +00:00
|
|
|
/>
|
2022-05-18 14:42:52 +00:00
|
|
|
)}
|
|
|
|
|
|
|
|
<ContractSearchInner
|
|
|
|
querySortOptions={querySortOptions}
|
|
|
|
filter={filter}
|
|
|
|
additionalFilter={{ category, ...additionalFilter }}
|
|
|
|
/>
|
2022-05-09 17:38:33 +00:00
|
|
|
</InstantSearch>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ContractSearchInner(props: {
|
|
|
|
querySortOptions?: {
|
|
|
|
defaultSort: Sort
|
|
|
|
shouldLoadFromStorage?: boolean
|
|
|
|
}
|
|
|
|
filter: filter
|
2022-05-17 17:56:10 +00:00
|
|
|
additionalFilter: {
|
|
|
|
creatorId?: string
|
|
|
|
tag?: string
|
|
|
|
category?: string
|
|
|
|
}
|
2022-05-09 17:38:33 +00:00
|
|
|
}) {
|
2022-05-17 17:56:10 +00:00
|
|
|
const { querySortOptions, filter, additionalFilter } = 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-17 17:56:10 +00:00
|
|
|
const { creatorId, category, tag } = additionalFilter
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
useFilterCreator(creatorId)
|
|
|
|
|
2022-05-17 17:56:10 +00:00
|
|
|
useFilterTag(tag ?? (category === 'all' ? undefined : category))
|
2022-05-09 17:38:33 +00:00
|
|
|
|
|
|
|
useFilterClosed(
|
|
|
|
filter === 'closed'
|
|
|
|
? true
|
|
|
|
: filter === 'all' || filter === 'resolved'
|
|
|
|
? undefined
|
|
|
|
: false
|
|
|
|
)
|
|
|
|
useFilterResolved(
|
|
|
|
filter === 'resolved' ? true : filter === 'all' ? undefined : false
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
const 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-05-09 17:38:33 +00:00
|
|
|
return (
|
2022-05-17 17:56:10 +00:00
|
|
|
<ContractsGrid
|
|
|
|
contracts={contracts}
|
|
|
|
loadMore={showMore}
|
|
|
|
hasMore={!isLastPage}
|
2022-05-18 21:06:13 +00:00
|
|
|
showCloseTime={index.endsWith('close-date')}
|
2022-05-17 17:56:10 +00:00
|
|
|
/>
|
2022-05-09 17:38:33 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const useFilterCreator = (creatorId: string | undefined) => {
|
|
|
|
const { refine } = useRefinementList({ attribute: 'creatorId' })
|
|
|
|
useEffect(() => {
|
|
|
|
if (creatorId) refine(creatorId)
|
|
|
|
}, [creatorId, refine])
|
|
|
|
}
|
|
|
|
|
|
|
|
const useFilterTag = (tag: string | undefined) => {
|
2022-05-17 17:56:10 +00:00
|
|
|
const { items, refine: deleteRefinement } = useCurrentRefinements({
|
|
|
|
includedAttributes: ['lowercaseTags'],
|
|
|
|
})
|
2022-05-09 17:38:33 +00:00
|
|
|
const { refine } = useRefinementList({ attribute: 'lowercaseTags' })
|
|
|
|
useEffect(() => {
|
2022-05-17 17:56:10 +00:00
|
|
|
const refinements = items[0]?.refinements ?? []
|
2022-05-09 17:38:33 +00:00
|
|
|
if (tag) refine(tag.toLowerCase())
|
2022-05-17 17:56:10 +00:00
|
|
|
if (refinements[0]) deleteRefinement(refinements[0])
|
|
|
|
}, [tag])
|
2022-05-09 17:38:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const useFilterClosed = (value: boolean | undefined) => {
|
|
|
|
const [now] = useState(Date.now())
|
|
|
|
useRange({
|
|
|
|
attribute: 'closeTime',
|
|
|
|
min: value === false ? now : undefined,
|
|
|
|
max: value ? now : undefined,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const useFilterResolved = (value: boolean | undefined) => {
|
2022-05-20 02:13:54 +00:00
|
|
|
const { items, refine: deleteRefinement } = useCurrentRefinements({
|
|
|
|
includedAttributes: ['isResolved'],
|
2022-05-09 17:38:33 +00:00
|
|
|
})
|
2022-05-20 02:13:54 +00:00
|
|
|
|
|
|
|
const { refine } = useRefinementList({ attribute: 'isResolved' })
|
|
|
|
|
2022-05-09 17:38:33 +00:00
|
|
|
useEffect(() => {
|
2022-05-20 02:13:54 +00:00
|
|
|
const refinements = items[0]?.refinements ?? []
|
|
|
|
|
|
|
|
if (value !== undefined) refine(`${value}`)
|
|
|
|
refinements.forEach((refinement) => deleteRefinement(refinement))
|
2022-05-09 17:38:33 +00:00
|
|
|
}, [value])
|
|
|
|
}
|