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 02:39:59 +00:00
|
|
|
import { useEffect, useRef, 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 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ZoekSearchResult {
|
|
|
|
|
result: {
|
|
|
|
|
QueryStr: string,
|
|
|
|
|
FileMatches: ZoekFileMatch[] | null,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 02:39:59 +00:00
|
|
|
/**
|
|
|
|
|
* @note : when the user navigates backwards/forwards, the defaultQuery
|
|
|
|
|
* will update, but the query state will not. This effect keeps things in
|
|
|
|
|
* sync for that scenario.
|
|
|
|
|
*/
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setQuery(defaultQuery);
|
|
|
|
|
}, [defaultQuery]);
|
2024-08-23 20:54:13 +00:00
|
|
|
|
2024-08-25 00:45:44 +00:00
|
|
|
return (
|
|
|
|
|
<main className="flex h-screen flex-col">
|
|
|
|
|
<div className="flex flex-row p-2 gap-4 items-center">
|
|
|
|
|
<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)}
|
|
|
|
|
onClear={() => setFileMatches([])}
|
|
|
|
|
onSearchResult={({ result }) => {
|
|
|
|
|
setFileMatches(result.FileMatches ?? []);
|
|
|
|
|
router.push(`?query=${query}&numResults=${numResults}`);
|
2024-08-25 00:45:44 +00:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<Separator />
|
|
|
|
|
<div className="bg-accent p-2">
|
|
|
|
|
<p className="text-sm font-medium">Results for: {fileMatches.length} files</p>
|
|
|
|
|
</div>
|
|
|
|
|
<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;
|
|
|
|
|
onQueryChange: (query: string) => void;
|
|
|
|
|
onSearchResult: (result: ZoekSearchResult) => void,
|
|
|
|
|
onClear: () => void,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SearchBar = ({
|
|
|
|
|
query,
|
|
|
|
|
numResults,
|
|
|
|
|
onQueryChange,
|
|
|
|
|
onSearchResult,
|
|
|
|
|
onClear,
|
|
|
|
|
}: SearchBarProps) => {
|
|
|
|
|
const SEARCH_DEBOUNCE_MS = 200;
|
|
|
|
|
|
|
|
|
|
const search = useDebouncedCallback((query: string) => {
|
|
|
|
|
if (query === "") {
|
|
|
|
|
onClear();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log('making query...');
|
|
|
|
|
fetch(`http://localhost:3000/zoekt/search?query=${query}&numResults=${numResults}`)
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(({ data }: { data: ZoekSearchResult }) => {
|
|
|
|
|
onSearchResult(data);
|
|
|
|
|
})
|
|
|
|
|
// @todo : error handling
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
console.log('done making query');
|
|
|
|
|
})
|
|
|
|
|
}, 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>
|
|
|
|
|
<p><span className="font-bold">{match.Repo}</span> | {match.FileName}</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-08-23 20:54:13 +00:00
|
|
|
}
|