Skip to content

Commit

Permalink
[8.15] [Dashboard] Fix share dashboard iframe embed code incorrectly (#…
Browse files Browse the repository at this point in the history
…194366) (#194774)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[Dashboard] Fix share dashboard iframe embed code incorrectly
(#194366)](#194366)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Jusheng
Huang","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-03T00:24:46Z","message":"[Dashboard]
Fix share dashboard iframe embed code incorrectly (#194366)\n\n##
Summary\r\n\r\nFixes
[#192980](https://github.com/elastic/kibana/issues/193447)\r\n\r\nMaybe
related pr: #179037 \r\n\r\nAfter fix it, having an URL that is like
this:\r\n```\r\n?embed=true&_g=(refreshInterval%3A(pause%3A!t%2Cvalue%3A60000)%2Ctime%3A(from%3Anow-15m%2Cto%3Anow))&hide-filter-bar=true\r\n```\r\n\r\n**Screenshot**\r\n<img
width=\"1918\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/de069fdb-895e-479a-b868-891bbb55594e\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>\r\nCo-authored-by: Tim
Sullivan
<[email protected]>","sha":"e4ff3bb44874f77a63c24de998fde2f6d7ac9404","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","💝community","v9.0.0","Team:SharedUX","v8.16.0","backport:version","v8.15.3"],"number":194366,"url":"https://github.com/elastic/kibana/pull/194366","mergeCommit":{"message":"[Dashboard]
Fix share dashboard iframe embed code incorrectly (#194366)\n\n##
Summary\r\n\r\nFixes
[#192980](https://github.com/elastic/kibana/issues/193447)\r\n\r\nMaybe
related pr: #179037 \r\n\r\nAfter fix it, having an URL that is like
this:\r\n```\r\n?embed=true&_g=(refreshInterval%3A(pause%3A!t%2Cvalue%3A60000)%2Ctime%3A(from%3Anow-15m%2Cto%3Anow))&hide-filter-bar=true\r\n```\r\n\r\n**Screenshot**\r\n<img
width=\"1918\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/de069fdb-895e-479a-b868-891bbb55594e\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>\r\nCo-authored-by: Tim
Sullivan
<[email protected]>","sha":"e4ff3bb44874f77a63c24de998fde2f6d7ac9404"}},"sourceBranch":"main","suggestedTargetBranches":["8.15"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194366","number":194366,"mergeCommit":{"message":"[Dashboard]
Fix share dashboard iframe embed code incorrectly (#194366)\n\n##
Summary\r\n\r\nFixes
[#192980](https://github.com/elastic/kibana/issues/193447)\r\n\r\nMaybe
related pr: #179037 \r\n\r\nAfter fix it, having an URL that is like
this:\r\n```\r\n?embed=true&_g=(refreshInterval%3A(pause%3A!t%2Cvalue%3A60000)%2Ctime%3A(from%3Anow-15m%2Cto%3Anow))&hide-filter-bar=true\r\n```\r\n\r\n**Screenshot**\r\n<img
width=\"1918\"
alt=\"image\"\r\nsrc=\"https://github.com/user-attachments/assets/de069fdb-895e-479a-b868-891bbb55594e\">\r\n\r\n---------\r\n\r\nCo-authored-by:
Elastic Machine
<[email protected]>\r\nCo-authored-by: Tim
Sullivan
<[email protected]>","sha":"e4ff3bb44874f77a63c24de998fde2f6d7ac9404"}},{"branch":"8.x","label":"v8.16.0","labelRegex":"^v8.16.0$","isSourceBranch":false,"url":"https://github.com/elastic/kibana/pull/194773","number":194773,"state":"OPEN"},{"branch":"8.15","label":"v8.15.3","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: Jusheng Huang <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2024
1 parent 9d7cc69 commit 20c068b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/plugins/share/public/components/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export interface IShareContext extends ShareContext {
anonymousAccess?: AnonymousAccessServiceContract;
urlService: BrowserUrlService;
snapshotShareWarning?: string;
isEmbedded: boolean;
theme: ThemeServiceSetup;
i18n: I18nStart;
publicAPIEnabled?: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 { mountWithIntl } from '@kbn/test-jest-helpers';
import { EmbedContent } from './embed_content';
import React from 'react';
import { ReactWrapper } from 'enzyme';

describe('Share modal embed content tab', () => {
describe('share url embedded', () => {
let component: ReactWrapper;

beforeEach(() => {
component = mountWithIntl(
<EmbedContent
objectType="dashboard"
setIsNotSaved={() => jest.fn()}
shareableUrl="/home#/"
/>
);
});

it('works for simple url', async () => {
component.setProps({ shareableUrl: 'http://localhost:5601/app/home#/' });
component.update();

const shareUrl = component
.find('button[data-test-subj="copyEmbedUrlButton"]')
.prop('data-share-url');
expect(shareUrl).toBe(
'<iframe src="http://localhost:5601/app/home#/?embed=true&_g=" height="600" width="800"></iframe>'
);
});

it('works if the url has a query string', async () => {
component.setProps({
shareableUrl:
'http://localhost:5601/app/dashboards#/create?_g=(refreshInterval%3A(pause%3A!t%2Cvalue%3A60000)%2Ctime%3A(from%3Anow-15m%2Cto%3Anow))',
});
component.update();

const shareUrl = component
.find('button[data-test-subj="copyEmbedUrlButton"]')
.prop('data-share-url');
expect(shareUrl).toBe(
'<iframe src="http://localhost:5601/app/dashboards#/create?embed=true&_g=(refreshInterval%3A(pause%3A!t%2Cvalue%3A60000)%2Ctime%3A(from%3Anow-15m%2Cto%3Anow))" height="600" width="800"></iframe>'
);
});
});
});
22 changes: 15 additions & 7 deletions src/plugins/share/public/components/tabs/embed/embed_content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type EmbedProps = Pick<
| 'shareableUrlLocatorParams'
| 'shareableUrlForSavedObject'
| 'shareableUrl'
| 'isEmbedded'
| 'embedUrlParamExtensions'
| 'objectType'
> & {
Expand All @@ -51,7 +50,6 @@ export const EmbedContent = ({
embedUrlParamExtensions: urlParamExtensions,
shareableUrlForSavedObject,
shareableUrl,
isEmbedded,
objectType,
setIsNotSaved,
}: EmbedProps) => {
Expand All @@ -68,6 +66,17 @@ export const EmbedContent = ({
if (objectType !== 'dashboard') setIsNotSaved();
}, [url, setIsNotSaved, objectType]);

const makeUrlEmbeddable = useCallback((tempUrl: string): string => {
const embedParam = '?embed=true';
const urlHasQueryString = tempUrl.indexOf('?') !== -1;

if (urlHasQueryString) {
return tempUrl.replace('?', `${embedParam}&`);
}

return `${tempUrl}${embedParam}`;
}, []);

const getUrlParamExtensions = useCallback(
(tempUrl: string): string => {
return urlParams
Expand All @@ -89,10 +98,11 @@ export const EmbedContent = ({

const updateUrlParams = useCallback(
(tempUrl: string) => {
tempUrl = makeUrlEmbeddable(tempUrl);
tempUrl = urlParams ? getUrlParamExtensions(tempUrl) : tempUrl;
return tempUrl;
},
[getUrlParamExtensions, urlParams]
[makeUrlEmbeddable, getUrlParamExtensions, urlParams]
);

const getSnapshotUrl = useCallback(
Expand Down Expand Up @@ -179,16 +189,14 @@ export const EmbedContent = ({
tempUrl = addUrlAnonymousAccessParameters(tempUrl!);
}

if (isEmbedded) {
tempUrl = makeIframeTag(tempUrl!);
}
tempUrl = makeIframeTag(tempUrl!);

setUrl(tempUrl!);
}, [
addUrlAnonymousAccessParameters,
exportUrlAs,
getSavedObjectUrl,
getSnapshotUrl,
isEmbedded,
shortUrlCache,
useShortUrl,
]);
Expand Down
11 changes: 2 additions & 9 deletions src/plugins/share/public/components/tabs/embed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ const embedTabReducer: IEmbedTab['reducer'] = (state = { url: '', isNotSaved: fa
};

const EmbedTabContent: NonNullable<IEmbedTab['content']> = ({ state, dispatch }) => {
const {
embedUrlParamExtensions,
shareableUrlForSavedObject,
shareableUrl,
isEmbedded,
objectType,
isDirty,
} = useShareTabsContext()!;
const { embedUrlParamExtensions, shareableUrlForSavedObject, shareableUrl, objectType, isDirty } =
useShareTabsContext()!;

const setIsNotSaved = useCallback(() => {
dispatch({
Expand All @@ -59,7 +53,6 @@ const EmbedTabContent: NonNullable<IEmbedTab['content']> = ({ state, dispatch })
embedUrlParamExtensions,
shareableUrlForSavedObject,
shareableUrl,
isEmbedded,
objectType,
isNotSaved: state?.isNotSaved,
setIsNotSaved,
Expand Down
1 change: 0 additions & 1 deletion src/plugins/share/public/services/share_menu_manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export class ShareMenuManager {
snapshotShareWarning,
disabledShareUrl,
isDirty,
isEmbedded: allowEmbed,
shareMenuItems: menuItems,
toasts,
onClose: () => {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/share/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@kbn/react-kibana-mount",
"@kbn/shared-ux-tabbed-modal",
"@kbn/core-theme-browser",
"@kbn/test-jest-helpers",
],
"exclude": [
"target/**/*",
Expand Down

0 comments on commit 20c068b

Please sign in to comment.