Save last sort to local storage

This commit is contained in:
James Grugett 2022-09-16 17:09:12 -05:00
parent 22d5c74818
commit ab3ed3fbf1
3 changed files with 17 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import { debounce, isEqual, sortBy } from 'lodash'
import { DEFAULT_CATEGORY_GROUPS } from 'common/categories' import { DEFAULT_CATEGORY_GROUPS } from 'common/categories'
import { Col } from './layout/col' import { Col } from './layout/col'
import clsx from 'clsx' import clsx from 'clsx'
import { safeLocalStorage } from 'web/lib/util/local'
const searchClient = algoliasearch( const searchClient = algoliasearch(
'GJQPAYENIF', 'GJQPAYENIF',
@ -209,6 +210,7 @@ export function ContractSearch(props: {
defaultFilter={defaultFilter} defaultFilter={defaultFilter}
defaultPill={defaultPill} defaultPill={defaultPill}
additionalFilter={additionalFilter} additionalFilter={additionalFilter}
persistPrefix={persistPrefix}
hideOrderSelector={hideOrderSelector} hideOrderSelector={hideOrderSelector}
useQueryUrlParam={useQueryUrlParam} useQueryUrlParam={useQueryUrlParam}
user={user} user={user}
@ -238,6 +240,7 @@ function ContractSearchControls(props: {
defaultFilter?: filter defaultFilter?: filter
defaultPill?: string defaultPill?: string
additionalFilter?: AdditionalFilter additionalFilter?: AdditionalFilter
persistPrefix?: string
hideOrderSelector?: boolean hideOrderSelector?: boolean
onSearchParametersChanged: (params: SearchParameters) => void onSearchParametersChanged: (params: SearchParameters) => void
useQueryUrlParam?: boolean useQueryUrlParam?: boolean
@ -251,6 +254,7 @@ function ContractSearchControls(props: {
defaultFilter, defaultFilter,
defaultPill, defaultPill,
additionalFilter, additionalFilter,
persistPrefix,
hideOrderSelector, hideOrderSelector,
onSearchParametersChanged, onSearchParametersChanged,
useQueryUrlParam, useQueryUrlParam,
@ -270,8 +274,11 @@ function ContractSearchControls(props: {
} }
) )
const sortKey = `${persistPrefix}-search-sort`
const savedSort = safeLocalStorage()?.getItem(sortKey)
const [sort, setSort] = usePersistentState( const [sort, setSort] = usePersistentState(
defaultSort ?? 'score', savedSort ?? defaultSort ?? 'score',
!useQueryUrlParam !useQueryUrlParam
? undefined ? undefined
: { : {
@ -298,6 +305,12 @@ function ContractSearchControls(props: {
} }
) )
useEffect(() => {
if (persistPrefix && sort) {
safeLocalStorage()?.setItem(sortKey, sort as string)
}
}, [persistPrefix, query, sort, sortKey])
const follows = useFollows(user?.id) const follows = useFollows(user?.id)
const memberGroups = (useMemberGroups(user?.id) ?? []).filter( const memberGroups = (useMemberGroups(user?.id) ?? []).filter(
(group) => !NEW_USER_GROUP_SLUGS.includes(group.slug) (group) => !NEW_USER_GROUP_SLUGS.includes(group.slug)

View File

@ -77,7 +77,7 @@ export default function Home() {
<Col className="pm:mx-10 gap-4 px-4 pb-12 pt-4 sm:pt-0"> <Col className="pm:mx-10 gap-4 px-4 pb-12 pt-4 sm:pt-0">
<Row className={'mb-2 w-full items-center gap-8'}> <Row className={'mb-2 w-full items-center gap-8'}>
<SearchRow /> <SearchRow />
<DailyStats className="" user={user} /> <DailyStats user={user} />
</Row> </Row>
{sections.map((section) => renderSection(section, user, groups))} {sections.map((section) => renderSection(section, user, groups))}

View File

@ -13,7 +13,8 @@ export default function Search() {
useTracking('view search') useTracking('view search')
const { query } = useRouter() const { query } = useRouter()
const autoFocus = !(query['q'] || query['s'] || query['p']) const { q, s, p } = query
const autoFocus = !(q || !s || !p)
return ( return (
<Page> <Page>