ShareChatModal.svelte 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <script lang="ts">
  2. import { getContext, onMount } from 'svelte';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { toast } from 'svelte-sonner';
  6. import { deleteSharedChatById, getChatById, shareChatById } from '$lib/apis/chats';
  7. import { chatId, modelfiles } from '$lib/stores';
  8. import { copyToClipboard } from '$lib/utils';
  9. import Modal from '../common/Modal.svelte';
  10. import Link from '../icons/Link.svelte';
  11. let chat = null;
  12. const i18n = getContext('i18n');
  13. const shareLocalChat = async () => {
  14. const _chat = chat;
  15. let chatShareUrl = '';
  16. if (_chat.share_id) {
  17. chatShareUrl = `${window.location.origin}/s/${_chat.share_id}`;
  18. } else {
  19. const sharedChat = await shareChatById(localStorage.token, $chatId);
  20. chatShareUrl = `${window.location.origin}/s/${sharedChat.id}`;
  21. }
  22. toast.success($i18n.t('Copied shared conversation URL to clipboard!'));
  23. copyToClipboard(chatShareUrl);
  24. chat = await getChatById(localStorage.token, $chatId);
  25. };
  26. const shareChat = async () => {
  27. const _chat = chat.chat;
  28. console.log('share', _chat);
  29. toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
  30. const url = 'https://openwebui.com';
  31. // const url = 'http://localhost:5173';
  32. const tab = await window.open(`${url}/chats/upload`, '_blank');
  33. window.addEventListener(
  34. 'message',
  35. (event) => {
  36. if (event.origin !== url) return;
  37. if (event.data === 'loaded') {
  38. tab.postMessage(
  39. JSON.stringify({
  40. chat: _chat,
  41. modelfiles: $modelfiles.filter((modelfile) =>
  42. _chat.models.includes(modelfile.tagName)
  43. )
  44. }),
  45. '*'
  46. );
  47. }
  48. },
  49. false
  50. );
  51. };
  52. const downloadChat = async () => {
  53. const _chat = chat.chat;
  54. console.log('download', chat);
  55. const chatText = _chat.messages.reduce((a, message, i, arr) => {
  56. return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
  57. }, '');
  58. let blob = new Blob([chatText], {
  59. type: 'text/plain'
  60. });
  61. saveAs(blob, `chat-${_chat.title}.txt`);
  62. };
  63. export let show = false;
  64. onMount(async () => {
  65. chat = await getChatById(localStorage.token, $chatId);
  66. console.log(chat);
  67. });
  68. </script>
  69. <Modal bind:show size="sm">
  70. <div>
  71. <div class=" flex justify-between dark:text-gray-300 px-5 py-4">
  72. <div class=" text-lg font-medium self-center">{$i18n.t('Share Chat')}</div>
  73. <button
  74. class="self-center"
  75. on:click={() => {
  76. show = false;
  77. }}
  78. >
  79. <svg
  80. xmlns="http://www.w3.org/2000/svg"
  81. viewBox="0 0 20 20"
  82. fill="currentColor"
  83. class="w-5 h-5"
  84. >
  85. <path
  86. 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"
  87. />
  88. </svg>
  89. </button>
  90. </div>
  91. <hr class=" dark:border-gray-800" />
  92. {#if chat}
  93. <div class="px-4 pt-4 pb-5 w-full flex flex-col justify-center">
  94. <div class=" text-sm dark:text-gray-300 mb-1">
  95. {#if chat.share_id}
  96. <a href="/s/{chat.share_id}" target="_blank"
  97. >You have shared this chat <span class=" underline">before</span>.</a
  98. >
  99. Click here to
  100. <button
  101. class="underline"
  102. on:click={async () => {
  103. const res = await deleteSharedChatById(localStorage.token, $chatId);
  104. if (res) {
  105. chat = await getChatById(localStorage.token, $chatId);
  106. }
  107. }}>delete this link</button
  108. > and create a new shared link.
  109. {:else}
  110. Messages you send after creating your link won't be shared. Anyone with the URL will be
  111. able to view the shared chat.
  112. {/if}
  113. </div>
  114. <div class="flex justify-end">
  115. <div class="flex flex-col items-end space-x-1 mt-1.5">
  116. <div class="flex gap-1">
  117. <button
  118. class=" self-center px-3.5 py-2 rounded-xl text-sm font-medium bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-white"
  119. type="button"
  120. on:click={() => {
  121. shareChat();
  122. show = false;
  123. }}
  124. >
  125. {$i18n.t('Share to OpenWebUI Community')}
  126. </button>
  127. <button
  128. class=" self-center flex items-center gap-1 px-3.5 py-2 rounded-xl text-sm font-medium bg-emerald-600 hover:bg-emerald-500 text-white"
  129. type="button"
  130. on:click={() => {
  131. shareLocalChat();
  132. show = false;
  133. }}
  134. >
  135. <Link />
  136. {#if chat.share_id}
  137. {$i18n.t('Update and Copy Link')}
  138. {:else}
  139. {$i18n.t('Copy Link')}
  140. {/if}
  141. </button>
  142. </div>
  143. <div class="flex gap-1 mt-1.5">
  144. <div class=" self-center text-gray-400 text-xs font-medium">{$i18n.t('or')}</div>
  145. <button
  146. class=" text-right rounded-full text-xs font-medium text-gray-700 dark:text-gray-500 underline"
  147. type="button"
  148. on:click={() => {
  149. downloadChat();
  150. show = false;
  151. }}
  152. >
  153. {$i18n.t('Download as a File')}
  154. </button>
  155. </div>
  156. </div>
  157. </div>
  158. </div>
  159. {/if}
  160. </div>
  161. </Modal>