-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add search function to action logs (#1436)
* feat: Add search function to action logs * fix: Revert unintended package.json script changes * feat: Apply style changes * fix: Remove unnecessary imports * fix: Fix review issues * feat: Extract search bar component * fix: Fix styling and some search highlight placement bugs
- Loading branch information
1 parent
6e37df8
commit 612f9c8
Showing
16 changed files
with
922 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import { | ||
Box, | ||
HStack, | ||
IconButton, | ||
Input, | ||
InputGroup, | ||
InputLeftElement, | ||
InputRightElement, | ||
Tooltip, | ||
} from "@chakra-ui/react" | ||
import { ArrowDown, ArrowUp, MatchCase, Search, WholeWord } from "@/icons" | ||
import { ReactElement, useEffect, useRef, useState } from "react" | ||
import { TSearchOptions } from "@/components/Terminal/useTerminalSearch" | ||
|
||
type TTerminalSearchBarProps = { | ||
prevSearchResult: VoidFunction | ||
nextSearchResult: VoidFunction | ||
totalSearchResults: number | ||
activeSearchResult: number | ||
onUpdateSearchOptions: (searchOptions: TSearchOptions) => void | ||
paddingX?: number | ||
paddingY?: number | ||
} | ||
|
||
export function TerminalSearchBar({ | ||
prevSearchResult, | ||
nextSearchResult, | ||
totalSearchResults, | ||
activeSearchResult, | ||
onUpdateSearchOptions, | ||
paddingY, | ||
paddingX, | ||
}: TTerminalSearchBarProps) { | ||
const [searchString, setSearchString] = useState<string | undefined>(undefined) | ||
const [debouncedSearchString, setDebouncedSearchString] = useState<string | undefined>(undefined) | ||
const [caseSensitive, setCaseSensitive] = useState<boolean>(false) | ||
const [wholeWordSearch, setWholeWordSearch] = useState<boolean>(false) | ||
|
||
const searchInputRef = useRef<HTMLInputElement | null>(null) | ||
|
||
// Debounce to prevent stutter when having a huge amount of results. | ||
useEffect(() => { | ||
// Sneaky heuristic: | ||
// If we have more than two characters, we're likely to have a more sane amount of results, so we can skip debouncing. | ||
const len = searchString?.length ?? 0 | ||
if (len > 2) { | ||
setDebouncedSearchString(searchString) | ||
|
||
return | ||
} | ||
|
||
const timeout = setTimeout(() => { | ||
setDebouncedSearchString(searchString) | ||
}, 200) | ||
|
||
return () => clearTimeout(timeout) | ||
}, [searchString]) | ||
|
||
// Pass on the search options to the outside world. We do it like this so the debouncing works nicely. | ||
useEffect(() => { | ||
onUpdateSearchOptions({ searchString: debouncedSearchString, caseSensitive, wholeWordSearch }) | ||
}, [debouncedSearchString, wholeWordSearch, caseSensitive, onUpdateSearchOptions]) | ||
|
||
return ( | ||
<HStack w={"full"} alignItems={"center"} paddingX={paddingX} paddingY={paddingY}> | ||
<InputGroup> | ||
<InputLeftElement cursor={"text"} onClick={() => searchInputRef.current?.focus()}> | ||
<Search boxSize={5} color={"text.tertiary"} /> | ||
</InputLeftElement> | ||
<Input | ||
ref={searchInputRef} | ||
value={searchString ?? ""} | ||
placeholder={"Search..."} | ||
spellCheck={false} | ||
bg={"white"} | ||
onKeyDown={(e) => { | ||
if (e.key === "Enter") { | ||
if (e.shiftKey) { | ||
prevSearchResult() | ||
} else { | ||
nextSearchResult() | ||
} | ||
} | ||
}} | ||
onChange={(e) => { | ||
setSearchString(e.target.value ? e.target.value : undefined) | ||
}} | ||
/> | ||
<InputRightElement w={"fit-content"} paddingX={"4"}> | ||
<HStack alignItems={"center"} w={"fit-content"}> | ||
<ToggleButton | ||
label={"Case sensitive"} | ||
icon={<MatchCase boxSize={5} />} | ||
value={caseSensitive} | ||
setValue={setCaseSensitive} | ||
/> | ||
<ToggleButton | ||
label={"Whole word"} | ||
icon={<WholeWord boxSize={5} />} | ||
value={wholeWordSearch} | ||
setValue={setWholeWordSearch} | ||
/> | ||
</HStack> | ||
</InputRightElement> | ||
</InputGroup> | ||
|
||
<Box | ||
flexShrink={0} | ||
minWidth={16} | ||
flexDirection={"row"} | ||
display={"flex"} | ||
justifyContent={"center"}> | ||
{totalSearchResults > 0 ? ( | ||
<Box marginLeft={2} marginRight={"1"} color={"text.tertiary"}> | ||
{activeSearchResult + 1} of {totalSearchResults} | ||
</Box> | ||
) : searchString ? ( | ||
<Box marginLeft={2} marginRight={"1"} color={"text.tertiary"}> | ||
0 of 0 | ||
</Box> | ||
) : ( | ||
<></> | ||
)} | ||
</Box> | ||
|
||
<Tooltip label={"Previous search result"}> | ||
<IconButton | ||
variant={"ghost"} | ||
onClick={prevSearchResult} | ||
aria-label={"Previous search result"} | ||
disabled={!totalSearchResults} | ||
icon={<ArrowUp boxSize={5} />} | ||
/> | ||
</Tooltip> | ||
|
||
<Tooltip label={"Next search result"}> | ||
<IconButton | ||
variant={"ghost"} | ||
onClick={nextSearchResult} | ||
aria-label={"Next search result"} | ||
disabled={!totalSearchResults} | ||
icon={<ArrowDown boxSize={5} />} | ||
/> | ||
</Tooltip> | ||
</HStack> | ||
) | ||
} | ||
|
||
function ToggleButton({ | ||
label, | ||
icon, | ||
value, | ||
setValue, | ||
}: { | ||
label: string | ||
icon: ReactElement | undefined | ||
value: boolean | ||
setValue: (value: boolean) => void | ||
}) { | ||
return ( | ||
<Tooltip label={label}> | ||
<IconButton | ||
borderRadius={"100%"} | ||
variant={"ghost"} | ||
color={value ? "white" : undefined} | ||
backgroundColor={value ? "primary.400" : undefined} | ||
_hover={{ | ||
bg: value ? "primary.600" : "gray.100", | ||
}} | ||
aria-label={label} | ||
fontFamily={"mono"} | ||
icon={icon} | ||
onClick={() => setValue(!value)} | ||
/> | ||
</Tooltip> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./Terminal" | ||
export * from "./useStreamingTerminal" | ||
export * from "./TerminalSearchBar" |
Oops, something went wrong.