Skip to content

Commit dc35d20

Browse files
author
Jicheng Lu
committed
refine conversation search
1 parent 7202647 commit dc35d20

File tree

6 files changed

+222
-64
lines changed

6 files changed

+222
-64
lines changed

src/lib/common/Label.svelte

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<script>
2+
3+
/** @type {string} */
4+
export let text = "";
5+
export let className = "";
6+
export let style = "";
7+
/** @type {string | number} */
8+
export let index;
9+
/** @type {(args0: number | string) => void} */
10+
export let onClose = () => {};
11+
12+
/**
13+
* @param {any} e
14+
* @param {string | number} index
15+
*/
16+
function handleClose(e, index) {
17+
e.preventDefault();
18+
onClose && onClose(index);
19+
}
20+
</script>
21+
22+
<div class={`label-container ${className}`} style={`${style}`}>
23+
<button class="btn btn-primary btn-rounded btn-sm label-btn">
24+
<span class="d-none d-sm-inline-block me-2">
25+
{text}
26+
</span>
27+
<i
28+
class="mdi mdi-window-close label-close"
29+
role="button"
30+
tabindex="0"
31+
on:keydown={() => {}}
32+
on:click={(e) => handleClose(e, index)}
33+
/>
34+
</button>
35+
</div>
36+
37+
38+
<style>
39+
.label-container {
40+
display: inline-block;
41+
margin-top: 10px;
42+
margin-bottom: 10px;
43+
margin-right: 5px;
44+
}
45+
46+
.label-btn {
47+
cursor: default;
48+
font-size: 12px;
49+
}
50+
51+
.label-close {
52+
cursor: pointer;
53+
}
54+
</style>
Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<script>
22
import { Button, Input, Modal, ModalBody, ModalFooter, ModalHeader, Row, Form, FormGroup } from "@sveltestrap/sveltestrap";
33
import _ from "lodash";
4-
import { page } from '$app/stores';
5-
import { conversationUserStateStore } from "$lib/helpers/store";
64
75
/** @type {boolean} */
86
export let isOpen;
@@ -23,22 +21,25 @@
2321
export let cancel;
2422
2523
/** @type {number} */
26-
const limit = 10;
27-
const params = $page.params;
24+
export let limit = 10;
25+
26+
/** @type {string} */
27+
export let title = "Add states";
28+
29+
/** @type {boolean} */
30+
export let validateKey = true;
31+
export let validateValue = true;
2832
2933
/** @type {import('$types').UserStateDetailModel[]} */
30-
let states = [];
34+
export let states = [];
3135
3236
$: {
3337
if (isOpen) {
34-
const conversationUserStates = conversationUserStateStore.get();
35-
if (!!conversationUserStates && conversationUserStates.conversationId == params.conversationId && !!conversationUserStates.states) {
36-
states = [...conversationUserStates.states];
38+
if (states?.length > 0) {
39+
states = [...states];
3740
} else {
3841
states = [{ key: { data: '', isValid: true }, value: { data: '', isValid: true } }];
3942
}
40-
} else {
41-
states = [];
4243
}
4344
}
4445
@@ -52,29 +53,19 @@
5253
states = states.map(state => {
5354
const key = _.trim(state.key.data);
5455
const value = _.trim(state.value.data);
55-
if (!!!key) {
56+
if (!!!key && validateKey) {
5657
state.key.isValid = false;
58+
isValidForm = isValidForm && state.key.isValid;
5759
}
5860
59-
if (!!!value) {
61+
if (!!!value && validateValue) {
6062
state.value.isValid = false;
63+
isValidForm = isValidForm && state.value.isValid;
6164
}
62-
63-
isValidForm = isValidForm && state.key.isValid && state.value.isValid;
6465
return state;
6566
});
6667
6768
if (!isValidForm) return;
68-
69-
const cleanStates = states.map(state => {
70-
state.key.data = _.trim(state.key.data);
71-
state.value.data = _.trim(state.value.data);
72-
return state;
73-
});
74-
conversationUserStateStore.put({
75-
conversationId: params.conversationId,
76-
states: cleanStates
77-
});
7869
confirm && confirm();
7970
}
8071
@@ -89,7 +80,7 @@
8980
}
9081
9182
/** @param {number} index */
92-
function removeState(index) {
83+
function remove(index) {
9384
states = states.filter((x, idx) => idx !== index);
9485
}
9586
@@ -123,7 +114,7 @@
123114
</script>
124115
125116
<Modal class={className} fade size={size} isOpen={isOpen} toggle={() => toggleModal && toggleModal()}>
126-
<ModalHeader>Add states</ModalHeader>
117+
<ModalHeader>{title}</ModalHeader>
127118
<ModalBody>
128119
<Form class="state-form">
129120
{#each states as state, idx (idx)}
@@ -149,7 +140,7 @@
149140
<Button
150141
class="btn btn-sm btn-rounded"
151142
color="danger"
152-
on:click={() => removeState(idx)}
143+
on:click={() => remove(idx)}
153144
>
154145
<i class="mdi mdi-window-close"></i>
155146
</Button>

src/lib/helpers/store.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { browser } from '$app/environment';
44

55
const conversationKey = "conversation";
66
const conversationUserStatesKey = "conversation_user_states";
7+
const conversationSearchStatesKey = "conversation_search_states";
78
const conversationUserMessageKey = "conversation_user_messages";
89

910
/** @type {Writable<import('$types').UserModel>} */
@@ -94,6 +95,23 @@ const createConversationUserStateStore = () => {
9495

9596
export const conversationUserStateStore = createConversationUserStateStore();
9697

98+
const createConversationSearchStateStore = () => {
99+
return {
100+
reset: () => {
101+
localStorage.removeItem(conversationSearchStatesKey);
102+
},
103+
get: () => {
104+
const json = localStorage.getItem(conversationSearchStatesKey);
105+
return json ? JSON.parse(json) : [];
106+
},
107+
put: (value) => {
108+
localStorage.setItem(conversationSearchStatesKey, JSON.stringify(value));
109+
}
110+
}
111+
};
112+
113+
export const conversationSearchStateStore = createConversationSearchStateStore();
114+
97115

98116
const createConversationUserMessageStore = () => {
99117
return {

src/lib/scss/custom/pages/_chat.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@
412412

413413
.state-form {
414414
padding-left: 25px;
415+
overflow-y: auto;
416+
overflow-x: hidden;
417+
max-height: 800px;
418+
scrollbar-width: thin;
415419

416420
.state-input {
417421
flex: 0.45;

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
import _ from "lodash";
3030
import { Pane, Splitpanes } from 'svelte-splitpanes';
3131
import StateLog from './stateLogs/state-log.svelte';
32-
import StateModal from './addStateModal/state-modal.svelte';
3332
import ChatImage from './chatImage/chat-image.svelte';
3433
import Swal from 'sweetalert2/dist/sweetalert2.js';
3534
import "sweetalert2/src/sweetalert2.scss";
3635
import moment from 'moment';
36+
import StateModal from '$lib/common/StateModal.svelte';
3737
3838
const options = {
3939
scrollbars: {
@@ -81,13 +81,16 @@
8181
/** @type {import('$types').ConversationStateLogModel[]} */
8282
let stateLogs = [];
8383
84+
/** @type {import('$types').UserStateDetailModel[]} */
85+
let userAddStates = [];
86+
8487
/** @type {boolean} */
8588
let isLoadContentLog = false;
8689
let isLoadStateLog = false;
8790
let isContentLogClosed = false; // initial condition
8891
let isStateLogClosed = false; // initial condition
8992
let isOpenEditMsgModal = false;
90-
let isOpenAddStateModal = false;
93+
let isOpenUserAddStateModal = false;
9194
let isSendingMsg = false;
9295
let isThinking = false;
9396
let isLite = false;
@@ -120,7 +123,7 @@
120123
isLoadContentLog = false;
121124
isLoadStateLog = false;
122125
isOpenEditMsgModal = false;
123-
isOpenAddStateModal = false;
126+
isOpenUserAddStateModal = false;
124127
}
125128
}
126129
@@ -416,8 +419,31 @@
416419
stateLogs = [];
417420
}
418421
419-
function toggleAddStateModal() {
420-
isOpenAddStateModal = !isOpenAddStateModal;
422+
function toggleUserAddStateModal() {
423+
isOpenUserAddStateModal = !isOpenUserAddStateModal;
424+
if (isOpenUserAddStateModal) {
425+
loadAddStates();
426+
}
427+
}
428+
429+
function loadAddStates() {
430+
const conversationUserStates = conversationUserStateStore.get();
431+
if (!!conversationUserStates && conversationUserStates.conversationId == params.conversationId && !!conversationUserStates.states) {
432+
userAddStates = [...conversationUserStates.states];
433+
}
434+
}
435+
436+
function handleConfirmUserAddStates() {
437+
const cleanStates = userAddStates.map(state => {
438+
state.key.data = _.trim(state.key.data);
439+
state.value.data = _.trim(state.value.data);
440+
return state;
441+
});
442+
conversationUserStateStore.put({
443+
conversationId: params.conversationId,
444+
states: cleanStates
445+
});
446+
toggleUserAddStateModal();
421447
}
422448
423449
function clearAddedStates() {
@@ -619,7 +645,6 @@
619645
<svelte:window on:resize={() => resizeChatWindow()}/>
620646
621647
<DialogModal
622-
className="custom-modal"
623648
title={'Edit message'}
624649
isOpen={isOpenEditMsgModal}
625650
toggleModal={toggleEditMsgModal}
@@ -631,11 +656,11 @@
631656
</DialogModal>
632657
633658
<StateModal
634-
className="custom-modal"
635-
isOpen={isOpenAddStateModal}
636-
toggleModal={toggleAddStateModal}
637-
confirm={toggleAddStateModal}
638-
cancel={toggleAddStateModal}
659+
isOpen={isOpenUserAddStateModal}
660+
bind:states={userAddStates}
661+
toggleModal={toggleUserAddStateModal}
662+
confirm={handleConfirmUserAddStates}
663+
cancel={toggleUserAddStateModal}
639664
/>
640665
641666
<HeadTitle title="Chat" addOn='' />
@@ -683,7 +708,7 @@
683708
{#if !isLite && !isLoadContentLog}
684709
<DropdownItem on:click={() => toggleContentLog()}>View Log</DropdownItem>
685710
{/if}
686-
{#if !isLite && (!isLoadStateLog || !isOpenAddStateModal)}
711+
{#if !isLite && (!isLoadStateLog || !isOpenUserAddStateModal)}
687712
<li>
688713
<Dropdown direction="right" class="state-menu">
689714
<DropdownToggle caret class="dropdown-item">
@@ -693,8 +718,8 @@
693718
{#if !isLoadStateLog}
694719
<DropdownItem on:click={() => toggleStateLog()}>View States</DropdownItem>
695720
{/if}
696-
{#if !isOpenAddStateModal}
697-
<DropdownItem on:click={() => toggleAddStateModal()}>Add States</DropdownItem>
721+
{#if !isOpenUserAddStateModal}
722+
<DropdownItem on:click={() => toggleUserAddStateModal()}>Add States</DropdownItem>
698723
{/if}
699724
<DropdownItem on:click={() => clearAddedStates()}>Clear States</DropdownItem>
700725
</DropdownMenu>

0 commit comments

Comments
 (0)