Sidebar.svelte 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { goto } from '$app/navigation';
  4. import {
  5. user,
  6. chats,
  7. settings,
  8. showSettings,
  9. chatId,
  10. tags,
  11. showSidebar,
  12. mobile,
  13. showArchivedChats,
  14. pinnedChats
  15. } from '$lib/stores';
  16. import { onMount, getContext, tick } from 'svelte';
  17. const i18n = getContext('i18n');
  18. import { updateUserSettings } from '$lib/apis/users';
  19. import {
  20. deleteChatById,
  21. getChatList,
  22. getChatById,
  23. getChatListByTagName,
  24. updateChatById,
  25. getAllChatTags,
  26. archiveChatById,
  27. cloneChatById
  28. } from '$lib/apis/chats';
  29. import { WEBUI_BASE_URL } from '$lib/constants';
  30. import ArchivedChatsModal from './Sidebar/ArchivedChatsModal.svelte';
  31. import UserMenu from './Sidebar/UserMenu.svelte';
  32. import ChatItem from './Sidebar/ChatItem.svelte';
  33. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  34. const BREAKPOINT = 768;
  35. let navElement;
  36. let search = '';
  37. let shiftKey = false;
  38. let selectedChatId = null;
  39. let deleteChat = null;
  40. let showDeleteConfirm = false;
  41. let showDropdown = false;
  42. let filteredChatList = [];
  43. $: filteredChatList = $chats.filter((chat) => {
  44. if (search === '') {
  45. return true;
  46. } else {
  47. let title = chat.title.toLowerCase();
  48. const query = search.toLowerCase();
  49. let contentMatches = false;
  50. // Access the messages within chat.chat.messages
  51. if (chat.chat && chat.chat.messages && Array.isArray(chat.chat.messages)) {
  52. contentMatches = chat.chat.messages.some((message) => {
  53. // Check if message.content exists and includes the search query
  54. return message.content && message.content.toLowerCase().includes(query);
  55. });
  56. }
  57. return title.includes(query) || contentMatches;
  58. }
  59. });
  60. onMount(async () => {
  61. mobile.subscribe((e) => {
  62. if ($showSidebar && e) {
  63. showSidebar.set(false);
  64. }
  65. if (!$showSidebar && !e) {
  66. showSidebar.set(true);
  67. }
  68. });
  69. showSidebar.set(window.innerWidth > BREAKPOINT);
  70. await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
  71. await chats.set(await getChatList(localStorage.token));
  72. let touchstart;
  73. let touchend;
  74. function checkDirection() {
  75. const screenWidth = window.innerWidth;
  76. const swipeDistance = Math.abs(touchend.screenX - touchstart.screenX);
  77. if (touchstart.clientX < 40 && swipeDistance >= screenWidth / 8) {
  78. if (touchend.screenX < touchstart.screenX) {
  79. showSidebar.set(false);
  80. }
  81. if (touchend.screenX > touchstart.screenX) {
  82. showSidebar.set(true);
  83. }
  84. }
  85. }
  86. const onTouchStart = (e) => {
  87. touchstart = e.changedTouches[0];
  88. console.log(touchstart.clientX);
  89. };
  90. const onTouchEnd = (e) => {
  91. touchend = e.changedTouches[0];
  92. checkDirection();
  93. };
  94. const onKeyDown = (e) => {
  95. if (e.key === 'Shift') {
  96. shiftKey = true;
  97. }
  98. };
  99. const onKeyUp = (e) => {
  100. if (e.key === 'Shift') {
  101. shiftKey = false;
  102. }
  103. };
  104. const onFocus = () => {};
  105. const onBlur = () => {
  106. shiftKey = false;
  107. selectedChatId = null;
  108. };
  109. window.addEventListener('keydown', onKeyDown);
  110. window.addEventListener('keyup', onKeyUp);
  111. window.addEventListener('touchstart', onTouchStart);
  112. window.addEventListener('touchend', onTouchEnd);
  113. window.addEventListener('focus', onFocus);
  114. window.addEventListener('blur', onBlur);
  115. return () => {
  116. window.removeEventListener('keydown', onKeyDown);
  117. window.removeEventListener('keyup', onKeyUp);
  118. window.removeEventListener('touchstart', onTouchStart);
  119. window.removeEventListener('touchend', onTouchEnd);
  120. window.removeEventListener('focus', onFocus);
  121. window.removeEventListener('blur', onBlur);
  122. };
  123. });
  124. // Helper function to fetch and add chat content to each chat
  125. const enrichChatsWithContent = async (chatList) => {
  126. const enrichedChats = await Promise.all(
  127. chatList.map(async (chat) => {
  128. const chatDetails = await getChatById(localStorage.token, chat.id).catch((error) => null); // Handle error or non-existent chat gracefully
  129. if (chatDetails) {
  130. chat.chat = chatDetails.chat; // Assuming chatDetails.chat contains the chat content
  131. }
  132. return chat;
  133. })
  134. );
  135. await chats.set(enrichedChats);
  136. };
  137. const saveSettings = async (updated) => {
  138. await settings.set({ ...$settings, ...updated });
  139. await updateUserSettings(localStorage.token, { ui: $settings });
  140. location.href = '/';
  141. };
  142. const deleteChatHandler = async (id) => {
  143. const res = await deleteChatById(localStorage.token, id).catch((error) => {
  144. toast.error(error);
  145. return null;
  146. });
  147. if (res) {
  148. if ($chatId === id) {
  149. await chatId.set('');
  150. await tick();
  151. goto('/');
  152. }
  153. await chats.set(await getChatList(localStorage.token));
  154. }
  155. };
  156. </script>
  157. <ArchivedChatsModal
  158. bind:show={$showArchivedChats}
  159. on:change={async () => {
  160. await chats.set(await getChatList(localStorage.token));
  161. }}
  162. />
  163. <DeleteConfirmDialog
  164. bind:show={showDeleteConfirm}
  165. title={$i18n.t('Delete chat?')}
  166. on:confirm={() => {
  167. deleteChatHandler(deleteChat.id);
  168. }}
  169. >
  170. <div class=" text-sm text-gray-500">
  171. {$i18n.t('This will delete')} <span class=" font-semibold">{deleteChat.title}</span>.
  172. </div>
  173. </DeleteConfirmDialog>
  174. <!-- svelte-ignore a11y-no-static-element-interactions -->
  175. {#if $showSidebar}
  176. <div
  177. 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"
  178. on:mousedown={() => {
  179. showSidebar.set(!$showSidebar);
  180. }}
  181. />
  182. {/if}
  183. <div
  184. bind:this={navElement}
  185. id="sidebar"
  186. class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar
  187. ? 'md:relative w-[260px]'
  188. : '-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 rounded-r-2xl
  189. "
  190. data-state={$showSidebar}
  191. >
  192. <div
  193. class="py-2.5 my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[260px] z-50 {$showSidebar
  194. ? ''
  195. : 'invisible'}"
  196. >
  197. <div class="px-2.5 flex justify-between space-x-1 text-gray-600 dark:text-gray-400">
  198. <a
  199. id="sidebar-new-chat-button"
  200. class="flex flex-1 justify-between rounded-xl px-2 py-2 hover:bg-gray-100 dark:hover:bg-gray-850 transition"
  201. href="/"
  202. draggable="false"
  203. on:click={async () => {
  204. selectedChatId = null;
  205. await goto('/');
  206. const newChatButton = document.getElementById('new-chat-button');
  207. setTimeout(() => {
  208. newChatButton?.click();
  209. if ($mobile) {
  210. showSidebar.set(false);
  211. }
  212. }, 0);
  213. }}
  214. >
  215. <div class="self-center mx-1.5">
  216. <img
  217. crossorigin="anonymous"
  218. src="{WEBUI_BASE_URL}/static/favicon.png"
  219. class=" size-6 -translate-x-1.5 rounded-full"
  220. alt="logo"
  221. />
  222. </div>
  223. <div class=" self-center font-medium text-sm text-gray-850 dark:text-white">
  224. {$i18n.t('New Chat')}
  225. </div>
  226. <div class="self-center ml-auto">
  227. <svg
  228. xmlns="http://www.w3.org/2000/svg"
  229. viewBox="0 0 20 20"
  230. fill="currentColor"
  231. class="size-5"
  232. >
  233. <path
  234. 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"
  235. />
  236. <path
  237. 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"
  238. />
  239. </svg>
  240. </div>
  241. </a>
  242. <button
  243. class=" cursor-pointer px-2 py-2 flex rounded-xl hover:bg-gray-100 dark:hover:bg-gray-850 transition"
  244. on:click={() => {
  245. showSidebar.set(!$showSidebar);
  246. }}
  247. >
  248. <div class=" m-auto self-center">
  249. <svg
  250. xmlns="http://www.w3.org/2000/svg"
  251. fill="none"
  252. viewBox="0 0 24 24"
  253. stroke-width="2"
  254. stroke="currentColor"
  255. class="size-5"
  256. >
  257. <path
  258. stroke-linecap="round"
  259. stroke-linejoin="round"
  260. d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25H12"
  261. />
  262. </svg>
  263. </div>
  264. </button>
  265. </div>
  266. {#if $user?.role === 'admin'}
  267. <div class="px-2.5 flex justify-center text-gray-800 dark:text-gray-200">
  268. <a
  269. class="flex-grow flex space-x-3 rounded-xl px-2.5 py-2 hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  270. href="/workspace"
  271. on:click={() => {
  272. selectedChatId = null;
  273. chatId.set('');
  274. if ($mobile) {
  275. showSidebar.set(false);
  276. }
  277. }}
  278. draggable="false"
  279. >
  280. <div class="self-center">
  281. <svg
  282. xmlns="http://www.w3.org/2000/svg"
  283. fill="none"
  284. viewBox="0 0 24 24"
  285. stroke-width="2"
  286. stroke="currentColor"
  287. class="size-[1.1rem]"
  288. >
  289. <path
  290. stroke-linecap="round"
  291. stroke-linejoin="round"
  292. 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"
  293. />
  294. </svg>
  295. </div>
  296. <div class="flex self-center">
  297. <div class=" self-center font-medium text-sm">{$i18n.t('Workspace')}</div>
  298. </div>
  299. </a>
  300. </div>
  301. {/if}
  302. <div class="relative flex flex-col flex-1 overflow-y-auto">
  303. {#if !($settings.saveChatHistory ?? true)}
  304. <div class="absolute z-40 w-full h-full bg-gray-50/90 dark:bg-black/90 flex justify-center">
  305. <div class=" text-left px-5 py-2">
  306. <div class=" font-medium">{$i18n.t('Chat History is off for this browser.')}</div>
  307. <div class="text-xs mt-2">
  308. {$i18n.t(
  309. "When history is turned off, new chats on this browser won't appear in your history on any of your devices."
  310. )}
  311. <span class=" font-semibold"
  312. >{$i18n.t('This setting does not sync across browsers or devices.')}</span
  313. >
  314. </div>
  315. <div class="mt-3">
  316. <button
  317. class="flex justify-center items-center space-x-1.5 px-3 py-2.5 rounded-lg text-xs bg-gray-100 hover:bg-gray-200 transition text-gray-800 font-medium w-full"
  318. type="button"
  319. on:click={() => {
  320. saveSettings({
  321. saveChatHistory: true
  322. });
  323. }}
  324. >
  325. <svg
  326. xmlns="http://www.w3.org/2000/svg"
  327. viewBox="0 0 16 16"
  328. fill="currentColor"
  329. class="w-3 h-3"
  330. >
  331. <path
  332. fill-rule="evenodd"
  333. d="M8 1a.75.75 0 0 1 .75.75v6.5a.75.75 0 0 1-1.5 0v-6.5A.75.75 0 0 1 8 1ZM4.11 3.05a.75.75 0 0 1 0 1.06 5.5 5.5 0 1 0 7.78 0 .75.75 0 0 1 1.06-1.06 7 7 0 1 1-9.9 0 .75.75 0 0 1 1.06 0Z"
  334. clip-rule="evenodd"
  335. />
  336. </svg>
  337. <div>{$i18n.t('Enable Chat History')}</div>
  338. </button>
  339. </div>
  340. </div>
  341. </div>
  342. {/if}
  343. <div class="px-2 mt-0.5 mb-2 flex justify-center space-x-2">
  344. <div class="flex w-full rounded-xl" id="chat-search">
  345. <div class="self-center pl-3 py-2 rounded-l-xl bg-transparent">
  346. <svg
  347. xmlns="http://www.w3.org/2000/svg"
  348. viewBox="0 0 20 20"
  349. fill="currentColor"
  350. class="w-4 h-4"
  351. >
  352. <path
  353. fill-rule="evenodd"
  354. d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z"
  355. clip-rule="evenodd"
  356. />
  357. </svg>
  358. </div>
  359. <input
  360. class="w-full rounded-r-xl py-1.5 pl-2.5 pr-4 text-sm bg-transparent dark:text-gray-300 outline-none"
  361. placeholder={$i18n.t('Search')}
  362. bind:value={search}
  363. on:focus={() => {
  364. enrichChatsWithContent($chats);
  365. }}
  366. />
  367. </div>
  368. </div>
  369. {#if $tags.filter((t) => t.name !== 'pinned').length > 0}
  370. <div class="px-2.5 mb-2 flex gap-1 flex-wrap">
  371. <button
  372. class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
  373. on:click={async () => {
  374. await chats.set(await getChatList(localStorage.token));
  375. }}
  376. >
  377. {$i18n.t('all')}
  378. </button>
  379. {#each $tags.filter((t) => t.name !== 'pinned') as tag}
  380. <button
  381. class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
  382. on:click={async () => {
  383. let chatIds = await getChatListByTagName(localStorage.token, tag.name);
  384. if (chatIds.length === 0) {
  385. await tags.set(await getAllChatTags(localStorage.token));
  386. chatIds = await getChatList(localStorage.token);
  387. }
  388. await chats.set(chatIds);
  389. }}
  390. >
  391. {tag.name}
  392. </button>
  393. {/each}
  394. </div>
  395. {/if}
  396. {#if $pinnedChats.length > 0}
  397. <div class="pl-2 py-2 flex flex-col space-y-1">
  398. <div class="">
  399. <div class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium pb-1.5">
  400. {$i18n.t('Pinned')}
  401. </div>
  402. {#each $pinnedChats as chat, idx}
  403. <ChatItem
  404. {chat}
  405. {shiftKey}
  406. selected={selectedChatId === chat.id}
  407. on:select={() => {
  408. selectedChatId = chat.id;
  409. }}
  410. on:unselect={() => {
  411. selectedChatId = null;
  412. }}
  413. on:delete={(e) => {
  414. if ((e?.detail ?? '') === 'shift') {
  415. deleteChatHandler(chat.id);
  416. } else {
  417. deleteChat = chat;
  418. showDeleteConfirm = true;
  419. }
  420. }}
  421. />
  422. {/each}
  423. </div>
  424. </div>
  425. {/if}
  426. <div class="pl-2 my-2 flex-1 flex flex-col space-y-1 overflow-y-auto scrollbar-hidden">
  427. {#each filteredChatList as chat, idx}
  428. {#if idx === 0 || (idx > 0 && chat.time_range !== filteredChatList[idx - 1].time_range)}
  429. <div
  430. class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium {idx === 0
  431. ? ''
  432. : 'pt-5'} pb-0.5"
  433. >
  434. {$i18n.t(chat.time_range)}
  435. <!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
  436. {$i18n.t('Today')}
  437. {$i18n.t('Yesterday')}
  438. {$i18n.t('Previous 7 days')}
  439. {$i18n.t('Previous 30 days')}
  440. {$i18n.t('January')}
  441. {$i18n.t('February')}
  442. {$i18n.t('March')}
  443. {$i18n.t('April')}
  444. {$i18n.t('May')}
  445. {$i18n.t('June')}
  446. {$i18n.t('July')}
  447. {$i18n.t('August')}
  448. {$i18n.t('September')}
  449. {$i18n.t('October')}
  450. {$i18n.t('November')}
  451. {$i18n.t('December')}
  452. -->
  453. </div>
  454. {/if}
  455. <ChatItem
  456. {chat}
  457. {shiftKey}
  458. selected={selectedChatId === chat.id}
  459. on:select={() => {
  460. selectedChatId = chat.id;
  461. }}
  462. on:unselect={() => {
  463. selectedChatId = null;
  464. }}
  465. on:delete={(e) => {
  466. if ((e?.detail ?? '') === 'shift') {
  467. deleteChatHandler(chat.id);
  468. } else {
  469. deleteChat = chat;
  470. showDeleteConfirm = true;
  471. }
  472. }}
  473. />
  474. {/each}
  475. </div>
  476. </div>
  477. <div class="px-2.5">
  478. <!-- <hr class=" border-gray-900 mb-1 w-full" /> -->
  479. <div class="flex flex-col">
  480. {#if $user !== undefined}
  481. <UserMenu
  482. role={$user.role}
  483. on:show={(e) => {
  484. if (e.detail === 'archived-chat') {
  485. showArchivedChats.set(true);
  486. }
  487. }}
  488. >
  489. <button
  490. class=" flex rounded-xl py-3 px-3.5 w-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  491. on:click={() => {
  492. showDropdown = !showDropdown;
  493. }}
  494. >
  495. <div class=" self-center mr-3">
  496. <img
  497. src={$user.profile_image_url}
  498. class=" max-w-[30px] object-cover rounded-full"
  499. alt="User profile"
  500. />
  501. </div>
  502. <div class=" self-center font-semibold">{$user.name}</div>
  503. </button>
  504. </UserMenu>
  505. {/if}
  506. </div>
  507. </div>
  508. </div>
  509. <!-- <div
  510. id="sidebar-handle"
  511. class=" hidden md:fixed left-0 top-[50dvh] -translate-y-1/2 transition-transform translate-x-[255px] md:translate-x-[260px] rotate-0"
  512. >
  513. <Tooltip
  514. placement="right"
  515. content={`${$showSidebar ? $i18n.t('Close') : $i18n.t('Open')} ${$i18n.t('sidebar')}`}
  516. touch={false}
  517. >
  518. <button
  519. id="sidebar-toggle-button"
  520. class=" group"
  521. on:click={() => {
  522. showSidebar.set(!$showSidebar);
  523. }}
  524. ><span class="" data-state="closed"
  525. ><div
  526. class="flex h-[72px] w-8 items-center justify-center opacity-50 group-hover:opacity-100 transition"
  527. >
  528. <div class="flex h-6 w-6 flex-col items-center">
  529. <div
  530. class="h-3 w-1 rounded-full bg-[#0f0f0f] dark:bg-white rotate-0 translate-y-[0.15rem] {$showSidebar
  531. ? 'group-hover:rotate-[15deg]'
  532. : 'group-hover:rotate-[-15deg]'}"
  533. />
  534. <div
  535. class="h-3 w-1 rounded-full bg-[#0f0f0f] dark:bg-white rotate-0 translate-y-[-0.15rem] {$showSidebar
  536. ? 'group-hover:rotate-[-15deg]'
  537. : 'group-hover:rotate-[15deg]'}"
  538. />
  539. </div>
  540. </div>
  541. </span>
  542. </button>
  543. </Tooltip>
  544. </div> -->
  545. </div>
  546. <style>
  547. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  548. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  549. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  550. visibility: visible;
  551. }
  552. .scrollbar-hidden::-webkit-scrollbar-thumb {
  553. visibility: hidden;
  554. }
  555. </style>