Sidebar.svelte 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. $socket.emit('join-channels', { auth: { token: $user.token } });
  339. await initChannels();
  340. showCreateChannel = false;
  341. }
  342. }}
  343. />
  344. <!-- svelte-ignore a11y-no-static-element-interactions -->
  345. {#if $showSidebar}
  346. <div
  347. 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"
  348. on:mousedown={() => {
  349. showSidebar.set(!$showSidebar);
  350. }}
  351. />
  352. {/if}
  353. <div
  354. bind:this={navElement}
  355. id="sidebar"
  356. class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar
  357. ? 'md:relative w-[260px] max-w-[260px]'
  358. : '-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
  359. "
  360. data-state={$showSidebar}
  361. >
  362. <div
  363. class="py-2 my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[260px] overflow-x-hidden z-50 {$showSidebar
  364. ? ''
  365. : 'invisible'}"
  366. >
  367. <div class="px-1.5 flex justify-between space-x-1 text-gray-600 dark:text-gray-400">
  368. <button
  369. class=" cursor-pointer p-[7px] flex rounded-xl hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  370. on:click={() => {
  371. showSidebar.set(!$showSidebar);
  372. }}
  373. >
  374. <div class=" m-auto self-center">
  375. <svg
  376. xmlns="http://www.w3.org/2000/svg"
  377. fill="none"
  378. viewBox="0 0 24 24"
  379. stroke-width="2"
  380. stroke="currentColor"
  381. class="size-5"
  382. >
  383. <path
  384. stroke-linecap="round"
  385. stroke-linejoin="round"
  386. d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25H12"
  387. />
  388. </svg>
  389. </div>
  390. </button>
  391. <a
  392. id="sidebar-new-chat-button"
  393. 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"
  394. href="/"
  395. draggable="false"
  396. on:click={async () => {
  397. selectedChatId = null;
  398. await goto('/');
  399. const newChatButton = document.getElementById('new-chat-button');
  400. setTimeout(() => {
  401. newChatButton?.click();
  402. if ($mobile) {
  403. showSidebar.set(false);
  404. }
  405. }, 0);
  406. }}
  407. >
  408. <div class="flex items-center">
  409. <div class="self-center mx-1.5">
  410. <img
  411. crossorigin="anonymous"
  412. src="{WEBUI_BASE_URL}/static/favicon.png"
  413. class=" size-5 -translate-x-1.5 rounded-full"
  414. alt="logo"
  415. />
  416. </div>
  417. <div class=" self-center font-medium text-sm text-gray-850 dark:text-white font-primary">
  418. {$i18n.t('New Chat')}
  419. </div>
  420. </div>
  421. <div>
  422. <PencilSquare className=" size-5" strokeWidth="2" />
  423. </div>
  424. </a>
  425. </div>
  426. {#if $user?.role === 'admin' || $user?.permissions?.workspace?.models || $user?.permissions?.workspace?.knowledge || $user?.permissions?.workspace?.prompts || $user?.permissions?.workspace?.tools}
  427. <div class="px-1.5 flex justify-center text-gray-800 dark:text-gray-200">
  428. <a
  429. class="flex-grow flex space-x-3 rounded-lg px-2 py-[7px] hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  430. href="/workspace"
  431. on:click={() => {
  432. selectedChatId = null;
  433. chatId.set('');
  434. if ($mobile) {
  435. showSidebar.set(false);
  436. }
  437. }}
  438. draggable="false"
  439. >
  440. <div class="self-center">
  441. <svg
  442. xmlns="http://www.w3.org/2000/svg"
  443. fill="none"
  444. viewBox="0 0 24 24"
  445. stroke-width="2"
  446. stroke="currentColor"
  447. class="size-[1.1rem]"
  448. >
  449. <path
  450. stroke-linecap="round"
  451. stroke-linejoin="round"
  452. 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"
  453. />
  454. </svg>
  455. </div>
  456. <div class="flex self-center">
  457. <div class=" self-center font-medium text-sm font-primary">{$i18n.t('Workspace')}</div>
  458. </div>
  459. </a>
  460. </div>
  461. {/if}
  462. <div class="relative {$temporaryChatEnabled ? 'opacity-20' : ''}">
  463. {#if $temporaryChatEnabled}
  464. <div class="absolute z-40 w-full h-full flex justify-center"></div>
  465. {/if}
  466. <SearchInput
  467. bind:value={search}
  468. on:input={searchDebounceHandler}
  469. placeholder={$i18n.t('Search')}
  470. />
  471. </div>
  472. <div
  473. class="relative flex flex-col flex-1 overflow-y-auto overflow-x-hidden {$temporaryChatEnabled
  474. ? 'opacity-20'
  475. : ''}"
  476. >
  477. {#if !search && $pinnedChats.length > 0}
  478. <div class="flex flex-col space-y-1 rounded-xl">
  479. <Folder
  480. className="px-2"
  481. bind:open={showPinnedChat}
  482. on:change={(e) => {
  483. localStorage.setItem('showPinnedChat', e.detail);
  484. console.log(e.detail);
  485. }}
  486. on:import={(e) => {
  487. importChatHandler(e.detail, true);
  488. }}
  489. on:drop={async (e) => {
  490. const { type, id, item } = e.detail;
  491. if (type === 'chat') {
  492. let chat = await getChatById(localStorage.token, id).catch((error) => {
  493. return null;
  494. });
  495. if (!chat && item) {
  496. chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
  497. }
  498. if (chat) {
  499. console.log(chat);
  500. if (chat.folder_id) {
  501. const res = await updateChatFolderIdById(
  502. localStorage.token,
  503. chat.id,
  504. null
  505. ).catch((error) => {
  506. toast.error(error);
  507. return null;
  508. });
  509. }
  510. if (!chat.pinned) {
  511. const res = await toggleChatPinnedStatusById(localStorage.token, chat.id);
  512. }
  513. initChatList();
  514. }
  515. }
  516. }}
  517. name={$i18n.t('Pinned')}
  518. >
  519. <div
  520. class="ml-3 pl-1 mt-[1px] flex flex-col overflow-y-auto scrollbar-hidden border-s border-gray-100 dark:border-gray-900"
  521. >
  522. {#each $pinnedChats as chat, idx}
  523. <ChatItem
  524. className=""
  525. id={chat.id}
  526. title={chat.title}
  527. {shiftKey}
  528. selected={selectedChatId === chat.id}
  529. on:select={() => {
  530. selectedChatId = chat.id;
  531. }}
  532. on:unselect={() => {
  533. selectedChatId = null;
  534. }}
  535. on:change={async () => {
  536. initChatList();
  537. }}
  538. on:tag={(e) => {
  539. const { type, name } = e.detail;
  540. tagEventHandler(type, name, chat.id);
  541. }}
  542. />
  543. {/each}
  544. </div>
  545. </Folder>
  546. </div>
  547. {/if}
  548. {#if $config?.features?.enable_channels && ($user.role === 'admin' || $channels.length > 0) && !search}
  549. <Folder
  550. className="px-2 mt-0.5"
  551. name={$i18n.t('Channels')}
  552. dragAndDrop={false}
  553. onAdd={$user.role === 'admin'
  554. ? () => {
  555. showCreateChannel = true;
  556. }
  557. : null}
  558. onAddLabel={$i18n.t('Create Channel')}
  559. >
  560. {#each $channels as channel}
  561. <ChannelItem
  562. {channel}
  563. onUpdate={async () => {
  564. await initChannels();
  565. }}
  566. />
  567. {/each}
  568. </Folder>
  569. {/if}
  570. {#if !search && folders}
  571. <Folders
  572. {folders}
  573. on:import={(e) => {
  574. const { folderId, items } = e.detail;
  575. importChatHandler(items, false, folderId);
  576. }}
  577. on:update={async (e) => {
  578. initChatList();
  579. }}
  580. on:change={async () => {
  581. initChatList();
  582. }}
  583. />
  584. {/if}
  585. <Folder
  586. collapsible={!search}
  587. className="px-2 mt-0.5"
  588. name={$i18n.t('Chats')}
  589. on:import={(e) => {
  590. importChatHandler(e.detail);
  591. }}
  592. on:drop={async (e) => {
  593. const { type, id, item } = e.detail;
  594. if (type === 'chat') {
  595. let chat = await getChatById(localStorage.token, id).catch((error) => {
  596. return null;
  597. });
  598. if (!chat && item) {
  599. chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
  600. }
  601. if (chat) {
  602. console.log(chat);
  603. if (chat.folder_id) {
  604. const res = await updateChatFolderIdById(localStorage.token, chat.id, null).catch(
  605. (error) => {
  606. toast.error(error);
  607. return null;
  608. }
  609. );
  610. }
  611. if (chat.pinned) {
  612. const res = await toggleChatPinnedStatusById(localStorage.token, chat, id);
  613. }
  614. initChatList();
  615. }
  616. } else if (type === 'folder') {
  617. if (folders[id].parent_id === null) {
  618. return;
  619. }
  620. const res = await updateFolderParentIdById(localStorage.token, id, null).catch(
  621. (error) => {
  622. toast.error(error);
  623. return null;
  624. }
  625. );
  626. if (res) {
  627. await initFolders();
  628. }
  629. }
  630. }}
  631. >
  632. {#if $temporaryChatEnabled}
  633. <div class="absolute z-40 w-full h-full flex justify-center"></div>
  634. {/if}
  635. <div class=" flex-1 flex flex-col overflow-y-auto scrollbar-hidden">
  636. <div class="pt-1.5">
  637. {#if $chats}
  638. {#each $chats as chat, idx}
  639. {#if idx === 0 || (idx > 0 && chat.time_range !== $chats[idx - 1].time_range)}
  640. <div
  641. class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium {idx ===
  642. 0
  643. ? ''
  644. : 'pt-5'} pb-1.5"
  645. >
  646. {$i18n.t(chat.time_range)}
  647. <!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
  648. {$i18n.t('Today')}
  649. {$i18n.t('Yesterday')}
  650. {$i18n.t('Previous 7 days')}
  651. {$i18n.t('Previous 30 days')}
  652. {$i18n.t('January')}
  653. {$i18n.t('February')}
  654. {$i18n.t('March')}
  655. {$i18n.t('April')}
  656. {$i18n.t('May')}
  657. {$i18n.t('June')}
  658. {$i18n.t('July')}
  659. {$i18n.t('August')}
  660. {$i18n.t('September')}
  661. {$i18n.t('October')}
  662. {$i18n.t('November')}
  663. {$i18n.t('December')}
  664. -->
  665. </div>
  666. {/if}
  667. <ChatItem
  668. className=""
  669. id={chat.id}
  670. title={chat.title}
  671. {shiftKey}
  672. selected={selectedChatId === chat.id}
  673. on:select={() => {
  674. selectedChatId = chat.id;
  675. }}
  676. on:unselect={() => {
  677. selectedChatId = null;
  678. }}
  679. on:change={async () => {
  680. initChatList();
  681. }}
  682. on:tag={(e) => {
  683. const { type, name } = e.detail;
  684. tagEventHandler(type, name, chat.id);
  685. }}
  686. />
  687. {/each}
  688. {#if $scrollPaginationEnabled && !allChatsLoaded}
  689. <Loader
  690. on:visible={(e) => {
  691. if (!chatListLoading) {
  692. loadMoreChats();
  693. }
  694. }}
  695. >
  696. <div
  697. class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2"
  698. >
  699. <Spinner className=" size-4" />
  700. <div class=" ">Loading...</div>
  701. </div>
  702. </Loader>
  703. {/if}
  704. {:else}
  705. <div class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2">
  706. <Spinner className=" size-4" />
  707. <div class=" ">Loading...</div>
  708. </div>
  709. {/if}
  710. </div>
  711. </div>
  712. </Folder>
  713. </div>
  714. <div class="px-2">
  715. <div class="flex flex-col font-primary">
  716. {#if $user !== undefined}
  717. <UserMenu
  718. role={$user.role}
  719. on:show={(e) => {
  720. if (e.detail === 'archived-chat') {
  721. showArchivedChats.set(true);
  722. }
  723. }}
  724. >
  725. <button
  726. class=" flex items-center rounded-xl py-2.5 px-2.5 w-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  727. on:click={() => {
  728. showDropdown = !showDropdown;
  729. }}
  730. >
  731. <div class=" self-center mr-3">
  732. <img
  733. src={$user.profile_image_url}
  734. class=" max-w-[30px] object-cover rounded-full"
  735. alt="User profile"
  736. />
  737. </div>
  738. <div class=" self-center font-medium">{$user.name}</div>
  739. </button>
  740. </UserMenu>
  741. {/if}
  742. </div>
  743. </div>
  744. </div>
  745. </div>
  746. <style>
  747. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  748. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  749. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  750. visibility: visible;
  751. }
  752. .scrollbar-hidden::-webkit-scrollbar-thumb {
  753. visibility: hidden;
  754. }
  755. </style>