1
0

Sidebar.svelte 26 KB

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