refac: knowledge file handling ui behaviour

This commit is contained in:
Timothy J. Baek 2024-10-07 14:04:06 -07:00
parent 48e7f47558
commit 958d882ff9
2 changed files with 19 additions and 2 deletions

View File

@ -94,7 +94,7 @@
: (user?.profile_image_url ?? '/user.png')} : (user?.profile_image_url ?? '/user.png')}
/> />
{/if} {/if}
<div class="w-full w-0 pl-1"> <div class="flex-auto w-0 max-w-full pl-1">
{#if !($settings?.chatBubble ?? true)} {#if !($settings?.chatBubble ?? true)}
<div> <div>
<Name> <Name>

View File

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import Fuse from 'fuse.js'; import Fuse from 'fuse.js';
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
import { v4 as uuidv4 } from 'uuid';
import { onMount, getContext, onDestroy, tick } from 'svelte'; import { onMount, getContext, onDestroy, tick } from 'svelte';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
@ -101,6 +102,7 @@
const uploadFileHandler = async (file) => { const uploadFileHandler = async (file) => {
console.log(file); console.log(file);
const tempItemId = uuidv4();
const fileItem = { const fileItem = {
type: 'file', type: 'file',
file: '', file: '',
@ -109,7 +111,8 @@
name: file.name, name: file.name,
size: file.size, size: file.size,
status: 'uploading', status: 'uploading',
error: '' error: '',
itemId: tempItemId
}; };
knowledge.files = [...(knowledge.files ?? []), fileItem]; knowledge.files = [...(knowledge.files ?? []), fileItem];
@ -131,10 +134,20 @@
try { try {
const uploadedFile = await uploadFile(localStorage.token, file).catch((e) => { const uploadedFile = await uploadFile(localStorage.token, file).catch((e) => {
toast.error(e); toast.error(e);
return null;
}); });
if (uploadedFile) { if (uploadedFile) {
console.log(uploadedFile); console.log(uploadedFile);
knowledge.files = knowledge.files.map((item) => {
if (item.itemId === tempItemId) {
item.id = uploadedFile.id;
}
// Remove temporary item id
delete item.itemId;
return item;
});
await addFileHandler(uploadedFile.id); await addFileHandler(uploadedFile.id);
} else { } else {
toast.error($i18n.t('Failed to upload file.')); toast.error($i18n.t('Failed to upload file.'));
@ -329,12 +342,16 @@
const updatedKnowledge = await addFileToKnowledgeById(localStorage.token, id, fileId).catch( const updatedKnowledge = await addFileToKnowledgeById(localStorage.token, id, fileId).catch(
(e) => { (e) => {
toast.error(e); toast.error(e);
return null;
} }
); );
if (updatedKnowledge) { if (updatedKnowledge) {
knowledge = updatedKnowledge; knowledge = updatedKnowledge;
toast.success($i18n.t('File added successfully.')); toast.success($i18n.t('File added successfully.'));
} else {
toast.error($i18n.t('Failed to add file.'));
knowledge.files = knowledge.files.filter((file) => file.id !== fileId);
} }
}; };