Spaces:
Running
Running
File size: 10,769 Bytes
81e42f7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
<script lang="ts">
import { toast } from 'svelte-sonner';
import { goto, invalidate, invalidateAll } from '$app/navigation';
import { onMount, getContext, createEventDispatcher, tick, onDestroy } from 'svelte';
const i18n = getContext('i18n');
const dispatch = createEventDispatcher();
import {
archiveChatById,
cloneChatById,
deleteChatById,
getAllTags,
getChatById,
getChatList,
getChatListByTagName,
getPinnedChatList,
updateChatById
} from '$lib/apis/chats';
import {
chatId,
chatTitle as _chatTitle,
chats,
mobile,
pinnedChats,
showSidebar,
currentChatPage,
tags
} from '$lib/stores';
import ChatMenu from './ChatMenu.svelte';
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import ShareChatModal from '$lib/components/chat/ShareChatModal.svelte';
import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte';
import DragGhost from '$lib/components/common/DragGhost.svelte';
import Check from '$lib/components/icons/Check.svelte';
import XMark from '$lib/components/icons/XMark.svelte';
import Document from '$lib/components/icons/Document.svelte';
export let className = '';
export let id;
export let title;
export let selected = false;
export let shiftKey = false;
let chat = null;
let mouseOver = false;
let draggable = false;
$: if (mouseOver) {
loadChat();
}
const loadChat = async () => {
if (!chat) {
draggable = false;
chat = await getChatById(localStorage.token, id);
draggable = true;
}
};
let showShareChatModal = false;
let confirmEdit = false;
let chatTitle = title;
const editChatTitle = async (id, title) => {
if (title === '') {
toast.error($i18n.t('Title cannot be an empty string.'));
} else {
await updateChatById(localStorage.token, id, {
title: title
});
if (id === $chatId) {
_chatTitle.set(title);
}
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getPinnedChatList(localStorage.token));
}
};
const cloneChatHandler = async (id) => {
const res = await cloneChatById(
localStorage.token,
id,
$i18n.t('Clone of {{TITLE}}', {
TITLE: title
})
).catch((error) => {
toast.error(`${error}`);
return null;
});
if (res) {
goto(`/c/${res.id}`);
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getPinnedChatList(localStorage.token));
}
};
const deleteChatHandler = async (id) => {
const res = await deleteChatById(localStorage.token, id).catch((error) => {
toast.error(`${error}`);
return null;
});
if (res) {
tags.set(await getAllTags(localStorage.token));
if ($chatId === id) {
await goto('/');
await chatId.set('');
await tick();
}
dispatch('change');
}
};
const archiveChatHandler = async (id) => {
await archiveChatById(localStorage.token, id);
dispatch('change');
};
const focusEdit = async (node: HTMLInputElement) => {
node.focus();
};
let itemElement;
let dragged = false;
let x = 0;
let y = 0;
const dragImage = new Image();
dragImage.src =
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=';
const onDragStart = (event) => {
event.stopPropagation();
event.dataTransfer.setDragImage(dragImage, 0, 0);
// Set the data to be transferred
event.dataTransfer.setData(
'text/plain',
JSON.stringify({
type: 'chat',
id: id,
item: chat
})
);
dragged = true;
itemElement.style.opacity = '0.5'; // Optional: Visual cue to show it's being dragged
};
const onDrag = (event) => {
event.stopPropagation();
x = event.clientX;
y = event.clientY;
};
const onDragEnd = (event) => {
event.stopPropagation();
itemElement.style.opacity = '1'; // Reset visual cue after drag
dragged = false;
};
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);
}
});
let showDeleteConfirm = false;
</script>
<ShareChatModal bind:show={showShareChatModal} chatId={id} />
<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>
{#if dragged && x && y}
<DragGhost {x} {y}>
<div class=" bg-black/80 backdrop-blur-2xl px-2 py-1 rounded-lg w-fit max-w-40">
<div class="flex items-center gap-1">
<Document className=" size-[18px]" strokeWidth="2" />
<div class=" text-xs text-white line-clamp-1">
{title}
</div>
</div>
</div>
</DragGhost>
{/if}
<div bind:this={itemElement} class=" w-full {className} relative group" {draggable}>
{#if confirmEdit}
<div
class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||
confirmEdit
? 'bg-gray-200 dark:bg-gray-900'
: selected
? 'bg-gray-100 dark:bg-gray-950'
: 'group-hover:bg-gray-100 dark:group-hover:bg-gray-950'} whitespace-nowrap text-ellipsis"
>
<input
use:focusEdit
bind:value={chatTitle}
id="chat-title-input-{id}"
class=" bg-transparent w-full outline-hidden mr-10"
/>
</div>
{:else}
<a
class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||
confirmEdit
? 'bg-gray-200 dark:bg-gray-900'
: selected
? 'bg-gray-100 dark:bg-gray-950'
: ' group-hover:bg-gray-100 dark:group-hover:bg-gray-950'} whitespace-nowrap text-ellipsis"
href="/c/{id}"
on:click={() => {
dispatch('select');
if ($mobile) {
showSidebar.set(false);
}
}}
on:dblclick={() => {
chatTitle = title;
confirmEdit = true;
}}
on:mouseenter={(e) => {
mouseOver = true;
}}
on:mouseleave={(e) => {
mouseOver = false;
}}
on:focus={(e) => {}}
draggable="false"
>
<div class=" flex self-center flex-1 w-full">
<div dir="auto" class="text-left self-center overflow-hidden w-full h-[20px]">
{title}
</div>
</div>
</a>
{/if}
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="
{id === $chatId || confirmEdit
? 'from-gray-200 dark:from-gray-900'
: selected
? 'from-gray-100 dark:from-gray-950'
: 'invisible group-hover:visible from-gray-100 dark:from-gray-950'}
absolute {className === 'pr-2'
? 'right-[8px]'
: 'right-0'} top-[4px] py-1 pr-0.5 mr-1.5 pl-5 bg-linear-to-l from-80%
to-transparent"
on:mouseenter={(e) => {
mouseOver = true;
}}
on:mouseleave={(e) => {
mouseOver = false;
}}
>
{#if confirmEdit}
<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')}>
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
editChatTitle(id, chatTitle);
confirmEdit = false;
chatTitle = '';
}}
>
<Check className=" size-3.5" strokeWidth="2.5" />
</button>
</Tooltip>
<Tooltip content={$i18n.t('Cancel')}>
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
confirmEdit = false;
chatTitle = '';
}}
>
<XMark strokeWidth="2.5" />
</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">
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
archiveChatHandler(id);
}}
type="button"
>
<ArchiveBox className="size-4 translate-y-[0.5px]" strokeWidth="2" />
</button>
</Tooltip>
<Tooltip content={$i18n.t('Delete')}>
<button
class=" self-center dark:hover:text-white transition"
on:click={() => {
deleteChatHandler(id);
}}
type="button"
>
<GarbageBin strokeWidth="2" />
</button>
</Tooltip>
</div>
{:else}
<div class="flex self-center space-x-1 z-10">
<ChatMenu
chatId={id}
cloneChatHandler={() => {
cloneChatHandler(id);
}}
shareHandler={() => {
showShareChatModal = true;
}}
archiveChatHandler={() => {
archiveChatHandler(id);
}}
renameHandler={async () => {
chatTitle = title;
confirmEdit = true;
await tick();
const input = document.getElementById(`chat-title-input-${id}`);
if (input) {
input.focus();
}
}}
deleteHandler={() => {
showDeleteConfirm = true;
}}
onClose={() => {
dispatch('unselect');
}}
on:change={async () => {
dispatch('change');
}}
on:tag={(e) => {
dispatch('tag', e.detail);
}}
>
<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>
{#if id === $chatId}
<!-- Shortcut support using "delete-chat-button" id -->
<button
id="delete-chat-button"
class="hidden"
on:click={() => {
showDeleteConfirm = true;
}}
>
<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>
|