|
1 | 1 | <script lang="ts" setup> |
2 | 2 | import type { RepoNode } from "../../server/utils/types"; |
3 | 3 | const search = useSessionStorage("search", ""); |
4 | | -const searchResults = ref< |
5 | | - Array<{ |
6 | | - id: string; |
7 | | - name: string; |
8 | | - owner: { login: string; avatarUrl: string }; |
9 | | - stars?: number; |
10 | | - }> |
11 | | ->([]); |
| 4 | +const searchResults = ref<RepoNode[]>([]); |
12 | 5 | const isLoading = ref(false); |
13 | 6 |
|
14 | | -let activeController: AbortController | null = null; |
15 | | -let searchToken = 0; |
| 7 | +const activeController = ref<AbortController | null>(null); |
16 | 8 | const throttledSearch = useThrottle(search, 500, true, false); |
17 | 9 |
|
18 | 10 | watch( |
19 | 11 | throttledSearch, |
20 | 12 | async (newValue) => { |
21 | | - activeController?.abort(); |
| 13 | + activeController.value?.abort(); |
22 | 14 | searchResults.value = []; |
23 | 15 | if (!newValue) { |
24 | 16 | isLoading.value = false; |
25 | 17 | return; |
26 | 18 | } |
27 | 19 |
|
28 | | - searchToken++; |
29 | | - const currentToken = searchToken; |
30 | 20 | isLoading.value = true; |
31 | | - activeController = new AbortController(); |
| 21 | + activeController.value = new AbortController(); |
32 | 22 | try { |
33 | 23 | const response = await fetch( |
34 | 24 | `/api/repo/search?text=${encodeURIComponent(newValue)}`, |
35 | | - { signal: activeController.signal }, |
| 25 | + { signal: activeController.value.signal }, |
36 | 26 | ); |
37 | 27 | const data = (await response.json()) as { nodes?: RepoNode[] }; |
38 | | - if (currentToken === searchToken) { |
39 | | - searchResults.value = data.nodes ? data.nodes : []; |
40 | | - } |
| 28 | + searchResults.value = data.nodes ? data.nodes : []; |
41 | 29 | } catch (err: any) { |
42 | 30 | if (err.name !== "AbortError") { |
43 | 31 | console.error(err); |
44 | 32 | } |
45 | 33 | } finally { |
46 | | - if (currentToken === searchToken) { |
47 | | - isLoading.value = false; |
48 | | - } |
| 34 | + isLoading.value = false; |
49 | 35 | } |
50 | 36 | }, |
51 | 37 | { immediate: false }, |
|
0 commit comments