Navbar.svelte 4.0 KB

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