Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .buildkite/scripts/steps/storybooks/build_and_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const STORYBOOKS = [
'dashboard_enhanced',
'dashboard',
'data_enhanced',
'engagement',
'embeddable',
'expression_error',
'expression_image',
Expand Down
1 change: 1 addition & 0 deletions .i18nrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dataViews": "src/plugins/data_views",
"devTools": "src/plugins/dev_tools",
"discover": "src/plugins/discover",
"engagement": "src/plugins/engagement",
"embeddableApi": "src/plugins/embeddable",
"embeddableExamples": "examples/embeddable_examples",
"esQuery": "packages/kbn-es-query/src",
Expand Down
4 changes: 4 additions & 0 deletions docs/developer/plugin-list.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ This API doesn't support angular, for registering angular dev tools, bootstrap a
|Embeddables are re-usable widgets that can be rendered on dashboard, but also in other apps. Developers can embed them directly in their plugin. End users can dynamically add them to embeddable _containers_.


|{kib-repo}blob/{branch}/src/plugins/engagement/README.md[engagement]
|A Kibana plugin


|{kib-repo}blob/{branch}/src/plugins/es_ui_shared/README.md[esUiShared]
|This plugin contains reusable code in the form of self-contained modules (or libraries). Each of these modules exports a set of functionality relevant to the domain of the module.

Expand Down
1 change: 1 addition & 0 deletions src/dev/storybook/aliases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const storybookAliases = {
dashboard: 'src/plugins/dashboard/.storybook',
data_enhanced: 'x-pack/plugins/data_enhanced/.storybook',
discover: 'src/plugins/discover/.storybook',
engagement: 'src/plugins/engagement/.storybook',
embeddable: 'src/plugins/embeddable/.storybook',
expression_error: 'src/plugins/expression_error/.storybook',
expression_image: 'src/plugins/expression_image/.storybook',
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/custom_integrations/storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
*/

export {
getCustomIntegrationsContextDecorator as getStorybookContextDecorator,
getCustomIntegrationsContextProvider as getStorybookContextProvider,
getCustomIntegrationsContextDecorator,
getCustomIntegrationsContextProvider,
} from '../storybook/decorator';
7 changes: 7 additions & 0 deletions src/plugins/engagement/.i18nrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"prefix": "engagement",
"paths": {
"engagement": "."
},
"translations": ["translations/ja-JP.json"]
}
40 changes: 40 additions & 0 deletions src/plugins/engagement/.storybook/decorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 from 'react';
import { DecoratorFn } from '@storybook/react';
import { getSharedUXContextProvider } from '../../shared_ux/.storybook/decorators';
import { ServicesProvider } from '../public/services';
import type { EngagementServices } from '../public/services';

// TODO: move to a storybook implementation of the service using parameters.
const services: EngagementServices = {
chat: {
enabled: true,
chatURL: 'https://elasticcloud-production-chat-us-east-1.s3.amazonaws.com/drift-iframe.html',
userID: '53877975',
userEmail: '[email protected]',
identityJWT:
'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1Mzg3Nzk3NSIsImV4cCI6MTY0MjUxNDc0Mn0.CcAZbD8R865UmoHGi27wKn0aH1bzkZXhX449yyDH2Vk',
},
};

export const getEngagementContextDecorator: DecoratorFn = (storyFn, context) => {
const SharedUXProvider = getSharedUXContextProvider();
const EngagementProvider = getEngagementContextProvider();
return (
<SharedUXProvider>
<EngagementProvider>{storyFn()}</EngagementProvider>
</SharedUXProvider>
);
};

export const getEngagementContextProvider: () => React.FC =
() =>
({ children }) =>
<ServicesProvider {...services}>{children}</ServicesProvider>;
9 changes: 9 additions & 0 deletions src/plugins/engagement/.storybook/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 { getEngagementContextDecorator, getEngagementContextProvider } from './decorator';
11 changes: 11 additions & 0 deletions src/plugins/engagement/.storybook/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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 { defaultConfig } from '@kbn/storybook';

module.exports = defaultConfig;
21 changes: 21 additions & 0 deletions src/plugins/engagement/.storybook/manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { addons } from '@storybook/addons';
import { create } from '@storybook/theming';
import { PANEL_ID } from '@storybook/addon-actions';

addons.setConfig({
theme: create({
base: 'light',
brandTitle: 'Engagement Storybook',
brandUrl: 'https://github.com/elastic/kibana/tree/main/src/plugins/engagement',
}),
showPanel: true.valueOf,
selectedPanel: PANEL_ID,
});
12 changes: 12 additions & 0 deletions src/plugins/engagement/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { addDecorator } from '@storybook/react';
import { getEngagementContextDecorator } from './decorator';

addDecorator(getEngagementContextDecorator);
5 changes: 5 additions & 0 deletions src/plugins/engagement/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# engagement

A Kibana plugin

---
14 changes: 14 additions & 0 deletions src/plugins/engagement/common/index.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.
*/

export const PLUGIN_ID = 'engagement';

export const GET_CHAT_TOKEN_ROUTE_PATH = '/internal/engagement/chat_token';
export interface GetChatTokenResponseBody {
token: string;
}
16 changes: 16 additions & 0 deletions src/plugins/engagement/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "engagement",
"version": "1.0.0",
"kibanaVersion": "kibana",
"owner": {
"name": "Shared UX",
"githubTeam": "shared-ux"
},
"description": "A plugin for engaging with the active user.",
"server": true,
"ui": true,
"requiredPlugins": [
"sharedUX"
],
"optionalPlugins": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 from 'react';

import { Chat } from './iframe_chat';

export default {
title: 'IFRAME Chat',
description: '',
parameters: {},
};

export const Component = () => {
return (
<div>
<Chat />
</div>
);
};
136 changes: 136 additions & 0 deletions src/plugins/engagement/public/components/iframe_chat/iframe_chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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, { useEffect, useRef, useState, CSSProperties } from 'react';
import { css } from '@emotion/react';
import { useChat } from '../../services';

type UseChatType =
| { enabled: false }
| {
enabled: true;
src: string;
ref: React.MutableRefObject<HTMLIFrameElement | null>;
style: CSSProperties;
};

const MESSAGE_READY = 'driftIframeReady';
const MESSAGE_RESIZE = 'driftIframeResize';
const MESSAGE_SET_CONTEXT = 'driftSetContext';

const iframeStyle = css`
position: fixed;
botton: 30px;
right: 30px;
display: block;
`;

// We're sending a lot of information to the frame, so this method puts together the specific
// properties, 1/ to avoid leaking too much, and 2/ to enumerate precisely what we're sending.
const getContext = () => {
const { location, navigator, innerHeight, innerWidth } = window;
const { hash, host, hostname, href, origin, pathname, port, protocol, search } = location;
const { language, userAgent } = navigator;
const { title, referrer } = document;
Comment on lines +35 to +39
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the purpose of this function compared to using window and document directly? To have more control over what's we're effectively sending and avoid potentially leaking things when calling chatIframe.contentWindow.postMessage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just understanding what we have to send to the frame. This is a refactored bit of another draft PR from the Journey team, and, since I don't have insight into the messaging API, splitting it out like this seemed like a good way to keep track of precisely what we need to send, (and for testing purposes).


return {
window: {
location: {
hash,
host,
hostname,
href,
origin,
pathname,
port,
protocol,
search,
},
navigator: { language, userAgent },
innerHeight,
innerWidth,
},
document: {
title,
referrer,
},
};
};

const useFrame = (): UseChatType => {
const ref = useRef<HTMLIFrameElement>(null);
const chat = useChat();
const [style, setStyle] = useState<CSSProperties>({});

useEffect(() => {
const handleMessage = (event: MessageEvent): void => {
const { current: chatIframe } = ref;

if (
!chat.enabled ||
!chatIframe?.contentWindow ||
event.source !== chatIframe?.contentWindow
) {
return;
}

const { data: message } = event;
const context = getContext();

switch (message.type) {
case MESSAGE_READY: {
const user = {
id: chat.userID,
attributes: {
email: chat.userEmail,
},
jwt: chat.identityJWT,
};

chatIframe.contentWindow.postMessage(
{
type: MESSAGE_SET_CONTEXT,
data: { context, user },
},
'*'
);
break;
}

case MESSAGE_RESIZE: {
const styles = message.data.styles || ({} as CSSProperties);
setStyle({ ...style, ...styles });
break;
}

default:
break;
}
};

window.addEventListener('message', handleMessage);

return () => window.removeEventListener('message', handleMessage);
}, [chat, style]);

if (chat.enabled) {
return { enabled: true, src: chat.chatURL, ref, style };
}

return { enabled: false };
};

export const Chat = () => {
const frameProps = useFrame();

if (!frameProps.enabled) {
return null;
}

return <iframe css={iframeStyle} data-test-id="iframe-chat" title="engagement" {...frameProps} />;
};
16 changes: 16 additions & 0 deletions src/plugins/engagement/public/components/iframe_chat/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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.
*/
/* eslint-disable import/no-default-export */

import { Chat } from './iframe_chat';
export { Chat } from './iframe_chat';

/**
* Exporting the Chat component as a default export so it can be loaded by React.lazy.
*/
export default Chat;
13 changes: 13 additions & 0 deletions src/plugins/engagement/public/components/index.ts
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.
*/

import React from 'react';
import { withSuspense } from '../../../shared_ux/public';

export const LazyEngagementChat = React.lazy(() => import('./iframe_chat'));
export const EngagementChat = withSuspense(LazyEngagementChat);
Loading