Sidebar.svelte 22 KB

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