Skip to content

Commit

Permalink
use context
Browse files Browse the repository at this point in the history
  • Loading branch information
Jicheng Lu committed Apr 12, 2024
1 parent b25295c commit 1d67dc6
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/lib/scss/custom/pages/_chat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
.video-element-card {
border: none;
box-shadow: none;
flex: 1 1 400px;
flex: 1 1 300px;

.card-body {
padding: 0 !important;
Expand Down
11 changes: 7 additions & 4 deletions src/routes/chat/[agentId]/[conversationId]/chat-box.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import 'overlayscrollbars/overlayscrollbars.css';
import { OverlayScrollbars } from 'overlayscrollbars';
import { page } from '$app/stores';
import { onMount, tick } from 'svelte';
import { onMount, setContext, tick } from 'svelte';
import Viewport from 'svelte-viewport-info';
import { PUBLIC_LIVECHAT_ENTRY_ICON } from '$env/static/public';
import { USER_SENDERS } from '$lib/helpers/constants';
Expand Down Expand Up @@ -107,6 +107,10 @@
let isThinking = false;
let isLite = false;
let isFrame = false;
setContext('chat-window-context', {
autoScrollToBottom: autoScrollToBottom
});
onMount(async () => {
dialogs = await GetDialogs(params.conversationId);
Expand Down Expand Up @@ -150,7 +154,7 @@
isFrame = $page.url.searchParams.get('isFrame') === 'true';
// initial condition
isContentLogClosed = false;
isStateLogClosed = true;
isStateLogClosed = false;
resizeChatWindow();
}
Expand Down Expand Up @@ -903,9 +907,8 @@
<div class="msg-container">
<RichContent
message={message}
displayExtraElements={message.message_id === lastBotMsg?.message_id && !isSendingMsg && !isThinking}
displayOptionElements={message.message_id === lastBotMsg?.message_id && !isSendingMsg && !isThinking}
disableOption={isSendingMsg || isThinking}
refresh={autoScrollToBottom}
onConfirm={confirmSelectedOption}
/>
<ChatAttachment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { Card, CardBody } from "@sveltestrap/sveltestrap";
import { onMount } from "svelte";
import { getContext, onMount } from "svelte";
/** @type {boolean} */
export let disableOption = false;
Expand All @@ -11,18 +11,17 @@
/** @type {(args0: string, args1: string) => any} */
export let onConfirm;
/** @type {() => any} */
export let refresh = () => {};
/** @type {any[]} */
let cards = [];
/** @type {any[]} */
let buttons = [];
const { autoScrollToBottom } = getContext('chat-window-context');
onMount(() => {
reset();
collectOptions(options);
refresh && refresh();
collectOptions(options)
autoScrollToBottom?.();
});
/** @param {any[]} options */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { onMount } from "svelte";
import { getContext, onMount } from "svelte";
import SveltePlayer from "svelte-player";
import { Card, CardBody } from "@sveltestrap/sveltestrap";
import { ElementType } from "$lib/helpers/enums";
Expand All @@ -16,9 +16,6 @@
/** @type {(args0: string, args1: string) => any} */
export let onConfirm = () => {};
/** @type {() => any} */
export let refresh = () => {};
/** @type {string} */
export let confirmBtnText = 'Continue';
Expand All @@ -33,10 +30,12 @@
/** @type {any[]} */
let videoOptions = [];
const { autoScrollToBottom } = getContext('chat-window-context');
onMount(() => {
reset();
collectOptions(options);
refresh && refresh();
autoScrollToBottom?.();
});
/** @param {any[]} options */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,38 @@
export let message;
/** @type {boolean} */
export let displayExtraElements = false;
export let displayOptionElements = false;
/** @type {boolean} */
export let disableOption = false;
/** @type {(args0: string, args1: string) => any} */
export let onConfirm = () => {};
/** @type {() => any} */
export let refresh = () => {};
/** @type {boolean} */
let isComplexElement = false;
let isMultiSelect = false;
/** @type {any[]} */
let options = [];
$: {
const isGeneric = message?.rich_content?.message?.rich_type === RichType.Generic;
// @ts-ignore
const hasSuboptions = message?.rich_content?.message?.elements?.some(x => x.buttons?.length > 0) || false;
isComplexElement = isGeneric && hasSuboptions;
if (displayOptionElements) {
const richType = message?.rich_content?.message?.rich_type;
if (richType === RichType.QuickReply) {
options = message?.rich_content?.message?.quick_replies;
} else if (richType === RichType.Button) {
options = message?.rich_content?.message?.buttons;
} else if (richType === RichType.MultiSelect) {
options = message?.rich_content?.message?.options;
isMultiSelect = true;
} else if (richType === RichType.Generic) {
options = message?.rich_content?.message?.elements;
// @ts-ignore
isComplexElement = message?.rich_content?.message?.elements?.some(x => x.buttons?.length > 0) || false;
}
}
}
/**
Expand All @@ -44,18 +57,10 @@
</div>
</div>
{#if displayExtraElements}
{#if message?.rich_content?.message?.rich_type === RichType.QuickReply}
<RcPlainOptions options={message?.rich_content?.message?.quick_replies} disableOption={disableOption} onConfirm={handleConfirm} refresh={refresh} />
{:else if message?.rich_content?.message?.rich_type === RichType.Button}
<RcPlainOptions options={message?.rich_content?.message?.buttons} disableOption={disableOption} onConfirm={handleConfirm} refresh={refresh} />
{:else if message?.rich_content?.message?.rich_type === RichType.MultiSelect}
<RcPlainOptions options={message?.rich_content?.message?.options} isMultiSelect disableOption={disableOption} onConfirm={handleConfirm} refresh={refresh} />
{:else if message?.rich_content?.message?.rich_type === RichType.Generic}
{#if isComplexElement}
<RcComplexOptions options={message?.rich_content?.message?.elements} disableOption={disableOption} onConfirm={handleConfirm} refresh={refresh} />
{:else}
<RcPlainOptions options={message?.rich_content?.message?.elements} disableOption={disableOption} onConfirm={handleConfirm} refresh={refresh} />
{/if}
{#if displayOptionElements}
{#if !isComplexElement}
<RcPlainOptions options={options} isMultiSelect={isMultiSelect} disableOption={disableOption} onConfirm={handleConfirm} />
{:else}
<RcComplexOptions options={options} disableOption={disableOption} onConfirm={handleConfirm} />
{/if}
{/if}

0 comments on commit 1d67dc6

Please sign in to comment.