Skip to content

Commit 0b211f7

Browse files
authored
fix: search endpoint (#425)
1 parent f7f6e23 commit 0b211f7

2 files changed

Lines changed: 163 additions & 241 deletions

File tree

packages/app/app/components/RepoSearch.vue

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
11
<script lang="ts" setup>
22
import type { RepoNode } from "../../server/utils/types";
3+
34
const search = useSessionStorage("search", "");
45
const searchResults = ref<RepoNode[]>([]);
56
const isLoading = ref(false);
67
7-
let activeController: AbortController | null = null;
8+
let abortController: AbortController | null = null;
89
const throttledSearch = useThrottle(search, 500, true, false);
910
1011
watch(
1112
throttledSearch,
12-
async (newValue) => {
13-
activeController?.abort();
13+
async (query) => {
14+
abortController?.abort();
1415
searchResults.value = [];
15-
if (!newValue) {
16+
17+
if (!query) {
1618
isLoading.value = false;
1719
return;
1820
}
1921
2022
const controller = new AbortController();
21-
activeController = controller;
22-
23+
abortController = controller;
2324
isLoading.value = true;
25+
2426
try {
2527
const response = await fetch(
26-
`/api/repo/search?text=${encodeURIComponent(newValue)}`,
27-
{ signal: activeController.signal },
28+
`/api/repo/search?text=${encodeURIComponent(query)}`,
29+
{ signal: controller.signal },
2830
);
29-
const data = (await response.json()) as { nodes: RepoNode[] };
30-
if (activeController === controller) {
31-
searchResults.value = data.nodes ?? [];
31+
const data = await response.json();
32+
33+
if (abortController !== controller) {
34+
return;
3235
}
36+
37+
searchResults.value = Array.isArray(data?.nodes)
38+
? (data.nodes as RepoNode[])
39+
: [];
3340
} catch (err: any) {
3441
if (err.name !== "AbortError") {
3542
console.error(err);
3643
}
3744
} finally {
38-
if (activeController === controller) {
45+
if (abortController === controller) {
3946
isLoading.value = false;
4047
}
4148
}

0 commit comments

Comments
 (0)