ChatItem.svelte 11 KB

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