ChatItem.svelte 11 KB

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