Sidebar.svelte 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. if (item.chat) {
  177. await importChat(localStorage.token, item.chat, pinned, folderId);
  178. }
  179. }
  180. initChatList();
  181. };
  182. const inputFilesHandler = async (files) => {
  183. console.log(files);
  184. for (const file of files) {
  185. const reader = new FileReader();
  186. reader.onload = async (e) => {
  187. const content = e.target.result;
  188. try {
  189. const chatItems = JSON.parse(content);
  190. importChatHandler(chatItems);
  191. } catch {
  192. toast.error($i18n.t(`Invalid file format.`));
  193. }
  194. };
  195. reader.readAsText(file);
  196. }
  197. };
  198. const tagEventHandler = async (type, tagName, chatId) => {
  199. console.log(type, tagName, chatId);
  200. if (type === 'delete') {
  201. initChatList();
  202. } else if (type === 'add') {
  203. initChatList();
  204. }
  205. };
  206. let draggedOver = false;
  207. const onDragOver = (e) => {
  208. e.preventDefault();
  209. // Check if a file is being draggedOver.
  210. if (e.dataTransfer?.types?.includes('Files')) {
  211. draggedOver = true;
  212. } else {
  213. draggedOver = false;
  214. }
  215. };
  216. const onDragLeave = () => {
  217. draggedOver = false;
  218. };
  219. const onDrop = async (e) => {
  220. e.preventDefault();
  221. console.log(e); // Log the drop event
  222. // Perform file drop check and handle it accordingly
  223. if (e.dataTransfer?.files) {
  224. const inputFiles = Array.from(e.dataTransfer?.files);
  225. if (inputFiles && inputFiles.length > 0) {
  226. console.log(inputFiles); // Log the dropped files
  227. inputFilesHandler(inputFiles); // Handle the dropped files
  228. }
  229. }
  230. draggedOver = false; // Reset draggedOver status after drop
  231. };
  232. let touchstart;
  233. let touchend;
  234. function checkDirection() {
  235. const screenWidth = window.innerWidth;
  236. const swipeDistance = Math.abs(touchend.screenX - touchstart.screenX);
  237. if (touchstart.clientX < 40 && swipeDistance >= screenWidth / 8) {
  238. if (touchend.screenX < touchstart.screenX) {
  239. showSidebar.set(false);
  240. }
  241. if (touchend.screenX > touchstart.screenX) {
  242. showSidebar.set(true);
  243. }
  244. }
  245. }
  246. const onTouchStart = (e) => {
  247. touchstart = e.changedTouches[0];
  248. console.log(touchstart.clientX);
  249. };
  250. const onTouchEnd = (e) => {
  251. touchend = e.changedTouches[0];
  252. checkDirection();
  253. };
  254. const onKeyDown = (e) => {
  255. if (e.key === 'Shift') {
  256. shiftKey = true;
  257. }
  258. };
  259. const onKeyUp = (e) => {
  260. if (e.key === 'Shift') {
  261. shiftKey = false;
  262. }
  263. };
  264. const onFocus = () => {};
  265. const onBlur = () => {
  266. shiftKey = false;
  267. selectedChatId = null;
  268. };
  269. onMount(async () => {
  270. showPinnedChat = localStorage?.showPinnedChat ? localStorage.showPinnedChat === 'true' : true;
  271. mobile.subscribe((e) => {
  272. if ($showSidebar && e) {
  273. showSidebar.set(false);
  274. }
  275. if (!$showSidebar && !e) {
  276. showSidebar.set(true);
  277. }
  278. });
  279. showSidebar.set(!$mobile ? localStorage.sidebar === 'true' : false);
  280. showSidebar.subscribe((value) => {
  281. localStorage.sidebar = value;
  282. });
  283. await initChatList();
  284. window.addEventListener('keydown', onKeyDown);
  285. window.addEventListener('keyup', onKeyUp);
  286. window.addEventListener('touchstart', onTouchStart);
  287. window.addEventListener('touchend', onTouchEnd);
  288. window.addEventListener('focus', onFocus);
  289. window.addEventListener('blur', onBlur);
  290. const dropZone = document.getElementById('sidebar');
  291. dropZone?.addEventListener('dragover', onDragOver);
  292. dropZone?.addEventListener('drop', onDrop);
  293. dropZone?.addEventListener('dragleave', onDragLeave);
  294. });
  295. onDestroy(() => {
  296. window.removeEventListener('keydown', onKeyDown);
  297. window.removeEventListener('keyup', onKeyUp);
  298. window.removeEventListener('touchstart', onTouchStart);
  299. window.removeEventListener('touchend', onTouchEnd);
  300. window.removeEventListener('focus', onFocus);
  301. window.removeEventListener('blur', onBlur);
  302. const dropZone = document.getElementById('sidebar');
  303. dropZone?.removeEventListener('dragover', onDragOver);
  304. dropZone?.removeEventListener('drop', onDrop);
  305. dropZone?.removeEventListener('dragleave', onDragLeave);
  306. });
  307. </script>
  308. <ArchivedChatsModal
  309. bind:show={$showArchivedChats}
  310. on:change={async () => {
  311. await initChatList();
  312. }}
  313. />
  314. <!-- svelte-ignore a11y-no-static-element-interactions -->
  315. {#if $showSidebar}
  316. <div
  317. 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"
  318. on:mousedown={() => {
  319. showSidebar.set(!$showSidebar);
  320. }}
  321. />
  322. {/if}
  323. <div
  324. bind:this={navElement}
  325. id="sidebar"
  326. class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar
  327. ? 'md:relative w-[260px] max-w-[260px]'
  328. : '-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
  329. "
  330. data-state={$showSidebar}
  331. >
  332. <div
  333. class="py-2.5 my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[260px] overflow-x-hidden z-50 {$showSidebar
  334. ? ''
  335. : 'invisible'}"
  336. >
  337. <div class="px-2.5 flex justify-between space-x-1 text-gray-600 dark:text-gray-400">
  338. <a
  339. id="sidebar-new-chat-button"
  340. class="flex flex-1 justify-between rounded-lg px-2 h-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  341. href="/"
  342. draggable="false"
  343. on:click={async () => {
  344. selectedChatId = null;
  345. await goto('/');
  346. const newChatButton = document.getElementById('new-chat-button');
  347. setTimeout(() => {
  348. newChatButton?.click();
  349. if ($mobile) {
  350. showSidebar.set(false);
  351. }
  352. }, 0);
  353. }}
  354. >
  355. <div class="self-center mx-1.5">
  356. <img
  357. crossorigin="anonymous"
  358. src="{WEBUI_BASE_URL}/static/favicon.png"
  359. class=" size-6 -translate-x-1.5 rounded-full"
  360. alt="logo"
  361. />
  362. </div>
  363. <div class=" self-center font-medium text-sm text-gray-850 dark:text-white font-primary">
  364. {$i18n.t('New Chat')}
  365. </div>
  366. <div class="self-center ml-auto">
  367. <svg
  368. xmlns="http://www.w3.org/2000/svg"
  369. viewBox="0 0 20 20"
  370. fill="currentColor"
  371. class="size-5"
  372. >
  373. <path
  374. 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"
  375. />
  376. <path
  377. 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"
  378. />
  379. </svg>
  380. </div>
  381. </a>
  382. <button
  383. class=" cursor-pointer px-2 py-2 flex rounded-lg hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  384. on:click={() => {
  385. showSidebar.set(!$showSidebar);
  386. }}
  387. >
  388. <div class=" m-auto self-center">
  389. <svg
  390. xmlns="http://www.w3.org/2000/svg"
  391. fill="none"
  392. viewBox="0 0 24 24"
  393. stroke-width="2"
  394. stroke="currentColor"
  395. class="size-5"
  396. >
  397. <path
  398. stroke-linecap="round"
  399. stroke-linejoin="round"
  400. d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25H12"
  401. />
  402. </svg>
  403. </div>
  404. </button>
  405. </div>
  406. {#if $user?.role === 'admin'}
  407. <div class="px-2.5 flex justify-center text-gray-800 dark:text-gray-200">
  408. <a
  409. class="flex-grow flex space-x-3 rounded-lg px-2.5 py-2 hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  410. href="/workspace"
  411. on:click={() => {
  412. selectedChatId = null;
  413. chatId.set('');
  414. if ($mobile) {
  415. showSidebar.set(false);
  416. }
  417. }}
  418. draggable="false"
  419. >
  420. <div class="self-center">
  421. <svg
  422. xmlns="http://www.w3.org/2000/svg"
  423. fill="none"
  424. viewBox="0 0 24 24"
  425. stroke-width="2"
  426. stroke="currentColor"
  427. class="size-[1.1rem]"
  428. >
  429. <path
  430. stroke-linecap="round"
  431. stroke-linejoin="round"
  432. 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"
  433. />
  434. </svg>
  435. </div>
  436. <div class="flex self-center">
  437. <div class=" self-center font-medium text-sm font-primary">{$i18n.t('Workspace')}</div>
  438. </div>
  439. </a>
  440. </div>
  441. {/if}
  442. <div class="relative {$temporaryChatEnabled ? 'opacity-20' : ''}">
  443. {#if $temporaryChatEnabled}
  444. <div class="absolute z-40 w-full h-full flex justify-center"></div>
  445. {/if}
  446. <div class="absolute z-40 right-4 top-1">
  447. <Tooltip content={$i18n.t('New folder')}>
  448. <button
  449. class="p-1 rounded-lg bg-gray-50 hover:bg-gray-100 dark:bg-gray-950 dark:hover:bg-gray-900 transition"
  450. on:click={() => {
  451. createFolder();
  452. }}
  453. >
  454. <Plus />
  455. </button>
  456. </Tooltip>
  457. </div>
  458. <SearchInput
  459. bind:value={search}
  460. on:input={searchDebounceHandler}
  461. placeholder={$i18n.t('Search')}
  462. />
  463. </div>
  464. <div
  465. class="relative flex flex-col flex-1 overflow-y-auto {$temporaryChatEnabled
  466. ? 'opacity-20'
  467. : ''}"
  468. >
  469. {#if $temporaryChatEnabled}
  470. <div class="absolute z-40 w-full h-full flex justify-center"></div>
  471. {/if}
  472. {#if !search && $pinnedChats.length > 0}
  473. <div class="flex flex-col space-y-1 rounded-xl">
  474. <Folder
  475. className="px-2"
  476. bind:open={showPinnedChat}
  477. on:change={(e) => {
  478. localStorage.setItem('showPinnedChat', e.detail);
  479. console.log(e.detail);
  480. }}
  481. on:import={(e) => {
  482. importChatHandler(e.detail, true);
  483. }}
  484. on:drop={async (e) => {
  485. const { type, id } = e.detail;
  486. if (type === 'chat') {
  487. const chat = await getChatById(localStorage.token, id);
  488. if (chat) {
  489. console.log(chat);
  490. if (chat.folder_id) {
  491. const res = await updateChatFolderIdById(
  492. localStorage.token,
  493. chat.id,
  494. null
  495. ).catch((error) => {
  496. toast.error(error);
  497. return null;
  498. });
  499. if (res) {
  500. initChatList();
  501. }
  502. }
  503. if (!chat.pinned) {
  504. const res = await toggleChatPinnedStatusById(localStorage.token, id);
  505. if (res) {
  506. initChatList();
  507. }
  508. }
  509. }
  510. }
  511. }}
  512. name={$i18n.t('Pinned')}
  513. >
  514. <div
  515. class="ml-3 pl-1 mt-[1px] flex flex-col overflow-y-auto scrollbar-hidden border-s border-gray-100 dark:border-gray-900"
  516. >
  517. {#each $pinnedChats as chat, idx}
  518. <ChatItem
  519. className=""
  520. id={chat.id}
  521. title={chat.title}
  522. {shiftKey}
  523. selected={selectedChatId === chat.id}
  524. on:select={() => {
  525. selectedChatId = chat.id;
  526. }}
  527. on:unselect={() => {
  528. selectedChatId = null;
  529. }}
  530. on:change={async () => {
  531. initChatList();
  532. }}
  533. on:tag={(e) => {
  534. const { type, name } = e.detail;
  535. tagEventHandler(type, name, chat.id);
  536. }}
  537. />
  538. {/each}
  539. </div>
  540. </Folder>
  541. </div>
  542. {/if}
  543. <div class=" flex-1 flex flex-col overflow-y-auto scrollbar-hidden">
  544. {#if !search && folders}
  545. <Folders
  546. {folders}
  547. on:import={(e) => {
  548. const { folderId, items } = e.detail;
  549. importChatHandler(items, false, folderId);
  550. }}
  551. on:update={async (e) => {
  552. initChatList();
  553. }}
  554. on:change={async () => {
  555. initChatList();
  556. }}
  557. />
  558. {/if}
  559. <Folder
  560. collapsible={!search}
  561. className="px-2 mt-0.5"
  562. name={$i18n.t('All chats')}
  563. on:import={(e) => {
  564. importChatHandler(e.detail);
  565. }}
  566. on:drop={async (e) => {
  567. const { type, id } = e.detail;
  568. if (type === 'chat') {
  569. const chat = await getChatById(localStorage.token, id);
  570. if (chat) {
  571. console.log(chat);
  572. if (chat.folder_id) {
  573. const res = await updateChatFolderIdById(localStorage.token, chat.id, null).catch(
  574. (error) => {
  575. toast.error(error);
  576. return null;
  577. }
  578. );
  579. if (res) {
  580. initChatList();
  581. }
  582. }
  583. if (chat.pinned) {
  584. const res = await toggleChatPinnedStatusById(localStorage.token, id);
  585. if (res) {
  586. initChatList();
  587. }
  588. }
  589. }
  590. } else if (type === 'folder') {
  591. if (folders[id].parent_id === null) {
  592. return;
  593. }
  594. const res = await updateFolderParentIdById(localStorage.token, id, null).catch(
  595. (error) => {
  596. toast.error(error);
  597. return null;
  598. }
  599. );
  600. if (res) {
  601. await initFolders();
  602. }
  603. }
  604. }}
  605. >
  606. <div class="pt-1.5">
  607. {#if $chats}
  608. {#each $chats as chat, idx}
  609. {#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)}
  610. <div
  611. class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium {idx ===
  612. 0
  613. ? ''
  614. : 'pt-5'} pb-1.5"
  615. >
  616. {$i18n.t(chat.time_range)}
  617. <!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
  618. {$i18n.t('Today')}
  619. {$i18n.t('Yesterday')}
  620. {$i18n.t('Previous 7 days')}
  621. {$i18n.t('Previous 30 days')}
  622. {$i18n.t('January')}
  623. {$i18n.t('February')}
  624. {$i18n.t('March')}
  625. {$i18n.t('April')}
  626. {$i18n.t('May')}
  627. {$i18n.t('June')}
  628. {$i18n.t('July')}
  629. {$i18n.t('August')}
  630. {$i18n.t('September')}
  631. {$i18n.t('October')}
  632. {$i18n.t('November')}
  633. {$i18n.t('December')}
  634. -->
  635. </div>
  636. {/if}
  637. <ChatItem
  638. className=""
  639. id={chat.id}
  640. title={chat.title}
  641. {shiftKey}
  642. selected={selectedChatId === chat.id}
  643. on:select={() => {
  644. selectedChatId = chat.id;
  645. }}
  646. on:unselect={() => {
  647. selectedChatId = null;
  648. }}
  649. on:change={async () => {
  650. initChatList();
  651. }}
  652. on:tag={(e) => {
  653. const { type, name } = e.detail;
  654. tagEventHandler(type, name, chat.id);
  655. }}
  656. />
  657. {/each}
  658. {#if $scrollPaginationEnabled && !allChatsLoaded}
  659. <Loader
  660. on:visible={(e) => {
  661. if (!chatListLoading) {
  662. loadMoreChats();
  663. }
  664. }}
  665. >
  666. <div
  667. class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2"
  668. >
  669. <Spinner className=" size-4" />
  670. <div class=" ">Loading...</div>
  671. </div>
  672. </Loader>
  673. {/if}
  674. {:else}
  675. <div class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2">
  676. <Spinner className=" size-4" />
  677. <div class=" ">Loading...</div>
  678. </div>
  679. {/if}
  680. </div>
  681. </Folder>
  682. </div>
  683. </div>
  684. <div class="px-2">
  685. <div class="flex flex-col font-primary">
  686. {#if $user !== undefined}
  687. <UserMenu
  688. role={$user.role}
  689. on:show={(e) => {
  690. if (e.detail === 'archived-chat') {
  691. showArchivedChats.set(true);
  692. }
  693. }}
  694. >
  695. <button
  696. class=" flex items-center rounded-xl py-2.5 px-2.5 w-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  697. on:click={() => {
  698. showDropdown = !showDropdown;
  699. }}
  700. >
  701. <div class=" self-center mr-3">
  702. <img
  703. src={$user.profile_image_url}
  704. class=" max-w-[30px] object-cover rounded-full"
  705. alt="User profile"
  706. />
  707. </div>
  708. <div class=" self-center font-medium">{$user.name}</div>
  709. </button>
  710. </UserMenu>
  711. {/if}
  712. </div>
  713. </div>
  714. </div>
  715. </div>
  716. <style>
  717. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  718. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  719. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  720. visibility: visible;
  721. }
  722. .scrollbar-hidden::-webkit-scrollbar-thumb {
  723. visibility: hidden;
  724. }
  725. </style>