Navbar.svelte 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <script lang="ts">
  2. import { onMount } from 'svelte';
  3. let show = false;
  4. let navElement;
  5. let importFileInputElement;
  6. let importFiles;
  7. export let selectedChatId = '';
  8. export let title: string = 'Ollama Web UI';
  9. export let chats = [];
  10. export let createNewChat: Function;
  11. export let loadChat: Function;
  12. export let deleteChat: Function;
  13. export let editChatTitle: Function;
  14. export let importChatHistory: Function;
  15. export let exportChatHistory: Function;
  16. export let deleteChatHistory: Function;
  17. export let openSettings: Function;
  18. let chatTitleEditIdx = null;
  19. let chatTitle = '';
  20. let _chats = chats.map((item, idx) => chats[chats.length - 1 - idx]);
  21. onMount(() => {});
  22. $: if (chats) {
  23. _chats = chats.map((item, idx) => chats[chats.length - 1 - idx]);
  24. }
  25. $: if (importFiles) {
  26. console.log(importFiles);
  27. let reader = new FileReader();
  28. reader.onload = (event) => {
  29. let chats = JSON.parse(event.target.result);
  30. console.log(chats);
  31. importChatHistory(chats);
  32. };
  33. reader.readAsText(importFiles[0]);
  34. }
  35. </script>
  36. <div
  37. class=" fixed top-0 flex flex-row justify-center dark:bg-stone-100/5 dark:text-gray-200 backdrop-blur-xl w-full z-30"
  38. >
  39. <div class="basis-full px-5">
  40. <nav class="py-3" id="nav">
  41. <div class="flex flex-row justify-between">
  42. <div class="pl-2">
  43. <button
  44. class=" cursor-pointer p-1 flex dark:hover:bg-gray-700 rounded-lg transition"
  45. on:click={() => {
  46. show = !show;
  47. }}
  48. >
  49. <div class=" m-auto self-center">
  50. <svg
  51. xmlns="http://www.w3.org/2000/svg"
  52. fill="none"
  53. viewBox="0 0 24 24"
  54. stroke-width="1.5"
  55. stroke="currentColor"
  56. class="w-5 h-5"
  57. >
  58. <path
  59. stroke-linecap="round"
  60. stroke-linejoin="round"
  61. d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25H12"
  62. />
  63. </svg>
  64. </div>
  65. </button>
  66. </div>
  67. <div class=" self-center">
  68. {title != '' ? title.split(' ').slice(0, 6).join(' ') : 'Ollama Web UI'}
  69. </div>
  70. <div class="pr-2">
  71. <button
  72. class=" cursor-pointer p-1 flex dark:hover:bg-gray-700 rounded-lg transition"
  73. on:click={() => {
  74. createNewChat();
  75. }}
  76. >
  77. <div class=" m-auto self-center">
  78. <svg
  79. xmlns="http://www.w3.org/2000/svg"
  80. fill="none"
  81. viewBox="0 0 24 24"
  82. stroke-width="1.5"
  83. stroke="currentColor"
  84. class="w-5 h-5"
  85. >
  86. <path
  87. stroke-linecap="round"
  88. stroke-linejoin="round"
  89. d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10"
  90. />
  91. </svg>
  92. </div>
  93. </button>
  94. </div>
  95. </div>
  96. </nav>
  97. </div>
  98. </div>
  99. <div
  100. bind:this={navElement}
  101. class="h-screen {show
  102. ? ''
  103. : '-translate-x-72'} w-72 fixed top-0 left-0 z-40 transition bg-gray-900 text-gray-200 shadow-2xl text-sm
  104. "
  105. >
  106. <div class="py-2.5 my-auto flex flex-col justify-between h-screen">
  107. <div class="px-2.5 flex justify-center space-x-2">
  108. <button
  109. class=" cursor-pointer flex-grow rounded-md border border-gray-600 p-3 flex"
  110. on:click={() => {
  111. createNewChat();
  112. }}
  113. >
  114. <div class="self-center mr-2">
  115. <svg
  116. xmlns="http://www.w3.org/2000/svg"
  117. viewBox="0 0 20 20"
  118. fill="currentColor"
  119. class="w-5 h-5"
  120. >
  121. <path
  122. d="M10.75 4.75a.75.75 0 00-1.5 0v4.5h-4.5a.75.75 0 000 1.5h4.5v4.5a.75.75 0 001.5 0v-4.5h4.5a.75.75 0 000-1.5h-4.5v-4.5z"
  123. />
  124. </svg>
  125. </div>
  126. <div class=" self-center">New Chat</div>
  127. </button>
  128. <button
  129. class=" cursor-pointer w-12 rounded-md border border-gray-600 flex"
  130. on:click={() => {
  131. show = !show;
  132. }}
  133. >
  134. <div class=" m-auto self-center">
  135. <svg
  136. xmlns="http://www.w3.org/2000/svg"
  137. viewBox="0 0 20 20"
  138. fill="currentColor"
  139. class="w-5 h-5"
  140. >
  141. <path
  142. fill-rule="evenodd"
  143. d="M3 4.25A2.25 2.25 0 015.25 2h5.5A2.25 2.25 0 0113 4.25v2a.75.75 0 01-1.5 0v-2a.75.75 0 00-.75-.75h-5.5a.75.75 0 00-.75.75v11.5c0 .414.336.75.75.75h5.5a.75.75 0 00.75-.75v-2a.75.75 0 011.5 0v2A2.25 2.25 0 0110.75 18h-5.5A2.25 2.25 0 013 15.75V4.25z"
  144. clip-rule="evenodd"
  145. />
  146. <path
  147. fill-rule="evenodd"
  148. d="M19 10a.75.75 0 00-.75-.75H8.704l1.048-.943a.75.75 0 10-1.004-1.114l-2.5 2.25a.75.75 0 000 1.114l2.5 2.25a.75.75 0 101.004-1.114l-1.048-.943h9.546A.75.75 0 0019 10z"
  149. clip-rule="evenodd"
  150. />
  151. </svg>
  152. </div>
  153. </button>
  154. </div>
  155. <div class="pl-2.5 my-3 flex-1 flex flex-col space-y-1 overflow-y-auto">
  156. {#each _chats as chat, i}
  157. <div class=" w-full pr-2 relative">
  158. <button
  159. class=" w-full flex justify-between rounded-md px-4 py-3 hover:bg-gray-800 {chat.id ===
  160. selectedChatId
  161. ? 'bg-gray-800'
  162. : ''} transition whitespace-nowrap text-ellipsis"
  163. on:click={() => {
  164. if (chat.id !== chatTitleEditIdx) {
  165. chatTitleEditIdx = null;
  166. chatTitle = '';
  167. }
  168. loadChat(chat.id);
  169. }}
  170. >
  171. <div class=" flex self-center flex-1">
  172. <div class=" self-center mr-3">
  173. <svg
  174. xmlns="http://www.w3.org/2000/svg"
  175. fill="none"
  176. viewBox="0 0 24 24"
  177. stroke-width="1.5"
  178. stroke="currentColor"
  179. class="w-4 h-4"
  180. >
  181. <path
  182. stroke-linecap="round"
  183. stroke-linejoin="round"
  184. d="M2.25 12.76c0 1.6 1.123 2.994 2.707 3.227 1.087.16 2.185.283 3.293.369V21l4.076-4.076a1.526 1.526 0 011.037-.443 48.282 48.282 0 005.68-.494c1.584-.233 2.707-1.626 2.707-3.228V6.741c0-1.602-1.123-2.995-2.707-3.228A48.394 48.394 0 0012 3c-2.392 0-4.744.175-7.043.513C3.373 3.746 2.25 5.14 2.25 6.741v6.018z"
  185. />
  186. </svg>
  187. </div>
  188. <div
  189. class=" text-left self-center overflow-hidden {chat.id === selectedChatId
  190. ? 'w-[150px]'
  191. : 'w-[200px]'} "
  192. >
  193. {#if chatTitleEditIdx === chat.id}
  194. <input bind:value={chatTitle} class=" bg-transparent w-full" />
  195. {:else}
  196. {chat.title}
  197. {/if}
  198. </div>
  199. </div>
  200. </button>
  201. {#if chat.id === selectedChatId}
  202. <div class=" absolute right-[22px] top-[14px]">
  203. {#if chatTitleEditIdx === chat.id}
  204. <div class="flex self-center space-x-1.5">
  205. <button
  206. class=" self-center hover:text-white transition"
  207. on:click={() => {
  208. editChatTitle(chat.id, chatTitle);
  209. chatTitleEditIdx = null;
  210. chatTitle = '';
  211. }}
  212. >
  213. <svg
  214. xmlns="http://www.w3.org/2000/svg"
  215. viewBox="0 0 20 20"
  216. fill="currentColor"
  217. class="w-4 h-4"
  218. >
  219. <path
  220. fill-rule="evenodd"
  221. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  222. clip-rule="evenodd"
  223. />
  224. </svg>
  225. </button>
  226. <button
  227. class=" self-center hover:text-white transition"
  228. on:click={() => {
  229. chatTitleEditIdx = null;
  230. chatTitle = '';
  231. }}
  232. >
  233. <svg
  234. xmlns="http://www.w3.org/2000/svg"
  235. viewBox="0 0 20 20"
  236. fill="currentColor"
  237. class="w-4 h-4"
  238. >
  239. <path
  240. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  241. />
  242. </svg>
  243. </button>
  244. </div>
  245. {:else}
  246. <div class="flex self-center space-x-1.5">
  247. <button
  248. class=" self-center hover:text-white transition"
  249. on:click={() => {
  250. chatTitle = chat.title;
  251. chatTitleEditIdx = chat.id;
  252. // editChatTitle(chat.id, 'a');
  253. }}
  254. >
  255. <svg
  256. xmlns="http://www.w3.org/2000/svg"
  257. fill="none"
  258. viewBox="0 0 24 24"
  259. stroke-width="1.5"
  260. stroke="currentColor"
  261. class="w-4 h-4"
  262. >
  263. <path
  264. stroke-linecap="round"
  265. stroke-linejoin="round"
  266. d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
  267. />
  268. </svg>
  269. </button>
  270. <button
  271. class=" self-center hover:text-white transition"
  272. on:click={() => {
  273. deleteChat(chat.id);
  274. }}
  275. >
  276. <svg
  277. xmlns="http://www.w3.org/2000/svg"
  278. fill="none"
  279. viewBox="0 0 24 24"
  280. stroke-width="1.5"
  281. stroke="currentColor"
  282. class="w-4 h-4"
  283. >
  284. <path
  285. stroke-linecap="round"
  286. stroke-linejoin="round"
  287. d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
  288. />
  289. </svg>
  290. </button>
  291. </div>
  292. {/if}
  293. </div>
  294. {/if}
  295. </div>
  296. {/each}
  297. </div>
  298. <div class="px-2.5">
  299. <hr class=" border-gray-800 mb-2 w-full" />
  300. <div class="flex flex-col">
  301. <div class="flex">
  302. <input bind:this={importFileInputElement} bind:files={importFiles} type="file" hidden />
  303. <button
  304. class=" flex rounded-md p-3.5 w-full hover:bg-gray-800 transition"
  305. on:click={() => {
  306. importFileInputElement.click();
  307. // importChatHistory();
  308. }}
  309. >
  310. <div class=" self-center mr-3">
  311. <svg
  312. xmlns="http://www.w3.org/2000/svg"
  313. fill="none"
  314. viewBox="0 0 24 24"
  315. stroke-width="1.5"
  316. stroke="currentColor"
  317. class="w-5 h-5"
  318. >
  319. <path
  320. stroke-linecap="round"
  321. stroke-linejoin="round"
  322. d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m6.75 12l-3-3m0 0l-3 3m3-3v6m-1.5-15H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z"
  323. />
  324. </svg>
  325. </div>
  326. <div class=" self-center">Import</div>
  327. </button>
  328. <button
  329. class=" flex rounded-md p-3.5 w-full hover:bg-gray-800 transition"
  330. on:click={() => {
  331. exportChatHistory();
  332. }}
  333. >
  334. <div class=" self-center mr-3">
  335. <svg
  336. xmlns="http://www.w3.org/2000/svg"
  337. fill="none"
  338. viewBox="0 0 24 24"
  339. stroke-width="1.5"
  340. stroke="currentColor"
  341. class="w-5 h-5"
  342. >
  343. <path
  344. stroke-linecap="round"
  345. stroke-linejoin="round"
  346. d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m.75 12l3 3m0 0l3-3m-3 3v-6m-1.5-9H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z"
  347. />
  348. </svg>
  349. </div>
  350. <div class=" self-center">Export</div>
  351. </button>
  352. </div>
  353. <button
  354. class=" flex rounded-md p-3.5 w-full hover:bg-gray-800 transition"
  355. on:click={() => {
  356. deleteChatHistory();
  357. }}
  358. >
  359. <div class=" self-center mr-3">
  360. <svg
  361. xmlns="http://www.w3.org/2000/svg"
  362. fill="none"
  363. viewBox="0 0 24 24"
  364. stroke-width="1.5"
  365. stroke="currentColor"
  366. class="w-5 h-5"
  367. >
  368. <path
  369. stroke-linecap="round"
  370. stroke-linejoin="round"
  371. d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0"
  372. />
  373. </svg>
  374. </div>
  375. <div class=" self-center">Clear conversations</div>
  376. </button>
  377. <button
  378. class=" flex rounded-md p-3.5 w-full hover:bg-gray-800 transition"
  379. on:click={() => {
  380. openSettings();
  381. }}
  382. >
  383. <div class=" self-center mr-3">
  384. <svg
  385. xmlns="http://www.w3.org/2000/svg"
  386. fill="none"
  387. viewBox="0 0 24 24"
  388. stroke-width="1.5"
  389. stroke="currentColor"
  390. class="w-5 h-5"
  391. >
  392. <path
  393. stroke-linecap="round"
  394. stroke-linejoin="round"
  395. d="M10.343 3.94c.09-.542.56-.94 1.11-.94h1.093c.55 0 1.02.398 1.11.94l.149.894c.07.424.384.764.78.93.398.164.855.142 1.205-.108l.737-.527a1.125 1.125 0 011.45.12l.773.774c.39.389.44 1.002.12 1.45l-.527.737c-.25.35-.272.806-.107 1.204.165.397.505.71.93.78l.893.15c.543.09.94.56.94 1.109v1.094c0 .55-.397 1.02-.94 1.11l-.893.149c-.425.07-.765.383-.93.78-.165.398-.143.854.107 1.204l.527.738c.32.447.269 1.06-.12 1.45l-.774.773a1.125 1.125 0 01-1.449.12l-.738-.527c-.35-.25-.806-.272-1.203-.107-.397.165-.71.505-.781.929l-.149.894c-.09.542-.56.94-1.11.94h-1.094c-.55 0-1.019-.398-1.11-.94l-.148-.894c-.071-.424-.384-.764-.781-.93-.398-.164-.854-.142-1.204.108l-.738.527c-.447.32-1.06.269-1.45-.12l-.773-.774a1.125 1.125 0 01-.12-1.45l.527-.737c.25-.35.273-.806.108-1.204-.165-.397-.505-.71-.93-.78l-.894-.15c-.542-.09-.94-.56-.94-1.109v-1.094c0-.55.398-1.02.94-1.11l.894-.149c.424-.07.765-.383.93-.78.165-.398.143-.854-.107-1.204l-.527-.738a1.125 1.125 0 01.12-1.45l.773-.773a1.125 1.125 0 011.45-.12l.737.527c.35.25.807.272 1.204.107.397-.165.71-.505.78-.929l.15-.894z"
  396. />
  397. <path
  398. stroke-linecap="round"
  399. stroke-linejoin="round"
  400. d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
  401. />
  402. </svg>
  403. </div>
  404. <div class=" self-center font-medium">Settings</div>
  405. </button>
  406. </div>
  407. </div>
  408. </div>
  409. </div>