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

[Shared UX] Migrate router from kibana react to shared ux #138544

Merged
merged 44 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
407ac63
router migration
rshen91 Aug 10, 2022
4947212
update bazel
rshen91 Aug 10, 2022
c840952
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Aug 10, 2022
ba819b6
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 15, 2022
63c5fd3
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Aug 15, 2022
9fda52d
some doc updates
rshen91 Aug 15, 2022
e86adf1
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 16, 2022
ac912df
make impl folder
rshen91 Aug 18, 2022
bb741e0
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 18, 2022
cac3d71
missed files
rshen91 Aug 18, 2022
1ee73ef
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 21, 2022
e0d159a
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 22, 2022
be43990
remove merge conflict issues
rshen91 Aug 22, 2022
ab2daeb
update
rshen91 Aug 22, 2022
34a6ee3
[CI] Auto-commit changed files from 'node scripts/generate packages_b…
kibanamachine Aug 22, 2022
4612e08
fix
rshen91 Aug 22, 2022
ee65d0d
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Aug 22, 2022
37df4aa
move bazel files into impl directory
rshen91 Aug 22, 2022
094b980
Adjust BAZEL.build settings; fix types package
clintandrewhall Aug 22, 2022
bd2e6e4
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 23, 2022
24e8ced
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Aug 23, 2022
e9896f1
create mock package for router
rshen91 Aug 23, 2022
7bcea9d
storybook support mdx
rshen91 Aug 24, 2022
7d66fd6
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Aug 24, 2022
278f93c
Merge branch 'main' into router-migrate
rshen91 Aug 29, 2022
4255db7
fix mdx import issue
rshen91 Aug 29, 2022
00f763b
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Aug 29, 2022
7dfe755
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Aug 29, 2022
6f0a92b
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Aug 29, 2022
a03d3c0
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Sep 6, 2022
35c3a9d
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Sep 6, 2022
3a69a1c
update
rshen91 Sep 6, 2022
7a1bc8c
Merge branch 'main' into router-migrate
rshen91 Sep 6, 2022
abd6d38
Merge remote-tracking branch 'upstream/main' into router-migrate
rshen91 Sep 7, 2022
e301437
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Sep 7, 2022
a31f7b1
improve documentation
rshen91 Sep 7, 2022
7f1cc68
update tests
rshen91 Sep 7, 2022
671ad52
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Sep 7, 2022
dc0b795
fix location key from rewriting
rshen91 Sep 7, 2022
6910e41
Merge remote-tracking branch 'origin/router-migrate' into router-migrate
rshen91 Sep 7, 2022
0556d16
apply ternary
rshen91 Sep 8, 2022
2443bb8
Merge branch 'main' into router-migrate
rshen91 Sep 9, 2022
b6e859f
[CI] Auto-commit changed files from 'node scripts/generate packages_b…
kibanamachine Sep 9, 2022
822e7d6
Merge branch 'main' into router-migrate
rshen91 Sep 9, 2022
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
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@
"@kbn/shared-ux-prompt-no-data-views": "link:bazel-bin/packages/shared-ux/prompt/no_data_views/impl",
"@kbn/shared-ux-prompt-no-data-views-mocks": "link:bazel-bin/packages/shared-ux/prompt/no_data_views/mocks",
"@kbn/shared-ux-prompt-no-data-views-types": "link:bazel-bin/packages/shared-ux/prompt/no_data_views/types",
"@kbn/shared-ux-storybook-config": "link:bazel-bin/packages/shared-ux/storybook/config",
"@kbn/shared-ux-router-mocks": "link:bazel-bin/packages/shared-ux/router/mocks",
"@kbn/shared-ux-services": "link:bazel-bin/packages/kbn-shared-ux-services",
"@kbn/shared-ux-storybook": "link:bazel-bin/packages/kbn-shared-ux-storybook",
"@kbn/shared-ux-storybook-mock": "link:bazel-bin/packages/shared-ux/storybook/mock",
"@kbn/shared-ux-utility": "link:bazel-bin/packages/kbn-shared-ux-utility",
"@kbn/std": "link:bazel-bin/packages/kbn-std",
Expand Down Expand Up @@ -1077,7 +1079,9 @@
"@types/kbn__shared-ux-prompt-no-data-views": "link:bazel-bin/packages/shared-ux/prompt/no_data_views/impl/npm_module_types",
"@types/kbn__shared-ux-prompt-no-data-views-mocks": "link:bazel-bin/packages/shared-ux/prompt/no_data_views/mocks/npm_module_types",
"@types/kbn__shared-ux-prompt-no-data-views-types": "link:bazel-bin/packages/shared-ux/prompt/no_data_views/types/npm_module_types",
"@types/kbn__shared-ux-storybook-config": "link:bazel-bin/packages/shared-ux/storybook/config/npm_module_types",
"@types/kbn__shared-ux-router-mocks": "link:bazel-bin/packages/shared-ux/router/mocks/npm_module_types",
"@types/kbn__shared-ux-services": "link:bazel-bin/packages/kbn-shared-ux-services/npm_module_types",
"@types/kbn__shared-ux-storybook": "link:bazel-bin/packages/kbn-shared-ux-storybook/npm_module_types",
"@types/kbn__shared-ux-storybook-mock": "link:bazel-bin/packages/shared-ux/storybook/mock/npm_module_types",
"@types/kbn__shared-ux-utility": "link:bazel-bin/packages/kbn-shared-ux-utility/npm_module_types",
"@types/kbn__some-dev-log": "link:bazel-bin/packages/kbn-some-dev-log/npm_module_types",
Expand Down
140 changes: 140 additions & 0 deletions packages/shared-ux/router/impl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
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 = "shared-ux-router"
PKG_REQUIRE_NAME = "@kbn/shared-ux-router"

