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

[sharedUX] Move to Package-based Architecture #127546

Merged
merged 16 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"@kbn/securitysolution-utils": "link:bazel-bin/packages/kbn-securitysolution-utils",
"@kbn/server-http-tools": "link:bazel-bin/packages/kbn-server-http-tools",
"@kbn/server-route-repository": "link:bazel-bin/packages/kbn-server-route-repository",
"@kbn/shared-ux-services": "link:bazel-bin/packages/kbn-shared-ux-services",
"@kbn/std": "link:bazel-bin/packages/kbn-std",
"@kbn/timelion-grammar": "link:bazel-bin/packages/kbn-timelion-grammar",
"@kbn/tinymath": "link:bazel-bin/packages/kbn-tinymath",
Expand Down Expand Up @@ -196,6 +197,7 @@
"@turf/helpers": "6.0.1",
"@turf/length": "^6.0.2",
"@types/jsonwebtoken": "^8.5.6",
"@types/kbn__shared-ux-services": "link:bazel-bin/packages/kbn-shared-ux-services/npm_module_types",
"@types/moment-duration-format": "^2.2.3",
"JSONStream": "1.3.5",
"abort-controller": "^3.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ filegroup(
"//packages/kbn-securitysolution-utils:build",
"//packages/kbn-server-http-tools:build",
"//packages/kbn-server-route-repository:build",
"//packages/kbn-shared-ux-services:build",
"//packages/kbn-spec-to-console:build",
"//packages/kbn-std:build",
"//packages/kbn-storybook:build",
Expand Down Expand Up @@ -138,6 +139,7 @@ filegroup(
"//packages/kbn-securitysolution-utils:build_types",
"//packages/kbn-server-http-tools:build_types",
"//packages/kbn-server-route-repository:build_types",
"//packages/kbn-shared-ux-services:build_types",
"//packages/kbn-std:build_types",
"//packages/kbn-storybook:build_types",
"//packages/kbn-telemetry-tools:build_types",
Expand Down
124 changes: 124 additions & 0 deletions packages/kbn-shared-ux-services/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")

PKG_DIRNAME = "kbn-shared-ux-services"
PKG_REQUIRE_NAME = "@kbn/shared-ux-services"

SOURCE_FILES = glob(
[
"src/**/*.ts",
"src/**/*.tsx",
],
exclude = [
"**/*.test.*",
],
)

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = SRCS,
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
]

# In this array place runtime dependencies, including other packages and NPM packages
# which must be available for this code to run.
#
# To reference other packages use:
# "//repo/relative/path/to/package"
# eg. "//packages/kbn-utils"
#
# To reference a NPM package use:
# "@npm//name-of-package"
# eg. "@npm//lodash"
RUNTIME_DEPS = [
"@npm//react",
]

# In this array place dependencies necessary to build the types, which will include the
# :npm_module_types target of other packages and packages from NPM, including @types/*
# packages.
#
# To reference the types for another package use:
# "//repo/relative/path/to/package:npm_module_types"
# eg. "//packages/kbn-utils:npm_module_types"
#
# References to NPM packages work the same as RUNTIME_DEPS
TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"@npm//@types/react",
]

jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)

jsts_transpiler(
name = "target_web",
srcs = SRCS,
build_pkg_name = package_name(),
web = True,
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)

ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
emit_declaration_only = True,
out_dir = "target_types",
root_dir = "src",
tsconfig = ":tsconfig",
)

js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node", ":target_web"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)

filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)

pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)

filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)
10 changes: 10 additions & 0 deletions packages/kbn-shared-ux-services/README.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
id: kibSharedUXServices
slug: /kibana-dev-docs/shared-ux/packages/kbn-shared-ux-services
title: Shared UX Services
summary:
date: 2022-03-11
tags: ['kibana', 'dev', 'sharedUX']
---

> TODO
13 changes: 13 additions & 0 deletions packages/kbn-shared-ux-services/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-shared-ux-services'],
};
8 changes: 8 additions & 0 deletions packages/kbn-shared-ux-services/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@kbn/shared-ux-services",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"browser": "./target_web/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
61 changes: 61 additions & 0 deletions packages/kbn-shared-ux-services/src/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import React, { FC, createContext, useContext } from 'react';

import type { SharedUxServices } from './types';

// The React Context used to provide the services to the SharedUX components.
const SharedUxServicesContext = createContext<SharedUxServices | null>(null);

/**
* The `React.Context` Provider component for the `SharedUxServices` context. Any
* plugin or environment that consumes SharedUX components needs to wrap their React
* tree with this provider.
*
* Within a plugin, you can use use the Shared UX plugin and retrieve a fully-configured
* context from the `start` contract.
*/
export const SharedUxServicesProvider: FC<SharedUxServices> = ({ children, ...services }) => (
<SharedUxServicesContext.Provider value={services}>{children}</SharedUxServicesContext.Provider>
);

