-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added terms and conditions to chat window (#1)
Every user needs to accept the terms and conditions to use the chat assistant TODO: waiting for terms and conditions details page --------- Signed-off-by: Yulong Ruan <[email protected]>
- Loading branch information
Showing
17 changed files
with
279 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export const CHAT_CONFIG_SAVED_OBJECT_TYPE = 'chat-config'; |
This file contains 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 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 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 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,67 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { useObservable } from 'react-use'; | ||
import { EuiButton, EuiEmptyPrompt, EuiLink, EuiText } from '@elastic/eui'; | ||
import { SavedObjectManager } from '../services/saved_object_manager'; | ||
import { useCore } from '../contexts/core_context'; | ||
import { ChatConfig } from '../types'; | ||
import { CHAT_CONFIG_SAVED_OBJECT_TYPE } from '../../common/constants/saved_objects'; | ||
|
||
interface Props { | ||
username: string; | ||
} | ||
|
||
export const TermsAndConditions = (props: Props) => { | ||
const core = useCore(); | ||
|
||
const chatConfigService = SavedObjectManager.getInstance<ChatConfig>( | ||
core.services.savedObjects.client, | ||
CHAT_CONFIG_SAVED_OBJECT_TYPE | ||
); | ||
const config = useObservable(chatConfigService.get$(props.username)); | ||
const loading = useObservable(chatConfigService.getLoadingStatus$(props.username)); | ||
const termsAccepted = Boolean(config?.terms_accepted); | ||
|
||
return ( | ||
<EuiEmptyPrompt | ||
style={{ padding: 0 }} | ||
iconType="cheer" | ||
iconColor="primary" | ||
titleSize="s" | ||
body={ | ||
<EuiText> | ||
<p>Welcome {props.username} to the OpenSearch Assistant</p> | ||
<p>I can help you analyze data, create visualizations, and get other insights.</p> | ||
<p>How can I help?</p> | ||
<EuiText size="xs" color="subdued"> | ||
The OpenSearch Assistant may produce inaccurate information. Verify all information | ||
before using it in any environment or workload. | ||
</EuiText> | ||
</EuiText> | ||
} | ||
actions={[ | ||
!termsAccepted && ( | ||
<EuiButton | ||
isLoading={loading} | ||
color="primary" | ||
fill | ||
onClick={() => | ||
chatConfigService.createOrUpdate(props.username, { terms_accepted: true }) | ||
} | ||
> | ||
Accept terms & go | ||
</EuiButton> | ||
), | ||
<EuiText size="xs"> | ||
<EuiLink target="_blank" href="/"> | ||
Terms & Conditions | ||
</EuiLink> | ||
</EuiText>, | ||
]} | ||
/> | ||
); | ||
}; |
This file contains 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 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 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 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 OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from '../../../../src/core/public'; | ||
import { SavedObjectService } from './saved_object_service'; | ||
|
||
export class SavedObjectManager { | ||
private static instances: Map<string, SavedObjectService<{}>> = new Map(); | ||
private constructor() {} | ||
|
||
public static getInstance<T extends {}>( | ||
savedObjectsClient: SavedObjectsClientContract, | ||
savedObjectType: string | ||
) { | ||
if (!SavedObjectManager.instances.has(savedObjectType)) { | ||
SavedObjectManager.instances.set( | ||
savedObjectType, | ||
new SavedObjectService<T>(savedObjectsClient, savedObjectType) | ||
); | ||
} | ||
return SavedObjectManager.instances.get(savedObjectType) as SavedObjectService<T>; | ||
} | ||
} |
This file contains 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,93 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { BehaviorSubject } from 'rxjs'; | ||
import { SavedObjectsClientContract } from '../../../../src/core/public'; | ||
|
||
export class SavedObjectService<T extends {}> { | ||
private objects: Record<string, BehaviorSubject<Partial<T> | null>> = {}; | ||
private loadingStatus: Record<string, BehaviorSubject<boolean>> = {}; | ||
|
||
constructor( | ||
private readonly client: SavedObjectsClientContract, | ||
private readonly savedObjectType: string | ||
) {} | ||
|
||
private setLoading(id: string, loading: boolean) { | ||
if (!this.loadingStatus[id]) { | ||
this.loadingStatus[id] = new BehaviorSubject(loading); | ||
} else { | ||
this.loadingStatus[id].next(loading); | ||
} | ||
} | ||
|
||
private async load(id: string) { | ||
// set loading to true | ||
this.setLoading(id, true); | ||
|
||
const savedObject = await this.client.get<Partial<T>>(this.savedObjectType, id); | ||
|
||
// set loading to false | ||
this.setLoading(id, false); | ||
|
||
if (!savedObject.error) { | ||
this.objects[id].next(savedObject.attributes); | ||
} | ||
return savedObject; | ||
} | ||
|
||
private async create(id: string, attributes: Partial<T>) { | ||
this.setLoading(id, true); | ||
const newObject = await this.client.create<Partial<T>>(this.savedObjectType, attributes, { | ||
id, | ||
}); | ||
this.objects[id].next({ ...newObject.attributes }); | ||
this.setLoading(id, false); | ||
return newObject.attributes; | ||
} | ||
|
||
private async update(id: string, attributes: Partial<T>) { | ||
this.setLoading(id, true); | ||
const newObject = await this.client.update<Partial<T>>(this.savedObjectType, id, attributes); | ||
this.objects[id].next({ ...newObject.attributes }); | ||
this.setLoading(id, false); | ||
return newObject.attributes; | ||
} | ||
|
||
private async initialize(id: string) { | ||
if (!this.objects[id]) { | ||
this.objects[id] = new BehaviorSubject<Partial<T> | null>(null); | ||
await this.load(id); | ||
} | ||
} | ||
|
||
public async get(id: string) { | ||
await this.initialize(id); | ||
return this.objects[id].getValue(); | ||
} | ||
|
||
public get$(id: string) { | ||
this.initialize(id); | ||
return this.objects[id]; | ||
} | ||
|
||
public getLoadingStatus$(id: string) { | ||
return this.loadingStatus[id]; | ||
} | ||
|
||
public async createOrUpdate(id: string, attributes: Partial<T>) { | ||
const currentObject = await this.load(id); | ||
|
||
if (currentObject.error) { | ||
// Object not found, create a new object | ||
if (currentObject.error.statusCode === 404) { | ||
return await this.create(id, attributes); | ||
} | ||
} else { | ||
// object found, update existing object | ||
return await this.update(id, attributes); | ||
} | ||
} | ||
} |
Oops, something went wrong.