Skip to content

Commit a8c863b

Browse files
authored
Merge pull request #48 from iceljc/features/auto-load-content-log
auto load content log view
2 parents d3fc931 + 9b5ef03 commit a8c863b

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

src/lib/common/LiveChatEntry.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
onMount(async () => {
1212
const agentSettings = await getSettingDetail("Agent");
13-
chatUrl = `${PUBLIC_LIVECHAT_HOST}chat/${agentSettings.hostAgentId}`;
13+
chatUrl = `${PUBLIC_LIVECHAT_HOST}chat/${agentSettings.hostAgentId}?isLite=true`;
1414
showChatIcon = true;
1515
});
1616

src/routes/chat/[agentId]/+page.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
}
3939
4040
conversationId = conversation.id;
41-
window.location.href = `chat/${agentId}/${conversationId}`;
41+
let charUrl = `chat/${agentId}/${conversationId}`;
42+
const isLite = $page.url.searchParams.get('isLite');
43+
if (isLite === 'true') {
44+
charUrl = `${charUrl}?isLite=true`
45+
}
46+
window.location.href = charUrl;
4247
});
4348
</script>
4449

src/routes/chat/[agentId]/[conversationId]/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
});
2323
</script>
2424

25-
{#if currentUser }
25+
{#if currentUser}
2626
<Chat currentUser={currentUser} agent={agent} />
2727
{/if}

src/routes/chat/[agentId]/[conversationId]/chat-box.svelte

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
onMount(async () => {
8686
dialogs = await GetDialogs(params.conversationId);
8787
initPrevSentMessages(dialogs);
88+
initContentLogView();
8889
8990
signalr.onMessageReceivedFromClient = onMessageReceivedFromClient;
9091
signalr.onMessageReceivedFromCsr = onMessageReceivedFromCsr;
@@ -109,18 +110,50 @@
109110
sentMsgIdx = prevSentMsgs.length;
110111
}
111112
113+
function initContentLogView() {
114+
isLoadContentLog = $page.url.searchParams.get('isLite') !== 'true';
115+
}
116+
117+
async function refresh() {
118+
// trigger UI render
119+
dialogs = dialogs?.map(item => { return { ...item }; }) || [];
120+
groupedDialogs = groupDialogs(dialogs);
121+
await tick();
122+
123+
setTimeout(() => {
124+
const { viewport } = scrollbar.elements();
125+
viewport.scrollTo({ top: viewport.scrollHeight, behavior: 'smooth' }); // set scroll offset
126+
}, 200);
127+
}
128+
129+
/** @param {import('$types').ChatResponseModel[]} dialogs */
130+
function groupDialogs(dialogs) {
131+
if (!!!dialogs) return [];
132+
const format = 'MMM D, YYYY';
133+
// @ts-ignore
134+
return _.groupBy(dialogs, (x) => {
135+
const createDate = moment.utc(x.created_at).local().format(format);
136+
if (createDate == moment.utc().local().format(format)) {
137+
return 'Today';
138+
} else if (createDate == moment.utc().local().subtract(1, 'days').format(format)) {
139+
return 'Yesterday';
140+
}
141+
return createDate;
142+
});
143+
}
144+
112145
113146
/** @param {import('$types').ChatResponseModel} message */
114147
function onMessageReceivedFromClient(message) {
115-
dialogs.push(message);
116-
refresh();
117-
text = "";
148+
dialogs.push(message);
149+
refresh();
150+
text = "";
118151
}
119152
120153
/** @param {import('$types').ChatResponseModel} message */
121154
function onMessageReceivedFromCsr(message) {
122-
dialogs.push(message);
123-
refresh();
155+
dialogs.push(message);
156+
refresh();
124157
}
125158
126159
/** @param {import('$types').ChatResponseModel} message */
@@ -168,49 +201,21 @@
168201
reject(err);
169202
});
170203
});
171-
172204
}
173205
174206
async function startListen() {
175207
microphoneIcon = "microphone";
176208
webSpeech.onSpeechToTextDetected = (transcript) => {
177209
text = transcript;
178210
if (!!!_.trim(text) || isSendingMsg) return;
211+
179212
sendTextMessage().then(() => {
180213
microphoneIcon = "microphone-off";
181214
}).catch(() => {
182215
microphoneIcon = "microphone-off";
183216
});
184217
}
185218
webSpeech.start();
186-
}
187-
188-
async function refresh() {
189-
// trigger UI render
190-
dialogs = dialogs?.map(item => { return { ...item }; }) || [];
191-
groupedDialogs = groupDialogs(dialogs);
192-
await tick();
193-
194-
setTimeout(() => {
195-
const { viewport } = scrollbar.elements();
196-
viewport.scrollTo({ top: viewport.scrollHeight, behavior: 'smooth' }); // set scroll offset
197-
}, 200);
198-
}
199-
200-
/** @param {import('$types').ChatResponseModel[]} dialogs */
201-
function groupDialogs(dialogs) {
202-
if (!!!dialogs) return [];
203-
const format = 'MMM D, YYYY';
204-
// @ts-ignore
205-
return _.groupBy(dialogs, (x) => {
206-
const createDate = moment.utc(x.created_at).local().format(format);
207-
if (createDate == moment.utc().local().format(format)) {
208-
return 'Today';
209-
} else if (createDate == moment.utc().local().subtract(1, 'days').format(format)) {
210-
return 'Yesterday';
211-
}
212-
return createDate;
213-
});
214219
}
215220
216221
/** @param {any} e */
@@ -260,6 +265,21 @@
260265
});
261266
}
262267
268+
/**
269+
* @param {string} payload
270+
* @param {import('$types').QuickReplyMessage} message
271+
*/
272+
function clickQuickReply(payload, message) {
273+
isSendingMsg = true;
274+
sendMessageToHub(params.agentId, params.conversationId, payload).then(() => {
275+
isSendingMsg = false;
276+
message.quick_replies = [];
277+
}).catch(() => {
278+
isSendingMsg = false;
279+
message.quick_replies = [];
280+
});
281+
}
282+
263283
function endChat() {
264284
if (window.location === window.parent.location) {
265285
// @ts-ignore
@@ -537,9 +557,8 @@
537557
<RcText message={message.rich_content.message} />
538558
{:else if message.rich_content && message.rich_content.message.rich_type == 'quick_reply'}
539559
<RcQuickReply
540-
agentId={params.agentId}
541-
conversationId={params.conversationId}
542-
message={message.rich_content.message}
560+
message={message.rich_content.message}
561+
onClickQuickReply={clickQuickReply}
543562
/>
544563
{:else}
545564
<span>{message.text}</span>

src/routes/chat/[agentId]/[conversationId]/rc-quick-reply.svelte

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,23 @@
44
55
/** @type {import('$types').QuickReplyMessage} */
66
export let message;
7-
/** @type {string} */
8-
export let agentId;
9-
/** @type {string} */
10-
export let conversationId;
117
12-
export const fn = {
13-
/**
14-
* @param {string} payload
15-
*/
16-
onTextSubmitted: (payload) => {}
17-
}
8+
/** @type {(arg0: string, arg1: import('$types').QuickReplyMessage) => void} */
9+
export let onClickQuickReply;
1810
1911
/**
12+
* @param {any} e
2013
* @param {string} payload
21-
*/
22-
async function onQuickReplyClick(payload) {
23-
await sendMessageToHub(agentId, conversationId, payload);
24-
message.quick_replies = [];
14+
*/
15+
async function handleQuickReplyClick(e, payload) {
16+
e.preventDefault();
17+
onClickQuickReply && onClickQuickReply(payload, message);
2518
}
2619
</script>
2720

2821
<span>{@html replaceNewLine(message.text)}</span>
2922
<div class="fixed-bottom p-2 text-center" style="margin-bottom: 10vh;">
3023
{#each message.quick_replies as reply}
31-
<button class="btn btn-primary btn-rounded btn-sm m-1" on:click={() => onQuickReplyClick(reply.payload)}>{reply.title}</button>
24+
<button class="btn btn-primary btn-rounded btn-sm m-1" on:click={(e) => handleQuickReplyClick(e, reply.payload)}>{reply.title}</button>
3225
{/each}
3326
</div>

0 commit comments

Comments
 (0)