/**
* React hook for accessing pre-wired `SharedUxServices`.
*/
export function useSharedUxServices() {
const context = useContext(SharedUxServicesContext);

if (!context) {
throw new Error(
'SharedUxServicesContext missing. Ensure your component or React root is wrapped with SharedUxServicesProvider.'
);
}

return context;
}

/**
* React hook for accessing the pre-wired `SharedUxPlatformService`.
*/
export const usePlatformService = () => useSharedUxServices().platform;

/**
* React hook for accessing the pre-wired `SharedUxPermissionsService`.
*/
export const usePermissions = () => useSharedUxServices().permissions;

/**
* React hook for accessing the pre-wired `SharedUxEditorsService`.
*/
export const useEditors = () => useSharedUxServices().editors;

/**
* React hook for accessing the pre-wired `SharedUxDocLinksService`.
*/
export const useDocLinks = () => useSharedUxServices().docLinks;
31 changes: 31 additions & 0 deletions packages/kbn-shared-ux-services/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export type { ServiceFactory, SharedUxServices, SharedUxServicesContext } from './types';
export type {
SharedUxDocLinksService,
SharedUxEditorsService,
SharedUxPlatformService,
SharedUxUserPermissionsService,
} from './services';

export {
SharedUxServicesProvider,
useDocLinks,
useEditors,
usePermissions,
usePlatformService,
useSharedUxServices,
} from './context';

export {
mockServiceFactories,
mockServicesFactory,
stubServiceFactories,
stubServicesFactory,
} from './services';
14 changes: 14 additions & 0 deletions packages/kbn-shared-ux-services/src/services/doc_links.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

/**
* A service providing links to documentation about various features in Kibana.
*/
export interface SharedUxDocLinksService {
dataViewsDocLink: string;
}
32 changes: 32 additions & 0 deletions packages/kbn-shared-ux-services/src/services/editors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

/**
* TODO: `DataView` is a class exported by `src/plugins/data_views/public`. Since this service
* is contained in this package-- and packages can only depend on other packages and never on
* plugins-- we have to set this to `unknown`. If and when `DataView` is exported from a
* stateless package, we can remove this.
*/
type DataView = unknown;
Copy link
Contributor

Choose a reason for hiding this comment

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

I accidentally left a comment in the PoC PR, so I'll just leave it here 😅
https://github.com/elastic/kibana/pull/127419/files#r826252775

Copy link
Contributor

Choose a reason for hiding this comment

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

This should really be prioritized imho.


/**
* A subset of the `DataViewEditorOptions` interface relevant to our service and components.
*
* @see: src/plugins/data_view_editor/public/types.ts
*/
interface DataViewEditorOptions {
onSave: (dataView: DataView) => void;
}

/**
* A service providing methods to invoke and interact with various editors provided
* in Kibana.
*/
export interface SharedUxEditorsService {
openDataViewEditor: (options: DataViewEditorOptions) => () => void;
}
15 changes: 15 additions & 0 deletions packages/kbn-shared-ux-services/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

export type { SharedUxDocLinksService } from './doc_links';
export type { SharedUxEditorsService } from './editors';
export type { SharedUxUserPermissionsService } from './permissions';
export type { SharedUxPlatformService } from './platform';

export { mockServicesFactory, mockServiceFactories } from './mock';
export { stubServicesFactory, stubServiceFactories } from './stub';
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
* Side Public License, v 1.
*/

import { PluginServiceFactory } from '../types';
import { SharedUXDocLinksService } from '../doc_links';
import type { ServiceFactory } from '../../types';
import type { SharedUxDocLinksService } from '../doc_links';

export type MockDockLinksServiceFactory = PluginServiceFactory<SharedUXDocLinksService>;
/**
* A factory function for creating a Jest implementation of `SharedUxDocLinksService`.
*/
export type MockDockLinksServiceFactory = ServiceFactory<SharedUxDocLinksService>;

/**
* A factory function for creating a Jest-based implementation of `SharedUXDocLinksService`.
* A factory function for creating a Jest-based implementation of `SharedUxDocLinksService`.
*/
export const docLinksServiceFactory: MockDockLinksServiceFactory = () => ({
dataViewsDocsLink: 'dummy link',
dataViewsDocLink: 'dummy link',
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
* Side Public License, v 1.
*/

import { PluginServiceFactory } from '../types';
import { SharedUXEditorsService } from '../editors';
import type { ServiceFactory } from '../../types';
import type { SharedUxEditorsService } from '../editors';

export type MockEditorsServiceFactory = PluginServiceFactory<SharedUXEditorsService>;
/**
* A factory function for creating a Jest-based implementation of `SharedUxEditorsService`.
*/
export type MockEditorsServiceFactory = ServiceFactory<SharedUxEditorsService>;

/**
* A factory function for creating a Jest-based implementation of `SharedUXEditorsService`.
* A factory function for creating a Jest-based implementation of `SharedUxEditorsService`.
*/
export const editorsServiceFactory: MockEditorsServiceFactory = () => ({
openDataViewEditor: jest.fn(),
Expand Down
Loading