open-webui/src/lib/components/layout/Sidebar/ChatItem.svelte

550 lines
13 KiB
Svelte
Raw Normal View History

2024-06-14 18:06:19 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
2024-06-14 20:02:07 +00:00
import { goto, invalidate, invalidateAll } from '$app/navigation';
2024-10-15 06:55:50 +00:00
import { onMount, getContext, createEventDispatcher, tick, onDestroy } from 'svelte';
2024-06-14 18:06:19 +00:00
const i18n = getContext('i18n');
const dispatch = createEventDispatcher();
import {
archiveChatById,
cloneChatById,
deleteChatById,
2024-10-15 05:57:11 +00:00
getAllTags,
2024-10-27 04:25:48 +00:00
getChatById,
2024-06-14 18:06:19 +00:00
getChatList,
2024-07-02 06:08:01 +00:00
getChatListByTagName,
2024-10-11 06:22:53 +00:00
getPinnedChatList,
2024-06-14 18:06:19 +00:00
updateChatById
} from '$lib/apis/chats';
2024-09-26 01:25:44 +00:00
import {
chatId,
chatTitle as _chatTitle,
chats,
mobile,
pinnedChats,
showSidebar,
2024-10-15 05:57:11 +00:00
currentChatPage,
2025-07-12 23:50:35 +00:00
tags,
selectedFolder
2024-09-26 01:25:44 +00:00
} from '$lib/stores';
2024-06-14 18:06:19 +00:00
import ChatMenu from './ChatMenu.svelte';
2024-10-18 02:09:01 +00:00
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
2024-06-14 18:06:19 +00:00
import ShareChatModal from '$lib/components/chat/ShareChatModal.svelte';
2024-06-14 20:02:07 +00:00
import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte';
2024-10-15 06:55:50 +00:00
import DragGhost from '$lib/components/common/DragGhost.svelte';
2024-10-17 04:05:03 +00:00
import Check from '$lib/components/icons/Check.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
import Document from '$lib/components/icons/Document.svelte';
2025-05-03 14:16:32 +00:00
import Sparkles from '$lib/components/icons/Sparkles.svelte';
2025-05-05 06:36:01 +00:00
import { generateTitle } from '$lib/apis';
2024-10-17 04:05:03 +00:00
2024-10-17 07:17:50 +00:00
export let className = '';
2024-10-17 04:05:03 +00:00
export let id;
export let title;
2024-06-14 18:06:19 +00:00
2024-06-14 18:24:15 +00:00
export let selected = false;
2024-06-14 20:02:07 +00:00
export let shiftKey = false;
2024-10-27 04:25:48 +00:00
let chat = null;
2024-06-14 20:02:07 +00:00
let mouseOver = false;
2024-10-27 04:25:48 +00:00
let draggable = false;
$: if (mouseOver) {
loadChat();
}
const loadChat = async () => {
if (!chat) {
draggable = false;
chat = await getChatById(localStorage.token, id);
draggable = true;
}
};
2024-06-14 18:06:19 +00:00
let showShareChatModal = false;
let confirmEdit = false;
2024-10-17 04:05:03 +00:00
let chatTitle = title;
2024-06-14 18:06:19 +00:00
2024-09-26 01:25:44 +00:00
const editChatTitle = async (id, title) => {
if (title === '') {
2024-06-14 18:06:19 +00:00
toast.error($i18n.t('Title cannot be an empty string.'));
} else {
await updateChatById(localStorage.token, id, {
2024-09-26 01:25:44 +00:00
title: title
2024-06-14 18:06:19 +00:00
});
2024-08-04 14:58:08 +00:00
2024-09-26 01:25:44 +00:00
if (id === $chatId) {
_chatTitle.set(title);
}
2024-08-04 15:04:15 +00:00
currentChatPage.set(1);
2024-08-04 14:58:08 +00:00
await chats.set(await getChatList(localStorage.token, $currentChatPage));
2024-10-11 06:22:53 +00:00
await pinnedChats.set(await getPinnedChatList(localStorage.token));
2025-04-09 18:56:49 +00:00
dispatch('change');
2024-06-14 18:06:19 +00:00
}
};
const cloneChatHandler = async (id) => {
2025-01-28 20:49:06 +00:00
const res = await cloneChatById(
localStorage.token,
id,
$i18n.t('Clone of {{TITLE}}', {
TITLE: title
})
).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-06-14 18:06:19 +00:00
return null;
});
if (res) {
goto(`/c/${res.id}`);
2024-08-04 14:58:08 +00:00
2024-08-04 15:04:15 +00:00
currentChatPage.set(1);
2024-08-04 14:58:08 +00:00
await chats.set(await getChatList(localStorage.token, $currentChatPage));
2024-10-11 06:22:53 +00:00
await pinnedChats.set(await getPinnedChatList(localStorage.token));
2024-06-14 18:06:19 +00:00
}
};
2024-10-18 02:09:01 +00:00
const deleteChatHandler = async (id) => {
const res = await deleteChatById(localStorage.token, id).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-10-18 02:09:01 +00:00
return null;
});
if (res) {
tags.set(await getAllTags(localStorage.token));
if ($chatId === id) {
2024-12-16 07:52:25 +00:00
await goto('/');
2024-10-18 02:09:01 +00:00
await chatId.set('');
await tick();
}
dispatch('change');
}
};
2024-06-14 18:06:19 +00:00
const archiveChatHandler = async (id) => {
await archiveChatById(localStorage.token, id);
2024-10-18 02:09:01 +00:00
dispatch('change');
2024-06-14 18:06:19 +00:00
};
2024-10-15 06:55:50 +00:00
let itemElement;
2025-05-05 06:36:01 +00:00
let generating = false;
let ignoreBlur = false;
2025-05-05 06:16:34 +00:00
let doubleClicked = false;
2024-10-17 04:05:03 +00:00
let dragged = false;
2024-10-15 06:55:50 +00:00
let x = 0;
let y = 0;
const dragImage = new Image();
2024-10-15 07:35:14 +00:00
dragImage.src =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
2024-10-15 06:55:50 +00:00
const onDragStart = (event) => {
event.stopPropagation();
2024-10-15 06:55:50 +00:00
event.dataTransfer.setDragImage(dragImage, 0, 0);
2024-10-15 09:12:39 +00:00
// Set the data to be transferred
event.dataTransfer.setData(
'text/plain',
JSON.stringify({
2024-10-17 04:05:03 +00:00
type: 'chat',
2024-10-27 04:25:48 +00:00
id: id,
item: chat
2024-10-15 09:12:39 +00:00
})
);
2024-10-17 04:05:03 +00:00
dragged = true;
2024-10-15 06:55:50 +00:00
itemElement.style.opacity = '0.5'; // Optional: Visual cue to show it's being dragged
};
const onDrag = (event) => {
event.stopPropagation();
2024-10-15 06:55:50 +00:00
x = event.clientX;
y = event.clientY;
};
const onDragEnd = (event) => {
event.stopPropagation();
2024-10-15 06:55:50 +00:00
itemElement.style.opacity = '1'; // Reset visual cue after drag
2024-10-17 04:05:03 +00:00
dragged = false;
2024-10-15 06:55:50 +00:00
};
2025-07-22 19:44:10 +00:00
const onClickOutside = (event) => {
if (confirmEdit && !event.target.closest(`#chat-title-input-${id}`)) {
confirmEdit = false;
ignoreBlur = false;
chatTitle = '';
}
};
2024-10-15 06:55:50 +00:00
onMount(() => {
if (itemElement) {
2025-07-22 19:44:10 +00:00
document.addEventListener('click', onClickOutside, true);
2024-10-15 06:55:50 +00:00
// Event listener for when dragging starts
itemElement.addEventListener('dragstart', onDragStart);
// Event listener for when dragging occurs (optional)
itemElement.addEventListener('drag', onDrag);
// Event listener for when dragging ends
itemElement.addEventListener('dragend', onDragEnd);
}
});
onDestroy(() => {
if (itemElement) {
2025-07-22 19:44:10 +00:00
document.removeEventListener('click', onClickOutside, true);
2024-10-15 06:55:50 +00:00
itemElement.removeEventListener('dragstart', onDragStart);
itemElement.removeEventListener('drag', onDrag);
itemElement.removeEventListener('dragend', onDragEnd);
}
});
2024-10-18 02:09:01 +00:00
let showDeleteConfirm = false;
2025-03-18 17:23:30 +00:00
const chatTitleInputKeydownHandler = (e) => {
if (e.key === 'Enter') {
e.preventDefault();
2025-06-04 02:49:52 +00:00
setTimeout(() => {
2025-06-04 21:12:28 +00:00
const input = document.getElementById(`chat-title-input-${id}`);
if (input) input.blur();
}, 0);
} else if (e.key === 'Escape') {
e.preventDefault();
confirmEdit = false;
chatTitle = '';
}
};
2025-05-03 14:16:32 +00:00
2025-05-05 06:16:34 +00:00
const renameHandler = async () => {
chatTitle = title;
confirmEdit = true;
2025-05-03 14:16:32 +00:00
await tick();
2025-05-05 06:16:34 +00:00
setTimeout(() => {
const input = document.getElementById(`chat-title-input-${id}`);
if (input) input.focus();
}, 0);
2025-05-03 14:16:32 +00:00
};
2025-05-05 06:36:01 +00:00
const generateTitleHandler = async () => {
generating = true;
if (!chat) {
chat = await getChatById(localStorage.token, id);
}
const messages = (chat.chat?.messages ?? []).map((message) => {
return {
role: message.role,
content: message.content
};
});
const model = chat.chat.models.at(0) ?? chat.models.at(0) ?? '';
chatTitle = '';
const generatedTitle = await generateTitle(localStorage.token, model, messages).catch(
(error) => {
toast.error(`${error}`);
return null;
}
);
if (generatedTitle) {
if (generatedTitle !== title) {
editChatTitle(id, generatedTitle);
}
confirmEdit = false;
2025-05-06 22:06:18 +00:00
} else {
chatTitle = title;
2025-05-05 06:36:01 +00:00
}
2025-05-06 22:06:18 +00:00
2025-05-05 06:36:01 +00:00
generating = false;
};
2024-06-14 18:06:19 +00:00
</script>
2024-10-17 04:05:03 +00:00
<ShareChatModal bind:show={showShareChatModal} chatId={id} />
2024-06-14 18:06:19 +00:00
2024-10-18 02:09:01 +00:00
<DeleteConfirmDialog
bind:show={showDeleteConfirm}
title={$i18n.t('Delete chat?')}
on:confirm={() => {
deleteChatHandler(id);
}}
>
<div class=" text-sm text-gray-500 flex-1 line-clamp-3">
{$i18n.t('This will delete')} <span class=" font-semibold">{title}</span>.
</div>
</DeleteConfirmDialog>
2024-10-17 04:05:03 +00:00
{#if dragged && x && y}
2024-10-15 06:55:50 +00:00
<DragGhost {x} {y}>
2024-10-17 04:49:22 +00:00
<div class=" bg-black/80 backdrop-blur-2xl px-2 py-1 rounded-lg w-fit max-w-40">
2024-10-17 04:05:03 +00:00
<div class="flex items-center gap-1">
2024-10-17 04:49:22 +00:00
<Document className=" size-[18px]" strokeWidth="2" />
2024-10-15 09:12:39 +00:00
<div class=" text-xs text-white line-clamp-1">
2024-10-17 04:05:03 +00:00
{title}
2024-10-15 06:55:50 +00:00
</div>
</div>
</div>
</DragGhost>
{/if}
2025-03-05 01:40:32 +00:00
<div
bind:this={itemElement}
class=" w-full {className} relative group"
draggable={draggable && !confirmEdit}
>
2024-06-14 18:06:19 +00:00
{#if confirmEdit}
<div
2024-10-18 21:11:13 +00:00
class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||
2024-10-17 04:05:03 +00:00
confirmEdit
2025-07-12 23:43:30 +00:00
? 'bg-gray-100 dark:bg-gray-900'
2024-06-14 18:06:19 +00:00
: selected
2024-08-13 10:12:35 +00:00
? 'bg-gray-100 dark:bg-gray-950'
2025-05-05 06:36:01 +00:00
: 'group-hover:bg-gray-100 dark:group-hover:bg-gray-950'} whitespace-nowrap text-ellipsis relative {generating
? 'cursor-not-allowed'
: ''}"
2024-06-14 18:06:19 +00:00
>
<input
id="chat-title-input-{id}"
2025-05-03 14:16:32 +00:00
bind:value={chatTitle}
2025-02-16 03:27:25 +00:00
class=" bg-transparent w-full outline-hidden mr-10"
2025-05-05 06:36:01 +00:00
placeholder={generating ? $i18n.t('Generating...') : ''}
disabled={generating}
2025-03-18 17:23:30 +00:00
on:keydown={chatTitleInputKeydownHandler}
2025-05-05 06:16:34 +00:00
on:blur={async (e) => {
2025-08-11 13:13:22 +00:00
// check if target is generate button
if (ignoreBlur) {
ignoreBlur = false;
2025-08-11 13:13:22 +00:00
if (e.relatedTarget?.id === 'generate-title-button') {
generateTitleHandler();
}
2025-05-05 06:36:01 +00:00
return;
}
2025-05-05 06:16:34 +00:00
if (doubleClicked) {
e.preventDefault();
e.stopPropagation();
await tick();
setTimeout(() => {
const input = document.getElementById(`chat-title-input-${id}`);
if (input) input.focus();
}, 0);
doubleClicked = false;
return;
2025-05-03 14:16:32 +00:00
}
2025-05-05 06:16:34 +00:00
if (chatTitle !== title) {
editChatTitle(id, chatTitle);
}
confirmEdit = false;
chatTitle = '';
2025-05-03 14:16:32 +00:00
}}
2024-06-14 18:06:19 +00:00
/>
</div>
{:else}
<a
2024-10-18 21:11:13 +00:00
class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||
2024-10-17 04:05:03 +00:00
confirmEdit
2025-07-12 23:43:30 +00:00
? 'bg-gray-100 dark:bg-gray-900'
2024-06-14 18:06:19 +00:00
: selected
2024-08-13 10:12:35 +00:00
? 'bg-gray-100 dark:bg-gray-950'
: ' group-hover:bg-gray-100 dark:group-hover:bg-gray-950'} whitespace-nowrap text-ellipsis"
2024-10-17 04:05:03 +00:00
href="/c/{id}"
2024-06-14 18:06:19 +00:00
on:click={() => {
dispatch('select');
2025-07-12 23:50:35 +00:00
if (
$selectedFolder &&
!($selectedFolder?.items?.chats.map((chat) => chat.id) ?? []).includes(id)
) {
selectedFolder.set(null); // Reset selected folder if the chat is not in it
}
2024-06-14 18:06:19 +00:00
if ($mobile) {
showSidebar.set(false);
}
}}
2025-05-05 06:16:34 +00:00
on:dblclick={async (e) => {
e.preventDefault();
e.stopPropagation();
doubleClicked = true;
renameHandler();
2024-06-14 18:06:19 +00:00
}}
2024-06-14 20:02:07 +00:00
on:mouseenter={(e) => {
mouseOver = true;
}}
on:mouseleave={(e) => {
mouseOver = false;
2024-06-14 18:06:19 +00:00
}}
on:focus={(e) => {}}
draggable="false"
>
<div class=" flex self-center flex-1 w-full">
2025-02-18 02:51:40 +00:00
<div dir="auto" class="text-left self-center overflow-hidden w-full h-[20px]">
2024-10-17 04:05:03 +00:00
{title}
2024-06-14 18:06:19 +00:00
</div>
</div>
</a>
{/if}
2024-06-14 20:02:07 +00:00
<!-- svelte-ignore a11y-no-static-element-interactions -->
2024-06-14 18:06:19 +00:00
<div
class="
2024-10-17 04:05:03 +00:00
{id === $chatId || confirmEdit
2025-07-12 23:43:30 +00:00
? 'from-gray-100 dark:from-gray-900'
2024-06-14 18:06:19 +00:00
: selected
2024-08-13 10:12:35 +00:00
? 'from-gray-100 dark:from-gray-950'
: 'invisible group-hover:visible from-gray-100 dark:from-gray-950'}
2024-10-17 04:05:03 +00:00
absolute {className === 'pr-2'
? 'right-[8px]'
2025-05-03 14:16:32 +00:00
: 'right-1'} top-[4px] py-1 pr-0.5 mr-1.5 pl-5 bg-linear-to-l from-80%
2024-06-14 18:06:19 +00:00
to-transparent"
2024-06-14 20:02:07 +00:00
on:mouseenter={(e) => {
mouseOver = true;
}}
on:mouseleave={(e) => {
mouseOver = false;
}}
2024-06-14 18:06:19 +00:00
>
{#if confirmEdit}
2024-10-17 04:05:03 +00:00
<div
class="flex self-center items-center space-x-1.5 z-10 translate-y-[0.5px] -translate-x-[0.5px]"
>
2025-05-03 14:16:32 +00:00
<Tooltip content={$i18n.t('Generate')}>
2025-05-05 06:36:01 +00:00
<button
class=" self-center dark:hover:text-white transition disabled:cursor-not-allowed"
2025-05-05 06:36:01 +00:00
id="generate-title-button"
disabled={generating}
on:mouseenter={() => {
ignoreBlur = true;
}}
2025-05-05 06:36:01 +00:00
>
2025-05-03 14:16:32 +00:00
<Sparkles strokeWidth="2" />
2024-06-14 20:02:07 +00:00
</button>
</Tooltip>
</div>
{:else if shiftKey && mouseOver}
<div class=" flex items-center self-center space-x-1.5">
<Tooltip content={$i18n.t('Archive')} className="flex items-center">
2024-06-14 20:02:07 +00:00
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
2024-10-17 04:05:03 +00:00
archiveChatHandler(id);
2024-06-14 20:02:07 +00:00
}}
type="button"
>
<ArchiveBox className="size-4 translate-y-[0.5px]" strokeWidth="2" />
</button>
</Tooltip>
<Tooltip content={$i18n.t('Delete')}>
2024-06-14 20:02:07 +00:00
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
2024-10-18 02:09:01 +00:00
deleteChatHandler(id);
2024-06-14 20:02:07 +00:00
}}
type="button"
2024-06-14 18:06:19 +00:00
>
2024-06-14 20:02:07 +00:00
<GarbageBin strokeWidth="2" />
</button>
</Tooltip>
2024-06-14 18:06:19 +00:00
</div>
{:else}
2025-05-03 14:16:32 +00:00
<div class="flex self-center z-10 items-end">
2024-06-14 18:06:19 +00:00
<ChatMenu
2024-10-17 04:05:03 +00:00
chatId={id}
2024-06-14 18:06:19 +00:00
cloneChatHandler={() => {
2024-10-17 04:05:03 +00:00
cloneChatHandler(id);
2024-06-14 18:06:19 +00:00
}}
shareHandler={() => {
showShareChatModal = true;
}}
archiveChatHandler={() => {
2024-10-17 04:05:03 +00:00
archiveChatHandler(id);
2024-06-14 18:06:19 +00:00
}}
2025-05-05 06:16:34 +00:00
{renameHandler}
2024-06-14 18:06:19 +00:00
deleteHandler={() => {
2024-10-18 02:09:01 +00:00
showDeleteConfirm = true;
2024-06-14 18:06:19 +00:00
}}
onClose={() => {
2024-06-16 18:09:55 +00:00
dispatch('unselect');
2024-06-14 18:06:19 +00:00
}}
2024-07-02 06:08:01 +00:00
on:change={async () => {
2024-10-17 04:05:03 +00:00
dispatch('change');
2024-07-02 06:08:01 +00:00
}}
2024-10-11 06:43:08 +00:00
on:tag={(e) => {
dispatch('tag', e.detail);
}}
2024-06-14 18:06:19 +00:00
>
<button
aria-label="Chat Menu"
2025-05-03 14:16:32 +00:00
class=" self-center dark:hover:text-white transition m-0"
2024-06-14 18:06:19 +00:00
on:click={() => {
dispatch('select');
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
fill="currentColor"
class="w-4 h-4"
>
<path
d="M2 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM6.5 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM12.5 6.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z"
/>
</svg>
</button>
</ChatMenu>
2024-10-17 04:05:03 +00:00
{#if id === $chatId}
2024-06-14 18:06:19 +00:00
<!-- Shortcut support using "delete-chat-button" id -->
<button
id="delete-chat-button"
class="hidden"
on:click={() => {
showDeleteConfirm = true;
2024-06-14 18:06:19 +00:00
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
fill="currentColor"
class="w-4 h-4"
>
<path
d="M2 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM6.5 8a1.5 1.5 0 1 1 3 0 1.5 1.5 0 0 1-3 0ZM12.5 6.5a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z"
/>
</svg>
</button>
{/if}
</div>
{/if}
</div>
</div>