ChatItem.svelte 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. </script>
  165. <ShareChatModal bind:show={showShareChatModal} chatId={id} />
  166. <DeleteConfirmDialog
  167. bind:show={showDeleteConfirm}
  168. title={$i18n.t('Delete chat?')}
  169. on:confirm={() => {
  170. deleteChatHandler(id);
  171. }}
  172. >
  173. <div class=" text-sm text-gray-500 flex-1 line-clamp-3">
  174. {$i18n.t('This will delete')} <span class=" font-semibold">{title}</span>.
  175. </div>
  176. </DeleteConfirmDialog>
  177. {#if dragged && x && y}
  178. <DragGhost {x} {y}>
  179. <div class=" bg-black/80 backdrop-blur-2xl px-2 py-1 rounded-lg w-fit max-w-40">
  180. <div class="flex items-center gap-1">
  181. <Document className=" size-[18px]" strokeWidth="2" />
  182. <div class=" text-xs text-white line-clamp-1">
  183. {title}
  184. </div>
  185. </div>
  186. </div>
  187. </DragGhost>
  188. {/if}
  189. <div bind:this={itemElement} class=" w-full {className} relative group" {draggable}>
  190. {#if confirmEdit}
  191. <div
  192. class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||
  193. confirmEdit
  194. ? 'bg-gray-200 dark:bg-gray-900'
  195. : selected
  196. ? 'bg-gray-100 dark:bg-gray-950'
  197. : 'group-hover:bg-gray-100 dark:group-hover:bg-gray-950'} whitespace-nowrap text-ellipsis"
  198. >
  199. <input
  200. use:focusEdit
  201. bind:value={chatTitle}
  202. id="chat-title-input-{id}"
  203. class=" bg-transparent w-full outline-hidden mr-10"
  204. />
  205. </div>
  206. {:else}
  207. <a
  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. href="/c/{id}"
  215. on:click={() => {
  216. dispatch('select');
  217. if ($mobile) {
  218. showSidebar.set(false);
  219. }
  220. }}
  221. on:dblclick={() => {
  222. chatTitle = title;
  223. confirmEdit = true;
  224. }}
  225. on:mouseenter={(e) => {
  226. mouseOver = true;
  227. }}
  228. on:mouseleave={(e) => {
  229. mouseOver = false;
  230. }}
  231. on:focus={(e) => {}}
  232. draggable="false"
  233. >
  234. <div class=" flex self-center flex-1 w-full">
  235. <div dir=auto class="text-left self-center overflow-hidden w-full h-[20px]">
  236. {title}
  237. </div>
  238. </div>
  239. </a>
  240. {/if}
  241. <!-- svelte-ignore a11y-no-static-element-interactions -->
  242. <div
  243. class="
  244. {id === $chatId || confirmEdit
  245. ? 'from-gray-200 dark:from-gray-900'
  246. : selected
  247. ? 'from-gray-100 dark:from-gray-950'
  248. : 'invisible group-hover:visible from-gray-100 dark:from-gray-950'}
  249. absolute {className === 'pr-2'
  250. ? 'right-[8px]'
  251. : 'right-0'} top-[4px] py-1 pr-0.5 mr-1.5 pl-5 bg-linear-to-l from-80%
  252. to-transparent"
  253. on:mouseenter={(e) => {
  254. mouseOver = true;
  255. }}
  256. on:mouseleave={(e) => {
  257. mouseOver = false;
  258. }}
  259. >
  260. {#if confirmEdit}
  261. <div
  262. class="flex self-center items-center space-x-1.5 z-10 translate-y-[0.5px] -translate-x-[0.5px]"
  263. >
  264. <Tooltip content={$i18n.t('Confirm')}>
  265. <button
  266. class=" self-center dark:hover:text-white transition"
  267. on:click={() => {
  268. editChatTitle(id, chatTitle);
  269. confirmEdit = false;
  270. chatTitle = '';
  271. }}
  272. >
  273. <Check className=" size-3.5" strokeWidth="2.5" />
  274. </button>
  275. </Tooltip>
  276. <Tooltip content={$i18n.t('Cancel')}>
  277. <button
  278. class=" self-center dark:hover:text-white transition"
  279. on:click={() => {
  280. confirmEdit = false;
  281. chatTitle = '';
  282. }}
  283. >
  284. <XMark strokeWidth="2.5" />
  285. </button>
  286. </Tooltip>
  287. </div>
  288. {:else if shiftKey && mouseOver}
  289. <div class=" flex items-center self-center space-x-1.5">
  290. <Tooltip content={$i18n.t('Archive')} className="flex items-center">
  291. <button
  292. class=" self-center dark:hover:text-white transition"
  293. on:click={() => {
  294. archiveChatHandler(id);
  295. }}
  296. type="button"
  297. >
  298. <ArchiveBox className="size-4 translate-y-[0.5px]" strokeWidth="2" />
  299. </button>
  300. </Tooltip>
  301. <Tooltip content={$i18n.t('Delete')}>
  302. <button
  303. class=" self-center dark:hover:text-white transition"
  304. on:click={() => {
  305. deleteChatHandler(id);
  306. }}
  307. type="button"
  308. >
  309. <GarbageBin strokeWidth="2" />
  310. </button>
  311. </Tooltip>
  312. </div>
  313. {:else}
  314. <div class="flex self-center space-x-1 z-10">
  315. <ChatMenu
  316. chatId={id}
  317. cloneChatHandler={() => {
  318. cloneChatHandler(id);
  319. }}
  320. shareHandler={() => {
  321. showShareChatModal = true;
  322. }}
  323. archiveChatHandler={() => {
  324. archiveChatHandler(id);
  325. }}
  326. renameHandler={async () => {
  327. chatTitle = title;
  328. confirmEdit = true;
  329. await tick();
  330. const input = document.getElementById(`chat-title-input-${id}`);
  331. if (input) {
  332. input.focus();
  333. }
  334. }}
  335. deleteHandler={() => {
  336. showDeleteConfirm = true;
  337. }}
  338. onClose={() => {
  339. dispatch('unselect');
  340. }}
  341. on:change={async () => {
  342. dispatch('change');
  343. }}
  344. on:tag={(e) => {
  345. dispatch('tag', e.detail);
  346. }}
  347. >
  348. <button
  349. aria-label="Chat Menu"
  350. class=" self-center dark:hover:text-white transition"
  351. on:click={() => {
  352. dispatch('select');
  353. }}
  354. >
  355. <svg
  356. xmlns="http://www.w3.org/2000/svg"
  357. viewBox="0 0 16 16"
  358. fill="currentColor"
  359. class="w-4 h-4"
  360. >
  361. <path
  362. 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"
  363. />
  364. </svg>
  365. </button>
  366. </ChatMenu>
  367. {#if id === $chatId}
  368. <!-- Shortcut support using "delete-chat-button" id -->
  369. <button
  370. id="delete-chat-button"
  371. class="hidden"
  372. on:click={() => {
  373. showDeleteConfirm = true;
  374. }}
  375. >
  376. <svg
  377. xmlns="http://www.w3.org/2000/svg"
  378. viewBox="0 0 16 16"
  379. fill="currentColor"
  380. class="w-4 h-4"
  381. >
  382. <path
  383. 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"
  384. />
  385. </svg>
  386. </button>
  387. {/if}
  388. </div>
  389. {/if}
  390. </div>
  391. </div>