Skip to content

Commit 496ecbc

Browse files
feature(code/frontend): search filter default repo options (#35202)
1 parent fdab56a commit 496ecbc

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

x-pack/plugins/code/public/components/query_bar/components/__snapshots__/query_bar.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugins/code/public/components/query_bar/components/options.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const Icon = styled(EuiIcon)`
3939
interface State {
4040
isFlyoutOpen: boolean;
4141
repoScope: Repository[];
42+
query: string;
4243
}
4344

4445
interface Props {
@@ -47,10 +48,12 @@ interface Props {
4748
repoSearchResults: any[];
4849
searchLoading: boolean;
4950
searchOptions: ISearchOptions;
51+
defaultRepoOptions: Repository[];
5052
}
5153

5254
export class SearchOptions extends Component<Props, State> {
5355
public state: State = {
56+
query: '',
5457
isFlyoutOpen: false,
5558
repoScope: this.props.searchOptions.repoScope,
5659
};
@@ -109,9 +112,15 @@ export class SearchOptions extends Component<Props, State> {
109112
<EuiComboBox
110113
placeholder="Search to add repos"
111114
async={true}
112-
options={this.props.repoSearchResults.map(repo => ({
113-
label: repo.name,
114-
}))}
115+
options={
116+
this.state.query
117+
? this.props.repoSearchResults.map(repo => ({
118+
label: repo.name,
119+
}))
120+
: this.props.defaultRepoOptions.map(repo => ({
121+
label: repo.name,
122+
}))
123+
}
115124
selectedOptions={[]}
116125
isLoading={this.props.searchLoading}
117126
onChange={this.onRepoChange}
@@ -144,6 +153,7 @@ export class SearchOptions extends Component<Props, State> {
144153
}
145154

146155
private onRepoSearchChange = (searchValue: string) => {
156+
this.setState({ query: searchValue });
147157
if (searchValue) {
148158
this.props.repositorySearch({ query: searchValue });
149159
}
@@ -153,7 +163,11 @@ export class SearchOptions extends Component<Props, State> {
153163
this.setState(prevState => ({
154164
repoScope: unique([
155165
...prevState.repoScope,
156-
...repos.map((r: any) => this.props.repoSearchResults.find(rs => rs.name === r.label)),
166+
...repos.map((r: any) =>
167+
[...this.props.repoSearchResults, ...this.props.defaultRepoOptions].find(
168+
rs => rs.name === r.label
169+
)
170+
),
157171
]),
158172
}));
159173
};

x-pack/plugins/code/public/components/query_bar/components/query_bar.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ test('render correctly with empty query string', () => {
4040
onSelect={emptyFn}
4141
onSearchScopeChanged={emptyFn}
4242
searchScope={SearchScope.DEFAULT}
43+
defaultRepoOptions={[]}
4344
/>
4445
);
4546
expect(toJson(queryBarComp)).toMatchSnapshot();
@@ -100,6 +101,7 @@ test('render correctly with input query string changed', done => {
100101
onSelect={emptyFn}
101102
onSearchScopeChanged={emptyFn}
102103
searchScope={SearchScope.DEFAULT}
104+
defaultRepoOptions={[]}
103105
/>
104106
</MemoryRouter>
105107
);

x-pack/plugins/code/public/components/query_bar/components/query_bar.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { matchPairs } from '../lib/match_pairs';
1818
import { SuggestionsComponent } from './typeahead/suggestions_component';
1919

20-
import { SearchScope } from '../../../../model';
20+
import { SearchScope, Repository } from '../../../../model';
2121
import { SearchScopePlaceholderText } from '../../../common/types';
2222
import { RootState } from '../../../reducers';
2323
import {
@@ -55,6 +55,7 @@ interface Props {
5555
searchLoading: boolean;
5656
searchScope: SearchScope;
5757
searchOptions: ISearchOptions;
58+
defaultRepoOptions: Repository[];
5859
}
5960

6061
interface State {
@@ -460,6 +461,7 @@ export class CodeQueryBar extends Component<Props, State> {
460461
role="textbox"
461462
/>
462463
<SearchOptions
464+
defaultRepoOptions={this.props.defaultRepoOptions}
463465
repositorySearch={this.props.repositorySearch}
464466
saveSearchOptions={this.props.saveSearchOptions}
465467
repoSearchResults={this.props.repoSearchResults}
@@ -494,6 +496,7 @@ const mapStateToProps = (state: RootState) => ({
494496
searchLoading: state.search.isScopeSearchLoading,
495497
searchScope: state.search.scope,
496498
searchOptions: state.search.searchOptions,
499+
defaultRepoOptions: state.repository.repositories.slice(0, 5),
497500
});
498501

499502
const mapDispatchToProps = {

x-pack/plugins/code/public/sagas/editor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
Match,
2727
resetRepoTree,
2828
revealPosition,
29+
fetchRepos,
2930
} from '../actions';
3031
import { loadRepo, loadRepoFailed, loadRepoSuccess } from '../actions/status';
3132
import { PathTypes } from '../common/types';
@@ -137,6 +138,8 @@ export function* watchLoadRepo() {
137138
}
138139

139140
function* handleMainRouteChange(action: Action<Match>) {
141+
// in source view page, we need repos as default repo scope options when no query input
142+
yield put(fetchRepos());
140143
const { location } = action.payload!;
141144
const search = location.search.startsWith('?') ? location.search.substring(1) : location.search;
142145
const queryParams = queryString.parse(search);

0 commit comments

Comments
 (0)