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