From 603bec9e884ac79f73272094a0690f32103a860e Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Thu, 23 Jun 2022 16:47:03 -0700 Subject: [PATCH] Ameliorate homepage search spam (#564) * Don't recompute search filters when follows loaded unnecessarily * Don't wait for router to get saved search sort --- web/components/contract-search.tsx | 4 ++-- web/hooks/use-sort-and-query-params.tsx | 12 +++++++++++- web/pages/home.tsx | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/web/components/contract-search.tsx b/web/components/contract-search.tsx index 4c557648..e75aefb8 100644 --- a/web/components/contract-search.tsx +++ b/web/components/contract-search.tsx @@ -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}` diff --git a/web/hooks/use-sort-and-query-params.tsx b/web/hooks/use-sort-and-query-params.tsx index 493c0dba..b7bfb288 100644 --- a/web/hooks/use-sort-and-query-params.tsx +++ b/web/hooks/use-sort-and-query-params.tsx @@ -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. diff --git a/web/pages/home.tsx b/web/pages/home.tsx index 14e6cf2b..75eae351 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -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 = () => { {