From 3e78bb44cad4388d0c0ca7f6b355e67caf2b1bac Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Mon, 29 Aug 2022 14:49:38 -0700 Subject: [PATCH] Respect options for persisting contract search --- web/components/contract-search.tsx | 43 +++++++++++++++---------- web/hooks/use-persistent-state.ts | 4 ++- web/pages/contract-search-firestore.tsx | 6 ++-- web/pages/home.tsx | 3 +- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/web/components/contract-search.tsx b/web/components/contract-search.tsx index 6a5bd0d3..90958c6e 100644 --- a/web/components/contract-search.tsx +++ b/web/components/contract-search.tsx @@ -16,10 +16,10 @@ import { useFollows } from 'web/hooks/use-follows' import { storageStore, historyStore, - urlParamsStore, + urlParamStore, usePersistentState, } from 'web/hooks/use-persistent-state' -import { safeSessionStorage } from 'web/lib/util/local' +import { safeLocalStorage } from 'web/lib/util/local' import { track, trackCallback } from 'web/lib/service/analytics' import ContractSearchFirestore from 'web/pages/contract-search-firestore' import { useMemberGroups } from 'web/hooks/use-group' @@ -81,8 +81,7 @@ export function ContractSearch(props: { } headerClassName?: string persistPrefix?: string - useQuerySortLocalStorage?: boolean - useQuerySortUrlParams?: boolean + useQueryUrlParam?: boolean isWholePage?: boolean maxItems?: number noControls?: boolean @@ -98,7 +97,7 @@ export function ContractSearch(props: { highlightOptions, headerClassName, persistPrefix, - useQuerySortUrlParams, + useQueryUrlParam, isWholePage, maxItems, noControls, @@ -110,7 +109,9 @@ export function ContractSearch(props: { pages: [] as Contract[][], showTime: null as ShowTime | null, }, - { key: `${persistPrefix}-search`, store: historyStore() } + !persistPrefix + ? undefined + : { key: `${persistPrefix}-search`, store: historyStore() } ) const searchParams = useRef(null) @@ -202,7 +203,7 @@ export function ContractSearch(props: { additionalFilter={additionalFilter} hideOrderSelector={hideOrderSelector} persistPrefix={persistPrefix ? `${persistPrefix}-controls` : undefined} - useQuerySortUrlParams={useQuerySortUrlParams} + useQueryUrlParam={useQueryUrlParam} user={user} onSearchParametersChanged={onSearchParametersChanged} noControls={noControls} @@ -227,7 +228,7 @@ function ContractSearchControls(props: { hideOrderSelector?: boolean onSearchParametersChanged: (params: SearchParameters) => void persistPrefix?: string - useQuerySortUrlParams?: boolean + useQueryUrlParam?: boolean user?: User | null noControls?: boolean }) { @@ -239,26 +240,34 @@ function ContractSearchControls(props: { hideOrderSelector, onSearchParametersChanged, persistPrefix, - useQuerySortUrlParams, + useQueryUrlParam, user, noControls, } = props const router = useRouter() - const [query, setQuery] = usePersistentState('', { - key: 'q', - store: urlParamsStore(router), - }) + const [query, setQuery] = usePersistentState( + '', + !useQueryUrlParam + ? undefined + : { + key: 'q', + store: urlParamStore(router), + } + ) + const [state, setState] = usePersistentState( { sort: defaultSort ?? 'score', filter: defaultFilter ?? 'open', pillFilter: null as string | null, }, - { - key: `${persistPrefix}-params`, - store: storageStore(safeSessionStorage()), - } + !persistPrefix + ? undefined + : { + key: `${persistPrefix}-params`, + store: storageStore(safeLocalStorage()), + } ) const follows = useFollows(user?.id) diff --git a/web/hooks/use-persistent-state.ts b/web/hooks/use-persistent-state.ts index 883a5750..55bd36c4 100644 --- a/web/hooks/use-persistent-state.ts +++ b/web/hooks/use-persistent-state.ts @@ -48,7 +48,7 @@ export const storageStore = (storage?: Storage): PersistentStore => ({ }, }) -export const urlParamsStore = (router: NextRouter) => ({ +export const urlParamStore = (router: NextRouter): PersistentStore => ({ get: (k: string) => { const v = router.query[k] return typeof v === 'string' ? v : undefined @@ -93,6 +93,8 @@ export const usePersistentState = ( ) => { const store = persist?.store const key = persist?.key + // note that it's important in some cases to get the state correct during the + // first render, or scroll restoration won't take into account the saved state const savedValue = key != null && store != null ? store.get(key) : undefined const [state, setState] = useStateCheckEquality(savedValue ?? initial) useEffect(() => { diff --git a/web/pages/contract-search-firestore.tsx b/web/pages/contract-search-firestore.tsx index 8f5d8fea..4691030c 100644 --- a/web/pages/contract-search-firestore.tsx +++ b/web/pages/contract-search-firestore.tsx @@ -6,7 +6,7 @@ import { ContractsGrid } from 'web/components/contract/contracts-grid' import { useContracts } from 'web/hooks/use-contracts' import { usePersistentState, - urlParamsStore, + urlParamStore, } from 'web/hooks/use-persistent-state' const MAX_CONTRACTS_RENDERED = 100 @@ -19,10 +19,10 @@ export default function ContractSearchFirestore(props: { groupSlug?: string } }) { - const contracts = useContracts() const { additionalFilter } = props + const contracts = useContracts() const router = useRouter() - const store = urlParamsStore(router) + const store = urlParamStore(router) const [query, setQuery] = usePersistentState('', { key: 'q', store }) const [sort, setSort] = usePersistentState('score', { key: 'sort', store }) diff --git a/web/pages/home.tsx b/web/pages/home.tsx index 1dadc1f0..65161398 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -34,8 +34,7 @@ const Home = (props: { auth: { user: User } | null }) => {