Respect options for persisting contract search

This commit is contained in:
Marshall Polaris 2022-08-29 14:49:38 -07:00
parent 01075a7e1f
commit 3e78bb44ca
4 changed files with 33 additions and 23 deletions

View File

@ -16,10 +16,10 @@ import { useFollows } from 'web/hooks/use-follows'
import { import {
storageStore, storageStore,
historyStore, historyStore,
urlParamsStore, urlParamStore,
usePersistentState, usePersistentState,
} from 'web/hooks/use-persistent-state' } 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 { track, trackCallback } from 'web/lib/service/analytics'
import ContractSearchFirestore from 'web/pages/contract-search-firestore' import ContractSearchFirestore from 'web/pages/contract-search-firestore'
import { useMemberGroups } from 'web/hooks/use-group' import { useMemberGroups } from 'web/hooks/use-group'
@ -81,8 +81,7 @@ export function ContractSearch(props: {
} }
headerClassName?: string headerClassName?: string
persistPrefix?: string persistPrefix?: string
useQuerySortLocalStorage?: boolean useQueryUrlParam?: boolean
useQuerySortUrlParams?: boolean
isWholePage?: boolean isWholePage?: boolean
maxItems?: number maxItems?: number
noControls?: boolean noControls?: boolean
@ -98,7 +97,7 @@ export function ContractSearch(props: {
highlightOptions, highlightOptions,
headerClassName, headerClassName,
persistPrefix, persistPrefix,
useQuerySortUrlParams, useQueryUrlParam,
isWholePage, isWholePage,
maxItems, maxItems,
noControls, noControls,
@ -110,7 +109,9 @@ export function ContractSearch(props: {
pages: [] as Contract[][], pages: [] as Contract[][],
showTime: null as ShowTime | null, showTime: null as ShowTime | null,
}, },
{ key: `${persistPrefix}-search`, store: historyStore() } !persistPrefix
? undefined
: { key: `${persistPrefix}-search`, store: historyStore() }
) )
const searchParams = useRef<SearchParameters | null>(null) const searchParams = useRef<SearchParameters | null>(null)
@ -202,7 +203,7 @@ export function ContractSearch(props: {
additionalFilter={additionalFilter} additionalFilter={additionalFilter}
hideOrderSelector={hideOrderSelector} hideOrderSelector={hideOrderSelector}
persistPrefix={persistPrefix ? `${persistPrefix}-controls` : undefined} persistPrefix={persistPrefix ? `${persistPrefix}-controls` : undefined}
useQuerySortUrlParams={useQuerySortUrlParams} useQueryUrlParam={useQueryUrlParam}
user={user} user={user}
onSearchParametersChanged={onSearchParametersChanged} onSearchParametersChanged={onSearchParametersChanged}
noControls={noControls} noControls={noControls}
@ -227,7 +228,7 @@ function ContractSearchControls(props: {
hideOrderSelector?: boolean hideOrderSelector?: boolean
onSearchParametersChanged: (params: SearchParameters) => void onSearchParametersChanged: (params: SearchParameters) => void
persistPrefix?: string persistPrefix?: string
useQuerySortUrlParams?: boolean useQueryUrlParam?: boolean
user?: User | null user?: User | null
noControls?: boolean noControls?: boolean
}) { }) {
@ -239,26 +240,34 @@ function ContractSearchControls(props: {
hideOrderSelector, hideOrderSelector,
onSearchParametersChanged, onSearchParametersChanged,
persistPrefix, persistPrefix,
useQuerySortUrlParams, useQueryUrlParam,
user, user,
noControls, noControls,
} = props } = props
const router = useRouter() const router = useRouter()
const [query, setQuery] = usePersistentState('', { const [query, setQuery] = usePersistentState(
key: 'q', '',
store: urlParamsStore(router), !useQueryUrlParam
}) ? undefined
: {
key: 'q',
store: urlParamStore(router),
}
)
const [state, setState] = usePersistentState( const [state, setState] = usePersistentState(
{ {
sort: defaultSort ?? 'score', sort: defaultSort ?? 'score',
filter: defaultFilter ?? 'open', filter: defaultFilter ?? 'open',
pillFilter: null as string | null, pillFilter: null as string | null,
}, },
{ !persistPrefix
key: `${persistPrefix}-params`, ? undefined
store: storageStore(safeSessionStorage()), : {
} key: `${persistPrefix}-params`,
store: storageStore(safeLocalStorage()),
}
) )
const follows = useFollows(user?.id) const follows = useFollows(user?.id)

View File

@ -48,7 +48,7 @@ export const storageStore = <T>(storage?: Storage): PersistentStore<T> => ({
}, },
}) })
export const urlParamsStore = (router: NextRouter) => ({ export const urlParamStore = (router: NextRouter): PersistentStore<string> => ({
get: (k: string) => { get: (k: string) => {
const v = router.query[k] const v = router.query[k]
return typeof v === 'string' ? v : undefined return typeof v === 'string' ? v : undefined
@ -93,6 +93,8 @@ export const usePersistentState = <T>(
) => { ) => {
const store = persist?.store const store = persist?.store
const key = persist?.key 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 savedValue = key != null && store != null ? store.get(key) : undefined
const [state, setState] = useStateCheckEquality(savedValue ?? initial) const [state, setState] = useStateCheckEquality(savedValue ?? initial)
useEffect(() => { useEffect(() => {

View File

@ -6,7 +6,7 @@ import { ContractsGrid } from 'web/components/contract/contracts-grid'
import { useContracts } from 'web/hooks/use-contracts' import { useContracts } from 'web/hooks/use-contracts'
import { import {
usePersistentState, usePersistentState,
urlParamsStore, urlParamStore,
} from 'web/hooks/use-persistent-state' } from 'web/hooks/use-persistent-state'
const MAX_CONTRACTS_RENDERED = 100 const MAX_CONTRACTS_RENDERED = 100
@ -19,10 +19,10 @@ export default function ContractSearchFirestore(props: {
groupSlug?: string groupSlug?: string
} }
}) { }) {
const contracts = useContracts()
const { additionalFilter } = props const { additionalFilter } = props
const contracts = useContracts()
const router = useRouter() const router = useRouter()
const store = urlParamsStore(router) const store = urlParamStore(router)
const [query, setQuery] = usePersistentState('', { key: 'q', store }) const [query, setQuery] = usePersistentState('', { key: 'q', store })
const [sort, setSort] = usePersistentState('score', { key: 'sort', store }) const [sort, setSort] = usePersistentState('score', { key: 'sort', store })

View File

@ -34,8 +34,7 @@ const Home = (props: { auth: { user: User } | null }) => {
<ContractSearch <ContractSearch
user={user} user={user}
persistPrefix="home-search" persistPrefix="home-search"
useQuerySortLocalStorage={true} useQueryUrlParam={true}
useQuerySortUrlParams={true}
/> />
</Col> </Col>
<button <button