2024-09-03 01:46:43 +00:00
'use client' ;
import {
ResizableHandle ,
ResizablePanel ,
ResizablePanelGroup ,
} from "@/components/ui/resizable" ;
import { Separator } from "@/components/ui/separator" ;
2024-10-28 17:30:29 +00:00
import useCaptureEvent from "@/hooks/useCaptureEvent" ;
2024-09-03 01:46:43 +00:00
import { useNonEmptyQueryParam } from "@/hooks/useNonEmptyQueryParam" ;
2024-11-27 05:49:41 +00:00
import { Repository , SearchQueryParams , SearchResultFile } from "@/lib/types" ;
2024-09-26 03:12:20 +00:00
import { createPathWithQueryParams } from "@/lib/utils" ;
2024-09-03 01:46:43 +00:00
import { SymbolIcon } from "@radix-ui/react-icons" ;
import { useQuery } from "@tanstack/react-query" ;
import Image from "next/image" ;
2024-09-10 19:59:42 +00:00
import { useRouter } from "next/navigation" ;
2024-10-28 17:30:29 +00:00
import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
2024-09-03 01:46:43 +00:00
import logoDark from "../../../public/sb_logo_dark.png" ;
import logoLight from "../../../public/sb_logo_light.png" ;
2024-11-27 05:49:41 +00:00
import { getRepos , search } from "../api/(client)/client" ;
Search suggestions (#85)
The motivation for building search suggestions is two-fold: (1) to make the zoekt query language more approachable by presenting all available options to the user, and (2) make it easier for power-users to craft complex queries.
The meat-n-potatoes of this change are concentrated in searchBar.tsx and searchSuggestionBox.tsx. The suggestions box works by maintaining a state-machine of "modes". By default, the box is in the refine mode, where suggestions for different prefixes (e.g., repo:, lang:, etc.) are suggested to the user. When one of these prefixes is matched, the state-machine transitions to the corresponding mode (e.g., repository, language, etc.) and surfaces suggestions for that mode (if any).
The query is split up into parts by spaces " " (e.g., 'test repo:hello' -> ['test', 'repo:hello']). See splitQuery. The part that has the cursor over it is considered the active part. We evaluate which mode the state machine is in based on the active part. When a suggestion is clicked, we only modify the active part of the query.
Three modes are currently missing suggestion data: file (file names), revision (branch / tag names), and symbol (symbol names). In future PRs, we will need to introduce endpoints into the backend to allow the frontend to fetch this data and surface it as suggestions..
2024-11-23 02:50:13 +00:00
import { SearchBar } from "../components/searchBar" ;
import { SettingsDropdown } from "../components/settingsDropdown" ;
2024-09-26 03:12:20 +00:00
import { CodePreviewPanel } from "./components/codePreviewPanel" ;
2024-10-28 17:30:29 +00:00
import { FilterPanel } from "./components/filterPanel" ;
2024-09-26 03:12:20 +00:00
import { SearchResultsPanel } from "./components/searchResultsPanel" ;
2024-10-28 17:30:29 +00:00
import { ImperativePanelHandle } from "react-resizable-panels" ;
2024-11-29 18:42:08 +00:00
import { useSearchHistory } from "@/hooks/useSearchHistory" ;
2024-09-26 06:31:51 +00:00
2024-10-30 16:32:05 +00:00
const DEFAULT_MAX_MATCH_DISPLAY_COUNT = 10000 ;
2024-09-26 06:31:51 +00:00
2024-09-03 01:46:43 +00:00
export default function SearchPage() {
const router = useRouter ( ) ;
2024-09-26 06:31:51 +00:00
const searchQuery = useNonEmptyQueryParam ( SearchQueryParams . query ) ? ? "" ;
const _maxMatchDisplayCount = parseInt ( useNonEmptyQueryParam ( SearchQueryParams . maxMatchDisplayCount ) ? ? ` ${ DEFAULT_MAX_MATCH_DISPLAY_COUNT } ` ) ;
const maxMatchDisplayCount = isNaN ( _maxMatchDisplayCount ) ? DEFAULT_MAX_MATCH_DISPLAY_COUNT : _maxMatchDisplayCount ;
2024-11-29 18:42:08 +00:00
const { setSearchHistory } = useSearchHistory ( ) ;
2024-09-17 04:37:34 +00:00
const captureEvent = useCaptureEvent ( ) ;
2024-09-03 01:46:43 +00:00
const { data : searchResponse , isLoading } = useQuery ( {
2024-09-26 06:31:51 +00:00
queryKey : [ "search" , searchQuery , maxMatchDisplayCount ] ,
2024-09-10 06:16:41 +00:00
queryFn : ( ) = > search ( {
query : searchQuery ,
2024-09-26 06:31:51 +00:00
maxMatchDisplayCount ,
2024-09-10 06:16:41 +00:00
} ) ,
2024-09-03 01:46:43 +00:00
enabled : searchQuery.length > 0 ,
2024-09-17 04:37:34 +00:00
refetchOnWindowFocus : false ,
2024-09-03 01:46:43 +00:00
} ) ;
2024-11-29 18:42:08 +00:00
// Write the query to the search history
useEffect ( ( ) = > {
if ( searchQuery . length === 0 ) {
return ;
}
const now = new Date ( ) . toUTCString ( ) ;
setSearchHistory ( ( searchHistory ) = > [
{
query : searchQuery ,
date : now ,
} ,
. . . searchHistory . filter ( search = > search . query !== searchQuery ) ,
] )
} , [ searchQuery , setSearchHistory ] ) ;
2024-11-27 05:49:41 +00:00
// Use the /api/repos endpoint to get a useful list of
// repository metadata (like host type, repo name, etc.)
// Convert this into a map of repo name to repo metadata
// for easy lookup.
const { data : repoMetadata } = useQuery ( {
queryKey : [ "repos" ] ,
queryFn : ( ) = > getRepos ( ) ,
select : ( data ) : Record < string , Repository > = >
data . List . Repos
. map ( r = > r . Repository )
. reduce (
( acc , repo ) = > ( {
. . . acc ,
[ repo . Name ] : repo ,
} ) ,
{ } ,
) ,
refetchOnWindowFocus : false ,
} ) ;
2024-09-17 04:37:34 +00:00
useEffect ( ( ) = > {
if ( ! searchResponse ) {
return ;
}
const fileLanguages = searchResponse . Result . Files ? . map ( file = > file . Language ) || [ ] ;
captureEvent ( "search_finished" , {
contentBytesLoaded : searchResponse.Result.ContentBytesLoaded ,
indexBytesLoaded : searchResponse.Result.IndexBytesLoaded ,
crashes : searchResponse.Result.Crashes ,
durationMs : searchResponse.Result.Duration / 1000000 ,
fileCount : searchResponse.Result.FileCount ,
shardFilesConsidered : searchResponse.Result.ShardFilesConsidered ,
filesConsidered : searchResponse.Result.FilesConsidered ,
filesLoaded : searchResponse.Result.FilesLoaded ,
filesSkipped : searchResponse.Result.FilesSkipped ,
shardsScanned : searchResponse.Result.ShardsScanned ,
shardsSkipped : searchResponse.Result.ShardsSkipped ,
shardsSkippedFilter : searchResponse.Result.ShardsSkippedFilter ,
matchCount : searchResponse.Result.MatchCount ,
ngramMatches : searchResponse.Result.NgramMatches ,
ngramLookups : searchResponse.Result.NgramLookups ,
wait : searchResponse.Result.Wait ,
matchTreeConstruction : searchResponse.Result.MatchTreeConstruction ,
matchTreeSearch : searchResponse.Result.MatchTreeSearch ,
regexpsConsidered : searchResponse.Result.RegexpsConsidered ,
flushReason : searchResponse.Result.FlushReason ,
fileLanguages ,
} ) ;
} , [ captureEvent , searchResponse ] ) ;
2024-11-27 05:49:41 +00:00
const { fileMatches , searchDurationMs , totalMatchCount , isBranchFilteringEnabled , repoUrlTemplates } = useMemo ( ( ) = > {
2024-09-03 01:46:43 +00:00
if ( ! searchResponse ) {
return {
fileMatches : [ ] ,
searchDurationMs : 0 ,
2024-11-07 02:28:10 +00:00
totalMatchCount : 0 ,
isBranchFilteringEnabled : false ,
2024-11-27 05:49:41 +00:00
repoUrlTemplates : { } ,
2024-09-03 01:46:43 +00:00
} ;
}
2024-09-10 06:16:41 +00:00
2024-11-07 02:28:10 +00:00
const isBranchFilteringEnabled = searchResponse . isBranchFilteringEnabled ;
let fileMatches = searchResponse . Result . Files ? ? [ ] ;
// We only want to show matches for the default branch when
// the user isn't explicitly filtering by branch.
if ( ! isBranchFilteringEnabled ) {
fileMatches = fileMatches . filter ( match = > {
// @note : this case handles local repos that don't have any branches.
if ( ! match . Branches ) {
return true ;
}
return match . Branches . includes ( "HEAD" ) ;
} ) ;
}
2024-09-03 01:46:43 +00:00
return {
2024-11-07 02:28:10 +00:00
fileMatches ,
2024-09-10 06:16:41 +00:00
searchDurationMs : Math.round ( searchResponse . Result . Duration / 1000000 ) ,
2024-11-07 02:28:10 +00:00
totalMatchCount : searchResponse.Result.MatchCount ,
isBranchFilteringEnabled ,
2024-11-27 05:49:41 +00:00
repoUrlTemplates : searchResponse.Result.RepoURLs ,
2024-09-03 01:46:43 +00:00
}
Search suggestions (#85)
The motivation for building search suggestions is two-fold: (1) to make the zoekt query language more approachable by presenting all available options to the user, and (2) make it easier for power-users to craft complex queries.
The meat-n-potatoes of this change are concentrated in searchBar.tsx and searchSuggestionBox.tsx. The suggestions box works by maintaining a state-machine of "modes". By default, the box is in the refine mode, where suggestions for different prefixes (e.g., repo:, lang:, etc.) are suggested to the user. When one of these prefixes is matched, the state-machine transitions to the corresponding mode (e.g., repository, language, etc.) and surfaces suggestions for that mode (if any).
The query is split up into parts by spaces " " (e.g., 'test repo:hello' -> ['test', 'repo:hello']). See splitQuery. The part that has the cursor over it is considered the active part. We evaluate which mode the state machine is in based on the active part. When a suggestion is clicked, we only modify the active part of the query.
Three modes are currently missing suggestion data: file (file names), revision (branch / tag names), and symbol (symbol names). In future PRs, we will need to introduce endpoints into the backend to allow the frontend to fetch this data and surface it as suggestions..
2024-11-23 02:50:13 +00:00
} , [ searchResponse ] ) ;
2024-09-03 01:46:43 +00:00
2024-09-10 19:59:42 +00:00
const isMoreResultsButtonVisible = useMemo ( ( ) = > {
2024-11-07 02:28:10 +00:00
return totalMatchCount > maxMatchDisplayCount ;
} , [ totalMatchCount , maxMatchDisplayCount ] ) ;
2024-09-26 06:31:51 +00:00
const numMatches = useMemo ( ( ) = > {
// Accumualtes the number of matches across all files
2024-11-07 02:28:10 +00:00
return fileMatches . reduce (
2024-09-26 06:31:51 +00:00
( acc , file ) = >
acc + file . ChunkMatches . reduce (
( acc , chunk ) = > acc + chunk . Ranges . length ,
0 ,
) ,
0 ,
2024-11-07 02:28:10 +00:00
) ;
} , [ fileMatches ] ) ;
2024-09-26 06:31:51 +00:00
const onLoadMoreResults = useCallback ( ( ) = > {
const url = createPathWithQueryParams ( '/search' ,
[ SearchQueryParams . query , searchQuery ] ,
[ SearchQueryParams . maxMatchDisplayCount , ` ${ maxMatchDisplayCount * 2 } ` ] ,
)
router . push ( url ) ;
} , [ maxMatchDisplayCount , router , searchQuery ] ) ;
2024-09-03 01:46:43 +00:00
return (
< div className = "flex flex-col h-screen overflow-clip" >
{ /* TopBar */ }
< div className = "sticky top-0 left-0 right-0 z-10" >
< div className = "flex flex-row justify-between items-center py-1.5 px-3 gap-4" >
< div className = "grow flex flex-row gap-4 items-center" >
< div
2024-09-24 05:30:58 +00:00
className = "shrink-0 cursor-pointer"
2024-09-03 01:46:43 +00:00
onClick = { ( ) = > {
router . push ( "/" ) ;
} }
>
< Image
src = { logoDark }
className = "h-4 w-auto hidden dark:block"
alt = { "Sourcebot logo" }
/ >
< Image
src = { logoLight }
className = "h-4 w-auto block dark:hidden"
alt = { "Sourcebot logo" }
/ >
< / div >
< SearchBar
size = "sm"
defaultQuery = { searchQuery }
Search suggestions (#85)
The motivation for building search suggestions is two-fold: (1) to make the zoekt query language more approachable by presenting all available options to the user, and (2) make it easier for power-users to craft complex queries.
The meat-n-potatoes of this change are concentrated in searchBar.tsx and searchSuggestionBox.tsx. The suggestions box works by maintaining a state-machine of "modes". By default, the box is in the refine mode, where suggestions for different prefixes (e.g., repo:, lang:, etc.) are suggested to the user. When one of these prefixes is matched, the state-machine transitions to the corresponding mode (e.g., repository, language, etc.) and surfaces suggestions for that mode (if any).
The query is split up into parts by spaces " " (e.g., 'test repo:hello' -> ['test', 'repo:hello']). See splitQuery. The part that has the cursor over it is considered the active part. We evaluate which mode the state machine is in based on the active part. When a suggestion is clicked, we only modify the active part of the query.
Three modes are currently missing suggestion data: file (file names), revision (branch / tag names), and symbol (symbol names). In future PRs, we will need to introduce endpoints into the backend to allow the frontend to fetch this data and surface it as suggestions..
2024-11-23 02:50:13 +00:00
className = "w-full"
2024-09-03 01:46:43 +00:00
/ >
< / div >
< SettingsDropdown
menuButtonClassName = "w-8 h-8"
/ >
< / div >
< Separator / >
2024-10-28 17:30:29 +00:00
{ ! isLoading && (
< div className = "bg-accent py-1 px-2 flex flex-row items-center gap-4" >
{
2024-11-07 02:28:10 +00:00
fileMatches . length > 0 ? (
< p className = "text-sm font-medium" > { ` [ ${ searchDurationMs } ms] Found ${ numMatches } matches in ${ fileMatches . length } ${ fileMatches . length > 1 ? 'files' : 'file' } ` } < / p >
2024-10-28 17:30:29 +00:00
) : (
< p className = "text-sm font-medium" > No results < / p >
)
}
{ isMoreResultsButtonVisible && (
< div
className = "cursor-pointer text-blue-500 text-sm hover:underline"
onClick = { onLoadMoreResults }
>
( load more )
< / div >
) }
< / div >
) }
2024-09-03 01:46:43 +00:00
< Separator / >
< / div >
2024-10-28 17:30:29 +00:00
{ isLoading ? (
< div className = "flex flex-col items-center justify-center h-full gap-2" >
< SymbolIcon className = "h-6 w-6 animate-spin" / >
< p className = "font-semibold text-center" > Searching . . . < / p >
< / div >
) : (
< PanelGroup
fileMatches = { fileMatches }
isMoreResultsButtonVisible = { isMoreResultsButtonVisible }
onLoadMoreResults = { onLoadMoreResults }
2024-11-07 02:28:10 +00:00
isBranchFilteringEnabled = { isBranchFilteringEnabled }
2024-11-27 05:49:41 +00:00
repoUrlTemplates = { repoUrlTemplates }
repoMetadata = { repoMetadata ? ? { } }
2024-10-28 17:30:29 +00:00
/ >
) }
2024-09-03 01:46:43 +00:00
< / div >
) ;
}
2024-10-28 17:30:29 +00:00
interface PanelGroupProps {
fileMatches : SearchResultFile [ ] ;
isMoreResultsButtonVisible? : boolean ;
onLoadMoreResults : ( ) = > void ;
2024-11-07 02:28:10 +00:00
isBranchFilteringEnabled : boolean ;
2024-11-27 05:49:41 +00:00
repoUrlTemplates : Record < string , string > ;
repoMetadata : Record < string , Repository > ;
2024-10-28 17:30:29 +00:00
}
const PanelGroup = ( {
fileMatches ,
isMoreResultsButtonVisible ,
onLoadMoreResults ,
2024-11-07 02:28:10 +00:00
isBranchFilteringEnabled ,
2024-11-27 05:49:41 +00:00
repoUrlTemplates ,
repoMetadata ,
2024-10-28 17:30:29 +00:00
} : PanelGroupProps ) = > {
const [ selectedMatchIndex , setSelectedMatchIndex ] = useState ( 0 ) ;
const [ selectedFile , setSelectedFile ] = useState < SearchResultFile | undefined > ( undefined ) ;
const [ filteredFileMatches , setFilteredFileMatches ] = useState < SearchResultFile [ ] > ( fileMatches ) ;
const codePreviewPanelRef = useRef < ImperativePanelHandle > ( null ) ;
useEffect ( ( ) = > {
if ( selectedFile ) {
codePreviewPanelRef . current ? . expand ( ) ;
} else {
codePreviewPanelRef . current ? . collapse ( ) ;
}
} , [ selectedFile ] ) ;
2024-10-30 16:32:05 +00:00
const onFilterChanged = useCallback ( ( matches : SearchResultFile [ ] ) = > {
setFilteredFileMatches ( matches ) ;
} , [ ] ) ;
2024-10-28 17:30:29 +00:00
return (
< ResizablePanelGroup
direction = "horizontal"
2024-12-03 20:46:42 +00:00
className = "h-full"
2024-10-28 17:30:29 +00:00
>
{ /* ~~ Filter panel ~~ */ }
< ResizablePanel
minSize = { 20 }
maxSize = { 30 }
defaultSize = { 20 }
collapsible = { true }
id = { 'filter-panel' }
order = { 1 }
>
< FilterPanel
matches = { fileMatches }
2024-10-30 16:32:05 +00:00
onFilterChanged = { onFilterChanged }
2024-11-27 05:49:41 +00:00
repoMetadata = { repoMetadata }
2024-10-28 17:30:29 +00:00
/ >
< / ResizablePanel >
< ResizableHandle
className = "bg-accent w-1 transition-colors delay-50 data-[resize-handle-state=drag]:bg-accent-foreground data-[resize-handle-state=hover]:bg-accent-foreground"
/ >
{ /* ~~ Search results ~~ */ }
< ResizablePanel
minSize = { 10 }
id = { 'search-results-panel' }
order = { 2 }
>
{ filteredFileMatches . length > 0 ? (
2024-10-30 16:32:05 +00:00
< SearchResultsPanel
fileMatches = { filteredFileMatches }
onOpenFileMatch = { ( fileMatch ) = > {
setSelectedFile ( fileMatch ) ;
} }
onMatchIndexChanged = { ( matchIndex ) = > {
setSelectedMatchIndex ( matchIndex ) ;
} }
isLoadMoreButtonVisible = { ! ! isMoreResultsButtonVisible }
onLoadMoreButtonClicked = { onLoadMoreResults }
2024-11-07 02:28:10 +00:00
isBranchFilteringEnabled = { isBranchFilteringEnabled }
2024-11-27 05:49:41 +00:00
repoMetadata = { repoMetadata }
2024-10-30 16:32:05 +00:00
/ >
2024-10-28 17:30:29 +00:00
) : (
< div className = "flex flex-col items-center justify-center h-full" >
< p className = "text-sm text-muted-foreground" > No results found < / p >
< / div >
) }
< / ResizablePanel >
< ResizableHandle
withHandle = { selectedFile !== undefined }
/ >
{ /* ~~ Code preview ~~ */ }
< ResizablePanel
ref = { codePreviewPanelRef }
minSize = { 10 }
collapsible = { true }
id = { 'code-preview-panel' }
order = { 3 }
>
< CodePreviewPanel
fileMatch = { selectedFile }
onClose = { ( ) = > setSelectedFile ( undefined ) }
selectedMatchIndex = { selectedMatchIndex }
onSelectedMatchIndexChange = { setSelectedMatchIndex }
2024-11-27 05:49:41 +00:00
repoUrlTemplates = { repoUrlTemplates }
2024-10-28 17:30:29 +00:00
/ >
< / ResizablePanel >
< / ResizablePanelGroup >
)
2024-12-03 20:46:42 +00:00
}