Skip to content

Commit a1c804d

Browse files
author
Jicheng Lu
committed
refine store
1 parent b992f37 commit a1c804d

File tree

7 files changed

+41
-33
lines changed

7 files changed

+41
-33
lines changed

src/lib/common/FileDropZone.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@
4242
let isSuccess = false;
4343
let reason = '';
4444
45+
const defaultSuccessMesssage = "Upload succeeded!";
46+
const defaultErrorMesssage = "Upload failed!";
47+
4548
$: {
4649
if (isLoading) {
4750
innerDropText = "Uploading...";
4851
disabled = true;
4952
} else if (isSuccess) {
50-
innerDropText = "Upload succeeded";
53+
innerDropText = defaultSuccessMesssage;
5154
disabled = true;
5255
} else if (isError) {
5356
innerDropText = reason;
@@ -281,7 +284,7 @@
281284
});
282285
283286
isError = true;
284-
reason = fileRejections[0]?.errors[0]?.message || "Upload failed";
287+
reason = fileRejections[0]?.errors[0]?.message || defaultErrorMesssage;
285288
setTimeout(() => {
286289
reason = '';
287290
isError = false;

src/lib/common/FileGallery.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414
/** @type {(args0: number) => void} */
1515
export let onDelete = () => {};
1616
17+
let isLite = false;
1718
const screenWidthThreshold = 500;
1819
20+
$: {
21+
isLite = Viewport.Width <= screenWidthThreshold;
22+
}
23+
1924
/**
2025
* @param {any} e
2126
* @param {number} idx
@@ -34,7 +39,7 @@
3439
<svelte:fragment slot="thumbnail">
3540
<div class="image-gallery-container">
3641
{#each files as file, idx (idx)}
37-
<div class={`gallery-item ${Viewport.Width <= screenWidthThreshold ? 'lite-gallery-item' : ''}`}>
42+
<div class={`gallery-item ${isLite ? 'lite-gallery-item' : ''}`}>
3843
<GalleryThumbnail style="width: 100%; height: 100%; display: flex;" id={idx}>
3944
<div class="gallery-item-wrapper">
4045
{#if needDelete}

src/lib/helpers/store.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,17 @@ export const conversationUserMessageStore = createConversationUserMessageStore()
134134

135135
const createConversationUserAttachmentStore = () => {
136136
return {
137-
reset: (conversationId) => {
138-
const key = buildAttachmentKey(conversationId);
139-
localStorage.removeItem(key);
137+
reset: () => {
138+
localStorage.removeItem(conversationUserAttachmentKey);
140139
},
141-
get: (conversationId) => {
142-
const key = buildAttachmentKey(conversationId);
143-
const json = localStorage.getItem(key);
140+
get: () => {
141+
const json = localStorage.getItem(conversationUserAttachmentKey);
144142
return json ? JSON.parse(json) : {};
145143
},
146-
put: (conversationId, value) => {
147-
const key = buildAttachmentKey(conversationId);
148-
localStorage.setItem(key, JSON.stringify(value));
144+
put: (value) => {
145+
localStorage.setItem(conversationUserAttachmentKey, JSON.stringify(value));
149146
}
150147
}
151148
};
152149

153-
function buildAttachmentKey(conversationId){
154-
return `${conversationUserAttachmentKey}_${conversationId}`;
155-
}
156-
157150
export const conversationUserAttachmentStore = createConversationUserAttachmentStore();

src/lib/scss/custom/components/_gallery.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
margin-top: 3px;
66

77
.gallery-item {
8-
width: 8em;
9-
height: 8em;
8+
width: 12em;
9+
height: 12em;
1010

1111
.gallery-item-wrapper {
1212
position: relative;

src/routes/(authentication)/login/+page.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import { onMount } from 'svelte';
3434
import {
3535
conversationSearchOptionStore,
36+
conversationUserAttachmentStore,
3637
conversationUserMessageStore,
3738
conversationUserStateStore
3839
} from '$lib/helpers/store';
@@ -96,6 +97,7 @@
9697
conversationUserStateStore.reset();
9798
conversationSearchOptionStore.reset();
9899
conversationUserMessageStore.reset();
100+
conversationUserAttachmentStore.reset();
99101
}
100102
</script>
101103

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
/** @type {import('$types').UserStateDetailModel[]} */
9494
let userAddStates = [];
9595
96+
/** @type {any[]} */
97+
let conversationFiles = [];
98+
9699
/** @type {boolean} */
97100
let isLoadContentLog = false;
98101
let isLoadStateLog = false;
@@ -146,8 +149,8 @@
146149
function initChatView() {
147150
isFrame = $page.url.searchParams.get('isFrame') === 'true';
148151
// initial condition
149-
isContentLogClosed = true;
150-
isStateLogClosed = true;
152+
isContentLogClosed = false;
153+
isStateLogClosed = false;
151154
resizeChatWindow();
152155
}
153156
@@ -332,9 +335,11 @@
332335
return new Promise((resolve, reject) => {
333336
sendMessageToHub(params.agentId, params.conversationId, msgText, messageData).then(res => {
334337
isSendingMsg = false;
338+
resetStorage();
335339
resolve(res);
336340
}).catch(err => {
337341
isSendingMsg = false;
342+
resetStorage();
338343
reject(err);
339344
});
340345
});
@@ -721,6 +726,11 @@
721726
stateChangeLogs = [];
722727
agentQueueChangeLogs = [];
723728
}
729+
730+
function resetStorage() {
731+
conversationUserAttachmentStore.reset();
732+
conversationFiles = [];
733+
}
724734
</script>
725735
726736
@@ -896,6 +906,7 @@
896906
<ChatAttachment
897907
displayComponents={message.message_id === lastBotMsg?.message_id}
898908
disabled={isSendingMsg || isThinking}
909+
bind:files={conversationFiles}
899910
/>
900911
<ChatImage message={message} />
901912
</div>

src/routes/chat/[agentId]/[conversationId]/chatImage/chat-attachment.svelte

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import FileDropZone from "$lib/common/FileDropZone.svelte";
33
import FileGallery from "$lib/common/FileGallery.svelte";
44
import { conversationUserAttachmentStore } from "$lib/helpers/store";
5-
import { page } from '$app/stores';
65
import { onMount } from "svelte";
76
87
/** @type {boolean} */
@@ -11,23 +10,20 @@
1110
/** @type {boolean} */
1211
export let disabled = false;
1312
13+
/** @type {any[]} */
14+
export let files = [];
15+
1416
onMount(() => {
15-
const conversationId = $page.params.conversationId;
16-
const savedAttachments = conversationUserAttachmentStore.get(conversationId);
17+
const savedAttachments = conversationUserAttachmentStore.get();
1718
files = savedAttachments.acceptedFiles || [];
1819
});
1920
20-
/** @type {any[]} */
21-
let files = [];
22-
2321
/** @param {any} e */
2422
async function handleFileDrop(e) {
2523
const { acceptedFiles, fileRejections } = e.detail;
26-
const conversationId = $page.params.conversationId;
27-
const savedAttachments = conversationUserAttachmentStore.get(conversationId);
24+
const savedAttachments = conversationUserAttachmentStore.get();
2825
const newAttachments = [...savedAttachments.acceptedFiles || [], ...acceptedFiles];
29-
conversationUserAttachmentStore.put(conversationId, {
30-
conversationId: conversationId,
26+
conversationUserAttachmentStore.put({
3127
acceptedFiles: newAttachments
3228
});
3329
files = newAttachments;
@@ -36,9 +32,7 @@
3632
/** @param {number} index */
3733
function deleteFile(index) {
3834
files = files?.filter((f, idx) => idx !== index) || [];
39-
const conversationId = $page.params.conversationId;
40-
conversationUserAttachmentStore.put(conversationId, {
41-
conversationId: conversationId,
35+
conversationUserAttachmentStore.put({
4236
acceptedFiles: files
4337
});
4438
}

0 commit comments

Comments
 (0)