Skip to content

Commit 5b0d173

Browse files
committed
feat: fix and improve keywords search and navigation
1 parent 8b420ad commit 5b0d173

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

app/composables/useStructuredFilters.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,21 @@ function matchesSecurity(pkg: NpmSearchResult, security: SecurityFilter): boolea
113113
*
114114
*/
115115
export function useStructuredFilters(options: UseStructuredFiltersOptions) {
116+
const route = useRoute()
117+
const router = useRouter()
116118
const { packages, initialFilters, initialSort } = options
117119

120+
const searchQuery = shallowRef(normalizeSearchParam(route.query.q))
121+
watch(
122+
() => route.query.q,
123+
urlQuery => {
124+
const value = normalizeSearchParam(urlQuery)
125+
if (searchQuery.value !== value) {
126+
searchQuery.value = value
127+
}
128+
},
129+
)
130+
118131
// Filter state
119132
const filters = ref<StructuredFilters>({
120133
...DEFAULT_FILTERS,
@@ -366,11 +379,17 @@ export function useStructuredFilters(options: UseStructuredFiltersOptions) {
366379
function addKeyword(keyword: string) {
367380
if (!filters.value.keywords.includes(keyword)) {
368381
filters.value.keywords = [...filters.value.keywords, keyword]
382+
const newQ = searchQuery.value
383+
? `${searchQuery.value.trim()} keyword:${keyword}`
384+
: `keyword:${keyword}`
385+
router.replace({ query: { ...route.query, q: newQ } })
369386
}
370387
}
371388

372389
function removeKeyword(keyword: string) {
373390
filters.value.keywords = filters.value.keywords.filter(k => k !== keyword)
391+
const newQ = searchQuery.value.replace(new RegExp(`keyword:${keyword}($| )`, 'g'), '').trim()
392+
router.replace({ query: { ...route.query, q: newQ || undefined } })
374393
}
375394

376395
function toggleKeyword(keyword: string) {

app/pages/@[org].vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const {
5252
} = useStructuredFilters({
5353
packages,
5454
initialFilters: {
55-
text: normalizeSearchParam(route.query.q),
55+
...parseSearchOperators(normalizeSearchParam(route.query.q)),
5656
},
5757
initialSort: (normalizeSearchParam(route.query.sort) as SortOption) ?? 'updated-desc',
5858
})

app/pages/search.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ const {
142142
clearAllFilters,
143143
} = useStructuredFilters({
144144
packages: resultsArray,
145+
initialFilters: {
146+
...parseSearchOperators(normalizeSearchParam(route.query.q)),
147+
},
145148
initialSort: 'relevance-desc', // Default to search relevance
146149
})
147150

0 commit comments

Comments
 (0)