Skip to content

Commit

Permalink
[Lens] Share link feature (elastic#148829)
Browse files Browse the repository at this point in the history
## Summary

Fixes elastic#75316

* Lens
  * [x] Refactored Top nav actions code to be more modular
  * [x] Created new Locator object for Lens 
    * [x] Enabled server side to make Short URL work with it
    * [x] Added unit tests for it
* [x] Extended `getEditPath` to support `filters` and `refreshInterval`
* [x] Extended mounting code in Lens to handle a new type of incoming
context
* [x] Add new Share action
* [x] Added new `objectTypeTitle` prop to have custom titles on Share
popover
  * [x] Replaced the `Download CSV` action and move it as menu item
    * [x] Refactor code into share item provider + (lazy) panel content
    * [x] Add debug flag to make CSV download testable
    * [x] Add functional tests for CSV download
  * [x] Add Permalink action
    * [x] Integrate Permalink with Short URL service
      * [x] Tweaked Permalink action to work with SO custom URL
      * [x] Tweaked Permalink action to handle disabled state
      * [x] Updated unit tests with new features
* [x] Added (basic) caching logic to avoid too many snapshot duplicate
Short URLs
* [x] New share function test suite created
* [x] Extended `browser` service with a new method to have a blank tab
in browser
  * [x] New helper functions in Lens to test Share feature
  
<img width="375" alt="Screenshot 2023-01-11 at 12 58 30"
src="https://user-images.githubusercontent.com/924948/211800819-60efe70a-9ebe-4bde-82e0-8fa264e8c4af.png">
<img width="427" alt="Screenshot 2023-01-11 at 12 58 36"
src="https://user-images.githubusercontent.com/924948/211800825-ae7b86d0-0e42-4227-a425-cdcd94ec78cb.png">
<img width="426" alt="Screenshot 2023-01-11 at 12 58 40"
src="https://user-images.githubusercontent.com/924948/211800827-73bfb773-b30e-495c-aa61-f5fd10f35d31.png">
<img width="428" alt="Screenshot 2023-01-11 at 12 58 46"
src="https://user-images.githubusercontent.com/924948/211800830-89539c37-7495-48f0-9de6-b7d6f15b7397.png">
<img width="427" alt="Screenshot 2023-01-11 at 12 59 03"
src="https://user-images.githubusercontent.com/924948/211800833-6f1843b9-ab22-49d9-adbd-8f5f588b52e7.png">

### Notes

#### Short URL requirement

This feature strictly requires the ShortURL service to be enabled to
work, otherwise the permalink feature is disabled for snapshot sharing.
This requirement is not clearly stated in the Share menu (yet) like
other app do as the Sharing flow had to be customised in Lens due to
some other technical challenges.
Would it be ok to workout a UI improvement as follow up?

#### Context tech debt

The way the locator works as injecting the shared state into the context
produced a discrete amount of branching, due to inconsistency of the
`context` type coming from different sources (Discover, Agg-based/TSVB,
Lens itself...). Perhaps it's worth discussing having a refactoring of
the context type here?

#### Missing locator service

Due to the way the sharing logic works in Lens the locator has not been
exported from the `plugin` functions. I thought to add a custom function
for it, but perhaps we could investigate a bit better whether this is
needed and eventually its implementation in a follow up task.

## How is the snapshot URL generated?

```mermaid
sequenceDiagram
    actor User
    User->>Share Snapshot URL: click
    Share Snapshot URL->> Lens ShortUrlService: Lens state
    Lens ShortUrlService->> Lens ShortUrlService: Check cache based on state
    Lens ShortUrlService->> ShortUrlService: Generate a Short URL
    ShortUrlService->> Lens ShortUrlService: new Short URL
    Lens ShortUrlService->> Application `getUrlForApp`: Build absolute URL
    Application `getUrlForApp`->> Lens ShortUrlService: final URL
    Lens ShortUrlService->>Share Snapshot URL: final URL
    Share Snapshot URL->>User: final URL copied in clipboard
```

### Checklist

Delete any items that are not applicable to this PR.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] Any UI touched in this PR is usable by keyboard only (learn more
about [keyboard accessibility](https://webaim.org/techniques/keyboard/))
- [ ] Any UI touched in this PR does not create any new axe failures
(run axe in browser:
[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),
[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))
- [ ] This was checked for [cross-browser
compatibility](https://www.elastic.co/support/matrix#matrix_browsers)


### Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to
identify risks that should be tested prior to the change/feature
release.

When forming the risk matrix, consider some of the following examples
and how they may potentially impact the change:

| Risk | Probability | Severity | Mitigation/Notes |

|---------------------------|-------------|----------|-------------------------|
| Multiple Spaces&mdash;unexpected behavior in non-default Kibana Space.
| Low | High | Integration tests will verify that all features are still
supported in non-default Kibana Space and when user switches between
spaces. |
| Multiple nodes&mdash;Elasticsearch polling might have race conditions
when multiple Kibana nodes are polling for the same tasks. | High | Low
| Tasks are idempotent, so executing them multiple times will not result
in logical error, but will degrade performance. To test for this case we
add plenty of unit tests around this logic and document manual testing
procedure. |
| Code should gracefully handle cases when feature X or plugin Y are
disabled. | Medium | High | Unit tests will verify that any feature flag
or plugin combination still results in our service operational. |
| [See more potential risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) |


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

Co-authored-by: Kaarina Tungseth <kaarina.tungseth@elastic.co>
Co-authored-by: Stratoula Kalafateli <efstratia.kalafateli@elastic.co>
Co-authored-by: Andrew Tate <drewctate@gmail.com>
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
5 people authored and wayneseymour committed Jan 19, 2023
1 parent 80880d1 commit f20364c
Show file tree
Hide file tree
Showing 41 changed files with 2,026 additions and 354 deletions.

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

16 changes: 16 additions & 0 deletions src/plugins/share/public/components/share_context_menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ test('should only render permalink panel when there are no other panels', () =>
expect(component).toMatchSnapshot();
});

test('should disable the share URL when set', () => {
const component = shallow(<ShareContextMenu {...defaultProps} disabledShareUrl />);
expect(component).toMatchSnapshot();
});

describe('shareContextMenuExtensions', () => {
const shareContextMenuItems: ShareMenuItem[] = [
{
Expand Down Expand Up @@ -69,4 +74,15 @@ describe('shareContextMenuExtensions', () => {
);
expect(component).toMatchSnapshot();
});

test('should render a custom panel title when provided', () => {
const component = shallow(
<ShareContextMenu
{...defaultProps}
objectTypeTitle="Custom object"
shareMenuItems={shareContextMenuItems}
/>
);
expect(component).toMatchSnapshot();
});
});
8 changes: 7 additions & 1 deletion src/plugins/share/public/components/share_context_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ShareContextMenuProps {
objectId?: string;
objectType: string;
shareableUrl?: string;
shareableUrlForSavedObject?: string;
shareMenuItems: ShareMenuItem[];
sharingData: any;
onClose: () => void;
Expand All @@ -33,6 +34,8 @@ export interface ShareContextMenuProps {
showPublicUrlSwitch?: (anonymousUserCapabilities: Capabilities) => boolean;
urlService: BrowserUrlService;
snapshotShareWarning?: string;
objectTypeTitle?: string;
disabledShareUrl?: boolean;
}

export class ShareContextMenu extends Component<ShareContextMenuProps> {
Expand Down Expand Up @@ -64,6 +67,7 @@ export class ShareContextMenu extends Component<ShareContextMenuProps> {
objectId={this.props.objectId}
objectType={this.props.objectType}
shareableUrl={this.props.shareableUrl}
shareableUrlForSavedObject={this.props.shareableUrlForSavedObject}
anonymousAccess={this.props.anonymousAccess}
showPublicUrlSwitch={this.props.showPublicUrlSwitch}
urlService={this.props.urlService}
Expand All @@ -78,6 +82,7 @@ export class ShareContextMenu extends Component<ShareContextMenuProps> {
icon: 'link',
panel: permalinkPanel.id,
sortOrder: 0,
disabled: Boolean(this.props.disabledShareUrl),
});
panels.push(permalinkPanel);

Expand All @@ -94,6 +99,7 @@ export class ShareContextMenu extends Component<ShareContextMenuProps> {
objectId={this.props.objectId}
objectType={this.props.objectType}
shareableUrl={this.props.shareableUrl}
shareableUrlForSavedObject={this.props.shareableUrlForSavedObject}
urlParamExtensions={this.props.embedUrlParamExtensions}
anonymousAccess={this.props.anonymousAccess}
showPublicUrlSwitch={this.props.showPublicUrlSwitch}
Expand Down Expand Up @@ -131,7 +137,7 @@ export class ShareContextMenu extends Component<ShareContextMenuProps> {
title: i18n.translate('share.contextMenuTitle', {
defaultMessage: 'Share this {objectType}',
values: {
objectType: this.props.objectType,
objectType: this.props.objectTypeTitle || this.props.objectType,
},
}),
items: menuItems
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/share/public/components/url_panel_content.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ describe('share url panel content', () => {
expect(component).toMatchSnapshot();
});

test('should use custom savedObjectUrl if provided for saved object export', () => {
const component = shallow(
<UrlPanelContent
{...defaultProps}
objectId="id1"
allowShortUrl={false}
shareableUrlForSavedObject="socustomurl:id1#"
/>
);

act(() => {
component.find(EuiRadioGroup).prop('onChange')!(ExportUrlAsType.EXPORT_URL_AS_SAVED_OBJECT);
});
expect(component.find(EuiCopy).prop('textToCopy')).toEqual('socustomurl:id1#?_g=');
});

test('should hide short url section when allowShortUrl is false', () => {
const component = shallow(
<UrlPanelContent {...defaultProps} allowShortUrl={false} objectId="id1" />
Expand Down
13 changes: 10 additions & 3 deletions src/plugins/share/public/components/url_panel_content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface UrlPanelContentProps {
objectId?: string;
objectType: string;
shareableUrl?: string;
shareableUrlForSavedObject?: string;
urlParamExtensions?: UrlParamExtension[];
anonymousAccess?: AnonymousAccessServiceContract;
showPublicUrlSwitch?: (anonymousUserCapabilities: Capabilities) => boolean;
Expand Down Expand Up @@ -242,7 +243,7 @@ export class UrlPanelContent extends Component<UrlPanelContentProps, State> {
return;
}

const url = this.getSnapshotUrl();
const url = this.getSnapshotUrl(true);

const parsedUrl = parseUrl(url);
if (!parsedUrl || !parsedUrl.hash) {
Expand All @@ -269,8 +270,14 @@ export class UrlPanelContent extends Component<UrlPanelContentProps, State> {
return this.updateUrlParams(formattedUrl);
};

private getSnapshotUrl = () => {
const url = this.props.shareableUrl || window.location.href;
private getSnapshotUrl = (forSavedObject?: boolean) => {
let url = '';
if (forSavedObject && this.props.shareableUrlForSavedObject) {
url = this.props.shareableUrlForSavedObject;
}
if (!url) {
url = this.props.shareableUrl || window.location.href;
}
return this.updateUrlParams(url);
};

Expand Down
6 changes: 6 additions & 0 deletions src/plugins/share/public/services/share_menu_manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ export class ShareMenuManager {
sharingData,
menuItems,
shareableUrl,
shareableUrlForSavedObject,
embedUrlParamExtensions,
theme,
showPublicUrlSwitch,
urlService,
anonymousAccess,
snapshotShareWarning,
onClose,
objectTypeTitle,
disabledShareUrl,
}: ShowShareMenuOptions & {
menuItems: ShareMenuItem[];
urlService: BrowserUrlService;
Expand Down Expand Up @@ -107,15 +110,18 @@ export class ShareMenuManager {
allowShortUrl={allowShortUrl}
objectId={objectId}
objectType={objectType}
objectTypeTitle={objectTypeTitle}
shareMenuItems={menuItems}
sharingData={sharingData}
shareableUrl={shareableUrl}
shareableUrlForSavedObject={shareableUrlForSavedObject}
onClose={onClose}
embedUrlParamExtensions={embedUrlParamExtensions}
anonymousAccess={anonymousAccess}
showPublicUrlSwitch={showPublicUrlSwitch}
urlService={urlService}
snapshotShareWarning={snapshotShareWarning}
disabledShareUrl={disabledShareUrl}
/>
</EuiWrappingPopover>
</KibanaThemeProvider>
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/share/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ export interface ShareContext {
* If not set it will default to `window.location.href`
*/
shareableUrl: string;
shareableUrlForSavedObject?: string;
sharingData: { [key: string]: unknown };
isDirty: boolean;
onClose: () => void;
showPublicUrlSwitch?: (anonymousUserCapabilities: Capabilities) => boolean;
disabledShareUrl?: boolean;
}

/**
Expand Down Expand Up @@ -99,4 +101,5 @@ export interface ShowShareMenuOptions extends Omit<ShareContext, 'onClose'> {
embedUrlParamExtensions?: UrlParamExtension[];
snapshotShareWarning?: string;
onClose?: () => void;
objectTypeTitle?: string;
}
8 changes: 8 additions & 0 deletions test/functional/services/common/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,14 @@ class BrowserService extends FtrService {
await this.driver.switchTo().window(tabs[tabIndex]);
}

/**
* Opens a blank new tab.
* @return {Promise<string>}
*/
public async openNewTab() {
await this.driver.switchTo().newWindow('tab');
}

/**
* Sets a value in local storage for the focused window/frame.
*
Expand Down

0 comments on commit f20364c

Please sign in to comment.