Sidebar.svelte 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
  155. }
  156. };
  157. </script>
  158. <ArchivedChatsModal
  159. bind:show={$showArchivedChats}
  160. on:change={async () => {
  161. await chats.set(await getChatList(localStorage.token));
  162. }}
  163. />
  164. <DeleteConfirmDialog
  165. bind:show={showDeleteConfirm}
  166. title={$i18n.t('Delete chat?')}
  167. on:confirm={() => {
  168. deleteChatHandler(deleteChat.id);
  169. }}
  170. >
  171. <div class=" text-sm text-gray-500">
  172. {$i18n.t('This will delete')} <span class=" font-semibold">{deleteChat.title}</span>.
  173. </div>
  174. </DeleteConfirmDialog>
  175. <!-- svelte-ignore a11y-no-static-element-interactions -->
  176. {#if $showSidebar}
  177. <div
  178. 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"
  179. on:mousedown={() => {
  180. showSidebar.set(!$showSidebar);
  181. }}
  182. />
  183. {/if}
  184. <div
  185. bind:this={navElement}
  186. id="sidebar"
  187. class="h-screen max-h-[100dvh] min-h-screen select-none {$showSidebar
  188. ? 'md:relative w-[260px]'
  189. : '-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
  190. "
  191. data-state={$showSidebar}
  192. >
  193. <div
  194. class="py-2.5 my-auto flex flex-col justify-between h-screen max-h-[100dvh] w-[260px] z-50 {$showSidebar
  195. ? ''
  196. : 'invisible'}"
  197. >
  198. <div class="px-2.5 flex justify-between space-x-1 text-gray-600 dark:text-gray-400">
  199. <a
  200. id="sidebar-new-chat-button"
  201. class="flex flex-1 justify-between rounded-xl px-2 py-2 hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  202. href="/"
  203. draggable="false"
  204. on:click={async () => {
  205. selectedChatId = null;
  206. await goto('/');
  207. const newChatButton = document.getElementById('new-chat-button');
  208. setTimeout(() => {
  209. newChatButton?.click();
  210. if ($mobile) {
  211. showSidebar.set(false);
  212. }
  213. }, 0);
  214. }}
  215. >
  216. <div class="self-center mx-1.5">
  217. <img
  218. crossorigin="anonymous"
  219. src="{WEBUI_BASE_URL}/static/favicon.png"
  220. class=" size-6 -translate-x-1.5 rounded-full"
  221. alt="logo"
  222. />
  223. </div>
  224. <div class=" self-center font-medium text-sm text-gray-850 dark:text-white font-primary">
  225. {$i18n.t('New Chat')}
  226. </div>
  227. <div class="self-center ml-auto">
  228. <svg
  229. xmlns="http://www.w3.org/2000/svg"
  230. viewBox="0 0 20 20"
  231. fill="currentColor"
  232. class="size-5"
  233. >
  234. <path
  235. 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"
  236. />
  237. <path
  238. 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"
  239. />
  240. </svg>
  241. </div>
  242. </a>
  243. <button
  244. class=" cursor-pointer px-2 py-2 flex rounded-xl hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  245. on:click={() => {
  246. showSidebar.set(!$showSidebar);
  247. }}
  248. >
  249. <div class=" m-auto self-center">
  250. <svg
  251. xmlns="http://www.w3.org/2000/svg"
  252. fill="none"
  253. viewBox="0 0 24 24"
  254. stroke-width="2"
  255. stroke="currentColor"
  256. class="size-5"
  257. >
  258. <path
  259. stroke-linecap="round"
  260. stroke-linejoin="round"
  261. d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25H12"
  262. />
  263. </svg>
  264. </div>
  265. </button>
  266. </div>
  267. {#if $user?.role === 'admin'}
  268. <div class="px-2.5 flex justify-center text-gray-800 dark:text-gray-200">
  269. <a
  270. class="flex-grow flex space-x-3 rounded-xl px-2.5 py-2 hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  271. href="/workspace"
  272. on:click={() => {
  273. selectedChatId = null;
  274. chatId.set('');
  275. if ($mobile) {
  276. showSidebar.set(false);
  277. }
  278. }}
  279. draggable="false"
  280. >
  281. <div class="self-center">
  282. <svg
  283. xmlns="http://www.w3.org/2000/svg"
  284. fill="none"
  285. viewBox="0 0 24 24"
  286. stroke-width="2"
  287. stroke="currentColor"
  288. class="size-[1.1rem]"
  289. >
  290. <path
  291. stroke-linecap="round"
  292. stroke-linejoin="round"
  293. 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"
  294. />
  295. </svg>
  296. </div>
  297. <div class="flex self-center">
  298. <div class=" self-center font-medium text-sm font-primary">{$i18n.t('Workspace')}</div>
  299. </div>
  300. </a>
  301. </div>
  302. {/if}
  303. <div class="relative flex flex-col flex-1 overflow-y-auto">
  304. {#if !($settings.saveChatHistory ?? true)}
  305. <div class="absolute z-40 w-full h-full bg-gray-50/90 dark:bg-black/90 flex justify-center">
  306. <div class=" text-left px-5 py-2">
  307. <div class=" font-medium">{$i18n.t('Chat History is off for this browser.')}</div>
  308. <div class="text-xs mt-2">
  309. {$i18n.t(
  310. "When history is turned off, new chats on this browser won't appear in your history on any of your devices."
  311. )}
  312. <span class=" font-semibold"
  313. >{$i18n.t('This setting does not sync across browsers or devices.')}</span
  314. >
  315. </div>
  316. <div class="mt-3">
  317. <button
  318. 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"
  319. type="button"
  320. on:click={() => {
  321. saveSettings({
  322. saveChatHistory: true
  323. });
  324. }}
  325. >
  326. <svg
  327. xmlns="http://www.w3.org/2000/svg"
  328. viewBox="0 0 16 16"
  329. fill="currentColor"
  330. class="w-3 h-3"
  331. >
  332. <path
  333. fill-rule="evenodd"
  334. 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"
  335. clip-rule="evenodd"
  336. />
  337. </svg>
  338. <div>{$i18n.t('Enable Chat History')}</div>
  339. </button>
  340. </div>
  341. </div>
  342. </div>
  343. {/if}
  344. <div class="px-2 mt-0.5 mb-2 flex justify-center space-x-2">
  345. <div class="flex w-full rounded-xl" id="chat-search">
  346. <div class="self-center pl-3 py-2 rounded-l-xl bg-transparent">
  347. <svg
  348. xmlns="http://www.w3.org/2000/svg"
  349. viewBox="0 0 20 20"
  350. fill="currentColor"
  351. class="w-4 h-4"
  352. >
  353. <path
  354. fill-rule="evenodd"
  355. 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"
  356. clip-rule="evenodd"
  357. />
  358. </svg>
  359. </div>
  360. <input
  361. class="w-full rounded-r-xl py-1.5 pl-2.5 pr-4 text-sm bg-transparent dark:text-gray-300 outline-none"
  362. placeholder={$i18n.t('Search')}
  363. bind:value={search}
  364. on:focus={() => {
  365. enrichChatsWithContent($chats);
  366. }}
  367. />
  368. </div>
  369. </div>
  370. {#if $tags.filter((t) => t.name !== 'pinned').length > 0}
  371. <div class="px-2.5 mb-2 flex gap-1 flex-wrap">
  372. <button
  373. class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
  374. on:click={async () => {
  375. await chats.set(await getChatList(localStorage.token));
  376. }}
  377. >
  378. {$i18n.t('all')}
  379. </button>
  380. {#each $tags.filter((t) => t.name !== 'pinned') as tag}
  381. <button
  382. class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
  383. on:click={async () => {
  384. let chatIds = await getChatListByTagName(localStorage.token, tag.name);
  385. if (chatIds.length === 0) {
  386. await tags.set(await getAllChatTags(localStorage.token));
  387. chatIds = await getChatList(localStorage.token);
  388. }
  389. await chats.set(chatIds);
  390. }}
  391. >
  392. {tag.name}
  393. </button>
  394. {/each}
  395. </div>
  396. {/if}
  397. {#if $pinnedChats.length > 0}
  398. <div class="pl-2 py-2 flex flex-col space-y-1">
  399. <div class="">
  400. <div class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium pb-1.5">
  401. {$i18n.t('Pinned')}
  402. </div>
  403. {#each $pinnedChats as chat, idx}
  404. <ChatItem
  405. {chat}
  406. {shiftKey}
  407. selected={selectedChatId === chat.id}
  408. on:select={() => {
  409. selectedChatId = chat.id;
  410. }}
  411. on:unselect={() => {
  412. selectedChatId = null;
  413. }}
  414. on:delete={(e) => {
  415. if ((e?.detail ?? '') === 'shift') {
  416. deleteChatHandler(chat.id);
  417. } else {
  418. deleteChat = chat;
  419. showDeleteConfirm = true;
  420. }
  421. }}
  422. />
  423. {/each}
  424. </div>
  425. </div>
  426. {/if}
  427. <div class="pl-2 my-2 flex-1 flex flex-col space-y-1 overflow-y-auto scrollbar-hidden">
  428. {#each filteredChatList as chat, idx}
  429. {#if idx === 0 || (idx > 0 && chat.time_range !== filteredChatList[idx - 1].time_range)}
  430. <div
  431. class="w-full pl-2.5 text-xs text-gray-500 dark:text-gray-500 font-medium {idx === 0
  432. ? ''
  433. : 'pt-5'} pb-0.5"
  434. >
  435. {$i18n.t(chat.time_range)}
  436. <!-- localisation keys for time_range to be recognized from the i18next parser (so they don't get automatically removed):
  437. {$i18n.t('Today')}
  438. {$i18n.t('Yesterday')}
  439. {$i18n.t('Previous 7 days')}
  440. {$i18n.t('Previous 30 days')}
  441. {$i18n.t('January')}
  442. {$i18n.t('February')}
  443. {$i18n.t('March')}
  444. {$i18n.t('April')}
  445. {$i18n.t('May')}
  446. {$i18n.t('June')}
  447. {$i18n.t('July')}
  448. {$i18n.t('August')}
  449. {$i18n.t('September')}
  450. {$i18n.t('October')}
  451. {$i18n.t('November')}
  452. {$i18n.t('December')}
  453. -->
  454. </div>
  455. {/if}
  456. <ChatItem
  457. {chat}
  458. {shiftKey}
  459. selected={selectedChatId === chat.id}
  460. on:select={() => {
  461. selectedChatId = chat.id;
  462. }}
  463. on:unselect={() => {
  464. selectedChatId = null;
  465. }}
  466. on:delete={(e) => {
  467. if ((e?.detail ?? '') === 'shift') {
  468. deleteChatHandler(chat.id);
  469. } else {
  470. deleteChat = chat;
  471. showDeleteConfirm = true;
  472. }
  473. }}
  474. />
  475. {/each}
  476. </div>
  477. </div>
  478. <div class="px-2.5">
  479. <!-- <hr class=" border-gray-900 mb-1 w-full" /> -->
  480. <div class="flex flex-col font-primary">
  481. {#if $user !== undefined}
  482. <UserMenu
  483. role={$user.role}
  484. on:show={(e) => {
  485. if (e.detail === 'archived-chat') {
  486. showArchivedChats.set(true);
  487. }
  488. }}
  489. >
  490. <button
  491. class=" flex rounded-xl py-3 px-3.5 w-full hover:bg-gray-100 dark:hover:bg-gray-900 transition"
  492. on:click={() => {
  493. showDropdown = !showDropdown;
  494. }}
  495. >
  496. <div class=" self-center mr-3">
  497. <img
  498. src={$user.profile_image_url}
  499. class=" max-w-[30px] object-cover rounded-full"
  500. alt="User profile"
  501. />
  502. </div>
  503. <div class=" self-center font-medium">{$user.name}</div>
  504. </button>
  505. </UserMenu>
  506. {/if}
  507. </div>
  508. </div>
  509. </div>
  510. </div>
  511. <style>
  512. .scrollbar-hidden:active::-webkit-scrollbar-thumb,
  513. .scrollbar-hidden:focus::-webkit-scrollbar-thumb,
  514. .scrollbar-hidden:hover::-webkit-scrollbar-thumb {
  515. visibility: visible;
  516. }
  517. .scrollbar-hidden::-webkit-scrollbar-thumb {
  518. visibility: hidden;
  519. }
  520. </style>