2024-08-25 00:45:44 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2024-08-23 20:54:13 +00:00
|
|
|
import Image from "next/image";
|
2024-08-25 00:45:44 +00:00
|
|
|
import logo from "../../public/sb_logo_large_3.png"
|
|
|
|
|
import { Input } from "@/components/ui/input"
|
2024-08-25 03:58:31 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2024-08-25 00:45:44 +00:00
|
|
|
import { useDebouncedCallback } from 'use-debounce';
|
|
|
|
|
import { Separator } from "@/components/ui/separator"
|
2024-08-25 02:39:59 +00:00
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import { useNonEmptyQueryParam } from "@/hooks/useNonEmptyQueryParam";
|
2024-08-25 03:58:31 +00:00
|
|
|
import { SymbolIcon } from "@radix-ui/react-icons";
|
2024-08-25 00:45:44 +00:00
|
|
|
|
|
|
|
|
interface ZoekMatch {
|
|
|
|
|
URL: string,
|
|
|
|
|
FileName: string,
|
|
|
|
|
LineNum: number,
|
|
|
|
|
Fragments: {
|
|
|
|
|
Pre: string,
|
|
|
|
|
Match: string,
|
|
|
|
|
Post: string
|
|
|
|
|
}[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ZoekFileMatch {
|
|
|
|
|
FileName: string,
|
|
|
|
|
Repo: string,
|
|
|
|
|
Language: string,
|
|
|
|
|
Matches: ZoekMatch[],
|
|
|
|
|
URL: string,
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 03:10:01 +00:00
|
|
|
interface ZoekResult {
|
|
|
|
|
QueryStr: string,
|
|
|
|
|
FileMatches: ZoekFileMatch[] | null,
|
2024-08-25 03:58:31 +00:00
|
|
|
Stats: {
|
|
|
|
|
// Duration in nanoseconds
|
|
|
|
|
Duration: number,
|
|
|
|
|
}
|
2024-08-25 00:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-25 03:10:01 +00:00
|
|
|
interface ZoekSearchResponse {
|
2024-08-25 03:58:31 +00:00
|
|
|
result: ZoekResult,
|
2024-08-25 03:10:01 +00:00
|
|
|
}
|
2024-08-23 20:54:13 +00:00
|
|
|
|
|
|
|
|
export default function Home() {
|
2024-08-25 02:39:59 +00:00
|
|
|
const router = useRouter();
|
|
|
|
|
const defaultQuery = useNonEmptyQueryParam("query") ?? "";
|
|
|
|
|
const defaultNumResults = useNonEmptyQueryParam("numResults");
|
|
|
|
|
|
|
|
|
|
const [query, setQuery] = useState(defaultQuery);
|
|
|
|
|
const [numResults, _setNumResults] = useState(defaultNumResults && !isNaN(Number(defaultNumResults)) ? Number(defaultNumResults) : 100);
|
2024-08-23 20:54:13 +00:00
|
|
|
|
2024-08-25 00:45:44 +00:00
|
|
|
const [fileMatches, setFileMatches] = useState<ZoekFileMatch[]>([]);
|
2024-08-25 03:58:31 +00:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
const [searchDurationMs, setSearchDurationMs] = useState(0);
|
2024-08-25 00:45:44 +00:00
|
|
|
|
2024-08-25 03:58:31 +00:00
|
|
|
// @todo: We need to be able to handle the case when the user navigates backwards / forwards.
|
|
|
|
|
// Currently we do not re-query.
|
2024-08-23 20:54:13 +00:00
|
|
|
|
2024-08-25 00:45:44 +00:00
|
|
|
return (
|
|
|
|
|
<main className="flex h-screen flex-col">
|
2024-08-25 04:18:53 +00:00
|
|
|
<div className="flex flex-row p-1 gap-4 items-center">
|
2024-08-25 00:45:44 +00:00
|
|
|
<Image
|
|
|
|
|
src={logo}
|
|
|
|
|
className="h-12 w-auto"
|
|
|
|
|
alt={"Sourcebot logo"}
|
|
|
|
|
/>
|
2024-08-25 02:39:59 +00:00
|
|
|
<SearchBar
|
|
|
|
|
query={query}
|
|
|
|
|
numResults={numResults}
|
|
|
|
|
onQueryChange={(query) => setQuery(query)}
|
2024-08-25 03:58:31 +00:00
|
|
|
onLoadingChange={(isLoading) => setIsLoading(isLoading)}
|
2024-08-25 03:10:01 +00:00
|
|
|
onSearchResult={(result) => {
|
2024-08-25 03:58:31 +00:00
|
|
|
if (result) {
|
|
|
|
|
setFileMatches(result.FileMatches ?? []);
|
|
|
|
|
setSearchDurationMs(Math.round(result.Stats.Duration / 1000000));
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 02:39:59 +00:00
|
|
|
router.push(`?query=${query}&numResults=${numResults}`);
|
2024-08-25 00:45:44 +00:00
|
|
|
}}
|
|
|
|
|
/>
|
2024-08-25 03:58:31 +00:00
|
|
|
{isLoading && (
|
|
|
|
|
<SymbolIcon className="h-4 w-4 animate-spin" />
|
|
|
|
|
)}
|
2024-08-25 00:45:44 +00:00
|
|
|
</div>
|
|
|
|
|
<Separator />
|
|
|
|
|
<div className="bg-accent p-2">
|
2024-08-25 03:58:31 +00:00
|
|
|
<p className="text-sm font-medium">Results for: {fileMatches.length} files in {searchDurationMs} ms</p>
|
2024-08-25 00:45:44 +00:00
|
|
|
</div>
|
2024-08-25 04:18:53 +00:00
|
|
|
<Separator />
|
2024-08-25 00:45:44 +00:00
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
{fileMatches.map((match, index) => (
|
|
|
|
|
<FileMatch key={index} match={match} />
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</main>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-08-23 20:54:13 +00:00
|
|
|
|
2024-08-25 02:39:59 +00:00
|
|
|
interface SearchBarProps {
|
|
|
|
|
query: string;
|
|
|
|
|
numResults: number;
|
2024-08-25 03:58:31 +00:00
|
|
|
onLoadingChange: (isLoading: boolean) => void;
|
2024-08-25 02:39:59 +00:00
|
|
|
onQueryChange: (query: string) => void;
|
2024-08-25 03:10:01 +00:00
|
|
|
onSearchResult: (result?: ZoekResult) => void,
|
2024-08-25 02:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SearchBar = ({
|
|
|
|
|
query,
|
|
|
|
|
numResults,
|
2024-08-25 03:58:31 +00:00
|
|
|
onLoadingChange,
|
2024-08-25 02:39:59 +00:00
|
|
|
onQueryChange,
|
|
|
|
|
onSearchResult,
|
|
|
|
|
}: SearchBarProps) => {
|
|
|
|
|
const SEARCH_DEBOUNCE_MS = 200;
|
|
|
|
|
|
2024-08-25 03:58:31 +00:00
|
|
|
// @todo : we should probably be cancelling any running requests
|
2024-08-25 02:39:59 +00:00
|
|
|
const search = useDebouncedCallback((query: string) => {
|
|
|
|
|
if (query === "") {
|
2024-08-25 03:10:01 +00:00
|
|
|
onSearchResult(undefined);
|
2024-08-25 02:39:59 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log('making query...');
|
2024-08-25 03:58:31 +00:00
|
|
|
|
|
|
|
|
onLoadingChange(true);
|
2024-08-25 02:39:59 +00:00
|
|
|
fetch(`http://localhost:3000/zoekt/search?query=${query}&numResults=${numResults}`)
|
|
|
|
|
.then(response => response.json())
|
2024-08-25 03:10:01 +00:00
|
|
|
.then(({ data }: { data: ZoekSearchResponse }) => {
|
|
|
|
|
onSearchResult(data.result);
|
2024-08-25 02:39:59 +00:00
|
|
|
})
|
|
|
|
|
// @todo : error handling
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
console.log('done making query');
|
2024-08-25 03:58:31 +00:00
|
|
|
onLoadingChange(false);
|
|
|
|
|
});
|
2024-08-25 02:39:59 +00:00
|
|
|
}, SEARCH_DEBOUNCE_MS);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
search(query);
|
|
|
|
|
}, [query]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Input
|
|
|
|
|
value={query}
|
|
|
|
|
className="max-w-lg"
|
|
|
|
|
placeholder="Search..."
|
|
|
|
|
onChange={(e) => {
|
|
|
|
|
const query = e.target.value;
|
|
|
|
|
onQueryChange(query);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 00:45:44 +00:00
|
|
|
interface FileMatchProps {
|
|
|
|
|
match: ZoekFileMatch;
|
|
|
|
|
}
|
2024-08-23 20:54:13 +00:00
|
|
|
|
2024-08-25 00:45:44 +00:00
|
|
|
const FileMatch = ({
|
|
|
|
|
match,
|
|
|
|
|
}: FileMatchProps) => {
|
2024-08-23 20:54:13 +00:00
|
|
|
|
2024-08-25 00:45:44 +00:00
|
|
|
return (
|
|
|
|
|
<div>
|
2024-08-25 04:18:53 +00:00
|
|
|
<div className="bg-cyan-200 primary-foreground px-2">
|
2024-08-25 04:34:24 +00:00
|
|
|
<span>{match.Repo} · {match.FileName}</span>
|
2024-08-25 04:18:53 +00:00
|
|
|
</div>
|
2024-08-25 04:34:24 +00:00
|
|
|
{match.Matches.map((match, index) => {
|
|
|
|
|
const fragment = match.Fragments[0];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div key={index} className="font-mono px-4 py-0.5 text-sm">
|
|
|
|
|
<p>{match.LineNum}: {fragment.Pre}<span className="font-bold">{fragment.Match}</span>{fragment.Post}</p>
|
|
|
|
|
<Separator />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
2024-08-25 00:45:44 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-08-23 20:54:13 +00:00
|
|
|
}
|