From 3b446565cba462c02f08e581cd37a9987ff0b240 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Sun, 28 Aug 2022 14:09:15 -0700 Subject: [PATCH] Tweaking stuff --- web/components/contract-search.tsx | 46 ++++++++++++------------------ web/hooks/use-persistent-state.ts | 22 +++++++------- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/web/components/contract-search.tsx b/web/components/contract-search.tsx index acaded5d..a76457c6 100644 --- a/web/components/contract-search.tsx +++ b/web/components/contract-search.tsx @@ -17,7 +17,6 @@ import { import { ShowTime } from './contract/contract-details' import { Row } from './layout/row' import { useEffect, useLayoutEffect, useRef, useMemo } from 'react' -import { unstable_batchedUpdates } from 'react-dom' import { ENV, IS_PRIVATE_MANIFOLD } from 'common/envs/constants' import { useFollows } from 'web/hooks/use-follows' import { @@ -103,14 +102,13 @@ export function ContractSearch(props: { return persistPrefix ? { prefix: persistPrefix, name, store } : undefined } - const [numPages, setNumPages] = usePersistentState(1, persistAs('numPages')) - const [pages, setPages] = usePersistentState( - [], - persistAs('pages') - ) - const [showTime, setShowTime] = usePersistentState( - null, - persistAs('showTime') + const [state, setState] = usePersistentState( + { + numPages: 1, + pages: [] as Contract[][], + showTime: null as ShowTime | null, + }, + persistAs('state') ) const searchParameters = useRef(null) @@ -139,8 +137,8 @@ export function ContractSearch(props: { const { query, sort, openClosedFilter, facetFilters } = searchParameters.current const id = ++requestId.current - const requestedPage = freshQuery ? 0 : pages.length - if (freshQuery || requestedPage < numPages) { + const requestedPage = freshQuery ? 0 : state.pages.length + if (freshQuery || requestedPage < state.numPages) { const index = query ? searchIndex : searchClient.initIndex(`${indexPrefix}contracts-${sort}`) @@ -161,20 +159,11 @@ export function ContractSearch(props: { const newPage = results.hits as any as Contract[] const showTime = sort === 'close-date' || sort === 'resolve-date' ? sort : null - - // this spooky looking function is the easiest way to get react to - // batch this and not do multiple renders. we can throw it out in react 18. - // 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]) - } + setState((curr) => { + const pages = freshQuery ? [newPage] : [...curr.pages, newPage] + return { numPages: results.nbPages, pages, showTime } }) + if (freshQuery && isWholePage) window.scrollTo(0, 0) } } } @@ -192,11 +181,13 @@ export function ContractSearch(props: { }, 100) ).current - const contracts = pages + const contracts = state.pages .flat() .filter((c) => !additionalFilter?.excludeContractIds?.includes(c.id)) 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) { return @@ -219,7 +210,7 @@ export function ContractSearch(props: { { + console.log('Calling updateQuery: ', newQuery) setQuery(newQuery) } diff --git a/web/hooks/use-persistent-state.ts b/web/hooks/use-persistent-state.ts index 27cabf52..81fc1b57 100644 --- a/web/hooks/use-persistent-state.ts +++ b/web/hooks/use-persistent-state.ts @@ -1,4 +1,5 @@ -import { useState, useEffect } from 'react' +import { useLayoutEffect, useEffect } from 'react' +import { useStateCheckEquality } from './use-state-check-equality' export type PersistenceOptions = { store?: Storage @@ -30,20 +31,21 @@ export const loadState = (key: string, store: Storage) => { } export const usePersistentState = ( - defaultValue: T, + initial: T, persist?: PersistenceOptions ) => { const store = persist?.store const key = persist ? getKey(persist.prefix, persist.name) : null - let initialValue - if (key != null && store != null) { - const saved = loadState(key, store) as T - console.log('Loading state for: ', key, saved) - if (saved !== undefined) { - initialValue = saved + useLayoutEffect(() => { + if (key != null && store != null) { + const saved = loadState(key, store) as T + console.log('Loading state for: ', key, saved) + if (saved !== undefined) { + setState(saved) + } } - } - const [state, setState] = useState(initialValue ?? defaultValue) + }, []) + const [state, setState] = useStateCheckEquality(initial) useEffect(() => { if (key != null && store != null) { console.log('Saving state for: ', key, state)