Skip to content

Commit

Permalink
wrapping subject into a context
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Sep 23, 2024
1 parent 31ee0df commit ddf8602
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
* 2.0.
*/

import { RedirectAppLinks } from '@kbn/shared-ux-link-redirect-app';
import React from 'react';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { type AppMountParameters, type CoreStart } from '@kbn/core/public';
import { RouteRenderer, RouterProvider } from '@kbn/typed-react-router-config';
import { HeaderMenuPortal } from '@kbn/observability-shared-plugin/public';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { InventoryContextProvider } from '../inventory_context_provider';
import { RedirectAppLinks } from '@kbn/shared-ux-link-redirect-app';
import { RouteRenderer, RouterProvider } from '@kbn/typed-react-router-config';
import React from 'react';
import { InventoryContextProvider } from '../../context/inventory_context_provider';
import { InventorySearchBarContextProvider } from '../../context/inventory_search_bar_context_provider';
import { inventoryRouter } from '../../routes/config';
import { HeaderActionMenuItems } from './header_action_menu';
import { InventoryStartDependencies } from '../../types';
import { InventoryServices } from '../../services/types';
import { InventoryStartDependencies } from '../../types';
import { HeaderActionMenuItems } from './header_action_menu';

export function AppRoot({
coreStart,
Expand All @@ -38,10 +39,12 @@ export function AppRoot({
return (
<InventoryContextProvider context={context}>
<RedirectAppLinks coreStart={coreStart}>
<RouterProvider history={history} router={inventoryRouter}>
<RouteRenderer />
<InventoryHeaderActionMenu appMountParameters={appMountParameters} />
</RouterProvider>
<InventorySearchBarContextProvider>
<RouterProvider history={history} router={inventoryRouter}>
<RouteRenderer />
<InventoryHeaderActionMenu appMountParameters={appMountParameters} />
</RouterProvider>
</InventorySearchBarContextProvider>
</RedirectAppLinks>
</InventoryContextProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import React from 'react';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import { useKibana } from '../../hooks/use_kibana';
import { SearchBar } from '../search_bar';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,19 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { i18n } from '@kbn/i18n';
import { SearchBarOwnProps } from '@kbn/unified-search-plugin/public/search_bar';
import deepEqual from 'fast-deep-equal';
import React, { useCallback, useEffect } from 'react';
import { Subject } from 'rxjs';
import { i18n } from '@kbn/i18n';
import { EntityType } from '../../../common/entities';
import { useInventorySearchBarContext } from '../../context/inventory_search_bar_context_provider';
import { useAdHocInventoryDataView } from '../../hooks/use_adhoc_inventory_data_view';
import { useInventoryParams } from '../../hooks/use_inventory_params';
import { useKibana } from '../../hooks/use_kibana';
import { EntityTypesControls } from './entity_types_controls';

export const searchBarContentSubject$ = new Subject<{
kuery?: string;
entityTypes?: EntityType[];
refresh: boolean;
}>();

export function SearchBar() {
const { searchBarContentSubject$ } = useInventorySearchBarContext();
const {
services: {
unifiedSearch,
Expand Down Expand Up @@ -58,7 +53,7 @@ export function SearchBar() {
(nextEntityTypes: EntityType[]) => {
searchBarContentSubject$.next({ kuery, entityTypes: nextEntityTypes, refresh: false });
},
[kuery]
[kuery, searchBarContentSubject$]
);

const handleQuerySubmit = useCallback<NonNullable<SearchBarOwnProps['onQuerySubmit']>>(
Expand All @@ -69,7 +64,7 @@ export function SearchBar() {
refresh: !isUpdate,
});
},
[entityTypes]
[entityTypes, searchBarContentSubject$]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import React from 'react';
import type { InventoryKibanaContext } from '../../hooks/use_kibana';

export function InventoryContextProvider({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { createContext, useContext, type ReactChild } from 'react';
import { Subject } from 'rxjs';
import { EntityType } from '../../../common/entities';

interface InventorySearchBarContextType {
searchBarContentSubject$: Subject<{
kuery?: string;
entityTypes?: EntityType[];
refresh: boolean;
}>;
}

const InventorySearchBarContext = createContext<InventorySearchBarContextType>({
searchBarContentSubject$: new Subject(),
});

export function InventorySearchBarContextProvider({ children }: { children: ReactChild }) {
return (
<InventorySearchBarContext.Provider value={{ searchBarContentSubject$: new Subject() }}>
{children}
</InventorySearchBarContext.Provider>
);
}

export function useInventorySearchBarContext() {
const context = useContext(InventorySearchBarContext);
if (!context) {
throw new Error('Context was not found');
}
return context;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { EuiDataGridSorting } from '@elastic/eui';
import React from 'react';
import useEffectOnce from 'react-use/lib/useEffectOnce';
import { EntitiesGrid } from '../../components/entities_grid';
import { searchBarContentSubject$ } from '../../components/search_bar';
import { useInventorySearchBarContext } from '../../context/inventory_search_bar_context_provider';
import { useInventoryAbortableAsync } from '../../hooks/use_inventory_abortable_async';
import { useInventoryParams } from '../../hooks/use_inventory_params';
import { useInventoryRouter } from '../../hooks/use_inventory_router';
import { useKibana } from '../../hooks/use_kibana';

export function InventoryPage() {
const { searchBarContentSubject$ } = useInventorySearchBarContext();
const {
services: { inventoryAPIClient },
} = useKibana();
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/observability_solution/inventory/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { from, map } from 'rxjs';
import {
AppMountParameters,
CoreSetup,
Expand All @@ -15,18 +13,20 @@ import {
Plugin,
PluginInitializerContext,
} from '@kbn/core/public';
import type { Logger } from '@kbn/logging';
import { INVENTORY_APP_ID } from '@kbn/deeplinks-observability/constants';
import { i18n } from '@kbn/i18n';
import type { Logger } from '@kbn/logging';
import { from, map } from 'rxjs';
import { createCallInventoryAPI } from './api';
import { TelemetryService } from './services/telemetry/telemetry_service';
import { InventoryServices } from './services/types';
import type {
ConfigSchema,
InventoryPublicSetup,
InventoryPublicStart,
InventorySetupDependencies,
InventoryStartDependencies,
} from './types';
import { InventoryServices } from './services/types';
import { createCallInventoryAPI } from './api';
import { TelemetryService } from './services/telemetry/telemetry_service';

export class InventoryPlugin
implements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { INVENTORY_APP_ID } from '@kbn/deeplinks-observability/constants';
import { jsonRt } from '@kbn/io-ts-utils';
import { createObservabilityEsClient } from '@kbn/observability-utils/es/client/create_observability_es_client';
import * as t from 'io-ts';
import { INVENTORY_APP_ID } from '@kbn/deeplinks-observability/constants';
import { entityTypeRt } from '../../../common/entities';
import { createInventoryServerRoute } from '../create_inventory_server_route';
import { getLatestEntities } from './get_latest_entities';
import { getEntityTypes } from './get_entity_types';
import { getLatestEntities } from './get_latest_entities';

export const getEntityTypesRoute = createInventoryServerRoute({
endpoint: 'GET /internal/inventory/entities/types',
Expand Down

0 comments on commit ddf8602

Please sign in to comment.