Skip to content

Commit

Permalink
Able to reload conversation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Jan 24, 2024
1 parent 43c75a1 commit 03889ab
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
28 changes: 27 additions & 1 deletion src/lib/helpers/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,30 @@ export const globalLoaderStore = writable(false);
*/
export function setGlobalLoad(value){
globalLoaderStore.set(value);
}
}

/** @type {Writable<import('$types').ConversationModel>}*/
export const conversationStore = writable({});

/**
* @returns {Writable<import('$types').ConversationModel>}
*/
export function getConversationStore () {
if (browser) {
// Access localStorage only if in the browser context
let json = localStorage.getItem('conversation');
if (json)
return JSON.parse(json);
else
return conversationStore;
} else {
// Return a default value for SSR
return conversationStore;
}
};

conversationStore.subscribe(value => {
if (browser && value.id) {
localStorage.setItem('conversation', JSON.stringify(value));
}
});
2 changes: 1 addition & 1 deletion src/routes/VerticalLayout/Footer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Container fluid>
<Row>
<Col sm={6}>
{new Date().getFullYear()} © {PUBLIC_COMPANY_NAME}.
{new Date().getFullYear()} © {PUBLIC_COMPANY_NAME}
</Col>
<Col sm={6}>
<div class="text-sm-end d-none d-sm-block">
Expand Down
16 changes: 8 additions & 8 deletions src/routes/chat/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<script>
import { Container, Row, Col } from '@sveltestrap/sveltestrap';
// This page is used to initialize a new conversation for client
import { page } from '$app/stores';
import { onMount } from 'svelte';
import { getSettingDetail } from '$lib/services/setting-service';
import { getAgents } from '$lib/services/agent-service.js'
const params = $page.params;
let agentId = "undefined";
/** @type {import('$types').AgentModel[]} */
let agents = [];
let agentId = 'undefined';
onMount(async () => {
onMount(async () => {
agents = await getAgents({isEvaluator: false});
agentId = agents[0].id;
});
const agentSettings = await getSettingDetail("Agent");
agentId = agentSettings.hostAgentId;
});
</script>

<Container fluid>
Expand Down Expand Up @@ -43,7 +43,7 @@
</Row>
<Row class="text-center">
<Col>
<p class="section-subtitle text-muted text-center pt-4 font-secondary">We craft digital, graphic and dimensional thinking, to create category leading brand experiences that have meaning and add a value for our clients.</p>
<p class="section-subtitle text-muted text-center pt-4 font-secondary">Select a bot you want to start chatting with and click the Start button.</p>
<div class="d-flex justify-content-center">
<a href="/chat/{agentId}" class="btn btn-primary">
<i class="mdi mdi-chat" />
Expand Down
11 changes: 8 additions & 3 deletions src/routes/chat/[agentId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { newConversation } from '$lib/services/conversation-service.js';
import { getToken, setToken } from '$lib/services/auth-service.js'
import { getUserStore } from '$lib/helpers/store.js';
import { conversationStore, getConversationStore } from '$lib/helpers/store.js';
const params = $page.params;
Expand All @@ -29,10 +30,14 @@
});
}
// new conversation
conversation = await newConversation(agentId);
conversationId = conversation.id;
conversation = getConversationStore();
if (!conversation.id || agentId != conversation.agent_id) {
// new conversation
conversation = await newConversation(agentId);
conversationStore.set(conversation);
}
conversationId = conversation.id;
window.location.href = `/chat/${agentId}/${conversationId}`;
});
</script>
Expand Down
11 changes: 11 additions & 0 deletions src/routes/chat/[agentId]/[conversationId]/chat-box.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import { signalr } from '$lib/services/signalr-service.js';
import { webSpeech } from '$lib/services/web-speech.js';
import { sendMessageToHub, GetDialogs } from '$lib/services/conversation-service.js';
import { newConversation } from '$lib/services/conversation-service';
import { conversationStore } from '$lib/helpers/store.js';
import { utcToLocal } from '$lib/helpers/datetime';
import RcText from './rc-text.svelte';
import RcQuickReply from './rc-quick-reply.svelte';
Expand Down Expand Up @@ -109,6 +111,12 @@
isLoadLog = true;
}
async function newConversationHandler() {
const conversation = await newConversation(params.agentId);
conversationStore.set(conversation);
window.location.href = `/chat/${params.agentId}/${conversation.id}`;
}
async function sendTextMessage() {
await sendMessageToHub(params.agentId, params.conversationId, text);
}
Expand Down Expand Up @@ -172,6 +180,9 @@
<DropdownMenu class="dropdown-menu-end">
<DropdownItem on:click={viewFullLogHandler} >View Log</DropdownItem>
</DropdownMenu>
<DropdownMenu class="dropdown-menu-end">
<DropdownItem on:click={newConversationHandler} >New Conversation</DropdownItem>
</DropdownMenu>
</Dropdown>
</li>
{/if}
Expand Down

0 comments on commit 03889ab

Please sign in to comment.