Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Separate config and service provided data
  • Loading branch information
SergeyPoluektov committed Jan 25, 2022
commit 76f1cbe73cd6bafa6d84d99bb2a1ce27efadd4e5
11 changes: 6 additions & 5 deletions src/plugins/engagement/.storybook/decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ 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 config = {
const services: EngagementServices = {
chat: {
enabled: true,
chatURL: 'https://elasticcloud-production-chat-us-east-1.s3.amazonaws.com/drift-iframe.html',
pocID: '53877975',
pocEmail: '[email protected]',
pocJWT:
userID: '53877975',
userEmail: '[email protected]',
identityJWT:
'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1Mzg3Nzk3NSIsImV4cCI6MTY0MjUxNDc0Mn0.CcAZbD8R865UmoHGi27wKn0aH1bzkZXhX449yyDH2Vk',
},
};
Expand All @@ -36,4 +37,4 @@ export const getEngagementContextDecorator: DecoratorFn = (storyFn, context) =>
export const getEngagementContextProvider: () => React.FC =
() =>
({ children }) =>
<ServicesProvider {...config}>{children}</ServicesProvider>;
<ServicesProvider {...services}>{children}</ServicesProvider>;
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ const useFrame = (): UseChatType => {
switch (message.type) {
case MESSAGE_READY: {
const user = {
id: chat.pocID,
id: chat.userID,
attributes: {
email: chat.pocEmail,
email: chat.userEmail,
},
jwt: chat.pocJWT,
jwt: chat.identityJWT,
};

chatIframe.contentWindow.postMessage(
Expand Down
12 changes: 6 additions & 6 deletions src/plugins/engagement/public/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@
*/

import React from 'react';
import { ServicesProvider } from '../public/services';
import { ServicesProvider, EngagementServices } from '../public/services';
import { EngagementPluginSetup, EngagementPluginStart } from './types';

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

const getEngagementContextProvider: () => React.FC =
() =>
({ children }) =>
<ServicesProvider {...config}>{children}</ServicesProvider>;
<ServicesProvider {...services}>{children}</ServicesProvider>;

const createEngagementPluginSetup = (): jest.Mocked<EngagementPluginSetup> => {
const mock: jest.Mocked<EngagementPluginSetup> = {};
Expand Down
44 changes: 35 additions & 9 deletions src/plugins/engagement/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ import type {
EngagementPluginSetupDeps,
EngagementPluginStartDeps,
} from './types';
import type { EngagementServices } from './services';
import { ServicesProvider } from './services';

export interface EngagementConfigType {
chat: {
enabled: boolean;
chatURL: string;

// These are here for PoC purposes *only*. They should be replaced with actual implementations
// as the PoC matures.
pocJWT: string;
pocID: string;
pocEmail: string;
};
}

interface ChatUser {
id: string;
email: string;
}

export class EngagementPlugin
implements
Plugin<
EngagementPluginSetup,
EngagementPluginStart,
Promise<EngagementPluginStart>,
EngagementPluginSetupDeps,
EngagementPluginStartDeps
>
Expand All @@ -44,11 +44,37 @@ export class EngagementPlugin
return {};
}

public start(_core: CoreStart, plugins: EngagementPluginStartDeps): EngagementPluginStart {
private async getChatUser(): Promise<ChatUser | null> {
// TODO: obtain kibana and/or cloud user information
return {
id: 'user-id',
email: '[email protected]',
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to find a way to obtain a Kibana or Cloud user data here. Particularly we need to get user ID (might be Kibana user's username) and user email (this should be the same as in Cloud for Cloud users)
@pgayvallet maybe you know how to do this?


private async getChatIdentityToken(): Promise<string | null> {
// TODO: fetch identity token from plugin internal endpoint
return 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1Mzg3Nzk3NSIsImV4cCI6MTY0MjUxNDc0Mn0.CcAZbD8R865UmoHGi27wKn0aH1bzkZXhX449yyDH2Vk';
}

public async start(_core: CoreStart, plugins: EngagementPluginStartDeps): Promise<EngagementPluginStart> {
const { sharedUX } = plugins;

const user = await this.getChatUser();

const chatIdentityToken = await this.getChatIdentityToken();

const config = this.initializerContext.config.get<EngagementConfigType>();
const chat = config?.chat || { enabled: false };

let chat: EngagementServices['chat'] = { enabled: false };
if (config.chat.enabled && user && chatIdentityToken) {
chat = {
...config.chat,
userID: user.id,
userEmail: user.email,
identityJWT: chatIdentityToken,
}
}

return {
ContextProvider: ({ children }) => (
Expand Down
15 changes: 6 additions & 9 deletions src/plugins/engagement/public/services/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@

import React, { FC, createContext, useContext } from 'react';

interface WithChat {
interface WithoutChat {
enabled: false;
}

interface WithoutChat {
interface WithChat {
enabled: true;
chatURL: string;

// These are here for PoC purposes *only*. They should be replaced with actual implementations
// as the PoC matures.
pocJWT: string;
pocID: string;
pocEmail: string;
identityJWT: string;
userID: string;
userEmail: string;
}

type ChatService = WithChat | WithoutChat;

interface EngagementServices {
export interface EngagementServices {
chat: ChatService;
}

Expand Down
3 changes: 0 additions & 3 deletions src/plugins/engagement/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import { PluginConfigDescriptor } from 'kibana/server';
const chatConfigSchema = schema.object({
enabled: schema.boolean({ defaultValue: false }),
chatURL: schema.maybe(schema.string()),
pocJWT: schema.maybe(schema.string()),
pocID: schema.maybe(schema.string()),
pocEmail: schema.maybe(schema.string()),
});

const configSchema = schema.object({
Expand Down