|
4 | 4 | DropdownToggle, |
5 | 5 | DropdownMenu, |
6 | 6 | DropdownItem, |
| 7 | + Modal, |
| 8 | + ModalHeader, |
| 9 | + ModalBody, |
| 10 | + ModalFooter, |
| 11 | + Button |
7 | 12 | } from '@sveltestrap/sveltestrap'; |
8 | 13 | import 'overlayscrollbars/overlayscrollbars.css'; |
9 | 14 | import { OverlayScrollbars } from 'overlayscrollbars'; |
|
12 | 17 | import Link from 'svelte-link'; |
13 | 18 | import { signalr } from '$lib/services/signalr-service.js'; |
14 | 19 | import { webSpeech } from '$lib/services/web-speech.js'; |
15 | | - import { sendMessageToHub, GetDialogs } from '$lib/services/conversation-service.js'; |
| 20 | + import { sendMessageToHub, GetDialogs, deleteConversationMessage } from '$lib/services/conversation-service.js'; |
16 | 21 | import { newConversation } from '$lib/services/conversation-service'; |
17 | 22 | import { conversationStore } from '$lib/helpers/store.js'; |
18 | 23 | import { utcToLocal } from '$lib/helpers/datetime'; |
|
40 | 45 | }; |
41 | 46 | const params = $page.params; |
42 | 47 | |
| 48 | + /** @type {string} */ |
43 | 49 | let text = ""; |
| 50 | + let editText = ""; |
| 51 | + let truncateMsgId = ""; |
44 | 52 | |
45 | 53 | /** @type {import('$types').AgentModel} */ |
46 | 54 | export let agent; |
|
62 | 70 | let stateLogs = []; |
63 | 71 |
|
64 | 72 | /** @type {boolean} */ |
65 | | - let isLoadLog = false; |
66 | | - let isLoadStates = false; |
| 73 | + let isLoadContentLog = false; |
| 74 | + let isLoadStateLog = false; |
| 75 | + let isShowEditMsgModal = false; |
67 | 76 | |
68 | 77 | onMount(async () => { |
69 | 78 | dialogs = await GetDialogs(params.conversationId); |
|
164 | 173 |
|
165 | 174 | function endChat() { |
166 | 175 | if (window.location === window.parent.location) { |
| 176 | + // @ts-ignore |
| 177 | + Swal.fire({ |
| 178 | + title: 'Are you sure?', |
| 179 | + text: "You will exit this conversation.", |
| 180 | + icon: 'warning', |
| 181 | + customClass: 'delete-modal', |
| 182 | + showCancelButton: true, |
| 183 | + confirmButtonText: 'Yes', |
| 184 | + cancelButtonText: 'No' |
| 185 | + // @ts-ignore |
| 186 | + }).then((result) => { |
| 187 | + if (result.value) { |
| 188 | + window.close(); |
| 189 | + } |
| 190 | + }); |
| 191 | + } else { |
| 192 | + window.parent.postMessage({ action: "close" }, "*"); |
| 193 | + } |
| 194 | + } |
| 195 | +
|
| 196 | + function toggleContentLog() { |
| 197 | + isLoadContentLog = !isLoadContentLog; |
| 198 | + } |
| 199 | +
|
| 200 | + function toggleStateLog() { |
| 201 | + isLoadStateLog = !isLoadStateLog; |
| 202 | + } |
| 203 | +
|
| 204 | + /** |
| 205 | + * @param {any} e |
| 206 | + * @param {string} messageText |
| 207 | + */ |
| 208 | + function copyMessage(e, messageText) { |
| 209 | + e.preventDefault(); |
| 210 | + if (!!!text) { |
| 211 | + text += messageText; |
| 212 | + } else { |
| 213 | + text += ' ' + messageText; |
| 214 | + } |
| 215 | + } |
| 216 | +
|
| 217 | + /** |
| 218 | + * @param {any} e |
| 219 | + * @param {string} messageId |
| 220 | + */ |
| 221 | + function deleteMessage(e, messageId) { |
| 222 | + e.preventDefault(); |
| 223 | +
|
167 | 224 | // @ts-ignore |
168 | 225 | Swal.fire({ |
169 | 226 | title: 'Are you sure?', |
170 | | - text: "You will exit this conversation.", |
| 227 | + text: "You won't be able to revert this!", |
171 | 228 | icon: 'warning', |
172 | 229 | customClass: 'delete-modal', |
173 | 230 | showCancelButton: true, |
174 | | - confirmButtonText: 'Yes', |
| 231 | + confirmButtonText: 'Yes, delete it!', |
175 | 232 | cancelButtonText: 'No' |
176 | 233 | // @ts-ignore |
177 | | - }).then((result) => { |
| 234 | + }).then(async (result) => { |
178 | 235 | if (result.value) { |
179 | | - window.close(); |
| 236 | + await handleDeleteMessage(messageId); |
180 | 237 | } |
181 | 238 | }); |
182 | | - } else { |
183 | | - window.parent.postMessage({ action: "close" }, "*"); |
184 | | - } |
185 | 239 | } |
186 | 240 |
|
187 | | - function viewContentLog() { |
188 | | - isLoadLog = true; |
189 | | - } |
| 241 | + /** @param {string} messageId */ |
| 242 | + async function handleDeleteMessage(messageId) { |
| 243 | + const isDeleted = truncateDialog(messageId); |
| 244 | + if (!isDeleted) return; |
| 245 | + await deleteConversationMessage(params.conversationId, messageId); |
| 246 | + } |
190 | 247 |
|
191 | | - function viewStateLog() { |
192 | | - isLoadStates = true; |
| 248 | + /** |
| 249 | + * @param {any} e |
| 250 | + * @param {import('$types').ChatResponseModel} message |
| 251 | + */ |
| 252 | + function editMessage(e, message) { |
| 253 | + e.preventDefault(); |
| 254 | + truncateMsgId = message?.message_id; |
| 255 | + editText = message?.text; |
| 256 | + isShowEditMsgModal = true; |
193 | 257 | } |
194 | 258 |
|
195 | | - function closeContentLog() { |
196 | | - isLoadLog = false; |
| 259 | + function toggleEditMsgModel() { |
| 260 | + isShowEditMsgModal = !isShowEditMsgModal; |
| 261 | + if (!isShowEditMsgModal) { |
| 262 | + truncateMsgId = ""; |
| 263 | + editText = ""; |
| 264 | + } |
197 | 265 | } |
198 | 266 |
|
199 | | - function closeStateLog() { |
200 | | - isLoadStates = false; |
| 267 | + async function confirmEditMsg() { |
| 268 | + const isDeleted = truncateDialog(truncateMsgId); |
| 269 | + if (!isDeleted) return; |
| 270 | + toggleEditMsgModel(); |
| 271 | + await sendMessageToHub(params.agentId, params.conversationId, editText, truncateMsgId); |
| 272 | + } |
| 273 | +
|
| 274 | + /** @param {string} messageId */ |
| 275 | + function truncateDialog(messageId) { |
| 276 | + const foundIdx = dialogs.findIndex(x => x.message_id === messageId); |
| 277 | + if (foundIdx < 0) return false; |
| 278 | + dialogs = dialogs.filter((x, idx) => idx < foundIdx); |
| 279 | + return true; |
201 | 280 | } |
202 | 281 | </script> |
203 | 282 |
|
| 283 | +<Modal class="delete-modal" fade size='xl' isOpen={isShowEditMsgModal} toggle={toggleEditMsgModel}> |
| 284 | + <ModalHeader>Edit message</ModalHeader> |
| 285 | + <ModalBody> |
| 286 | + <textarea class="form-control chat-input" rows="10" maxlength={500} bind:value={editText} placeholder="Enter Message..." /> |
| 287 | + </ModalBody> |
| 288 | + <ModalFooter> |
| 289 | + <Button color="primary" on:click={confirmEditMsg} disabled={!!!_.trim(editText)}>Confirm</Button> |
| 290 | + <Button color="secondary" on:click={toggleEditMsgModel}>Cancel</Button> |
| 291 | + </ModalFooter> |
| 292 | +</Modal> |
| 293 | +
|
204 | 294 | <div class="d-lg-flex"> |
205 | 295 | <Splitpanes> |
206 | | - {#if isLoadStates} |
| 296 | + {#if isLoadStateLog} |
207 | 297 | <Pane size={30} minSize={20} maxSize={50} > |
208 | | - <StateLog stateLogs={stateLogs} closeWindow={closeStateLog} /> |
| 298 | + <StateLog stateLogs={stateLogs} closeWindow={toggleStateLog} /> |
209 | 299 | </Pane> |
210 | 300 | {/if} |
211 | 301 | <Pane minSize={20}> |
|
229 | 319 | <i class="bx bx-dots-horizontal-rounded" /> |
230 | 320 | </DropdownToggle> |
231 | 321 | <DropdownMenu class="dropdown-menu-end"> |
232 | | - {#if !isLoadLog} |
233 | | - <DropdownItem on:click={() => viewContentLog()}>View Log</DropdownItem> |
| 322 | + {#if !isLoadContentLog} |
| 323 | + <DropdownItem on:click={() => toggleContentLog()}>View Log</DropdownItem> |
234 | 324 | {/if} |
235 | | - {#if !isLoadStates} |
236 | | - <DropdownItem on:click={() => viewStateLog()}>View States</DropdownItem> |
| 325 | + {#if !isLoadStateLog} |
| 326 | + <DropdownItem on:click={() => toggleStateLog()}>View States</DropdownItem> |
237 | 327 | {/if} |
238 | 328 | <DropdownItem on:click={newConversationHandler}>New Conversation</DropdownItem> |
239 | 329 | </DropdownMenu> |
|
270 | 360 | <i class="bx bx-dots-vertical-rounded" /> |
271 | 361 | </DropdownToggle> |
272 | 362 | <DropdownMenu class="dropdown-menu-end"> |
273 | | - <DropdownItem href="#">Edit</DropdownItem> |
274 | | - <DropdownItem href="#">Copy</DropdownItem> |
275 | | - <DropdownItem href="#">Delete</DropdownItem> |
| 363 | + <DropdownItem on:click={(e) => editMessage(e, message)}>Edit</DropdownItem> |
| 364 | + <DropdownItem on:click={(e) => copyMessage(e, message.text)}>Copy</DropdownItem> |
| 365 | + <DropdownItem on:click={(e) => deleteMessage(e, message.message_id)}>Delete</DropdownItem> |
276 | 366 | </DropdownMenu> |
277 | 367 | </Dropdown> |
278 | 368 | {:else} |
|
355 | 445 | </div> |
356 | 446 | </div> |
357 | 447 | </Pane> |
358 | | - {#if isLoadLog} |
| 448 | + {#if isLoadContentLog} |
359 | 449 | <Pane size={30} minSize={20} maxSize={50}> |
360 | | - <ContentLog logs={contentLogs} closeWindow={closeContentLog} /> |
| 450 | + <ContentLog logs={contentLogs} closeWindow={toggleContentLog} /> |
361 | 451 | </Pane> |
362 | 452 | {/if} |
363 | 453 | </Splitpanes> |
|
0 commit comments