Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UnifiedDocViewer] Remove usage of KibanaContextProvider #167202

Merged
merged 19 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c23e0f3
[UnifiedDocViewer] Remove usage of KibanaContextProvider
lukasolson Sep 25, 2023
30b05e4
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Sep 25, 2023
2f72cb4
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Oct 11, 2023
018cadd
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Oct 26, 2023
a5f450d
Fix jest test mocks
lukasolson Oct 26, 2023
673054f
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 13, 2023
a457a0a
Merge branch 'unified-doc-viewer/remove-context' of github.com:lukaso…
lukasolson Nov 13, 2023
67021ee
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 15, 2023
7e49228
Fix duplicate identifier
lukasolson Nov 15, 2023
8a1b264
Merge branch 'main' into unified-doc-viewer/remove-context
kibanamachine Nov 20, 2023
01ec512
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 21, 2023
71115a2
Move types
lukasolson Nov 21, 2023
e6dc248
Merge branch 'unified-doc-viewer/remove-context' of github.com:lukaso…
lukasolson Nov 21, 2023
ededc1f
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 28, 2023
59e5e3b
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 28, 2023
d266e45
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 30, 2023
7d7db58
Merge branch 'unified-doc-viewer/remove-context' of github.com:lukaso…
lukasolson Nov 30, 2023
f679f4b
Fix import
lukasolson Nov 30, 2023
bf6f295
Merge branch 'main' into unified-doc-viewer/remove-context
lukasolson Nov 30, 2023
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 @@ -18,6 +18,7 @@ import { dataViewMock } from '@kbn/discover-utils/src/__mocks__';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { setUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/plugin';
import { mockUnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/__mocks__';
import { UnifiedDocViewerServices } from '@kbn/unified-doc-viewer-plugin/public/hooks';

const mockSearchApi = jest.fn();

Expand Down Expand Up @@ -68,7 +69,14 @@ async function mountDoc(update = false) {
locator: { getUrl: jest.fn(() => Promise.resolve('mock-url')) },
chrome: { setBreadcrumbs: jest.fn() },
};
setUnifiedDocViewerServices(mockUnifiedDocViewerServices);
setUnifiedDocViewerServices({
...mockUnifiedDocViewerServices,
data: {
search: {
search: mockSearchApi,
},
},
} as unknown as UnifiedDocViewerServices);
await act(async () => {
comp = mountWithIntl(
<KibanaContextProvider services={services}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@
*/

import React from 'react';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import type { DocViewRenderProps } from '@kbn/unified-doc-viewer/types';
import { DocViewer } from '@kbn/unified-doc-viewer';
import { getUnifiedDocViewerServices } from '../../plugin';

export function UnifiedDocViewer(props: DocViewRenderProps) {
const services = getUnifiedDocViewerServices();
return (
<KibanaContextProvider services={services}>
<DocViewer docViews={services.unifiedDocViewer.getDocViews(props.hit)} {...props} />
</KibanaContextProvider>
);
const { unifiedDocViewer } = getUnifiedDocViewerServices();
return <DocViewer docViews={unifiedDocViewer.getDocViews(props.hit)} {...props} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,42 @@
*/

import React from 'react';
import type { DataView } from '@kbn/data-views-plugin/public';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { DocViewerSource } from './source';
import * as hooks from '../../hooks/use_es_doc_search';
import * as useUiSettingHook from '@kbn/kibana-react-plugin/public/ui_settings/use_ui_setting';
import { EuiButton, EuiEmptyPrompt, EuiLoadingSpinner } from '@elastic/eui';
import { JsonCodeEditorCommon } from '../json_code_editor';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { buildDataTableRecord } from '@kbn/discover-utils';
import { of } from 'rxjs';
import { setUnifiedDocViewerServices } from '../../plugin';
import { UnifiedDocViewerServices } from '../../hooks';

const mockDataView = {
getComputedFields: () => [],
} as never;
const getMock = jest.fn(() => Promise.resolve(mockDataView));
const mockDataViewService = {
get: getMock,
} as unknown as DataView;
const services = {
setUnifiedDocViewerServices({
uiSettings: {
get: (key: string) => {
if (key === 'discover:useNewFieldsApi') {
return true;
}
},
},
data: {
dataViewService: mockDataViewService,
},
theme: {
theme$: of({ darkMode: false }),
},
};
} as UnifiedDocViewerServices);

describe('Source Viewer component', () => {
test('renders loading state', () => {
jest.spyOn(hooks, 'useEsDocSearch').mockImplementation(() => [0, null, () => {}]);

const comp = mountWithIntl(
<KibanaContextProvider services={services}>
<DocViewerSource
id={'1'}
index={'index1'}
dataView={mockDataView}
width={123}
hasLineNumbers={true}
onRefresh={() => {}}
/>
</KibanaContextProvider>
<DocViewerSource
id={'1'}
index={'index1'}
dataView={mockDataView}
width={123}
hasLineNumbers={true}
onRefresh={() => {}}
/>
);
const loadingIndicator = comp.find(EuiLoadingSpinner);
expect(loadingIndicator).not.toBe(null);
Expand All @@ -65,16 +52,14 @@ describe('Source Viewer component', () => {
jest.spyOn(hooks, 'useEsDocSearch').mockImplementation(() => [3, null, () => {}]);

const comp = mountWithIntl(
<KibanaContextProvider services={services}>
<DocViewerSource
id={'1'}
index={'index1'}
dataView={mockDataView}
width={123}
hasLineNumbers={true}
onRefresh={() => {}}
/>
</KibanaContextProvider>
<DocViewerSource
id={'1'}
index={'index1'}
dataView={mockDataView}
width={123}
hasLineNumbers={true}
onRefresh={() => {}}
/>
);
const errorPrompt = comp.find(EuiEmptyPrompt);
expect(errorPrompt.length).toBe(1);
Expand Down Expand Up @@ -105,16 +90,14 @@ describe('Source Viewer component', () => {
return false;
});
const comp = mountWithIntl(
<KibanaContextProvider services={services}>
<DocViewerSource
id={'1'}
index={'index1'}
dataView={mockDataView}
width={123}
hasLineNumbers={true}
onRefresh={() => {}}
/>
</KibanaContextProvider>
<DocViewerSource
id={'1'}
index={'index1'}
dataView={mockDataView}
width={123}
hasLineNumbers={true}
onRefresh={() => {}}
/>
);
const jsonCodeEditor = comp.find(JsonCodeEditorCommon);
expect(jsonCodeEditor).not.toBe(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import type { DataView } from '@kbn/data-views-plugin/public';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { ElasticRequestState } from '@kbn/unified-doc-viewer';
import { DOC_TABLE_LEGACY, SEARCH_FIELDS_FROM_SOURCE } from '@kbn/discover-utils';
import { useEsDocSearch, useUnifiedDocViewerServices } from '../../hooks';
import { getUnifiedDocViewerServices } from '../../plugin';
import { useEsDocSearch } from '../../hooks';
import { getHeight } from './get_height';
import { JSONCodeEditorCommonMemoized } from '../json_code_editor';

Expand Down Expand Up @@ -49,7 +50,7 @@ export const DocViewerSource = ({
const [editor, setEditor] = useState<monaco.editor.IStandaloneCodeEditor>();
const [editorHeight, setEditorHeight] = useState<number>();
const [jsonValue, setJsonValue] = useState<string>('');
const { uiSettings } = useUnifiedDocViewerServices();
const { uiSettings } = getUnifiedDocViewerServices();
const useNewFieldsApi = !uiSettings.get(SEARCH_FIELDS_FROM_SOURCE);
const useDocExplorer = !uiSettings.get(DOC_TABLE_LEGACY);
const [requestState, hit] = useEsDocSearch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { findTestSubject } from '@elastic/eui/lib/test';
import { DocViewerLegacyTable } from './table';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { DocViewRenderProps } from '@kbn/unified-doc-viewer/types';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { buildDataTableRecord } from '@kbn/discover-utils';
import type { UnifiedDocViewerServices } from '../../../hooks';
import { setUnifiedDocViewerServices } from '../../../plugin';

const services = {
uiSettings: {
Expand Down Expand Up @@ -77,11 +77,8 @@ const mountComponent = (
props: DocViewRenderProps,
overrides?: Partial<UnifiedDocViewerServices>
) => {
return mountWithIntl(
<KibanaContextProvider services={{ ...services, ...overrides }}>
<DocViewerLegacyTable {...props} />{' '}
</KibanaContextProvider>
);
setUnifiedDocViewerServices({ ...services, ...overrides } as UnifiedDocViewerServices);
return mountWithIntl(<DocViewerLegacyTable {...props} />);
};

describe('DocViewTable at Discover', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
isNestedFieldParent,
} from '@kbn/discover-utils';
import type { DocViewRenderProps, FieldRecordLegacy } from '@kbn/unified-doc-viewer/types';
import { useUnifiedDocViewerServices } from '../../../hooks';
import { getUnifiedDocViewerServices } from '../../../plugin';
import { ACTIONS_COLUMN, MAIN_COLUMNS } from './table_columns';

export const DocViewerLegacyTable = ({
Expand All @@ -30,7 +30,7 @@ export const DocViewerLegacyTable = ({
onAddColumn,
onRemoveColumn,
}: DocViewRenderProps) => {
const { fieldFormats, uiSettings } = useUnifiedDocViewerServices();
const { fieldFormats, uiSettings } = getUnifiedDocViewerServices();
const showMultiFields = useMemo(() => uiSettings.get(SHOW_MULTIFIELDS), [uiSettings]);

const mapping = useCallback((name: string) => dataView.fields.getByName(name), [dataView.fields]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
import { fieldNameWildcardMatcher, getFieldSearchMatchingHighlight } from '@kbn/field-utils';
import type { DocViewRenderProps, FieldRecordLegacy } from '@kbn/unified-doc-viewer/types';
import { FieldName } from '@kbn/unified-doc-viewer';
import { useUnifiedDocViewerServices } from '../../hooks';
import { getUnifiedDocViewerServices } from '../../plugin';
import { TableFieldValue } from './table_cell_value';
import { TableActions } from './table_cell_actions';

Expand Down Expand Up @@ -116,7 +116,7 @@ export const DocViewerTable = ({
}: DocViewRenderProps) => {
const showActionsInsideTableCell = useIsWithinBreakpoints(['xl'], true);

const { fieldFormats, storage, uiSettings } = useUnifiedDocViewerServices();
const { fieldFormats, storage, uiSettings } = getUnifiedDocViewerServices();
const showMultiFields = uiSettings.get(SHOW_MULTIFIELDS);
const currentDataViewId = dataView.id!;

Expand Down
davismcphee marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
import type { IUiSettingsClient } from '@kbn/core-ui-settings-browser';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { UnifiedDocViewerStart } from '../plugin';

export interface UnifiedDocViewerServices {
Expand All @@ -22,9 +21,3 @@ export interface UnifiedDocViewerServices {
uiSettings: IUiSettingsClient;
unifiedDocViewer: UnifiedDocViewerStart;
}

export function useUnifiedDocViewerServices(): UnifiedDocViewerServices {
const { services } = useKibana<UnifiedDocViewerServices>();
const { analytics, data, fieldFormats, storage, uiSettings, unifiedDocViewer } = services;
return { analytics, data, fieldFormats, storage, uiSettings, unifiedDocViewer };
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
SEARCH_FIELDS_FROM_SOURCE as mockSearchFieldsFromSource,
buildDataTableRecord,
} from '@kbn/discover-utils';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import React from 'react';
import { setUnifiedDocViewerServices } from '../plugin';
import { UnifiedDocViewerServices } from './use_doc_viewer_services';

const index = 'test-index';
const mockSearchResult = new Subject();
const services = {
setUnifiedDocViewerServices({
data: {
search: {
search: jest.fn(() => {
Expand All @@ -35,7 +35,7 @@ const services = {
}
},
},
};
} as unknown as UnifiedDocViewerServices);

describe('Test of <Doc /> helper / hook', () => {
test('buildSearchBody given useNewFieldsApi is false', () => {
Expand Down Expand Up @@ -230,9 +230,6 @@ describe('Test of <Doc /> helper / hook', () => {

const hook = renderHook((p: EsDocSearchProps) => useEsDocSearch(p), {
initialProps: props,
wrapper: ({ children }) => (
<KibanaContextProvider services={services}>{children}</KibanaContextProvider>
),
});

expect(hook.result.current.slice(0, 2)).toEqual([ElasticRequestState.Loading, null]);
Expand All @@ -254,9 +251,6 @@ describe('Test of <Doc /> helper / hook', () => {

const hook = renderHook((p: EsDocSearchProps) => useEsDocSearch(p), {
initialProps: props,
wrapper: ({ children }) => (
<KibanaContextProvider services={services}>{children}</KibanaContextProvider>
),
});

await act(async () => {
Expand Down Expand Up @@ -308,9 +302,6 @@ describe('Test of <Doc /> helper / hook', () => {

const hook = renderHook((p: EsDocSearchProps) => useEsDocSearch(p), {
initialProps: props,
wrapper: ({ children }) => (
<KibanaContextProvider services={services}>{children}</KibanaContextProvider>
),
});

expect(hook.result.current.slice(0, 2)).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
import type { DataTableRecord } from '@kbn/discover-utils/types';
import { SEARCH_FIELDS_FROM_SOURCE, buildDataTableRecord } from '@kbn/discover-utils';
import { ElasticRequestState } from '@kbn/unified-doc-viewer';
import { useUnifiedDocViewerServices } from './use_doc_viewer_services';
import { getUnifiedDocViewerServices } from '../plugin';

type RequestBody = Pick<estypes.SearchRequest, 'body'>;

Expand Down Expand Up @@ -53,7 +53,7 @@ export function useEsDocSearch({
}: EsDocSearchProps): [ElasticRequestState, DataTableRecord | null, () => void] {
const [status, setStatus] = useState(ElasticRequestState.Loading);
const [hit, setHit] = useState<DataTableRecord | null>(null);
const { data, uiSettings, analytics } = useUnifiedDocViewerServices();
const { data, uiSettings, analytics } = getUnifiedDocViewerServices();
const useNewFieldsApi = useMemo(() => !uiSettings.get(SEARCH_FIELDS_FROM_SOURCE), [uiSettings]);

const requestData = useCallback(async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/unified_doc_viewer/public/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export const UnifiedDocViewer = withSuspense<DocViewRenderProps>(
</EuiDelayRender>
);

export { useEsDocSearch, useUnifiedDocViewerServices } from './hooks';
export { useEsDocSearch } from './hooks';

export const plugin = () => new UnifiedDocViewerPublicPlugin();
5 changes: 2 additions & 3 deletions src/plugins/unified_doc_viewer/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createGetterSetter, Storage } from '@kbn/kibana-utils-plugin/public';
import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { CoreStart } from '@kbn/core/public';
import { type UnifiedDocViewerServices, useUnifiedDocViewerServices } from './hooks';
import { type UnifiedDocViewerServices } from './hooks';

export const [getUnifiedDocViewerServices, setUnifiedDocViewerServices] =
createGetterSetter<UnifiedDocViewerServices>('UnifiedDocViewerServices');
Expand Down Expand Up @@ -50,8 +50,7 @@ export class UnifiedDocViewerPublicPlugin
}),
order: 10,
component: (props) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { uiSettings } = useUnifiedDocViewerServices();
const { uiSettings } = getUnifiedDocViewerServices();
const DocView = uiSettings.get(DOC_TABLE_LEGACY) ? DocViewerLegacyTable : DocViewerTable;

return (
Expand Down
Loading