|
1 | 1 | 'use client';
|
2 | 2 |
|
| 3 | +import { useClickListener } from "@/hooks/useClickListener"; |
3 | 4 | import { useTailwind } from "@/hooks/useTailwind";
|
4 |
| -import { Repository, SearchQueryParams } from "@/lib/types"; |
| 5 | +import { SearchQueryParams } from "@/lib/types"; |
5 | 6 | import { cn, createPathWithQueryParams } from "@/lib/utils";
|
6 | 7 | import {
|
7 | 8 | cursorCharLeft,
|
@@ -31,12 +32,10 @@ import { createTheme } from '@uiw/codemirror-themes';
|
31 | 32 | import CodeMirror, { Annotation, EditorView, KeyBinding, keymap, ReactCodeMirrorRef } from "@uiw/react-codemirror";
|
32 | 33 | import { cva } from "class-variance-authority";
|
33 | 34 | import { useRouter } from "next/navigation";
|
34 |
| -import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 35 | +import { useCallback, useMemo, useRef, useState } from "react"; |
35 | 36 | 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"; |
40 | 39 | import { zoekt } from "./zoektLanguageExtension";
|
41 | 40 |
|
42 | 41 | interface SearchBarProps {
|
@@ -107,68 +106,10 @@ export const SearchBar = ({
|
107 | 106 | return _query.replaceAll(/\n/g, " ");
|
108 | 107 | }, [_query]);
|
109 | 108 |
|
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 | + }); |
172 | 113 |
|
173 | 114 | const theme = useMemo(() => {
|
174 | 115 | return createTheme({
|
|
0 commit comments