|
1 |
| -import { useState, useEffect } from 'react'; |
| 1 | +import { useEffect, useState } from 'react'; |
2 | 2 |
|
3 | 3 | export const useSearchScopes = () => {
|
4 | 4 | const [scopes, setScopes] = useState([]);
|
5 |
| - const [currentScope, setCurrentScope] = useState(null); |
6 |
| - |
7 |
| - useEffect(() => { |
8 |
| - const select = document.getElementById('searchscope'); |
9 |
| - |
10 |
| - const scope = (event) => { |
11 |
| - const slug = event.target.value.split('/').filter(Boolean).join('/'); |
12 |
| - setCurrentScope(slug); |
13 |
| - }; |
14 |
| - |
15 |
| - select?.addEventListener('change', scope); |
16 |
| - setCurrentScope(select?.value); |
17 |
| - |
18 |
| - return () => { |
19 |
| - select?.removeEventListener('change', scope); |
20 |
| - }; |
21 |
| - }, []); |
22 | 5 |
|
23 | 6 | useEffect(() => {
|
24 | 7 | const initialScopes = [];
|
25 |
| - |
26 |
| - if (currentScope) { |
27 |
| - const packageName = currentScope.split('/').slice(1, 3).join('/'); |
28 |
| - const version = currentScope.split('/').slice(3, 4)[0]?.split('.')[0]; |
29 |
| - initialScopes.push({ type: 'manual', title: packageName, slug: currentScope }); |
30 |
| - initialScopes.push({ type: 'version', title: version }); |
31 |
| - } else { |
32 |
| - const url = new URL(window.location.href); |
33 |
| - url.searchParams?.forEach((value, key) => { |
34 |
| - if (key === 'scope') { |
35 |
| - const slug = decodeURIComponent(value).split('/').filter(Boolean).join('/'); |
36 |
| - const packageName = slug.split('/').slice(1, 3).join('/'); |
37 |
| - initialScopes.push({ type: 'manual', title: packageName, slug }); |
38 |
| - } else if (key.startsWith('filters[')) { |
39 |
| - const filterExp = new RegExp(/filters\[(.*?)\]\[(.*?)\]/); |
40 |
| - const [, type, filterValue] = key.match(filterExp); |
41 |
| - initialScopes.push({ |
42 |
| - type: type === 'optionsaggs' ? 'option' : type, |
43 |
| - title: filterValue |
44 |
| - }); |
45 |
| - } |
46 |
| - }); |
47 |
| - } |
| 8 | + const url = new URL(window.location.href); |
| 9 | + url.searchParams?.forEach((value, key) => { |
| 10 | + if (key === 'scope' && value) { |
| 11 | + const slug = decodeURIComponent(value).split('/').filter(Boolean).join('/'); |
| 12 | + const packageName = slug.split('/').slice(1, 3).join('/'); |
| 13 | + initialScopes.push({ type: 'manual', title: packageName, slug }); |
| 14 | + } else if (key.startsWith('filters[')) { |
| 15 | + const filterExp = new RegExp(/filters\[(.*?)\]\[(.*?)\]/); |
| 16 | + const [, type, filterValue] = key.match(filterExp); |
| 17 | + initialScopes.push({ |
| 18 | + type: type === 'optionsaggs' ? 'option' : type, |
| 19 | + title: filterValue |
| 20 | + }); |
| 21 | + } |
| 22 | + }); |
48 | 23 |
|
49 | 24 | setScopes(initialScopes);
|
50 |
| - }, [currentScope]); |
| 25 | + }, []); |
51 | 26 |
|
52 | 27 | return [scopes, setScopes];
|
53 | 28 | };
|
0 commit comments