Sidebar.svelte 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { v4 as uuidv4 } from 'uuid';
  4. import { goto } from '$app/navigation';
  5. import {
  6. user,
  7. chats,
  8. settings,
  9. showSettings,
  10. chatId,
  11. tags,
  12. showSidebar,
  13. mobile,
  14. showArchivedChats,
  15. pinnedChats,
  16. scrollPaginationEnabled,
  17. currentChatPage,
  18. temporaryChatEnabled
  19. } from '$lib/stores';
  20. import { onMount, getContext, tick, onDestroy } from 'svelte';
  21. const i18n = getContext('i18n');
  22. import {
  23. deleteChatById,
  24. getChatList,
  25. getAllTags,
  26. getChatListBySearchText,
  27. createNewChat,
  28. getPinnedChatList,
  29. toggleChatPinnedStatusById,
  30. getChatPinnedStatusById,
  31. getChatById,
  32. updateChatFolderIdById,
  33. importChat
  34. } from '$lib/apis/chats';
  35. import { WEBUI_BASE_URL } from '$lib/constants';
  36. import ArchivedChatsModal from './Sidebar/ArchivedChatsModal.svelte';
  37. import UserMenu from './Sidebar/UserMenu.svelte';
  38. import ChatItem from './Sidebar/ChatItem.svelte';
  39. import Spinner from '../common/Spinner.svelte';
  40. import Loader from '../common/Loader.svelte';
  41. import AddFilesPlaceholder from '../AddFilesPlaceholder.svelte';
  42. import SearchInput from './Sidebar/SearchInput.svelte';
  43. import Folder from '../common/Folder.svelte';
  44. import Plus from '../icons/Plus.svelte';
  45. import Tooltip from '../common/Tooltip.svelte';
  46. import { createNewFolder, getFolders, updateFolderParentIdById } from '$lib/apis/folders';
  47. import Folders from './Sidebar/Folders.svelte';
  48. const BREAKPOINT = 768;
  49. let navElement;
  50. let search = '';
  51. let shiftKey = false;
  52. let selectedChatId = null;
  53. let showDropdown = false;
  54. let showPinnedChat = true;
  55. // Pagination variables
  56. let chatListLoading = false;
  57. let allChatsLoaded = false;
  58. let folders = {};
  59. const initFolders = async () => {
  60. const folderList = await getFolders(localStorage.token).catch((error) => {
  61. toast.error(error);
  62. return [];
  63. });
  64. folders = {};
  65. // First pass: Initialize all folder entries
  66. for (const folder of folderList) {
  67. // Ensure folder is added to folders with its data
  68. folders[folder.id] = { ...(folders[folder.id] || {}), ...folder };
  69. }
  70. // Second pass: Tie child folders to their parents
  71. for (const folder of folderList) {
  72. if (folder.parent_id) {
  73. // Ensure the parent folder is initialized if it doesn't exist
  74. if (!folders[folder.parent_id]) {
  75. folders[folder.parent_id] = {}; // Create a placeholder if not already present
  76. }
  77. // Initialize childrenIds array if it doesn't exist and add the current folder id
  78. folders[folder.parent_id].childrenIds = folders[folder.parent_id].childrenIds
  79. ? [...folders[folder.parent_id].childrenIds, folder.id]
  80. : [folder.id];
  81. // Sort the children by updated_at field
  82. folders[folder.parent_id].childrenIds.sort((a, b) => {
  83. return folders[b].updated_at - folders[a].updated_at;
  84. });
  85. }
  86. }
  87. };
  88. const createFolder = async (name = 'Untitled') => {
  89. if (name === '') {
  90. toast.error($i18n.t('Folder name cannot be empty.'));
  91. return;
  92. }
  93. const rootFolders = Object.values(folders).filter((folder) => folder.parent_id === null);
  94. if (rootFolders.find((folder) => folder.name.toLowerCase() === name.toLowerCase())) {
  95. // If a folder with the same name already exists, append a number to the name
  96. let i = 1;
  97. while (
  98. rootFolders.find((folder) => folder.name.toLowerCase() === `${name} ${i}`.toLowerCase())
  99. ) {
  100. i++;
  101. }
  102. name = `${name} ${i}`;
  103. }
  104. // Add a dummy folder to the list to show the user that the folder is being created
  105. const tempId = uuidv4();
  106. folders = {
  107. ...folders,
  108. tempId: {
  109. id: tempId,
  110. name: name,
  111. created_at: Date.now(),
  112. updated_at: Date.now()
  113. }
  114. };
  115. const res = await createNewFolder(localStorage.token, name).catch((error) => {
  116. toast.error(error);
  117. return null;
  118. });
  119. if (res) {
  120. await initFolders();
  121. }
  122. };
  123. const initChatList = async () => {
  124. // Reset pagination variables
  125. tags.set(await getAllTags(localStorage.token));
  126. pinnedChats.set(await getPinnedChatList(localStorage.token));
  127. initFolders();
  128. currentChatPage.set(1);
  129. allChatsLoaded = false;
  130. if (search) {
  131. await chats.set(await getChatListBySearchText(localStorage.token, search, $currentChatPage));
  132. } else {
  133. await chats.set(await getChatList(localStorage.token, $currentChatPage));
  134. }
  135. // Enable pagination
  136. scrollPaginationEnabled.set(true);
  137. };
  138. const loadMoreChats = async () => {
  139. chatListLoading = true;
  140. currentChatPage.set($currentChatPage + 1);
  141. let newChatList = [];
  142. if (search) {
  143. newChatList = await getChatListBySearchText(localStorage.token, search, $currentChatPage);
  144. } else {
  145. newChatList = await getChatList(localStorage.token, $currentChatPage);
  146. }
  147. // once the bottom of the list has been reached (no results) there is no need to continue querying
  148. allChatsLoaded = newChatList.length === 0;
  149. await chats.set([...($chats ? $chats : []), ...newChatList]);
  150. chatListLoading = false;
  151. };
  152. let searchDebounceTimeout;
  153. const searchDebounceHandler = async () => {
  154. console.log('search', search);
  155. chats.set(null);
  156. if (searchDebounceTimeout) {
  157. clearTimeout(searchDebounceTimeout);
  158. }
  159. if (search === '') {
  160. await initChatList();
  161. return;
  162. } else {
  163. searchDebounceTimeout = setTimeout(async () => {
  164. allChatsLoaded = false;
  165. currentChatPage.set(1);
  166. await chats.set(await getChatListBySearchText(localStorage.token, search));
  167. if ($chats.length === 0) {
  168. tags.set(await getAllTags(localStorage.token));
  169. }
  170. }, 1000);
  171. }
  172. };
  173. const importChatHandler = async (items, pinned = false, folderId = null) => {
  174. console.log('importChatHandler', items, pinned, folderId);
  175. for (const item of items) {
  176. console.log(item);
  177. if (item.chat) {
  178. await importChat(localStorage.token, item.chat, item?.meta ?? {}, pinned, folderId);
  179. }
  180. }
  181. initChatList();
  182. };
  183. const inputFilesHandler = async (files) => {
  184. console.log(files);
  185. for (const file of files) {
  186. const reader = new FileReader();
  187. reader.onload = async (e) => {
  188. const content = e.target.result;
  189. try {
  190. const chatItems = JSON.parse(content);
  191. importChatHandler(chatItems);
  192. } catch {
  193. toast.error($i18n.t(`Invalid file format.`));
  194. }
  195. };
  196. reader.readAsText(file);
  197. }
  198. };
  199. const tagEventHandler = async (type, tagName, chatId) => {
  200. console.log(type, tagName, chatId);
  201. if (type === 'delete') {
  202. initChatList();
  203. } else if (type === 'add') {
  204. initChatList();
  205. }
  206. };
  207. let draggedOver = false;
  208. const onDragOver = (e) => {
  209. e.preventDefault();
  210. // Check if a file is being draggedOver.
  211. if (e.dataTransfer?.types?.includes('Files')) {
  212. draggedOver = true;
  213. } else {
  214. draggedOver = false;
  215. }
  216. };
  217. const onDragLeave = () => {
  218. draggedOver = false;
  219. };
  220. const onDrop = async (e) => {
  221. e.preventDefault();
  222. console.log(e); // Log the drop event
  223. // Perform file drop check and handle it accordingly
  224. if (e.dataTransfer?.files) {
  225. const inputFiles = Array.from(e.dataTransfer?.files);
  226. if (inputFiles && inputFiles.length > 0) {
  227. console.log(inputFiles); // Log the dropped files
  228. inputFilesHandler(inputFiles); // Handle the dropped files
  229. }
  230. }
  231. draggedOver = false; // Reset draggedOver status after drop
  232. };
  233. let touchstart;
  234. let touchend;
  235. function checkDirection() {
  236. const screenWidth = window.innerWidth;
  237. const swipeDistance = Math.abs(touchend.screenX - touchstart.screenX);
  238. if (touchstart.clientX < 40 && swipeDistance >= screenWidth / 8) {
  239. if (touchend.screenX < touchstart.screenX) {
  240. showSidebar.set(false);
  241. }
  242. if (touchend.screenX > touchstart.screenX) {
  243. showSidebar.set(true);
  244. }
  245. }
  246. }
  247. const onTouchStart = (e) => {
  248. touchstart = e.changedTouches[0];
  249. console.log(touchstart.clientX);
  250. };
  251. const onTouchEnd = (e) => {
  252. touchend = e.changedTouches[0];
  253. checkDirection();
  254. };
  255. const onKeyDown = (e) => {
  256. if (e.key === 'Shift') {
  257. shiftKey = true;
  258. }
  259. };
  260. const onKeyUp = (e) => {
  261. if (e.key === 'Shift') {
  262. shiftKey = false;
  263. }
  264. };
  265. const onFocus = () => {};
  266. const onBlur = () => {
  267. shiftKey = false;
  268. selectedChatId = null;
  269. };
  270. onMount(async () => {
  271. showPinnedChat = localStorage?.showPinnedChat ? localStorage.showPinnedChat === 'true' : true;
  272. mobile.subscribe((e) => {
  273. if ($showSidebar && e) {
  274. showSidebar.set(false);
  275. }
  276. if (!$showSidebar && !e) {
  277. showSidebar.set(true);
  278. }
  279. });
  280. showSidebar.set(!$mobile ? localStorage.sidebar === 'true' : false);
  281. showSidebar.subscribe((value) => {
  282. localStorage.sidebar = value;
  283. });
  284. await initChatList();
  285. window.addEventListener('keydown', onKeyDown);
  286. window.addEventListener('keyup', onKeyUp);
  287. window.addEventListener('touchstart', onTouchStart);
  288. window.addEventListener('touchend', onTouchEnd);
  289. window.addEventListener('focus', onFocus);
  290. window.addEventListener('blur', onBlur);
  291. const dropZone = document.getElementById('sidebar');
  292. dropZone?.addEventListener('dragover', onDragOver);
  293. dropZone?.addEventListener('drop', onDrop);
  294. dropZone?.addEventListener('dragleave', onDragLeave);
  295. });
  296. onDestroy(() => {
  297. window.removeEventListener('keydown', onKeyDown);
  298. window.removeEventListener('keyup', onKeyUp);
  299. window.removeEventListener('touchstart', onTouchStart);
  300. window.removeEventListener('touchend', onTouchEnd);
  301. window.removeEventListener('focus', onFocus);
  302. window.removeEventListener('blur', onBlur);
  303. const dropZone = document.getElementById('sidebar');
  304. dropZone?.removeEventListener('dragover', onDragOver);
  305. dropZone?.removeEventListener('drop', onDrop);
  306. dropZone?.removeEventListener('dragleave', onDragLeave);
  307. });
  308. </script>
  309. <ArchivedChatsModal
  310. bind:show={$showArchivedChats}
  311. on:change={async () => {
  312. await initChatList();
  313. }}
  314. />
  315. <!-- svelte-ignore a11y-no-static-element-interactions -->
  316. {#if $showSidebar}
  317. <div
  318. class=" fixed md:hidden z-40 top-0 right-0 left-0 bottom-0 bg-black/60 w-full min-h-screen h-screen flex justify-center overflow-hidden overscroll-contain"
  319. on:mousedown={() => {
  320. showSidebar.set(!$showSidebar);
  321. }}
  322. />
  323. {/if}
  324. <div
  325. bind:this={navElement}
  326. id="sidebar"
  327. class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar
  328. ? 'md:relative w-[260px] max-w-[260px]'
  329. : '-translate-x-[260px] w-[0px]'} bg-gray-50 text-gray-900 dark:bg-gray-950 dark:text-gray-200 text-sm transition fixed z-50 top-0 left-0 overflow-x-hidden
  330. "
  331. data-state={$showSidebar}
  332. >
  333. <div
  334. class="py-2.5 my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[260px] overflow-x-hidden z-50 {$showSidebar
  335. ? ''
  336. : 'invisible'}"
  337. >
  338. <div class="px-2.5 flex justify-between space-x-1 text-gray-600 dark:text-gray-400">
  339. <a
  340. id="sidebar-new-chat-button"
  341. class="flex flex-1 justify-between rounded-lg px-2 h-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  342. href="/"
  343. draggable="false"
  344. on:click={async () => {
  345. selectedChatId = null;
  346. await goto('/');
  347. const newChatButton = document.getElementById('new-chat-button');
  348. setTimeout(() => {
  349. newChatButton?.click();
  350. if ($mobile) {
  351. showSidebar.set(false);
  352. }
  353. }, 0);
  354. }}
  355. >
  356. <div class="self-center mx-1.5">
  357. <img
  358. crossorigin="anonymous"
  359. src="{WEBUI_BASE_URL}/static/favicon.png"
  360. class=" size-6 -translate-x-1.5 rounded-full"
  361. alt="logo"
  362. />
  363. </div>
  364. <div class=" self-center font-medium text-sm text-gray-850 dark:text-white font-primary">
  365. {$i18n.t('New Chat')}
  366. </div>
  367. <div class="self-center ml-auto">
  368. <svg
  369. xmlns="http://www.w3.org/2000/svg"
  370. viewBox="0 0 20 20"
  371. fill="currentColor"
  372. class="size-5"
  373. >
  374. <path
  375. d="M5.433 13.917l1.262-3.155A4 4 0 017.58 9.42l6.92-6.918a2.121 2.121 0 013 3l-6.92 6.918c-.383.383-.84.685-1.343.886l-3.154 1.262a.5.5 0 01-.65-.65z"
  376. />
  377. <path
  378. d="M3.5 5.75c0-.69.56-1.25 1.25-1.25H10A.75.75 0 0010 3H4.75A2.75 2.75 0 002 5.75v9.5A2.75 2.75 0 004.75 18h9.5A2.75 2.75 0 0017 15.25V10a.75.75 0 00-1.5 0v5.25c0 .69-.56 1.25-1.25 1.25h-9.5c-.69 0-1.25-.56-1.25-1.25v-9.5z"
  379. />
  380. </svg>
  381. </div>
  382. </a>
  383. <button
  384. class=" cursor-pointer px-2 py-2 flex rounded-lg hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  385. on:click={() => {
  386. showSidebar.set(!$showSidebar);
  387. }}
  388. >
  389. <div class=" m-auto self-center">
  390. <svg
  391. xmlns="http://www.w3.org/2000/svg"
  392. fill="none"
  393. viewBox="0 0 24 24"
  394. stroke-width="2"
  395. stroke="currentColor"
  396. class="size-5"
  397. >
  398. <path
  399. stroke-linecap="round"
  400. stroke-linejoin="round"
  401. d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25H12"
  402. />
  403. </svg>
  404. </div>
  405. </button>
  406. </div>
  407. {#if $user?.role === 'admin'}
  408. <div class="px-2.5 flex justify-center text-gray-800 dark:text-gray-200">
  409. <a
  410. class="flex-grow flex space-x-3 rounded-lg px-2.5 py-2 hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  411. href="/workspace"
  412. on:click={() => {
  413. selectedChatId = null;
  414. chatId.set('');
  415. if ($mobile) {
  416. showSidebar.set(false);
  417. }
  418. }}
  419. draggable="false"
  420. >
  421. <div class="self-center">
  422. <svg
  423. xmlns="http://www.w3.org/2000/svg"
  424. fill="none"
  425. viewBox="0 0 24 24"
  426. stroke-width="2"
  427. stroke="currentColor"
  428. class="size-[1.1rem]"
  429. >
  430. <path
  431. stroke-linecap="round"
  432. stroke-linejoin="round"
  433. d="M13.5 16.875h3.375m0 0h3.375m-3.375 0V13.5m0 3.375v3.375M6 10.5h2.25a2.25 2.25 0 0 0 2.25-2.25V6a2.25 2.25 0 0 0-2.25-2.25H6A2.25 2.25 0 0 0 3.75 6v2.25A2.25 2.25 0 0 0 6 10.5Zm0 9.75h2.25A2.25 2.25 0 0 0 10.5 18v-2.25a2.25 2.25 0 0 0-2.25-2.25H6a2.25 2.25 0 0 0-2.25 2.25V18A2.25 2.25 0 0 0 6 20.25Zm9.75-9.75H18a2.25 2.25 0 0 0 2.25-2.25V6A2.25 2.25 0 0 0 18 3.75h-2.25A2.25 2.25 0 0 0 13.5 6v2.25a2.25 2.25 0 0 0 2.25 2.25Z"
  434. />
  435. </svg>
  436. </div>
  437. <div class="flex self-center">
  438. <div class=" self-center font-medium text-sm font-primary">{$i18n.t('Workspace')}</div>
  439. </div>
  440. </a>
  441. </div>
  442. {/if}
  443. <div class="relative {$temporaryChatEnabled ? 'opacity-20' : ''}">
  444. {#if $temporaryChatEnabled}
  445. <div class="absolute z-40 w-full h-full flex justify-center"></div>
  446. {/if}
  447. <div class="absolute z-40 right-4 top-1">
  448. <Tooltip content={$i18n.t('New folder')}>
  449. <button
  450. class="p-1 rounded-lg bg-gray-50 hover:bg-gray-100 dark:bg-gray-950 dark:hover:bg-gray-900 transition"
  451. on:click={() => {
  452. createFolder();
  453. }}
  454. >
  455. <Plus />
  456. </button>
  457. </Tooltip>
  458. </div>
  459. <SearchInput
  460. bind:value={search}
  461. on:input={searchDebounceHandler}
  462. placeholder={$i18n.t('Search')}
  463. />
  464. </div>
  465. <div
  466. class="relative flex flex-col flex-1 overflow-y-auto {$temporaryChatEnabled
  467. ? 'opacity-20'
  468. : ''}"
  469. >
  470. {#if $temporaryChatEnabled}
  471. <div class="absolute z-40 w-full h-full flex justify-center"></div>
  472. {/if}
  473. {#if !search && $pinnedChats.length > 0}
  474. <div class="flex flex-col space-y-1 rounded-xl">
  475. <Folder
  476. className="px-2"
  477. bind:open={showPinnedChat}
  478. on:change={(e) => {
  479. localStorage.setItem('showPinnedChat', e.detail);
  480. console.log(e.detail);
  481. }}
  482. on:import={(e) => {
  483. importChatHandler(e.detail, true);
  484. }}
  485. on:drop={async (e) => {
  486. const { type, id, item } = e.detail;
  487. if (type === 'chat') {
  488. let chat = await getChatById(localStorage.token, id).catch((error) => {
  489. return null;
  490. });
  491. if (!chat && item) {
  492. chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
  493. }
  494. if (chat) {
  495. console.log(chat);
  496. if (chat.folder_id) {
  497. const res = await updateChatFolderIdById(
  498. localStorage.token,
  499. chat.id,
  500. null
  501. ).catch((error) => {
  502. toast.error(error);
  503. return null;
  504. });
  505. }
  506. if (!chat.pinned) {
  507. const res = await toggleChatPinnedStatusById(localStorage.token, id);
  508. }
  509. initChatList();
  510. }
  511. }
  512. }}
  513. name={$i18n.t('Pinned')}
  514. >
  515. <div
  516. class="ml-3 pl-1 mt-[1px] flex flex-col overflow-y-auto scrollbar-hidden border-s border-gray-100 dark:border-gray-900"
  517. >
  518. {#each $pinnedChats as chat, idx}
  519. <ChatItem
  520. className=""
  521. id={chat.id}
  522. title={chat.title}
  523. {shiftKey}
  524. selected={selectedChatId === chat.id}
  525. on:select={() => {
  526. selectedChatId = chat.id;
  527. }}
  528. on:unselect={() => {
  529. selectedChatId = null;
  530. }}
  531. on:change={async () => {
  532. initChatList();
  533. }}
  534. on:tag={(e) => {
  535. const { type, name } = e.detail;
  536. tagEventHandler(type, name, chat.id);
  537. }}
  538. />
  539. {/each}
  540. </div>
  541. </Folder>
  542. </div>
  543. {/if}
  544. <div class=" flex-1 flex flex-col overflow-y-auto scrollbar-hidden">
  545. {#if !search && folders}
  546. <Folders
  547. {folders}
  548. on:import={(e) => {
  549. const { folderId, items } = e.detail;
  550. importChatHandler(items, false, folderId);
  551. }}
  552. on:update={async (e) => {
  553. initChatList();
  554. }}
  555. on:change={async () => {
  556. initChatList();
  557. }}
  558. />
  559. {/if}
  560. <Folder
  561. collapsible={!search}
  562. className="px-2 mt-0.5"
  563. name={$i18n.t('All chats')}
  564. on:import={(e) => {
  565. importChatHandler(e.detail);
  566. }}
  567. on:drop={async (e) => {
  568. const { type, id, item } = e.detail;
  569. if (type === 'chat') {
  570. let chat = await getChatById(localStorage.token, id).catch((error) => {
  571. return null;
  572. });
  573. if (!chat && item) {
  574. chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
  575. }
  576. if (chat) {
  577. console.log(chat);
  578. if (chat.folder_id) {
  579. const res = await updateChatFolderIdById(localStorage.token, chat.id, null).catch(
  580. (error) => {
  581. toast.error(error);
  582. return null;
  583. }
  584. );
  585. }
  586. if (chat.pinned) {
  587. const res = await toggleChatPinnedStatusById(localStorage.token, id);
  588. }
  589. initChatList();
  590. }
  591. } else if (type === 'folder') {
  592. if (folders[id].parent_id === null) {
  593. return;
  594. }
  595. const res = await updateFolderParentIdById(localStorage.token, id, null).catch(
  596. (error) => {
  597. toast.error(error);
  598. return null;
  599. }
  600. );
  601. if (res) {
  602. await initFolders();
  603. }
  604. }
  605. }}
  606. >
  607. <div class="pt-1.5">
  608. {#if $chats}
  609. {#each $chats as chat, idx}
  610. {#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)}
  611. <div
  612. class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium {idx ===
  613. 0
  614. ? ''
  615. : 'pt-5'} pb-1.5"
  616. >
  617. {$i18n.t(chat.time_range)}
  618. <!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
  619. {$i18n.t('Today')}
  620. {$i18n.t('Yesterday')}
  621. {$i18n.t('Previous 7 days')}
  622. {$i18n.t('Previous 30 days')}
  623. {$i18n.t('January')}
  624. {$i18n.t('February')}
  625. {$i18n.t('March')}
  626. {$i18n.t('April')}
  627. {$i18n.t('May')}
  628. {$i18n.t('June')}
  629. {$i18n.t('July')}
  630. {$i18n.t('August')}
  631. {$i18n.t('September')}
  632. {$i18n.t('October')}
  633. {$i18n.t('November')}
  634. {$i18n.t('December')}
  635. -->
  636. </div>
  637. {/if}
  638. <ChatItem
  639. className=""
  640. id={chat.id}
  641. title={chat.title}
  642. {shiftKey}
  643. selected={selectedChatId === chat.id}
  644. on:select={() => {
  645. selectedChatId = chat.id;
  646. }}
  647. on:unselect={() => {
  648. selectedChatId = null;
  649. }}
  650. on:change={async () => {
  651. initChatList();
  652. }}
  653. on:tag={(e) => {
  654. const { type, name } = e.detail;
  655. tagEventHandler(type, name, chat.id);
  656. }}
  657. />
  658. {/each}
  659. {#if $scrollPaginationEnabled && !allChatsLoaded}
  660. <Loader
  661. on:visible={(e) => {
  662. if (!chatListLoading) {
  663. loadMoreChats();
  664. }
  665. }}
  666. >
  667. <div
  668. class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2"
  669. >
  670. <Spinner className=" size-4" />
  671. <div class=" ">Loading...</div>
  672. </div>
  673. </Loader>
  674. {/if}
  675. {:else}
  676. <div class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2">
  677. <Spinner className=" size-4" />
  678. <div class=" ">Loading...</div>
  679. </div>
  680. {/if}
  681. </div>
  682. </Folder>
  683. </div>
  684. </div>
  685. <div class="px-2">
  686. <div class="flex flex-col font-primary">
  687. {#if $user !== undefined}
  688. <UserMenu
  689. role={$user.role}
  690. on:show={(e) => {
  691. if (e.detail === 'archived-chat') {
  692. showArchivedChats.set(true);
  693. }
  694. }}
  695. >
  696. <button
  697. class=" flex items-center rounded-xl py-2.5 px-2.5 w-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  698. on:click={() => {
  699. showDropdown = !showDropdown;
  700. }}
  701. >
  702. <div class=" self-center mr-3">
  703. <img
  704. src={$user.profile_image_url}
  705. class=" max-w-[30px] object-cover rounded-full"
  706. alt="User profile"
  707. />
  708. </div>
  709. <div class=" self-center font-medium">{$user.name}</div>
  710. </button>
  711. </UserMenu>
  712. {/if}
  713. </div>
  714. </div>
  715. </div>
  716. </div>
  717. <style>
  718. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  719. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  720. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  721. visibility: visible;
  722. }
  723. .scrollbar-hidden::-webkit-scrollbar-thumb {
  724. visibility: hidden;
  725. }
  726. </style>