Skip to content

Commit 5a1f53c

Browse files
refactor data fetching into react hook. Use react-query
1 parent 85d235e commit 5a1f53c

File tree

2 files changed

+84
-68
lines changed

2 files changed

+84
-68
lines changed

packages/web/src/app/components/searchBar/searchBar.tsx

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use client';
22

3+
import { useClickListener } from "@/hooks/useClickListener";
34
import { useTailwind } from "@/hooks/useTailwind";
4-
import { Repository, SearchQueryParams } from "@/lib/types";
5+
import { SearchQueryParams } from "@/lib/types";
56
import { cn, createPathWithQueryParams } from "@/lib/utils";
67
import {
78
cursorCharLeft,
@@ -31,12 +32,10 @@ import { createTheme } from '@uiw/codemirror-themes';
3132
import CodeMirror, { Annotation, EditorView, KeyBinding, keymap, ReactCodeMirrorRef } from "@uiw/react-codemirror";
3233
import { cva } from "class-variance-authority";
3334
import { useRouter } from "next/navigation";
34-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
35+
import { useCallback, useMemo, useRef, useState } from "react";
3536
import { useHotkeys } from 'react-hotkeys-hook';
36-
import { SearchSuggestionsBox, Suggestion, SuggestionMode } from "./searchSuggestionsBox";
37-
import { useClickListener } from "@/hooks/useClickListener";
38-
import { getRepos, search } from "../../api/(client)/client";
39-
import languages from "./languages";
37+
import { SearchSuggestionsBox, SuggestionMode } from "./searchSuggestionsBox";
38+
import { useSuggestionsData } from "./useSuggestionsData";
4039
import { zoekt } from "./zoektLanguageExtension";
4140

4241
interface SearchBarProps {
@@ -107,68 +106,10 @@ export const SearchBar = ({
107106
return _query.replaceAll(/\n/g, " ");
108107
}, [_query]);
109108

110-
const [repos, setRepos] = useState<Repository[]>([]);
111-
useEffect(() => {
112-
getRepos().then((response) => {
113-
setRepos(response.List.Repos.map(r => r.Repository));
114-
});
115-
}, []);
116-
117-
const [files, setFiles] = useState<string[]>([]);
118-
119-
useEffect(() => {
120-
if (suggestionMode === "file") {
121-
search({
122-
query: `file:${suggestionQuery}`,
123-
maxMatchDisplayCount: 15,
124-
}).then((response) => {
125-
const filenames = response.Result.Files?.map((file) => {
126-
return file.FileName;
127-
});
128-
if (filenames) {
129-
setFiles(filenames);
130-
}
131-
}).catch((error) => {
132-
console.error(error);
133-
})
134-
}
135-
}, [suggestionMode, suggestionQuery]);
136-
137-
const suggestionData = useMemo(() => {
138-
const repoSuggestions: Suggestion[] = repos.map((repo) => {
139-
return {
140-
value: repo.Name,
141-
}
142-
});
143-
144-
const fileSuggestions: Suggestion[] = files.map((file) => {
145-
return {
146-
value: file,
147-
}
148-
});
149-
150-
const languageSuggestions: Suggestion[] = languages.map((lang) => {
151-
const spotlight = [
152-
"Python",
153-
"Java",
154-
"TypeScript",
155-
"Go",
156-
"C++",
157-
"C#"
158-
].includes(lang);
159-
160-
return {
161-
value: lang,
162-
spotlight,
163-
};
164-
})
165-
166-
return {
167-
repos: repoSuggestions,
168-
languages: languageSuggestions,
169-
files: fileSuggestions,
170-
}
171-
}, [repos, files]);
109+
const suggestionData = useSuggestionsData({
110+
suggestionMode,
111+
suggestionQuery,
112+
});
172113

173114
const theme = useMemo(() => {
174115
return createTheme({
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use client';
2+
3+
import { useQuery } from "@tanstack/react-query";
4+
import { Suggestion, SuggestionMode } from "./searchSuggestionsBox";
5+
import { getRepos, search } from "@/app/api/(client)/client";
6+
import { useMemo } from "react";
7+
import languages from "./languages";
8+
9+
interface Props {
10+
suggestionMode: SuggestionMode;
11+
suggestionQuery: string;
12+
}
13+
14+
/**
15+
* Fetches suggestions for the search bar.
16+
*/
17+
export const useSuggestionsData = ({
18+
suggestionMode,
19+
suggestionQuery,
20+
}: Props) => {
21+
const { data: repoSuggestions } = useQuery({
22+
queryKey: ["repoSuggestions"],
23+
queryFn: getRepos,
24+
select: (data): Suggestion[] => {
25+
return data.List.Repos
26+
.map(r => r.Repository)
27+
.map(r => ({
28+
value: r.Name
29+
}));
30+
},
31+
enabled: suggestionMode === "repo",
32+
});
33+
34+
const { data: fileSuggestions } = useQuery({
35+
queryKey: ["fileSuggestions", suggestionQuery],
36+
queryFn: () => search({
37+
query: `file:${suggestionQuery}`,
38+
maxMatchDisplayCount: 15,
39+
}),
40+
select: (data): Suggestion[] => {
41+
return data.Result.Files?.map((file) => ({
42+
value: file.FileName
43+
})) ?? [];
44+
},
45+
enabled: suggestionMode === "file"
46+
});
47+
48+
const languageSuggestions = useMemo((): Suggestion[] => {
49+
return languages.map((lang) => {
50+
const spotlight = [
51+
"Python",
52+
"Java",
53+
"TypeScript",
54+
"Go",
55+
"C++",
56+
"C#"
57+
].includes(lang);
58+
59+
return {
60+
value: lang,
61+
spotlight,
62+
};
63+
});
64+
}, []);
65+
66+
const data = useMemo(() => {
67+
return {
68+
repos: repoSuggestions ?? [],
69+
languages: languageSuggestions,
70+
files: fileSuggestions ?? [],
71+
}
72+
}, [repoSuggestions, fileSuggestions, languageSuggestions]);
73+
74+
return data;
75+
}

0 commit comments

Comments
 (0)