Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ import { v4 as uuidv4 } from 'uuid';
import { createPortal } from 'react-dom';
import useObservable from 'react-use/lib/useObservable';
import useLocalStorage from 'react-use/lib/useLocalStorage';
import { firstValueFrom, of } from 'rxjs';
import { QuerySource } from '@kbn/esql-types';
import { useCanCreateLookupIndex, useLookupIndexCommand } from './lookup_join';
import { useLookupIndexCommand } from './lookup_join';
import { useFieldsBrowser } from './resource_browser/use_fields_browser';
import { EditorFooter } from './editor_footer';
import { QuickSearchVisor } from './editor_visor';
Expand Down Expand Up @@ -664,8 +663,6 @@ const ESQLEditorInternal = function ESQLEditor({
return { cache: fn.cache, memoizedHistoryStarredItems: fn };
}, []);

const canCreateLookupIndex = useCanCreateLookupIndex();

// Extract source command and build minimal query with cluster prefixes
const minimalQuery = useMemo(() => {
const prefix = code.match(/\b(FROM|TS)\b/i)?.[1]?.toUpperCase();
Expand Down Expand Up @@ -714,11 +711,6 @@ const ESQLEditorInternal = function ESQLEditor({
});
useEsqlEditorActionsRegistration(editorActions);

const isResourceBrowserEnabled = useCallback(async () => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to its own hook

const currentApp = await firstValueFrom(application?.currentAppId$ ?? of(undefined));
return Boolean(enableResourceBrowser && currentApp === 'discover');
}, [application?.currentAppId$, enableResourceBrowser]);

const esqlCallbacks = useEsqlCallbacks({
core,
data,
Expand All @@ -727,7 +719,6 @@ const ESQLEditorInternal = function ESQLEditor({
esqlService,
histogramBarTarget,
activeSolutionId: activeSolutionId ?? undefined,
canCreateLookupIndex,
minimalQueryRef,
abortControllerRef,
dataSourcesCache,
Expand All @@ -738,7 +729,7 @@ const ESQLEditorInternal = function ESQLEditor({
memoizedHistoryStarredItems,
favoritesClient,
getJoinIndicesCallback,
isResourceBrowserEnabled,
enableResourceBrowser,
});

const {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { useCallback } from 'react';
import { firstValueFrom, of } from 'rxjs';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { ESQLEditorDeps } from '../types';

export const useCanSuggestResourceBrowser = (enableResourceBrowser: boolean) => {
const {
services: { application },
} = useKibana<ESQLEditorDeps>();

return useCallback(async () => {
const currentApp = await firstValueFrom(application?.currentAppId$ ?? of(undefined));
return Boolean(enableResourceBrowser && currentApp === 'discover');
}, [application?.currentAppId$, enableResourceBrowser]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import { clearCacheWhenOld } from './helpers';
import { getHistoryItems } from './history_local_storage';
import type { ESQLEditorDeps } from './types';
import type { StarredQueryMetadata } from './editor_footer/esql_starred_queries_service';
import { useCanCreateLookupIndex } from './lookup_join';
import { useCanSuggestResourceBrowser } from './resource_browser/use_can_suggest_resource_browser';

type MemoizedFn<TArgs extends unknown[], TResult> = (...args: TArgs) => {
timestamp: number;
Expand Down Expand Up @@ -68,7 +70,6 @@ interface UseEsqlCallbacksParams {
esqlService?: ESQLEditorDeps['esql'];
histogramBarTarget: number;
activeSolutionId?: Parameters<typeof getEditorExtensions>[2];
canCreateLookupIndex: ESQLCallbacks['canCreateLookupIndex'];
minimalQueryRef: MutableRefObject<string>;
abortControllerRef: MutableRefObject<AbortController>;
dataSourcesCache: MapCache;
Expand All @@ -79,7 +80,7 @@ interface UseEsqlCallbacksParams {
memoizedHistoryStarredItems: MemoizedHistoryStarredItems;
favoritesClient: FavoritesClient<StarredQueryMetadata>;
getJoinIndicesCallback: Required<ESQLCallbacks>['getJoinIndices'];
isResourceBrowserEnabled: () => Promise<boolean>;
enableResourceBrowser: boolean;
}

export const useEsqlCallbacks = ({
Expand All @@ -90,7 +91,6 @@ export const useEsqlCallbacks = ({
esqlService,
histogramBarTarget,
activeSolutionId,
canCreateLookupIndex,
minimalQueryRef,
abortControllerRef,
dataSourcesCache,
Expand All @@ -101,7 +101,7 @@ export const useEsqlCallbacks = ({
memoizedHistoryStarredItems,
favoritesClient,
getJoinIndicesCallback,
isResourceBrowserEnabled,
enableResourceBrowser,
}: UseEsqlCallbacksParams): ESQLCallbacks => {
const getSources = useCallback(async () => {
clearCacheWhenOld(dataSourcesCache, minimalQueryRef.current);
Expand Down Expand Up @@ -212,6 +212,9 @@ export const useEsqlCallbacks = ({

const isServerless = Boolean(esqlService?.isServerless);

const canCreateLookupIndex = useCanCreateLookupIndex();
const canSuggestResourceBrowser = useCanSuggestResourceBrowser(enableResourceBrowser);

const getKqlSuggestions = useCallback(
async (kqlQuery: string, cursorPositionInKql: number) => {
const hasQuerySuggestions = kql?.autocomplete?.hasQuerySuggestions('kuery');
Expand Down Expand Up @@ -264,7 +267,7 @@ export const useEsqlCallbacks = ({
canCreateLookupIndex,
isServerless,
getKqlSuggestions,
isResourceBrowserEnabled,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed it to canSuggestResourceBrowser to follow the same pattern as canCreateLookupIndex

canSuggestResourceBrowser,
}),
[
getSources,
Expand All @@ -285,7 +288,7 @@ export const useEsqlCallbacks = ({
canCreateLookupIndex,
isServerless,
getKqlSuggestions,
isResourceBrowserEnabled,
canSuggestResourceBrowser,
]
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export async function getIndicesBrowserSuggestion({
context?: ICommandContext;
innerText?: string;
}): Promise<ISuggestionItem | undefined> {
const isResourceBrowserEnabled = (await callbacks?.isResourceBrowserEnabled?.()) ?? false;
if (!isResourceBrowserEnabled || context?.isCursorInSubquery) {
const canSuggestResourceBrowser = (await callbacks?.canSuggestResourceBrowser?.()) ?? false;
if (!canSuggestResourceBrowser || context?.isCursorInSubquery) {
return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('FROM Autocomplete', () => {
test('suggests Browse indices in empty source slots when enabled', async () => {
mockCallbacks = {
...mockCallbacks,
isResourceBrowserEnabled: jest.fn().mockResolvedValue(true),
canSuggestResourceBrowser: jest.fn().mockResolvedValue(true),
};

const suggest = async (query: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('TS Autocomplete', () => {
};

test('suggests Browse indices in empty source slots when enabled', async () => {
mockCallbacks.isResourceBrowserEnabled = jest.fn().mockResolvedValue(true);
mockCallbacks.canSuggestResourceBrowser = jest.fn().mockResolvedValue(true);

const suggestions = await suggest('TS ');
const labels = suggestions.map((s) => s.label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export interface ICommandCallbacks {
canCreateLookupIndex?: (indexName: string) => Promise<boolean>;
isServerless?: boolean;
getKqlSuggestions?: ESQLCallbacks['getKqlSuggestions'];
isResourceBrowserEnabled?: () => Promise<boolean>;
canSuggestResourceBrowser?: () => Promise<boolean>;
}

export interface ICommandContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ async function getSuggestionsWithinCommandExpression(
);

const isInsideSubquery = astContext.isCursorInSubquery; // We only show resource browser suggestions in the main query
const isResourceBrowserEnabled = (await callbacks?.isResourceBrowserEnabled?.()) ?? false;
const canSuggestResourceBrowser = (await callbacks?.canSuggestResourceBrowser?.()) ?? false;

const context = {
...references,
...additionalCommandContext,
activeProduct: callbacks?.getActiveProduct?.(),
isCursorInSubquery: astContext.isCursorInSubquery,
isFieldsBrowserEnabled: isResourceBrowserEnabled && !isInsideSubquery,
isFieldsBrowserEnabled: canSuggestResourceBrowser && !isInsideSubquery,
unmappedFieldsStrategy,
};

Expand All @@ -291,7 +291,7 @@ async function getSuggestionsWithinCommandExpression(
hasMinimumLicenseRequired,
getKqlSuggestions: callbacks?.getKqlSuggestions,
canCreateLookupIndex: callbacks?.canCreateLookupIndex,
isResourceBrowserEnabled: callbacks?.isResourceBrowserEnabled,
canSuggestResourceBrowser: callbacks?.canSuggestResourceBrowser,
isServerless: callbacks?.isServerless,
},
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export interface ESQLCallbacks {
canCreateLookupIndex?: (indexName: string) => Promise<boolean>;
isServerless?: boolean;
/** Enables the "Browse indices" suggestion and command integration. */
isResourceBrowserEnabled?: () => Promise<boolean>;
canSuggestResourceBrowser?: () => Promise<boolean>;
getKqlSuggestions?: (
kqlQuery: string,
cursorPositionInKql: number
Expand Down
Loading