mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-11 20:05:25 +00:00
File suggestions (#88)
This commit is contained in:
parent
4b1a782539
commit
c73c34428c
4 changed files with 125 additions and 42 deletions
|
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added file suggestions as a suggestion type. ([#88](https://github.com/sourcebot-dev/sourcebot/pull/88))
|
||||||
|
|
||||||
## [2.5.0] - 2024-11-22
|
## [2.5.0] - 2024-11-22
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
import { useClickListener } from "@/hooks/useClickListener";
|
||||||
import { useTailwind } from "@/hooks/useTailwind";
|
import { useTailwind } from "@/hooks/useTailwind";
|
||||||
import { Repository, SearchQueryParams } from "@/lib/types";
|
import { SearchQueryParams } from "@/lib/types";
|
||||||
import { cn, createPathWithQueryParams } from "@/lib/utils";
|
import { cn, createPathWithQueryParams } from "@/lib/utils";
|
||||||
import {
|
import {
|
||||||
cursorCharLeft,
|
cursorCharLeft,
|
||||||
|
|
@ -31,12 +32,10 @@ import { createTheme } from '@uiw/codemirror-themes';
|
||||||
import CodeMirror, { Annotation, EditorView, KeyBinding, keymap, ReactCodeMirrorRef } from "@uiw/react-codemirror";
|
import CodeMirror, { Annotation, EditorView, KeyBinding, keymap, ReactCodeMirrorRef } from "@uiw/react-codemirror";
|
||||||
import { cva } from "class-variance-authority";
|
import { cva } from "class-variance-authority";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
import { useCallback, useMemo, useRef, useState } from "react";
|
||||||
import { useHotkeys } from 'react-hotkeys-hook';
|
import { useHotkeys } from 'react-hotkeys-hook';
|
||||||
import { SearchSuggestionsBox, Suggestion } from "./searchSuggestionsBox";
|
import { SearchSuggestionsBox, SuggestionMode } from "./searchSuggestionsBox";
|
||||||
import { useClickListener } from "@/hooks/useClickListener";
|
import { useSuggestionsData } from "./useSuggestionsData";
|
||||||
import { getRepos } from "../../api/(client)/client";
|
|
||||||
import languages from "./languages";
|
|
||||||
import { zoekt } from "./zoektLanguageExtension";
|
import { zoekt } from "./zoektLanguageExtension";
|
||||||
|
|
||||||
interface SearchBarProps {
|
interface SearchBarProps {
|
||||||
|
|
@ -97,6 +96,8 @@ export const SearchBar = ({
|
||||||
|
|
||||||
const focusEditor = useCallback(() => editorRef.current?.view?.focus(), []);
|
const focusEditor = useCallback(() => editorRef.current?.view?.focus(), []);
|
||||||
const focusSuggestionsBox = useCallback(() => suggestionBoxRef.current?.focus(), []);
|
const focusSuggestionsBox = useCallback(() => suggestionBoxRef.current?.focus(), []);
|
||||||
|
const [suggestionMode, setSuggestionMode] = useState<SuggestionMode>("refine");
|
||||||
|
const [suggestionQuery, setSuggestionQuery] = useState("");
|
||||||
|
|
||||||
const [_query, setQuery] = useState(defaultQuery ?? "");
|
const [_query, setQuery] = useState(defaultQuery ?? "");
|
||||||
const query = useMemo(() => {
|
const query = useMemo(() => {
|
||||||
|
|
@ -105,41 +106,10 @@ export const SearchBar = ({
|
||||||
return _query.replaceAll(/\n/g, " ");
|
return _query.replaceAll(/\n/g, " ");
|
||||||
}, [_query]);
|
}, [_query]);
|
||||||
|
|
||||||
const [repos, setRepos] = useState<Repository[]>([]);
|
const suggestionData = useSuggestionsData({
|
||||||
useEffect(() => {
|
suggestionMode,
|
||||||
getRepos().then((response) => {
|
suggestionQuery,
|
||||||
setRepos(response.List.Repos.map(r => r.Repository));
|
});
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const suggestionData = useMemo(() => {
|
|
||||||
const repoSuggestions: Suggestion[] = repos.map((repo) => {
|
|
||||||
return {
|
|
||||||
value: repo.Name,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const languageSuggestions: Suggestion[] = languages.map((lang) => {
|
|
||||||
const spotlight = [
|
|
||||||
"Python",
|
|
||||||
"Java",
|
|
||||||
"TypeScript",
|
|
||||||
"Go",
|
|
||||||
"C++",
|
|
||||||
"C#"
|
|
||||||
].includes(lang);
|
|
||||||
|
|
||||||
return {
|
|
||||||
value: lang,
|
|
||||||
spotlight,
|
|
||||||
};
|
|
||||||
})
|
|
||||||
|
|
||||||
return {
|
|
||||||
repos: repoSuggestions,
|
|
||||||
languages: languageSuggestions,
|
|
||||||
}
|
|
||||||
}, [repos]);
|
|
||||||
|
|
||||||
const theme = useMemo(() => {
|
const theme = useMemo(() => {
|
||||||
return createTheme({
|
return createTheme({
|
||||||
|
|
@ -286,6 +256,12 @@ export const SearchBar = ({
|
||||||
}}
|
}}
|
||||||
cursorPosition={cursorPosition}
|
cursorPosition={cursorPosition}
|
||||||
data={suggestionData}
|
data={suggestionData}
|
||||||
|
onSuggestionModeChanged={(suggestionMode) => {
|
||||||
|
setSuggestionMode(suggestionMode);
|
||||||
|
}}
|
||||||
|
onSuggestionQueryChanged={(suggestionQuery) => {
|
||||||
|
setSuggestionQuery(suggestionQuery);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -47,10 +47,13 @@ interface SearchSuggestionsBoxProps {
|
||||||
onFocus: () => void;
|
onFocus: () => void;
|
||||||
onBlur: () => void;
|
onBlur: () => void;
|
||||||
onReturnFocus: () => void;
|
onReturnFocus: () => void;
|
||||||
|
onSuggestionModeChanged: (suggestionMode: SuggestionMode) => void;
|
||||||
|
onSuggestionQueryChanged: (suggestionQuery: string) => void;
|
||||||
|
|
||||||
data: {
|
data: {
|
||||||
repos: Suggestion[];
|
repos: Suggestion[];
|
||||||
languages: Suggestion[];
|
languages: Suggestion[];
|
||||||
|
files: Suggestion[];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,6 +67,8 @@ const SearchSuggestionsBox = forwardRef(({
|
||||||
onFocus,
|
onFocus,
|
||||||
onBlur,
|
onBlur,
|
||||||
onReturnFocus,
|
onReturnFocus,
|
||||||
|
onSuggestionModeChanged,
|
||||||
|
onSuggestionQueryChanged,
|
||||||
}: SearchSuggestionsBoxProps, ref: Ref<HTMLDivElement>) => {
|
}: SearchSuggestionsBoxProps, ref: Ref<HTMLDivElement>) => {
|
||||||
|
|
||||||
const [highlightedSuggestionIndex, setHighlightedSuggestionIndex] = useState(0);
|
const [highlightedSuggestionIndex, setHighlightedSuggestionIndex] = useState(0);
|
||||||
|
|
@ -137,6 +142,7 @@ const SearchSuggestionsBox = forwardRef(({
|
||||||
list,
|
list,
|
||||||
isHighlightEnabled = false,
|
isHighlightEnabled = false,
|
||||||
isSpotlightEnabled = false,
|
isSpotlightEnabled = false,
|
||||||
|
isClientSideSearchEnabled = true,
|
||||||
onSuggestionClicked,
|
onSuggestionClicked,
|
||||||
Icon,
|
Icon,
|
||||||
} = ((): {
|
} = ((): {
|
||||||
|
|
@ -145,6 +151,7 @@ const SearchSuggestionsBox = forwardRef(({
|
||||||
list: Suggestion[],
|
list: Suggestion[],
|
||||||
isHighlightEnabled?: boolean,
|
isHighlightEnabled?: boolean,
|
||||||
isSpotlightEnabled?: boolean,
|
isSpotlightEnabled?: boolean,
|
||||||
|
isClientSideSearchEnabled?: boolean,
|
||||||
onSuggestionClicked: (value: string) => void,
|
onSuggestionClicked: (value: string) => void,
|
||||||
Icon?: Icon
|
Icon?: Icon
|
||||||
} => {
|
} => {
|
||||||
|
|
@ -192,6 +199,11 @@ const SearchSuggestionsBox = forwardRef(({
|
||||||
onSuggestionClicked: createOnSuggestionClickedHandler({ trailingSpace: false }),
|
onSuggestionClicked: createOnSuggestionClickedHandler({ trailingSpace: false }),
|
||||||
}
|
}
|
||||||
case "file":
|
case "file":
|
||||||
|
return {
|
||||||
|
list: data.files,
|
||||||
|
onSuggestionClicked: createOnSuggestionClickedHandler(),
|
||||||
|
isClientSideSearchEnabled: false,
|
||||||
|
}
|
||||||
case "revision":
|
case "revision":
|
||||||
case "content":
|
case "content":
|
||||||
case "symbol":
|
case "symbol":
|
||||||
|
|
@ -228,6 +240,10 @@ const SearchSuggestionsBox = forwardRef(({
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isClientSideSearchEnabled) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
return fuse.search(suggestionQuery, {
|
return fuse.search(suggestionQuery, {
|
||||||
limit,
|
limit,
|
||||||
}).map(result => result.item);
|
}).map(result => result.item);
|
||||||
|
|
@ -240,13 +256,25 @@ const SearchSuggestionsBox = forwardRef(({
|
||||||
onSuggestionClicked,
|
onSuggestionClicked,
|
||||||
}
|
}
|
||||||
|
|
||||||
}, [suggestionQuery, suggestionMode, onCompletion, cursorPosition, data.repos, data.languages, query]);
|
}, [suggestionQuery, suggestionMode, query, cursorPosition, onCompletion, data.repos, data.files, data.languages]);
|
||||||
|
|
||||||
// When the list of suggestions change, reset the highlight index
|
// When the list of suggestions change, reset the highlight index
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setHighlightedSuggestionIndex(0);
|
setHighlightedSuggestionIndex(0);
|
||||||
}, [suggestions]);
|
}, [suggestions]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isDefined(suggestionMode)) {
|
||||||
|
onSuggestionModeChanged(suggestionMode);
|
||||||
|
}
|
||||||
|
}, [onSuggestionModeChanged, suggestionMode]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isDefined(suggestionQuery)) {
|
||||||
|
onSuggestionQueryChanged(suggestionQuery);
|
||||||
|
}
|
||||||
|
}, [onSuggestionQueryChanged, suggestionQuery]);
|
||||||
|
|
||||||
const suggestionModeText = useMemo(() => {
|
const suggestionModeText = useMemo(() => {
|
||||||
if (!suggestionMode) {
|
if (!suggestionMode) {
|
||||||
return "";
|
return "";
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useQuery } from "@tanstack/react-query";
|
||||||
|
import { Suggestion, SuggestionMode } from "./searchSuggestionsBox";
|
||||||
|
import { getRepos, search } from "@/app/api/(client)/client";
|
||||||
|
import { useMemo } from "react";
|
||||||
|
import languages from "./languages";
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
suggestionMode: SuggestionMode;
|
||||||
|
suggestionQuery: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches suggestions for the search bar.
|
||||||
|
*/
|
||||||
|
export const useSuggestionsData = ({
|
||||||
|
suggestionMode,
|
||||||
|
suggestionQuery,
|
||||||
|
}: Props) => {
|
||||||
|
const { data: repoSuggestions } = useQuery({
|
||||||
|
queryKey: ["repoSuggestions"],
|
||||||
|
queryFn: getRepos,
|
||||||
|
select: (data): Suggestion[] => {
|
||||||
|
return data.List.Repos
|
||||||
|
.map(r => r.Repository)
|
||||||
|
.map(r => ({
|
||||||
|
value: r.Name
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
enabled: suggestionMode === "repo",
|
||||||
|
});
|
||||||
|
|
||||||
|
const { data: fileSuggestions } = useQuery({
|
||||||
|
queryKey: ["fileSuggestions", suggestionQuery],
|
||||||
|
queryFn: () => search({
|
||||||
|
query: `file:${suggestionQuery}`,
|
||||||
|
maxMatchDisplayCount: 15,
|
||||||
|
}),
|
||||||
|
select: (data): Suggestion[] => {
|
||||||
|
return data.Result.Files?.map((file) => ({
|
||||||
|
value: file.FileName
|
||||||
|
})) ?? [];
|
||||||
|
},
|
||||||
|
enabled: suggestionMode === "file"
|
||||||
|
});
|
||||||
|
|
||||||
|
const languageSuggestions = useMemo((): Suggestion[] => {
|
||||||
|
return languages.map((lang) => {
|
||||||
|
const spotlight = [
|
||||||
|
"Python",
|
||||||
|
"Java",
|
||||||
|
"TypeScript",
|
||||||
|
"Go",
|
||||||
|
"C++",
|
||||||
|
"C#"
|
||||||
|
].includes(lang);
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: lang,
|
||||||
|
spotlight,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const data = useMemo(() => {
|
||||||
|
return {
|
||||||
|
repos: repoSuggestions ?? [],
|
||||||
|
languages: languageSuggestions,
|
||||||
|
files: fileSuggestions ?? [],
|
||||||
|
}
|
||||||
|
}, [repoSuggestions, fileSuggestions, languageSuggestions]);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue