Skip to content

Commit

Permalink
[dagit] Fix SearchBootstrapQuery to be lazy (#7981)
Browse files Browse the repository at this point in the history
### Summary & Motivation

Make the global search dialog's bootstrap query be lazy (upon opening the search dialog) instead of loading it up front.

I had thought this was already lazy, but apparently not.

### How I Tested These Changes

Load Dagit, verify that no `SearchBootstrapQuery` is fired. Click "Search", verify that it is fired and that search behaves normally.
  • Loading branch information
hellendag committed May 20, 2022
1 parent 129a8d6 commit b49cfe4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
8 changes: 6 additions & 2 deletions js_modules/dagit/packages/core/src/search/SearchDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ const initialState: State = {
export const SearchDialog: React.FC<{searchPlaceholder: string}> = ({searchPlaceholder}) => {
const location = useLocation();
const history = useHistory();
const {loading, performSearch} = useRepoSearch();
const {performBootstrapQuery, loading, performSearch} = useRepoSearch();

const [state, dispatch] = React.useReducer(reducer, initialState);
const {shown, queryString, results, highlight, loaded} = state;

const renderedResults = results.slice(0, MAX_DISPLAYED_RESULTS);
const numRenderedResults = renderedResults.length;

const openSearch = React.useCallback(() => dispatch({type: 'show-dialog'}), []);
const openSearch = React.useCallback(() => {
performBootstrapQuery();
dispatch({type: 'show-dialog'});
}, [performBootstrapQuery]);

const onChange = React.useCallback((e) => {
dispatch({type: 'change-query', queryString: e.target.value});
}, []);
Expand Down
22 changes: 11 additions & 11 deletions js_modules/dagit/packages/core/src/search/useRepoSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {gql, useLazyQuery, useQuery} from '@apollo/client';
import {gql, useLazyQuery} from '@apollo/client';
import Fuse from 'fuse.js';
import * as React from 'react';

Expand Down Expand Up @@ -127,15 +127,15 @@ const secondaryDataToSearchResults = (data?: SearchSecondaryQuery) => {
};

export const useRepoSearch = () => {
const {data: bootstrapData, loading: bootstrapLoading} = useQuery<SearchBootstrapQuery>(
SEARCH_BOOTSTRAP_QUERY,
{
fetchPolicy: 'cache-and-network',
},
);
const [
performBootstrapQuery,
{data: bootstrapData, loading: bootstrapLoading},
] = useLazyQuery<SearchBootstrapQuery>(SEARCH_BOOTSTRAP_QUERY, {
fetchPolicy: 'cache-and-network',
});

const [
performQuery,
performSecondaryQuery,
{data: secondaryData, loading: secondaryLoading, called: secondaryQueryCalled},
] = useLazyQuery<SearchSecondaryQuery>(SEARCH_SECONDARY_QUERY, {
fetchPolicy: 'cache-and-network',
Expand All @@ -151,16 +151,16 @@ export const useRepoSearch = () => {
const performSearch = React.useCallback(
(queryString: string, buildSecondary?: boolean): Fuse.FuseResult<SearchResult>[] => {
if ((queryString || buildSecondary) && !secondaryQueryCalled) {
performQuery();
performSecondaryQuery();
}
const bootstrapResults: Fuse.FuseResult<SearchResult>[] = bootstrapFuse.search(queryString);
const secondaryResults: Fuse.FuseResult<SearchResult>[] = secondaryFuse.search(queryString);
return [...bootstrapResults, ...secondaryResults];
},
[bootstrapFuse, secondaryFuse, performQuery, secondaryQueryCalled],
[bootstrapFuse, secondaryFuse, performSecondaryQuery, secondaryQueryCalled],
);

return {loading, performSearch};
return {performBootstrapQuery, loading, performSearch};
};

const SEARCH_BOOTSTRAP_QUERY = gql`
Expand Down

0 comments on commit b49cfe4

Please sign in to comment.