Skip to content

Commit

Permalink
Autofocus adhoc query editor at start (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
czgu authored Jan 25, 2021
1 parent 5c4b0af commit 86065f1
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions querybook/webapp/components/QueryComposer/QueryComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ const useQuery = (dispatch: Dispatch, environmentId: number) => {

const useQueryComposerSearchAndReplace = (
query: string,
setQuery: (s: string) => any,
searchAndReplaceRef: React.MutableRefObject<ISearchAndReplaceHandles>
): ISearchAndReplaceProps => {
setQuery: (s: string) => any
) => {
const searchAndReplaceRef = useRef<ISearchAndReplaceHandles>(null);

const getSearchResults = useCallback(
(searchString: string, searchOptions: ISearchOptions) =>
searchText(query, searchString, searchOptions),
Expand All @@ -144,13 +145,42 @@ const useQueryComposerSearchAndReplace = (
searchAndReplaceRef.current?.performSearch();
}, [query]);

return {
const searchAndReplaceProps: ISearchAndReplaceProps = {
getSearchResults,
replace,
jumpToResult,
};

return {
searchAndReplaceProps,
searchAndReplaceRef,
};
};

function useQueryEditorHelpers() {
const queryEditorRef = useRef<QueryEditor>(null);
const handleFormatQuery = useCallback(() => {
if (queryEditorRef.current) {
queryEditorRef.current.formatQuery();
}
}, [queryEditorRef.current]);

const handleFocusEditor = useCallback(() => {
if (queryEditorRef.current) {
queryEditorRef.current.getEditor()?.focus();
}
}, [queryEditorRef.current]);

useEffect(() => {
handleFocusEditor();
}, []);

return {
queryEditorRef,
handleFormatQuery,
};
}

export const QueryComposer: React.FC = () => {
useBrowserTitle('Adhoc Query');

Expand All @@ -167,20 +197,20 @@ export const QueryComposer: React.FC = () => {
dispatch,
environmentId
);
const queryEditorRef = useRef<QueryEditor>(null);
const runButtonRef = useRef<IQueryRunButtonHandles>(null);
const searchAndReplaceRef = useRef<ISearchAndReplaceHandles>(null);

const {
searchAndReplaceProps,
searchAndReplaceRef,
} = useQueryComposerSearchAndReplace(query, setQuery);

const runButtonRef = useRef<IQueryRunButtonHandles>(null);
const clickOnRunButton = useCallback(() => {
if (runButtonRef.current) {
runButtonRef.current.clickRunButton();
}
}, [runButtonRef.current]);
const handleFormatQuery = useCallback(() => {
if (queryEditorRef.current) {
queryEditorRef.current.formatQuery();
}
}, [queryEditorRef.current]);

const { queryEditorRef, handleFormatQuery } = useQueryEditorHelpers();

const handleCreateDataDoc = useCallback(async () => {
let dataDoc = null;
Expand All @@ -203,12 +233,6 @@ export const QueryComposer: React.FC = () => {
navigateWithinEnv(`/datadoc/${dataDoc.id}/`);
}, [executionId, query]);

const searchAndReplaceProps = useQueryComposerSearchAndReplace(
query,
setQuery,
searchAndReplaceRef
);

const handleRunQuery = React.useCallback(async () => {
// Just to throttle to prevent double running
await sleep(250);
Expand All @@ -224,6 +248,7 @@ export const QueryComposer: React.FC = () => {

setExecutionId(id);
}, [queryEditorRef.current, query, engine]);

const keyMap = useMemo(
() => ({
'Shift-Enter': clickOnRunButton,
Expand Down

0 comments on commit 86065f1

Please sign in to comment.