Navbar.svelte 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <script lang="ts">
  2. import toast from 'svelte-french-toast';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { getChatById } from '$lib/apis/chats';
  6. import { chatId, modelfiles } from '$lib/stores';
  7. export let initNewChat: Function;
  8. export let title: string = 'Ollama Web UI';
  9. export let shareEnabled: boolean = false;
  10. const shareChat = async () => {
  11. const chat = (await getChatById(localStorage.token, $chatId)).chat;
  12. console.log('share', chat);
  13. toast.success('Redirecting you to OllamaHub');
  14. const url = 'https://ollamahub.com';
  15. // const url = 'http://localhost:5173';
  16. const tab = await window.open(`${url}/chats/upload`, '_blank');
  17. window.addEventListener(
  18. 'message',
  19. (event) => {
  20. if (event.origin !== url) return;
  21. if (event.data === 'loaded') {
  22. tab.postMessage(
  23. JSON.stringify({
  24. chat: chat,
  25. modelfiles: $modelfiles.filter((modelfile) => chat.models.includes(modelfile.tagName))
  26. }),
  27. '*'
  28. );
  29. }
  30. },
  31. false
  32. );
  33. };
  34. const downloadChat = async () => {
  35. const chat = (await getChatById(localStorage.token, $chatId)).chat;
  36. console.log('download', chat);
  37. const chatText = chat.messages.reduce((a, message, i, arr) => {
  38. return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
  39. }, '');
  40. let blob = new Blob([chatText], {
  41. type: 'text/plain'
  42. });
  43. saveAs(blob, `chat-${chat.title}.txt`);
  44. };
  45. </script>
  46. <nav
  47. id="nav"
  48. class=" fixed py-2.5 top-0 flex flex-row justify-center bg-white/95 dark:bg-gray-800/90 dark:text-gray-200 backdrop-blur-xl w-screen z-30"
  49. >
  50. <div class=" flex max-w-3xl w-full mx-auto px-3">
  51. <div class="flex w-full max-w-full">
  52. <div class="pr-2 self-center">
  53. <button
  54. id="new-chat-button"
  55. class=" cursor-pointer p-1 flex dark:hover:bg-gray-700 rounded-lg transition"
  56. on:click={initNewChat}
  57. >
  58. <div class=" m-auto self-center">
  59. <svg
  60. xmlns="http://www.w3.org/2000/svg"
  61. viewBox="0 0 20 20"
  62. fill="currentColor"
  63. class="w-5 h-5"
  64. >
  65. <path
  66. 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"
  67. />
  68. <path
  69. 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"
  70. />
  71. </svg>
  72. </div>
  73. </button>
  74. </div>
  75. <div class=" flex-1 self-center font-medium text-ellipsis whitespace-nowrap overflow-hidden">
  76. {title != '' ? title : 'Ollama Web UI'}
  77. </div>
  78. {#if shareEnabled}
  79. <div class="pl-2 flex space-x-1.5">
  80. <button
  81. class=" cursor-pointer p-2 flex dark:hover:bg-gray-700 rounded-lg transition border dark:border-gray-600"
  82. on:click={async () => {
  83. downloadChat();
  84. }}
  85. >
  86. <div class=" m-auto self-center">
  87. <svg
  88. xmlns="http://www.w3.org/2000/svg"
  89. viewBox="0 0 16 16"
  90. fill="currentColor"
  91. class="w-4 h-4"
  92. >
  93. <path
  94. d="M8.75 2.75a.75.75 0 0 0-1.5 0v5.69L5.03 6.22a.75.75 0 0 0-1.06 1.06l3.5 3.5a.75.75 0 0 0 1.06 0l3.5-3.5a.75.75 0 0 0-1.06-1.06L8.75 8.44V2.75Z"
  95. />
  96. <path
  97. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  98. />
  99. </svg>
  100. </div>
  101. </button>
  102. <button
  103. class=" cursor-pointer p-2 flex dark:hover:bg-gray-700 rounded-lg transition border dark:border-gray-600"
  104. on:click={async () => {
  105. shareChat();
  106. }}
  107. >
  108. <div class=" m-auto self-center">
  109. <svg
  110. xmlns="http://www.w3.org/2000/svg"
  111. viewBox="0 0 16 16"
  112. fill="currentColor"
  113. class="w-4 h-4"
  114. >
  115. <path
  116. d="M7.25 10.25a.75.75 0 0 0 1.5 0V4.56l2.22 2.22a.75.75 0 1 0 1.06-1.06l-3.5-3.5a.75.75 0 0 0-1.06 0l-3.5 3.5a.75.75 0 0 0 1.06 1.06l2.22-2.22v5.69Z"
  117. />
  118. <path
  119. d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
  120. />
  121. </svg>
  122. </div>
  123. </button>
  124. </div>
  125. {/if}
  126. </div>
  127. </div>
  128. </nav>