Navbar.svelte 4.0 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, settings } from '$lib/stores';
  7. import ShareChatModal from '../chat/ShareChatModal.svelte';
  8. import TagInput from '../common/Tags/TagInput.svelte';
  9. import Tags from '../common/Tags.svelte';
  10. import { WEBUI_NAME } from '$lib/constants';
  11. export let initNewChat: Function;
  12. export let title: string = WEBUI_NAME;
  13. export let shareEnabled: boolean = false;
  14. export let tags = [];
  15. export let addTag: Function;
  16. export let deleteTag: Function;
  17. let showShareChatModal = false;
  18. let tagName = '';
  19. let showTagInput = false;
  20. const shareChat = async () => {
  21. const chat = (await getChatById(localStorage.token, $chatId)).chat;
  22. console.log('share', chat);
  23. toast.success('Redirecting you to OllamaHub');
  24. const url = 'https://ollamahub.com';
  25. // const url = 'http://localhost:5173';
  26. const tab = await window.open(`${url}/chats/upload`, '_blank');
  27. window.addEventListener(
  28. 'message',
  29. (event) => {
  30. if (event.origin !== url) return;
  31. if (event.data === 'loaded') {
  32. tab.postMessage(
  33. JSON.stringify({
  34. chat: chat,
  35. modelfiles: $modelfiles.filter((modelfile) => chat.models.includes(modelfile.tagName))
  36. }),
  37. '*'
  38. );
  39. }
  40. },
  41. false
  42. );
  43. };
  44. const downloadChat = async () => {
  45. const chat = (await getChatById(localStorage.token, $chatId)).chat;
  46. console.log('download', chat);
  47. const chatText = chat.messages.reduce((a, message, i, arr) => {
  48. return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
  49. }, '');
  50. let blob = new Blob([chatText], {
  51. type: 'text/plain'
  52. });
  53. saveAs(blob, `chat-${chat.title}.txt`);
  54. };
  55. </script>
  56. <ShareChatModal bind:show={showShareChatModal} {downloadChat} {shareChat} />
  57. <nav
  58. id="nav"
  59. class=" fixed py-2.5 top-0 flex flex-row justify-center bg-white/95 dark:bg-gray-900/90 dark:text-gray-200 backdrop-blur-xl w-screen z-30"
  60. >
  61. <div
  62. class=" flex {$settings?.fullScreenMode ?? null
  63. ? 'max-w-full'
  64. : 'max-w-3xl'} w-full mx-auto px-3"
  65. >
  66. <div class="flex items-center w-full max-w-full">
  67. <div class="pr-2 self-start">
  68. <button
  69. id="new-chat-button"
  70. class=" cursor-pointer p-1.5 flex dark:hover:bg-gray-700 rounded-lg transition"
  71. on:click={initNewChat}
  72. >
  73. <div class=" m-auto self-center">
  74. <svg
  75. xmlns="http://www.w3.org/2000/svg"
  76. viewBox="0 0 20 20"
  77. fill="currentColor"
  78. class="w-5 h-5"
  79. >
  80. <path
  81. 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"
  82. />
  83. <path
  84. 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"
  85. />
  86. </svg>
  87. </div>
  88. </button>
  89. </div>
  90. <div class=" flex-1 self-center font-medium line-clamp-1">
  91. <div>
  92. {title != '' ? title : WEBUI_NAME}
  93. </div>
  94. </div>
  95. <div class="pl-2 self-center flex items-center space-x-2">
  96. {#if shareEnabled}
  97. <Tags {tags} {deleteTag} {addTag} />
  98. <button
  99. class=" cursor-pointer p-1.5 flex dark:hover:bg-gray-700 rounded-lg transition border dark:border-gray-600"
  100. on:click={async () => {
  101. showShareChatModal = !showShareChatModal;
  102. // console.log(showShareChatModal);
  103. }}
  104. >
  105. <div class=" m-auto self-center">
  106. <svg
  107. xmlns="http://www.w3.org/2000/svg"
  108. viewBox="0 0 24 24"
  109. fill="currentColor"
  110. class="w-4 h-4"
  111. >
  112. <path
  113. fill-rule="evenodd"
  114. d="M15.75 4.5a3 3 0 1 1 .825 2.066l-8.421 4.679a3.002 3.002 0 0 1 0 1.51l8.421 4.679a3 3 0 1 1-.729 1.31l-8.421-4.678a3 3 0 1 1 0-4.132l8.421-4.679a3 3 0 0 1-.096-.755Z"
  115. clip-rule="evenodd"
  116. />
  117. </svg>
  118. </div>
  119. </button>
  120. {/if}
  121. </div>
  122. </div>
  123. </div>
  124. </nav>