-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[engagement] Create Engagement plugin PoC #123339
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3aa3697
[engagement] Create engagement plugin PoC
clintandrewhall 2ccdd63
fixes
clintandrewhall f1bba3b
Fixed type and mock issue
clintandrewhall 1b475df
Made chat divide more clear by removing references to 'drift'
clintandrewhall 9bdf552
Fixing mocks
clintandrewhall 71574ff
Merge branch 'main' into cloud/engagement
clintandrewhall fbbe312
Addressing feedback
clintandrewhall 76f1cbe
Separate config and service provided data
SergeyPoluektov 30e8ca9
Add JWT token endpoint
SergeyPoluektov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "prefix": "engagement", | ||
| "paths": { | ||
| "engagement": "." | ||
| }, | ||
| "translations": ["translations/ja-JP.json"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # engagement | ||
|
|
||
| A Kibana plugin | ||
|
|
||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": [] | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/plugins/engagement/public/components/iframe_chat/iframe_chat.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
136
src/plugins/engagement/public/components/iframe_chat/iframe_chat.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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
16
src/plugins/engagement/public/components/iframe_chat/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
clintandrewhall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
windowanddocumentdirectly? To have more control over what's we're effectively sending and avoid potentially leaking things when callingchatIframe.contentWindow.postMessage?There was a problem hiding this comment.
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).