1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-10 01:16:39 +02:00

feat: search only features when there is search string (#7450)

Now the search hook is inside another component, so we do not get
searches without search query.
Also we had 2 state variable handing the search query. Removed one of
them.
This commit is contained in:
Jaanus Sellin 2024-06-25 15:17:17 +02:00 committed by GitHub
parent 3a3b6a29ff
commit b5e329e22d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 26 deletions

View File

@ -24,7 +24,7 @@ import { PageSuggestions } from './PageSuggestions';
import { useRoutes } from 'component/layout/MainLayout/NavigationSidebar/useRoutes'; import { useRoutes } from 'component/layout/MainLayout/NavigationSidebar/useRoutes';
import { useAsyncDebounce } from 'react-table'; import { useAsyncDebounce } from 'react-table';
import useProjects from 'hooks/api/getters/useProjects/useProjects'; import useProjects from 'hooks/api/getters/useProjects/useProjects';
import { useFeatureSearch } from 'hooks/api/getters/useFeatureSearch/useFeatureSearch'; import { CommandFeatures } from './CommandFeatures';
export const CommandResultsPaper = styled(Paper)(({ theme }) => ({ export const CommandResultsPaper = styled(Paper)(({ theme }) => ({
position: 'absolute', position: 'absolute',
@ -124,17 +124,6 @@ export const CommandBar = () => {
setShowSuggestions(false); setShowSuggestions(false);
}; };
const [value, setValue] = useState<string>('');
const { features = [] } = useFeatureSearch(
{
query: searchString,
limit: '3',
},
{
revalidateOnFocus: false,
},
);
const { projects } = useProjects(); const { projects } = useProjects();
const debouncedSetSearchState = useAsyncDebounce((query) => { const debouncedSetSearchState = useAsyncDebounce((query) => {
@ -163,7 +152,6 @@ export const CommandBar = () => {
const onSearchChange = (value: string) => { const onSearchChange = (value: string) => {
debouncedSetSearchState(value); debouncedSetSearchState(value);
setValue(value);
}; };
const hotkey = useKeyboardShortcut( const hotkey = useKeyboardShortcut(
@ -190,11 +178,6 @@ export const CommandBar = () => {
useOnClickOutside([searchContainerRef], hideSuggestions); useOnClickOutside([searchContainerRef], hideSuggestions);
useOnBlur(searchContainerRef, hideSuggestions); useOnBlur(searchContainerRef, hideSuggestions);
const flags: CommandResultGroupItem[] = features.map((feature) => ({
name: feature.name,
link: `/projects/${feature.project}/features/${feature.name}`,
}));
return ( return (
<StyledContainer ref={searchContainerRef} active={showSuggestions}> <StyledContainer ref={searchContainerRef} active={showSuggestions}>
<StyledSearch> <StyledSearch>
@ -211,7 +194,7 @@ export const CommandBar = () => {
'aria-label': placeholder, 'aria-label': placeholder,
'data-testid': SEARCH_INPUT, 'data-testid': SEARCH_INPUT,
}} }}
value={value} value={searchString}
onChange={(e) => onSearchChange(e.target.value)} onChange={(e) => onSearchChange(e.target.value)}
onFocus={() => { onFocus={() => {
setShowSuggestions(true); setShowSuggestions(true);
@ -220,7 +203,7 @@ export const CommandBar = () => {
<Box sx={{ width: (theme) => theme.spacing(4) }}> <Box sx={{ width: (theme) => theme.spacing(4) }}>
<ConditionallyRender <ConditionallyRender
condition={Boolean(value)} condition={Boolean(searchString)}
show={ show={
<Tooltip title='Clear search query' arrow> <Tooltip title='Clear search query' arrow>
<IconButton <IconButton
@ -243,14 +226,12 @@ export const CommandBar = () => {
</StyledSearch> </StyledSearch>
<ConditionallyRender <ConditionallyRender
condition={Boolean(value) && showSuggestions} condition={Boolean(searchString) && showSuggestions}
show={ show={
<CommandResultsPaper> <CommandResultsPaper>
<CommandResultGroup {searchString !== undefined && (
groupName={'Flags'} <CommandFeatures searchString={searchString} />
icon={'flag'} )}
items={flags}
/>
<CommandResultGroup <CommandResultGroup
groupName={'Projects'} groupName={'Projects'}
icon={'flag'} icon={'flag'}

View File

@ -0,0 +1,29 @@
import {
CommandResultGroup,
type CommandResultGroupItem,
} from './RecentlyVisited/CommandResultGroup';
import { useFeatureSearch } from 'hooks/api/getters/useFeatureSearch/useFeatureSearch';
interface ICommandBar {
searchString: string;
}
export const CommandFeatures = ({ searchString }: ICommandBar) => {
const { features = [] } = useFeatureSearch(
{
query: searchString,
limit: '3',
},
{
revalidateOnFocus: false,
},
);
const flags: CommandResultGroupItem[] = features.map((feature) => ({
name: feature.name,
link: `/projects/${feature.project}/features/${feature.name}`,
}));
return (
<CommandResultGroup groupName={'Flags'} icon={'flag'} items={flags} />
);
};