From 22e85e9d89525c3df9bedd33b11e76c6ad5599f9 Mon Sep 17 00:00:00 2001 From: Ifu Aniemeka Date: Thu, 4 Jan 2024 17:03:09 -0600 Subject: [PATCH 1/2] search is global when community tag removed from search bar --- .../client/scripts/hooks/useSearchResults.ts | 4 +++- .../commonwealth/client/scripts/models/SearchQuery.ts | 9 ++++----- .../new_designs/CWSearchBar/CWSearchBar.tsx | 6 ++++-- .../client/scripts/views/pages/search/index.tsx | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/commonwealth/client/scripts/hooks/useSearchResults.ts b/packages/commonwealth/client/scripts/hooks/useSearchResults.ts index 7fdbd2dae36..3f9b7ac76e2 100644 --- a/packages/commonwealth/client/scripts/hooks/useSearchResults.ts +++ b/packages/commonwealth/client/scripts/hooks/useSearchResults.ts @@ -31,7 +31,9 @@ const useSearchResults = ( ): { searchResults: SearchResults; } => { - const communityId = app.activeChainId() || 'all_communities'; + const communityId = filters.includes('communities') + ? 'all_communities' + : app.activeChainId(); const debouncedSearchTerm = useDebounce(searchTerm, 500); const sharedQueryOptions = { diff --git a/packages/commonwealth/client/scripts/models/SearchQuery.ts b/packages/commonwealth/client/scripts/models/SearchQuery.ts index e52b0e8404b..fd09a9f0f26 100644 --- a/packages/commonwealth/client/scripts/models/SearchQuery.ts +++ b/packages/commonwealth/client/scripts/models/SearchQuery.ts @@ -24,7 +24,6 @@ export enum SearchSort { export interface SearchParams { communityScope?: string; - chainScope?: string; isSearchPreview?: boolean; searchScope?: Array; sort?: SearchSort; @@ -33,7 +32,7 @@ export interface SearchParams { export default class SearchQuery implements SearchParams { public searchTerm: Lowercase; - public chainScope?: string; + public communityScope?: string; public isSearchPreview?: boolean; public searchScope: Array; public sort: SearchSort; @@ -41,7 +40,7 @@ export default class SearchQuery implements SearchParams { constructor(searchTerm = '', params?: SearchParams) { this.searchTerm = >searchTerm.toLowerCase(); this.searchScope = params?.searchScope || [SearchScope.All]; - this.chainScope = params?.chainScope; + this.communityScope = params?.communityScope; this.isSearchPreview = !!params?.isSearchPreview; this.sort = params?.sort || SearchSort.Best; } @@ -49,7 +48,7 @@ export default class SearchQuery implements SearchParams { public toEncodedString() { let encodedString = this.searchTerm.trim().replace(/\s+/g, '%20') + - (this.chainScope ? ` chainScope=${this.chainScope}` : '') + + (this.communityScope ? ` communityScope=${this.communityScope}` : '') + (this.isSearchPreview ? ` isSearchPreview=${this.isSearchPreview}` : '') + (this.sort ? ` sort=${this.sort}` : ''); @@ -107,7 +106,7 @@ export default class SearchQuery implements SearchParams { public static fromUrlParams(url: Record) { const sq = new SearchQuery(url['q']); - sq.chainScope = url['chainScope'] || undefined; + sq.communityScope = url['communityScope'] || undefined; sq.isSearchPreview = url['preview'] === 'true'; sq.sort = url['sort'] || SearchSort.Best; sq.searchScope = url['scope'] || [SearchScope.All]; diff --git a/packages/commonwealth/client/scripts/views/components/component_kit/new_designs/CWSearchBar/CWSearchBar.tsx b/packages/commonwealth/client/scripts/views/components/component_kit/new_designs/CWSearchBar/CWSearchBar.tsx index 7a4969f441c..89b7a37e590 100644 --- a/packages/commonwealth/client/scripts/views/components/component_kit/new_designs/CWSearchBar/CWSearchBar.tsx +++ b/packages/commonwealth/client/scripts/views/components/component_kit/new_designs/CWSearchBar/CWSearchBar.tsx @@ -52,7 +52,9 @@ export const CWSearchBar: FC = ({ }) => { const navigate = useCommonNavigate(); const [showTag, setShowTag] = useState(true); - const communityId = app.activeChainId() || 'all_communities'; + const communityId = showTag + ? app.activeChainId() || 'all_communities' + : 'all_communities'; const community = app.config.chains.getById(communityId); const [searchTerm, setSearchTerm] = useState(''); const { searchResults } = useSearchResults(searchTerm, [ @@ -95,7 +97,7 @@ export const CWSearchBar: FC = ({ const handleGoToSearchPage = () => { const searchQuery = new SearchQuery(searchTerm, { isSearchPreview: false, - chainScope: showTag ? communityId : 'all_communities', + communityScope: showTag ? communityId : 'all_communities', }); goToSearchPage(searchQuery, navigate); resetSearchBar(); diff --git a/packages/commonwealth/client/scripts/views/pages/search/index.tsx b/packages/commonwealth/client/scripts/views/pages/search/index.tsx index 14e8b694799..06acc609829 100644 --- a/packages/commonwealth/client/scripts/views/pages/search/index.tsx +++ b/packages/commonwealth/client/scripts/views/pages/search/index.tsx @@ -103,7 +103,7 @@ const SearchPage = () => { SORT_MAP[queryParams.sort] || DEFAULT_SORT_OPTIONS; const sharedQueryOptions = { - communityId: app.activeChainId() || 'all_communities', + communityId: community, searchTerm: queryParams.q, limit: 20, orderBy, From ff76d1fc7ffbf5baa18de4bc5d3e2bc38143e2b3 Mon Sep 17 00:00:00 2001 From: Ifu Aniemeka Date: Thu, 4 Jan 2024 17:33:06 -0600 Subject: [PATCH 2/2] update chainScope to communityScope in search controller --- .../scripts/controllers/server/search.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/commonwealth/client/scripts/controllers/server/search.ts b/packages/commonwealth/client/scripts/controllers/server/search.ts index b3ae6c70d0a..350ac4ef551 100644 --- a/packages/commonwealth/client/scripts/controllers/server/search.ts +++ b/packages/commonwealth/client/scripts/controllers/server/search.ts @@ -31,7 +31,7 @@ class SearchController { return this.getByQuery(searchQuery); } const searchCache = this._store.getOrAdd(searchQuery); - const { searchTerm, chainScope, isSearchPreview, sort } = searchQuery; + const { searchTerm, communityScope, isSearchPreview, sort } = searchQuery; const pageSize = isSearchPreview ? SEARCH_PREVIEW_SIZE : SEARCH_PAGE_SIZE; const scope = searchQuery.getSearchScope(); @@ -42,7 +42,7 @@ class SearchController { ) { const discussions = await this.searchDiscussions(searchTerm, { pageSize, - chainScope, + communityScope, sort, }); @@ -58,7 +58,7 @@ class SearchController { if (scope.includes(SearchScope.Replies)) { const comments = await this.searchComments(searchTerm, { pageSize, - chainScope, + communityScope, sort, }); @@ -90,7 +90,7 @@ class SearchController { if (scope.includes(SearchScope.Members)) { const { profiles } = await this.searchMentionableProfiles( searchTerm, - chainScope, + communityScope, ); searchCache.results[SearchScope.Members] = profiles @@ -143,10 +143,10 @@ class SearchController { page: number, pageSize: number, ) { - const { searchTerm, chainScope, sort } = searchQuery; + const { searchTerm, communityScope, sort } = searchQuery; const searchParams = { pageSize, - chainScope, + communityScope, sort, }; switch (tab) { @@ -188,10 +188,10 @@ class SearchController { params: SearchParams, page?: number, ) => { - const { pageSize, chainScope, communityScope, sort } = params; + const { pageSize, communityScope, sort } = params; try { const queryParams = { - chain: chainScope, + chain: communityScope, community: communityScope, search: searchTerm, page_size: pageSize, @@ -218,10 +218,10 @@ class SearchController { params: SearchParams, page?: number, ) => { - const { pageSize, chainScope, communityScope, sort } = params; + const { pageSize, communityScope, sort } = params; try { const queryParams = { - chain: chainScope, + chain: communityScope, community: communityScope, search: searchTerm, page_size: pageSize, @@ -247,11 +247,11 @@ class SearchController { searchTerm: string, params: SearchParams, ): Promise => { - const { pageSize, chainScope, communityScope } = params; + const { pageSize, communityScope } = params; try { const response = await axios.get(`${app.serverUrl()}/threads`, { params: { - chain: chainScope, + chain: communityScope, community: communityScope, search: searchTerm, results_size: pageSize, @@ -272,14 +272,14 @@ class SearchController { public searchMentionableProfiles = async ( searchTerm: string, - chainScope: string, + communityScope: string, pageSize?: number, page?: number, includeRoles?: boolean, ) => { const response = await axios.get(`${app.serverUrl()}/profiles`, { params: { - chain: chainScope, + chain: communityScope, search: searchTerm, page_size: pageSize, page,