Ameliorate homepage search spam (#564)

* Don't recompute search filters when follows loaded unnecessarily

* Don't wait for router to get saved search sort
This commit is contained in:
Marshall Polaris 2022-06-23 16:47:03 -07:00 committed by GitHub
parent 4f9e303daa
commit 603bec9e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

@ -116,8 +116,8 @@ export function ContractSearch(props: {
showCategorySelector,
mode,
Object.values(additionalFilter ?? {}).join(','),
followedCategories?.join(','),
follows?.join(','),
(followedCategories ?? []).join(','),
(follows ?? []).join(','),
])
const indexName = `${indexPrefix}contracts-${sort}`

View File

@ -20,6 +20,16 @@ export function checkAgainstQuery(query: string, corpus: string) {
return queryWords.every((word) => corpus.toLowerCase().includes(word))
}
export function getSavedSort() {
// TODO: this obviously doesn't work with SSR, common sense would suggest
// that we should save things like this in cookies so the server has them
if (typeof window !== 'undefined') {
return localStorage.getItem(MARKETS_SORT) as Sort | null
} else {
return null
}
}
export function useInitialQueryAndSort(options?: {
defaultSort: Sort
shouldLoadFromStorage?: boolean
@ -45,7 +55,7 @@ export function useInitialQueryAndSort(options?: {
if (!sort && shouldLoadFromStorage) {
console.log('ready loading from storage ', sort ?? defaultSort)
const localSort = localStorage.getItem(MARKETS_SORT) as Sort
const localSort = getSavedSort()
if (localSort) {
router.query.s = localSort
// Use replace to not break navigating back.

View File

@ -5,6 +5,7 @@ import { PlusSmIcon } from '@heroicons/react/solid'
import { Page } from 'web/components/page'
import { Col } from 'web/components/layout/col'
import { useUser } from 'web/hooks/use-user'
import { getSavedSort } from 'web/hooks/use-sort-and-query-params'
import { ContractSearch } from 'web/components/contract-search'
import { Contract } from 'common/contract'
import { ContractPageContent } from './[username]/[contractSlug]'
@ -17,7 +18,6 @@ const Home = () => {
const [contract, setContract] = useContractPage()
const router = useRouter()
useTracking('view home')
if (user === null) {
@ -32,7 +32,7 @@ const Home = () => {
<ContractSearch
querySortOptions={{
shouldLoadFromStorage: true,
defaultSort: '24-hour-vol',
defaultSort: getSavedSort() ?? '24-hour-vol',
}}
showCategorySelector
onContractClick={(c) => {