-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[cloud] Create IFRAME chat component; add to Unified Integrations #123772
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
Changes from 4 commits
75ba747
e380174
92720ac
4cd7d5a
9b8ca41
1d50335
c5a5059
85f9504
4b56959
a9e65a4
7e078f5
a556ecd
3743c5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
| import { DecoratorFn } from '@storybook/react'; | ||
| import { ServicesProvider, CloudServices } from '../public/services'; | ||
|
|
||
| // TODO: move to a storybook implementation of the service using parameters. | ||
| const services: CloudServices = { | ||
| chat: { | ||
| enabled: true, | ||
| chatURL: 'https://elasticcloud-production-chat-us-east-1.s3.amazonaws.com/drift-iframe.html', | ||
| userID: 'user-id', | ||
| userEmail: '[email protected]', | ||
| // this doesn't affect chat appearance, | ||
| // but a user identity in Drift only | ||
| identityJWT: 'identity-jwt', | ||
| }, | ||
| }; | ||
|
|
||
| export const getEngagementContextDecorator: DecoratorFn = (storyFn) => { | ||
| const EngagementProvider = getEngagementContextProvider(); | ||
| return <EngagementProvider>{storyFn()}</EngagementProvider>; | ||
| }; | ||
|
|
||
| export const getEngagementContextProvider: () => React.FC = | ||
| () => | ||
| ({ children }) => | ||
| <ServicesProvider {...services}>{children}</ServicesProvider>; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| export { getEngagementContextDecorator, getEngagementContextProvider } from './decorator'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| export { defaultConfig } from '@kbn/storybook'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { addons } from '@storybook/addons'; | ||
| import { create } from '@storybook/theming'; | ||
| import { PANEL_ID } from '@storybook/addon-actions'; | ||
|
|
||
| addons.setConfig({ | ||
| theme: create({ | ||
| base: 'light', | ||
| brandTitle: 'Cloud Storybook', | ||
| brandUrl: 'https://github.com/elastic/kibana/tree/main/x-pack/plugins/cloud', | ||
| }), | ||
| showPanel: true.valueOf, | ||
| selectedPanel: PANEL_ID, | ||
| }); |
| 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { addDecorator } from '@storybook/react'; | ||
| import { getEngagementContextDecorator } from './decorator'; | ||
|
|
||
| addDecorator(getEngagementContextDecorator); |
| 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| export interface GetChatUserDataResponseBody { | ||
| token: string; | ||
| email: string; | ||
| id: string; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import { Chat } from './iframe'; | ||
|
|
||
| export default { | ||
| title: 'IFRAME Chat', | ||
| description: '', | ||
| parameters: {}, | ||
| }; | ||
|
|
||
| export const Component = () => { | ||
| return <Chat />; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| 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 = () => { | ||
clintandrewhall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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(); | ||
clintandrewhall marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we should add a layer of abstraction between a user / chat and the actual chat provider used, so that we have a user with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Believe me, that's the goal, (see #123339 ) |
||
| 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} />; | ||
| }; | ||
| 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| /* eslint-disable import/no-default-export */ | ||
clintandrewhall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import { Chat } from './iframe'; | ||
| export { Chat } from './iframe'; | ||
|
|
||
| /** | ||
| * Exporting the Chat component as a default export so it can be loaded by React.lazy. | ||
| */ | ||
| export default Chat; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import React, { Suspense } from 'react'; | ||
| import { EuiErrorBoundary } from '@elastic/eui'; | ||
|
|
||
| export const LazyChat = React.lazy(() => import('./chat')); | ||
| export const Chat = () => ( | ||
| <EuiErrorBoundary> | ||
| <Suspense fallback={<div />}> | ||
| <LazyChat /> | ||
| </Suspense> | ||
| </EuiErrorBoundary> | ||
| ); |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import { CloudStart } from '.'; | ||
| import { ServicesProvider } from '../public/services'; | ||
|
|
||
| function createSetupMock() { | ||
| return { | ||
| cloudId: 'mock-cloud-id', | ||
| isCloudEnabled: true, | ||
| cname: 'cname', | ||
| baseUrl: 'base-url', | ||
| deploymentUrl: 'deployment-url', | ||
| profileUrl: 'profile-url', | ||
| organizationUrl: 'organization-url', | ||
| }; | ||
| } | ||
|
|
||
| const config = { | ||
| chat: { | ||
| enabled: true, | ||
| chatURL: 'chat-url', | ||
| userID: 'user-id', | ||
| userEmail: '[email protected]', | ||
| identityJWT: 'identity-jwt', | ||
| }, | ||
| }; | ||
|
|
||
| const getContextProvider: () => React.FC = | ||
| () => | ||
| ({ children }) => | ||
| <ServicesProvider {...config}>{children}</ServicesProvider>; | ||
|
|
||
| const createStartMock = (): jest.Mocked<CloudStart> => ({ | ||
| CloudContextProvider: jest.fn(getContextProvider()), | ||
| }); | ||
|
|
||
| export const cloudMock = { | ||
| createSetup: createSetupMock, | ||
| createStart: createStartMock, | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.