Tweaking stuff
This commit is contained in:
parent
50d7782e2c
commit
3b446565cb
|
@ -17,7 +17,6 @@ import {
|
||||||
import { ShowTime } from './contract/contract-details'
|
import { ShowTime } from './contract/contract-details'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { useEffect, useLayoutEffect, useRef, useMemo } from 'react'
|
import { useEffect, useLayoutEffect, useRef, useMemo } from 'react'
|
||||||
import { unstable_batchedUpdates } from 'react-dom'
|
|
||||||
import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants'
|
||||||
import { useFollows } from 'web/hooks/use-follows'
|
import { useFollows } from 'web/hooks/use-follows'
|
||||||
import {
|
import {
|
||||||
|
@ -103,14 +102,13 @@ export function ContractSearch(props: {
|
||||||
return persistPrefix ? { prefix: persistPrefix, name, store } : undefined
|
return persistPrefix ? { prefix: persistPrefix, name, store } : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const [numPages, setNumPages] = usePersistentState(1, persistAs('numPages'))
|
const [state, setState] = usePersistentState(
|
||||||
const [pages, setPages] = usePersistentState<Contract[][]>(
|
{
|
||||||
[],
|
numPages: 1,
|
||||||
persistAs('pages')
|
pages: [] as Contract[][],
|
||||||
)
|
showTime: null as ShowTime | null,
|
||||||
const [showTime, setShowTime] = usePersistentState<ShowTime | null>(
|
},
|
||||||
null,
|
persistAs('state')
|
||||||
persistAs('showTime')
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const searchParameters = useRef<SearchParameters | null>(null)
|
const searchParameters = useRef<SearchParameters | null>(null)
|
||||||
|
@ -139,8 +137,8 @@ export function ContractSearch(props: {
|
||||||
const { query, sort, openClosedFilter, facetFilters } =
|
const { query, sort, openClosedFilter, facetFilters } =
|
||||||
searchParameters.current
|
searchParameters.current
|
||||||
const id = ++requestId.current
|
const id = ++requestId.current
|
||||||
const requestedPage = freshQuery ? 0 : pages.length
|
const requestedPage = freshQuery ? 0 : state.pages.length
|
||||||
if (freshQuery || requestedPage < numPages) {
|
if (freshQuery || requestedPage < state.numPages) {
|
||||||
const index = query
|
const index = query
|
||||||
? searchIndex
|
? searchIndex
|
||||||
: searchClient.initIndex(`${indexPrefix}contracts-${sort}`)
|
: searchClient.initIndex(`${indexPrefix}contracts-${sort}`)
|
||||||
|
@ -161,20 +159,11 @@ export function ContractSearch(props: {
|
||||||
const newPage = results.hits as any as Contract[]
|
const newPage = results.hits as any as Contract[]
|
||||||
const showTime =
|
const showTime =
|
||||||
sort === 'close-date' || sort === 'resolve-date' ? sort : null
|
sort === 'close-date' || sort === 'resolve-date' ? sort : null
|
||||||
|
setState((curr) => {
|
||||||
// this spooky looking function is the easiest way to get react to
|
const pages = freshQuery ? [newPage] : [...curr.pages, newPage]
|
||||||
// batch this and not do multiple renders. we can throw it out in react 18.
|
return { numPages: results.nbPages, pages, showTime }
|
||||||
// see https://github.com/reactwg/react-18/discussions/21
|
|
||||||
unstable_batchedUpdates(() => {
|
|
||||||
setShowTime(showTime)
|
|
||||||
setNumPages(results.nbPages)
|
|
||||||
if (freshQuery) {
|
|
||||||
setPages([newPage])
|
|
||||||
if (isWholePage) window.scrollTo(0, 0)
|
|
||||||
} else {
|
|
||||||
setPages((pages) => [...pages, newPage])
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
if (freshQuery && isWholePage) window.scrollTo(0, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,11 +181,13 @@ export function ContractSearch(props: {
|
||||||
}, 100)
|
}, 100)
|
||||||
).current
|
).current
|
||||||
|
|
||||||
const contracts = pages
|
const contracts = state.pages
|
||||||
.flat()
|
.flat()
|
||||||
.filter((c) => !additionalFilter?.excludeContractIds?.includes(c.id))
|
.filter((c) => !additionalFilter?.excludeContractIds?.includes(c.id))
|
||||||
const renderedContracts =
|
const renderedContracts =
|
||||||
pages.length === 0 ? undefined : contracts.slice(0, maxItems)
|
state.pages.length === 0 ? undefined : contracts.slice(0, maxItems)
|
||||||
|
|
||||||
|
console.log('Rendering: ', renderedContracts)
|
||||||
|
|
||||||
if (IS_PRIVATE_MANIFOLD || process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
|
if (IS_PRIVATE_MANIFOLD || process.env.NEXT_PUBLIC_FIREBASE_EMULATE) {
|
||||||
return <ContractSearchFirestore additionalFilter={additionalFilter} />
|
return <ContractSearchFirestore additionalFilter={additionalFilter} />
|
||||||
|
@ -219,7 +210,7 @@ export function ContractSearch(props: {
|
||||||
<ContractsGrid
|
<ContractsGrid
|
||||||
contracts={renderedContracts}
|
contracts={renderedContracts}
|
||||||
loadMore={noControls ? undefined : performQuery}
|
loadMore={noControls ? undefined : performQuery}
|
||||||
showTime={showTime ?? undefined}
|
showTime={state.showTime ?? undefined}
|
||||||
onContractClick={onContractClick}
|
onContractClick={onContractClick}
|
||||||
highlightOptions={highlightOptions}
|
highlightOptions={highlightOptions}
|
||||||
cardHideOptions={cardHideOptions}
|
cardHideOptions={cardHideOptions}
|
||||||
|
@ -344,6 +335,7 @@ function ContractSearchControls(props: {
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateQuery = (newQuery: string) => {
|
const updateQuery = (newQuery: string) => {
|
||||||
|
console.log('Calling updateQuery: ', newQuery)
|
||||||
setQuery(newQuery)
|
setQuery(newQuery)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { useState, useEffect } from 'react'
|
import { useLayoutEffect, useEffect } from 'react'
|
||||||
|
import { useStateCheckEquality } from './use-state-check-equality'
|
||||||
|
|
||||||
export type PersistenceOptions = {
|
export type PersistenceOptions = {
|
||||||
store?: Storage
|
store?: Storage
|
||||||
|
@ -30,20 +31,21 @@ export const loadState = (key: string, store: Storage) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const usePersistentState = <T>(
|
export const usePersistentState = <T>(
|
||||||
defaultValue: T,
|
initial: T,
|
||||||
persist?: PersistenceOptions
|
persist?: PersistenceOptions
|
||||||
) => {
|
) => {
|
||||||
const store = persist?.store
|
const store = persist?.store
|
||||||
const key = persist ? getKey(persist.prefix, persist.name) : null
|
const key = persist ? getKey(persist.prefix, persist.name) : null
|
||||||
let initialValue
|
useLayoutEffect(() => {
|
||||||
if (key != null && store != null) {
|
if (key != null && store != null) {
|
||||||
const saved = loadState(key, store) as T
|
const saved = loadState(key, store) as T
|
||||||
console.log('Loading state for: ', key, saved)
|
console.log('Loading state for: ', key, saved)
|
||||||
if (saved !== undefined) {
|
if (saved !== undefined) {
|
||||||
initialValue = saved
|
setState(saved)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}, [])
|
||||||
const [state, setState] = useState<T>(initialValue ?? defaultValue)
|
const [state, setState] = useStateCheckEquality(initial)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (key != null && store != null) {
|
if (key != null && store != null) {
|
||||||
console.log('Saving state for: ', key, state)
|
console.log('Saving state for: ', key, state)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user