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

[KibanaReact] Use settings service in useUiSetting hook #154710

Merged
merged 54 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
913d527
[KibanaReact] Use settings service in useUiSetting hook
majagrubic Apr 11, 2023
4638c37
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Apr 11, 2023
79622b1
Fix failing jest tests
majagrubic Apr 12, 2023
9ea8d52
Merge branch 'fix-149347-2' of github.com:majagrubic/kibana into fix-…
majagrubic Apr 12, 2023
34b0032
Merge branch 'main' into fix-149347-2
kibanamachine Apr 12, 2023
003bce1
Add settings dependency to lens, expressions_explorer and transform p…
majagrubic Apr 13, 2023
bc5deb9
Merge branch 'fix-149347-2' of github.com:majagrubic/kibana into fix-…
majagrubic Apr 13, 2023
f49d8ac
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 13, 2023
68a31a8
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Apr 13, 2023
6df5e78
Add settings dependency to url_drilldown
majagrubic Apr 13, 2023
43330ac
Add settings dependency to ingest_pipelines plugin
majagrubic Apr 13, 2023
78ca5cd
Merge main
majagrubic Apr 13, 2023
8ca6ab8
Merge branch 'main' into fix-149347-2
kibanamachine Apr 14, 2023
10d68b8
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 14, 2023
61301d8
Linting
majagrubic Apr 14, 2023
3923e03
Merge branch 'fix-149347-2' of github.com:majagrubic/kibana into fix-…
majagrubic Apr 14, 2023
6fe6b7e
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 17, 2023
60c2dd9
Merge branch 'main' into fix-149347-2
majagrubic Apr 17, 2023
6cf83d7
Add settings as dependency to data_view_management
majagrubic Apr 17, 2023
3c8850f
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Apr 18, 2023
1082a2c
Merge branch 'main' into fix-149347-2
Apr 18, 2023
1d81ba3
Merge branch 'main' into fix-149347-2
kibanamachine Apr 18, 2023
06d8244
Merge branch 'main' into fix-149347-2
Apr 19, 2023
bbc7446
Merge branch 'main' into fix-149347-2
kibanamachine Apr 21, 2023
ddb7474
Remove unnecessary dependency
majagrubic Apr 21, 2023
a4b201e
Merge branch 'fix-149347-2' of github.com:majagrubic/kibana into fix-…
majagrubic Apr 21, 2023
c079a65
Merge main
majagrubic Apr 30, 2023
6fb9a25
Remove unnecessary dependency
majagrubic Apr 30, 2023
ee3bffe
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine May 1, 2023
284f404
Merge branch 'main' into fix-149347-2
majagrubic May 1, 2023
7b1a941
Merge branch 'fix-149347-2' of github.com:majagrubic/kibana into fix-…
majagrubic May 1, 2023
09fedf3
Fix the cofusion between the two plugins
majagrubic May 1, 2023
65a0b1a
Remove unncessary comma
majagrubic May 1, 2023
a91cb61
Merge branch 'main' into fix-149347-2
kibanamachine May 2, 2023
169b998
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine May 2, 2023
99336a5
Add settings dependency to snapshot_restore plugin
majagrubic May 2, 2023
eb8d105
Add settings dependency to watcher plugin
majagrubic May 2, 2023
4561f53
Fix typescript
majagrubic May 2, 2023
3ba92bb
Merge branch 'main' into fix-149347-2
kibanamachine May 3, 2023
8ccde25
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine May 3, 2023
5837604
Add settings dependency to index_management plugin
majagrubic May 4, 2023
52f3741
Merge branch 'fix-149347-2' of github.com:majagrubic/kibana into fix-…
majagrubic May 8, 2023
40d4c06
Merge main
majagrubic May 8, 2023
0a0bf97
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine May 8, 2023
09787a5
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine May 8, 2023
ebf77a2
Remove accidentally added files
majagrubic May 8, 2023
fab19ed
Merge branch 'main' into fix-149347-2
majagrubic May 8, 2023
371e888
Merge branch 'main' into fix-149347-2
patrykkopycinski May 9, 2023
1886c3f
Merge branch 'main' into fix-149347-2
patrykkopycinski May 10, 2023
64ee080
Merge branch 'main' into fix-149347-2
majagrubic May 11, 2023
dc74b84
Add mock to fix failing unit test
majagrubic May 11, 2023
5e0e33c
Fix remaining failing unit tests
majagrubic May 11, 2023
e5509d8
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine May 11, 2023
3868340
Merge branch 'main' into fix-149347-2
patrykkopycinski May 11, 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
9 changes: 7 additions & 2 deletions examples/expressions_explorer/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ExpressionsStart } from '@kbn/expressions-plugin/public';
import { Start as InspectorStart } from '@kbn/inspector-plugin/public';
import { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import { createKibanaReactContext } from '@kbn/kibana-react-plugin/public';
import { SettingsStart } from '@kbn/core-ui-settings-browser';
import { RunExpressionsExample } from './run_expressions';
import { RenderExpressionsExample } from './render_expressions';
import { ActionsExpressionsExample } from './actions_and_expressions';
Expand All @@ -33,10 +34,14 @@ interface Props {
inspector: InspectorStart;
actions: UiActionsStart;
uiSettings: IUiSettingsClient;
settings: SettingsStart;
}

const ExpressionsExplorer = ({ expressions, inspector, actions, uiSettings }: Props) => {
const { Provider: KibanaReactContextProvider } = createKibanaReactContext({ uiSettings });
const ExpressionsExplorer = ({ expressions, inspector, actions, uiSettings, settings }: Props) => {
const { Provider: KibanaReactContextProvider } = createKibanaReactContext({
uiSettings,
settings,
});
return (
<KibanaReactContextProvider>
<EuiPage>
Expand Down
1 change: 1 addition & 0 deletions examples/expressions_explorer/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class ExpressionsExplorerPlugin implements Plugin<void, void, SetupDeps,
inspector: depsStart.inspector,
actions: depsStart.uiActions,
uiSettings: core.uiSettings,
settings: core.settings,
},
params
);
Expand Down
1 change: 1 addition & 0 deletions examples/expressions_explorer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
"@kbn/developer-examples-plugin",
"@kbn/i18n",
"@kbn/i18n-react",
"@kbn/core-ui-settings-browser",
]
}
3 changes: 2 additions & 1 deletion src/plugins/data_view_field_editor/public/open_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ export const getFieldEditorOpener =
apiService,
}: Dependencies) =>
(options: OpenFieldEditorOptions): CloseEditor => {
const { uiSettings, overlays, docLinks, notifications } = core;
const { uiSettings, overlays, docLinks, notifications, settings } = core;
const { Provider: KibanaReactContextProvider } = createKibanaReactContext({
uiSettings,
docLinks,
http: core.http,
settings,
});

let overlayRef: OverlayRef | null = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function mountManagementSection(
params: ManagementAppMountParams
) {
const [
{ application, chrome, uiSettings, notifications, overlays, http, docLinks, theme },
{ application, chrome, uiSettings, settings, notifications, overlays, http, docLinks, theme },
{
data,
dataViewFieldEditor,
Expand All @@ -63,6 +63,7 @@ export async function mountManagementSection(
application,
chrome,
uiSettings,
settings,
notifications,
overlays,
unifiedSearch,
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data_view_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const docLinks = {
const createIndexPatternManagmentContext = (): {
[key in keyof IndexPatternManagmentContext]: any;
} => {
const { application, chrome, uiSettings, notifications, overlays, theme } =
const { application, chrome, uiSettings, notifications, overlays, theme, settings } =
coreMock.createStart();
const { http } = coreMock.createSetup();
const data = dataPluginMock.createStartContract();
Expand All @@ -70,6 +70,7 @@ const createIndexPatternManagmentContext = (): {
application,
chrome,
uiSettings,
settings,
notifications,
overlays,
http,
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data_view_management/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import { SpacesPluginStart } from '@kbn/spaces-plugin/public';
import { SavedObjectsManagementPluginStart } from '@kbn/saved-objects-management-plugin/public';
import type { SettingsStart } from '@kbn/core-ui-settings-browser';
import { IndexPatternManagementStart } from '.';

export interface IndexPatternManagmentContext {
application: ApplicationStart;
chrome: ChromeStart;
uiSettings: IUiSettingsClient;
settings: SettingsStart;
notifications: NotificationsStart;
overlays: OverlayStart;
http: HttpSetup;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_view_management/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@kbn/utility-types-jest",
"@kbn/config-schema",
"@kbn/shared-ux-router",
"@kbn/core-ui-settings-browser",
],
"exclude": [
"target/**/*",
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/discover/public/build_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import type { SavedObjectsTaggingApi } from '@kbn/saved-objects-tagging-oss-plug
import type { SavedObjectsManagementPluginStart } from '@kbn/saved-objects-management-plugin/public';
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type { LensPublicStart } from '@kbn/lens-plugin/public';
import type { SettingsStart } from '@kbn/core-ui-settings-browser';
import { getHistory } from './kibana_services';
import { DiscoverStartPlugins } from './plugin';
import { DiscoverContextAppLocator } from './application/context/services/locator';
Expand Down Expand Up @@ -83,6 +84,7 @@ export interface DiscoverServices {
toastNotifications: ToastsStart;
notifications: NotificationsStart;
uiSettings: IUiSettingsClient;
settings: SettingsStart;
trackUiMetric?: (metricType: UiCounterMetricType, eventName: string | string[]) => void;
dataViewFieldEditor: IndexPatternFieldEditorStart;
dataViewEditor: DataViewEditorStart;
Expand Down Expand Up @@ -137,6 +139,7 @@ export const buildServices = memoize(function (
toastNotifications: core.notifications.toasts,
notifications: core.notifications,
uiSettings: core.uiSettings,
settings: core.settings,
storage,
trackUiMetric: usageCollection?.reportUiCounter.bind(usageCollection, 'discover'),
dataViewFieldEditor: plugins.dataViewFieldEditor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import { buildDataTableRecord } from '../../utils/build_data_record';
import { EsHitRecord } from '../../types';

const mockServices = {
settings: {
client: {
get: (key: string) => key === 'discover:maxDocFieldsDisplayed' && 200,
},
},
uiSettings: {
get: (key: string) => key === 'discover:maxDocFieldsDisplayed' && 200,
},
Expand Down
1 change: 1 addition & 0 deletions src/plugins/inspector/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class InspectorPublicPlugin implements Plugin<Setup, Start> {
http: core.http,
uiSettings: core.uiSettings,
share: startDeps.share,
settings: core.settings,
}}
/>,
{ theme$: core.theme.theme$ }
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/inspector/public/ui/inspector_panel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type { ApplicationStart, HttpSetup, IUiSettingsClient } from '@kbn/core/p
import { SharePluginStart } from '@kbn/share-plugin/public';
import { sharePluginMock } from '@kbn/share-plugin/public/mocks';
import { applicationServiceMock } from '@kbn/core/public/mocks';
import { settingsServiceMock } from '@kbn/core-ui-settings-browser-mocks';
import type { SettingsStart } from '@kbn/core-ui-settings-browser';

describe('InspectorPanel', () => {
let adapters: Adapters;
Expand All @@ -24,11 +26,13 @@ describe('InspectorPanel', () => {
http: {},
share: sharePluginMock.createStartContract(),
uiSettings: {},
settings: settingsServiceMock.createStartContract(),
} as unknown as {
application: ApplicationStart;
http: HttpSetup;
share: SharePluginStart;
uiSettings: IUiSettingsClient;
settings: SettingsStart;
};

beforeEach(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/inspector/public/ui/inspector_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { ApplicationStart, HttpStart, IUiSettingsClient } from '@kbn/core/public';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { SharePluginStart } from '@kbn/share-plugin/public';
import type { SettingsStart } from '@kbn/core-ui-settings-browser';
import { InspectorViewDescription } from '../types';
import { Adapters } from '../../common';
import { InspectorViewChooser } from './inspector_view_chooser';
Expand All @@ -46,6 +47,7 @@ interface InspectorPanelProps {
http: HttpStart;
uiSettings: IUiSettingsClient;
share: SharePluginStart;
settings: SettingsStart;
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/inspector/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"@kbn/test-jest-helpers",
"@kbn/i18n-react",
"@kbn/monaco",
"@kbn/core-ui-settings-browser-mocks",
"@kbn/core-ui-settings-browser",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ useObservableSpy.mockImplementation((observable, def) => def);

const mock = (): [KibanaServices, Subject<any>] => {
const core = coreMock.createStart();
const get = core.uiSettings.get;
const get$ = core.uiSettings.get$;
const get = core.settings.client.get;
const get$ = core.settings.client.get$;
const subject = new Subject();

get.mockImplementation(() => 'bar');
Expand Down Expand Up @@ -73,8 +73,8 @@ describe('useUiSetting', () => {

const strong = container!.querySelector('strong');
expect(strong!.textContent).toBe('bar');
expect(core.uiSettings!.get).toHaveBeenCalledTimes(1);
expect((core.uiSettings!.get as any).mock.calls[0][0]).toBe('foo');
expect(core.settings!.client.get).toHaveBeenCalledTimes(1);
expect((core.settings!.client.get as any).mock.calls[0][0]).toBe('foo');
});

test('calls uiSettings.get() method with correct key and default value', async () => {
Expand All @@ -88,9 +88,10 @@ describe('useUiSetting', () => {
container
);

expect(core.uiSettings!.get).toHaveBeenCalledTimes(1);
expect((core.uiSettings!.get as any).mock.calls[0][0]).toBe('foo');
expect((core.uiSettings!.get as any).mock.calls[0][1]).toBe('DEFAULT');
expect(core.uiSettings!.get).toHaveBeenCalledTimes(0);
expect(core.settings!.client.get).toHaveBeenCalledTimes(1);
expect((core.settings!.client.get as any).mock.calls[0][0]).toBe('foo');
expect((core.settings!.client.get as any).mock.calls[0][1]).toBe('DEFAULT');
});
});

Expand Down Expand Up @@ -183,8 +184,8 @@ describe('useUiSetting$', () => {

const strong = container!.querySelector('strong');
expect(strong!.textContent).toBe('bar');
expect(core.uiSettings!.get).toHaveBeenCalledTimes(1);
expect((core.uiSettings!.get as any).mock.calls[0][0]).toBe('foo');
expect(core.settings!.client.get).toHaveBeenCalledTimes(1);
expect((core.settings!.client.get as any).mock.calls[0][0]).toBe('foo');
});

test('calls Core with correct arguments', async () => {
Expand All @@ -198,7 +199,7 @@ describe('useUiSetting$', () => {
container
);

expect(core.uiSettings!.get).toHaveBeenCalledWith('non_existing', 'DEFAULT');
expect(core.settings!.client.get).toHaveBeenCalledWith('non_existing', 'DEFAULT');
});

test('subscribes to observable using useObservable', async () => {
Expand Down Expand Up @@ -235,7 +236,7 @@ describe('useUiSetting$', () => {
Simulate.click(container!.querySelector('button')!, {});
});

expect(core.uiSettings!.set).toHaveBeenCalledTimes(1);
expect(core.uiSettings!.set).toHaveBeenCalledWith('a', 'c');
expect(core.settings!.client.set).toHaveBeenCalledTimes(1);
expect(core.settings!.client.set).toHaveBeenCalledWith('a', 'c');
});
});
50 changes: 26 additions & 24 deletions src/plugins/kibana_react/public/ui_settings/use_ui_setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import { useKibana } from '../context';
* ```
*/
export const useUiSetting = <T>(key: string, defaultValue?: T): T => {
const { services } = useKibana();
const {
services: { settings },
} = useKibana();

if (typeof services.uiSettings !== 'object') {
if (!settings) {
throw new TypeError('uiSettings service not available in kibana-react context.');
}

return services.uiSettings.get(key, defaultValue);
return settings.client.get(key, defaultValue);
};

/**
Expand All @@ -39,13 +41,15 @@ export const useUiSetting = <T>(key: string, defaultValue?: T): T => {
* ```
*/
export const useGlobalUiSetting = <T>(key: string, defaultValue?: T): T => {
const { services } = useKibana();
const {
services: { settings },
} = useKibana();

if (typeof services.settings !== 'object') {
if (!settings) {
throw new TypeError('uiSettings service not available in kibana-react context.');
}

return services.settings.globalClient.get(key, defaultValue);
return settings.globalClient.get(key, defaultValue);
};

type Setter<T> = (newValue: T) => Promise<boolean>;
Expand All @@ -64,18 +68,20 @@ type Setter<T> = (newValue: T) => Promise<boolean>;
* ```
*/
export const useUiSetting$ = <T>(key: string, defaultValue?: T): [T, Setter<T>] => {
const { services } = useKibana();
const {
services: { settings },
} = useKibana();

if (typeof services.uiSettings !== 'object') {
if (!settings) {
throw new TypeError('uiSettings service not available in kibana-react context.');
}

const observable$ = useMemo(
() => services.uiSettings!.get$(key, defaultValue),
[key, defaultValue, services.uiSettings]
() => settings!.client.get$(key, defaultValue),
[key, defaultValue, settings]
);
const value = useObservable<T>(observable$, services.uiSettings!.get(key, defaultValue));
const set = useCallback((newValue: T) => services.uiSettings!.set(key, newValue), [key]);
const value = useObservable<T>(observable$, settings!.client.get(key, defaultValue));
const set = useCallback((newValue: T) => settings!.client.set(key, newValue), [key]);

return [value, set];
};
Expand All @@ -94,24 +100,20 @@ export const useUiSetting$ = <T>(key: string, defaultValue?: T): [T, Setter<T>]
* ```
*/
export const useGlobalUiSetting$ = <T>(key: string, defaultValue?: T): [T, Setter<T>] => {
const { services } = useKibana();
const {
services: { settings },
} = useKibana();

if (typeof services.settings !== 'object') {
if (!settings) {
throw new TypeError('uiSettings service not available in kibana-react context.');
}

const observable$ = useMemo(
() => services.settings!.globalClient.get$(key, defaultValue),
[key, defaultValue, services.settings!.globalClient]
);
const value = useObservable<T>(
observable$,
services.settings!.globalClient.get(key, defaultValue)
);
const set = useCallback(
(newValue: T) => services.settings!.globalClient.set(key, newValue),
[key]
() => settings!.globalClient.get$(key, defaultValue),
[key, defaultValue, settings!.globalClient]
);
const value = useObservable<T>(observable$, settings!.globalClient.get(key, defaultValue));
const set = useCallback((newValue: T) => settings!.globalClient.set(key, newValue), [key]);

return [value, set];
};
Loading