SOURCE_FILES = glob(
[
"**/*.ts",
"**/*.tsx",
"**/*.mdx"
],
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",
"@npm//react-router-dom",
"@npm//react-use",
"@npm//rxjs",
"//packages/kbn-shared-ux-utility",
"//packages/kbn-test-jest-helpers",
]

# 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",
"@npm//@types/react-router-dom",
"@npm//react-use",
"@npm//rxjs",
"//packages/kbn-shared-ux-utility:npm_module_types",
"//packages/shared-ux/router/types:npm_module_types",
"//packages/kbn-ambient-ui-types",
]

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,
additional_args = [
"--copy-files",
"--quiet"
],
)

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,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
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"],
)
53 changes: 53 additions & 0 deletions packages/shared-ux/router/impl/README.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
id: sharedUX/Router
slug: /shared-ux/router
title: Router
description: A router component
tags: ['shared-ux', 'component', 'router', 'route']
date: 2022-08-12
---

## Summary
This is a wrapper around the `react-router-dom` Route component that inserts MatchPropagator in every application route. It helps track all route changes and send them to the execution context, later used to enrich APM 'route-change' transactions.
The component does not require any props and accepts props from the RouteProps interface such as a `path`, or a component like `AppContainer`.


```jsx
<Route path="/url" />
```

### Explanation of RouteProps

```jsx
export interface RouteProps {
location?: H.Location;
component?: React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
render?: (props: RouteComponentProps<any>) => React.ReactNode;
children?: ((props: RouteChildrenProps<any>) => React.ReactNode) | React.ReactNode;
path?: string | string[];
exact?: boolean;
sensitive?: boolean;
strict?: boolean;
}
```

All props are optional

| Prop Name | Prop Type | Description |
|---|---|---|
| `location` | `H.Location` | the location of one instance of history |
| `component` | `React.ComponentType<RouteComponentProps<any>>` or `React.ComponentType<any>;` | a react component |
| `render` | `(props: RouteComponentProps<any>) => React.ReactNode;` | render props to a react node|
| `children` | `((props: RouteChildrenProps<any>) => React.ReactNode)` or `React.ReactNode;` | pass children to a react node |
| `path` | `string` or `string[];` | a url path or array of paths |
| `exact` | `boolean` | exact match for a route (see: https://stackoverflow.com/questions/52275146/usage-of-exact-and-strict-props) |
| `sensitive` | `boolean` | case senstive route |
| `strict` | `boolean` | strict entry of the requested path in the path name |



This component removes the need for manual calls to `useExecutionContext` and they should be removed.

## EUI Promotion Status

This component is not currently considered for promotion to EUI.
35 changes: 35 additions & 0 deletions packages/shared-ux/router/impl/__snapshots__/router.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/shared-ux/router/impl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 { Route } from './router';
13 changes: 13 additions & 0 deletions packages/shared-ux/router/impl/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/shared-ux/router/impl'],
};
8 changes: 8 additions & 0 deletions packages/shared-ux/router/impl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@kbn/shared-ux-router",
"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"
}
45 changes: 45 additions & 0 deletions packages/shared-ux/router/impl/router.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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, { Component, FC } from 'react';
import { shallow } from 'enzyme';
import { Route } from './router';
import { createMemoryHistory } from 'history';

describe('Route', () => {
test('renders', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a more extensive test suite? Given that the render logic depends on a lot of props, would be nice to test some of those scenarios.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added some of the react dom route props in the test suite - let me know what you think. I think for testing exact, sensitive and strict it might involve an integration test?

const example = shallow(<Route />);
expect(example).toMatchSnapshot();
});

test('location renders as expected', () => {
// create a history
const historyLocation = createMemoryHistory();
// add the path to the history
historyLocation.push('/app/wow');
// prevent the location key from remaking itself each jest test
historyLocation.location.key = 's5brde';
// the Route component takes the history location
const example = shallow(<Route location={historyLocation.location} />);
expect(example).toMatchSnapshot();
});

test('component prop renders', () => {
const sampleComponent: FC<{}> = () => {
return <Component>Test</Component>;
};
const example = shallow(<Route component={sampleComponent} />);
expect(example).toMatchSnapshot();
});

test('render prop renders', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice!

const sampleReactNode = React.createElement('li', { id: 'li1' }, 'one');
const example = shallow(<Route render={() => sampleReactNode} />);
expect(example).toMatchSnapshot();
});
});
Loading