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

458 lines
11 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,
tags
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';
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));
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
};
const focusEdit = async (node: HTMLInputElement) => {
node.focus();
};
2024-10-15 06:55:50 +00:00
let itemElement;
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
};
onMount(() => {
if (itemElement) {
// 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) {
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();
editChatTitle(id, chatTitle);
confirmEdit = false;
chatTitle = '';
} else if (e.key === 'Escape') {
e.preventDefault();
confirmEdit = false;
chatTitle = '';
}
};
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
2024-06-14 18:06:19 +00:00
? 'bg-gray-200 dark:bg-gray-900'
: 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-06-14 18:06:19 +00:00
>
<input
use:focusEdit
bind:value={chatTitle}
id="chat-title-input-{id}"
2025-02-16 03:27:25 +00:00
class=" bg-transparent w-full outline-hidden mr-10"
2025-03-18 17:23:30 +00:00
on:keydown={chatTitleInputKeydownHandler}
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
2024-06-14 18:06:19 +00:00
? 'bg-gray-200 dark:bg-gray-900'
: 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');
if ($mobile) {
showSidebar.set(false);
}
}}
on:dblclick={() => {
2024-10-17 04:05:03 +00:00
chatTitle = title;
2024-06-14 18:06:19 +00:00
confirmEdit = true;
}}
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
2024-06-14 18:06:19 +00:00
? 'from-gray-200 dark:from-gray-900'
: 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-02-16 03:27:25 +00:00
: 'right-0'} 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]"
>
<Tooltip content={$i18n.t('Confirm')}>
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
editChatTitle(id, chatTitle);
2024-06-14 20:02:07 +00:00
confirmEdit = false;
chatTitle = '';
}}
2024-06-14 18:06:19 +00:00
>
2024-10-17 04:05:03 +00:00
<Check className=" size-3.5" strokeWidth="2.5" />
2024-06-14 20:02:07 +00:00
</button>
</Tooltip>
<Tooltip content={$i18n.t('Cancel')}>
2024-06-14 20:02:07 +00:00
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
confirmEdit = false;
chatTitle = '';
}}
>
2024-10-17 04:05:03 +00:00
<XMark strokeWidth="2.5" />
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}
<div class="flex self-center space-x-1 z-10">
<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
}}
renameHandler={async () => {
2024-10-17 04:05:03 +00:00
chatTitle = title;
2024-06-14 18:06:19 +00:00
confirmEdit = true;
await tick();
const input = document.getElementById(`chat-title-input-${id}`);
if (input) {
input.focus();
}
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"
class=" self-center dark:hover:text-white transition"
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>