MCPcopy Index your code
hub / github.com/npmx-dev/npmx.dev / useGlobalSearch

Function useGlobalSearch

app/composables/useGlobalSearch.ts:9–117  ·  view source on GitHub ↗
(place: 'header' | 'content' = 'content')

Source from the content-addressed store, hash-verified

7const SEARCH_DEBOUNCE_MS = 100
8
9export function useGlobalSearch(place: 'header' | 'content' = 'content') {
10 const { settings } = useSettings()
11 const { searchProvider } = useSearchProvider()
12 const searchProviderValue = computed(() => {
13 const p = normalizeSearchParam(route.query.p)
14 if (p === 'npm' || searchProvider.value === 'npm') return 'npm'
15 return 'algolia'
16 })
17
18 const router = useRouter()
19 const route = useRoute()
20 // Internally used searchQuery state
21 const searchQuery = useState<string>('search-query', () => {
22 if (pagesWithLocalFilter.has(route.name as string)) {
23 return ''
24 }
25 return normalizeSearchParam(route.query.q)
26 })
27
28 // Committed search query: last value submitted by user
29 // Syncs instantly when instantSearch is on, but only on Enter press when off
30 const committedSearchQuery = useState<string>('committed-search-query', () => searchQuery.value)
31
32 const commitSearchQuery = debounce((val: string) => {
33 committedSearchQuery.value = val
34 }, SEARCH_DEBOUNCE_MS)
35
36 // This is basically doing instant search as user types
37 watch(searchQuery, val => {
38 if (settings.value.instantSearch) {
39 commitSearchQuery(val)
40 }
41 })
42
43 // clean search input when navigating away from search page
44 watch(
45 () => route.query.q,
46 urlQuery => {
47 const value = normalizeSearchParam(urlQuery)
48 if (!value) searchQuery.value = ''
49 if (!searchQuery.value) searchQuery.value = value
50 },
51 )
52
53 // Updates URL when search query changes (immediately for instantSearch or after Enter hit otherwise)
54 const updateUrlQueryImpl = (value: string, provider: 'npm' | 'algolia') => {
55 const isSameQuery = route.query.q === value && route.query.p === provider
56 // Don't navigate away from pages that use ?q for local filtering
57 if ((pagesWithLocalFilter.has(route.name as string) && place === 'content') || isSameQuery) {
58 return
59 }
60
61 if (route.name === 'search') {
62 router.replace({
63 query: {
64 ...route.query,
65 q: value || undefined,
66 p: provider === 'npm' ? 'npm' : undefined,

Callers

nothing calls this directly

Calls 4

normalizeSearchParamFunction · 0.90
useSettingsFunction · 0.85
useSearchProviderFunction · 0.85
updateUrlQueryImplFunction · 0.85

Tested by

no test coverage detected